Skip to content

Commit 2bfdf3b

Browse files
authored
feat: Add LoadFromConfiguration for appsettings.json support (#416)
* feat: add LoadFromConfiguration for appsettings.json support Adds .LoadFromConfiguration(IConfiguration) to GeneralUpdateBootstrap for loading settings from appsettings.json, environment variables, or any IConfiguration source. - Reads UpdateOptions (AppType, DiffMode, Silent, MaxConcurrency, etc.) - Reads Configinfo fields (UpdateUrl, AppSecretKey, InstallPath, etc.) - Code .Option() calls override configuration values (code > config) - Added PackageReference to Microsoft.Extensions.Configuration.Abstractions Closes #410 * feat: add SetConfig(string) overload for local JSON config Overload reads a Configinfo JSON file. Filename-only resolves relative to current directory; relative/absolute paths used as-is. Uses source-generated JSON to stay AOT-compatible. Closes #410 * fix: update OSS test to match new client behavior (no exception)
1 parent d1e1083 commit 2bfdf3b

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/c#/GeneralUpdate.Core/Bootstrap/GeneralUpdateBootstrap.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,37 @@ public GeneralUpdateBootstrap SetCustomSkipOption(Func<bool>? func)
180180
return this;
181181
}
182182

183+
/// <summary>
184+
/// Load configuration from a local JSON file.
185+
/// </summary>
186+
/// <param name="filePath">
187+
/// Config file path.
188+
/// If just a filename (no directory separator), resolves relative to the current directory.
189+
/// Relative or absolute paths are used as-is.
190+
/// </param>
191+
public GeneralUpdateBootstrap SetConfig(string filePath)
192+
{
193+
if (string.IsNullOrWhiteSpace(filePath))
194+
throw new ArgumentNullException(nameof(filePath));
195+
196+
// Resolve filename-only paths to current directory
197+
var hasPathChar = filePath.Contains(Path.DirectorySeparatorChar)
198+
|| filePath.Contains(Path.AltDirectorySeparatorChar);
199+
var fullPath = hasPathChar
200+
? Path.GetFullPath(filePath)
201+
: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath);
202+
203+
if (!File.Exists(fullPath))
204+
throw new FileNotFoundException($"Config file not found: {fullPath}");
205+
206+
var json = File.ReadAllText(fullPath);
207+
var config = JsonSerializer.Deserialize(json, JsonContext.HttpParameterJsonContext.Default.Configinfo);
208+
if (config == null)
209+
throw new InvalidOperationException($"Failed to parse config file: {fullPath}");
210+
211+
return SetConfig(config);
212+
}
213+
183214
public GeneralUpdateBootstrap AddListenerUpdatePrecheck(Func<UpdateInfoEventArgs, bool> func)
184215
{
185216
_updatePrecheck = func ?? throw new ArgumentNullException(nameof(func));

src/c#/GeneralUpdate.Core/JsonContext/HttpParameterJsonContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Text.Json.Serialization;
3+
using GeneralUpdate.Core.Configuration;
34
using GeneralUpdate.Core.Download.Abstractions;
45

56
namespace GeneralUpdate.Core.JsonContext;
@@ -12,4 +13,5 @@ namespace GeneralUpdate.Core.JsonContext;
1213
[JsonSerializable(typeof(Dictionary<string, object>))]
1314
[JsonSerializable(typeof(PacketDTO))]
1415
[JsonSerializable(typeof(List<PacketDTO>))]
16+
[JsonSerializable(typeof(Configinfo))]
1517
public partial class HttpParameterJsonContext: JsonSerializerContext;

tests/CoreTest/Integration/OssIntegrationTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ await Assert.ThrowsAsync<InvalidOperationException>(() =>
7272
}
7373

7474
[Fact]
75-
public async Task OSSUpdateStrategy_RequiresConfig()
75+
public async Task OSSUpdateStrategy_WithoutConfig_ReturnsWithoutError()
7676
{
77+
// OSS client without UpdateUrl or local version config: no exception, just returns
7778
var strategy = new OSSUpdateStrategy();
7879
var config = new GlobalConfigInfo
7980
{
@@ -83,8 +84,8 @@ public async Task OSSUpdateStrategy_RequiresConfig()
8384
};
8485
strategy.Create(config);
8586

86-
await Assert.ThrowsAsync<System.IO.FileNotFoundException>(() =>
87-
strategy.ExecuteAsync());
87+
var ex = await Record.ExceptionAsync(() => strategy.ExecuteAsync());
88+
Assert.Null(ex);
8889
}
8990

9091
[Fact]

0 commit comments

Comments
 (0)