Skip to content

Commit 39ac0d9

Browse files
CopilotJusterZhu
andcommitted
Add JSON configuration example and update documentation
Co-authored-by: JusterZhu <11714536+JusterZhu@users.noreply.github.com>
1 parent 2229b5b commit 39ac0d9

3 files changed

Lines changed: 178 additions & 21 deletions

File tree

src/c#/GeneralUpdate.Common/Shared/Object/ConfiginfoBuilder-Usage.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
11
# ConfiginfoBuilder Usage Guide
22

3-
The `ConfiginfoBuilder` class provides a simple and convenient way to create `Configinfo` objects for the GeneralUpdate system. It only requires three essential parameters while automatically generating platform-appropriate defaults for all other configuration items.
3+
The `ConfiginfoBuilder` class provides a simple and convenient way to create `Configinfo` objects for the GeneralUpdate system. It supports both JSON-based configuration and programmatic configuration.
44

55
**Design Philosophy**: Inspired by zero-configuration patterns from projects like [Velopack](https://github.com/velopack/velopack), this builder minimizes required configuration while maintaining flexibility through optional fluent setters.
66

7+
## Configuration Methods
8+
9+
There are two main ways to configure the update system:
10+
11+
### 1. JSON Configuration File (Recommended)
12+
13+
Place an `update_config.json` file in your application's running directory. When this file exists, ConfiginfoBuilder automatically loads all settings from it, giving the configuration file the highest priority.
14+
15+
**Example `update_config.json`:**
16+
```json
17+
{
18+
"UpdateUrl": "https://api.example.com/updates",
19+
"Token": "your-authentication-token",
20+
"Scheme": "https",
21+
"AppName": "Update.exe",
22+
"MainAppName": "MyApplication.exe",
23+
"ClientVersion": "1.0.0",
24+
"InstallPath": "/path/to/installation",
25+
"BlackFormats": [".log", ".tmp", ".cache"],
26+
"SkipDirectorys": ["/temp", "/logs"]
27+
}
28+
```
29+
30+
See [update_config.example.json](update_config.example.json) for a complete example with all available options.
31+
32+
**Usage:**
33+
```csharp
34+
// The Create method will automatically load from update_config.json if it exists
35+
var config = ConfiginfoBuilder
36+
.Create("fallback-url", "fallback-token", "https")
37+
.Build();
38+
// If update_config.json exists, all values come from the file
39+
// Parameters are only used as fallback if file doesn't exist
40+
```
41+
42+
### 2. Programmatic Configuration
43+
44+
Configure the update system entirely through code using the fluent API:
45+
746
## Automatic Configuration Detection
847

948
The ConfiginfoBuilder implements intelligent zero-configuration by automatically extracting information from your project:

src/c#/GeneralUpdate.Common/Shared/Object/README-ConfiginfoBuilder.md

Lines changed: 107 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,37 @@
44

55
The `ConfiginfoBuilder` class provides a simple, fluent API for creating `Configinfo` objects with minimal effort. It automatically handles platform-specific defaults and only requires three essential parameters.
66

7+
**NEW**: ConfiginfoBuilder now supports loading configuration from a JSON file (`update_config.json`) placed in the running directory. When this file exists, it takes the highest priority, overriding any parameters passed to the builder.
8+
79
**Design Inspiration**: The zero-configuration approach is inspired by projects like [Velopack](https://github.com/velopack/velopack), focusing on sensible defaults extracted from the running application with minimal user input required.
810

11+
## Configuration Priority
12+
13+
The ConfiginfoBuilder follows this priority order:
14+
15+
1. **JSON Configuration File** (`update_config.json`) - **HIGHEST PRIORITY**
16+
2. **Builder Setter Methods** (via `.SetXXX()` methods)
17+
3. **Default Values** (platform-specific defaults)
18+
19+
## JSON Configuration File Support
20+
21+
Place an `update_config.json` file in your application's running directory to configure all update settings. When this file is present, ConfiginfoBuilder will automatically load settings from it, ignoring parameters passed to the `Create()` method.
22+
23+
**Example `update_config.json`:**
24+
```json
25+
{
26+
"UpdateUrl": "https://api.example.com/updates",
27+
"Token": "your-authentication-token",
28+
"Scheme": "https",
29+
"AppName": "Update.exe",
30+
"MainAppName": "MyApplication.exe",
31+
"ClientVersion": "1.0.0",
32+
"InstallPath": "/path/to/installation"
33+
}
34+
```
35+
36+
See [update_config.example.json](update_config.example.json) for a complete example with all available options.
37+
938
## Auto-Configuration Features
1039

1140
🔍 **Application Name Detection**: Automatically reads `<AssemblyName>` from `.csproj`
@@ -19,30 +48,36 @@ The `ConfiginfoBuilder` class provides a simple, fluent API for creating `Config
1948
```csharp
2049
using GeneralUpdate.Common.Shared.Object;
2150

22-
// Method 1: Direct constructor
23-
var config = new ConfiginfoBuilder(
24-
updateUrl: "https://api.example.com/updates",
25-
token: "your-auth-token",
26-
scheme: "https"
27-
).Build();
51+
// Method 1: With JSON configuration file (RECOMMENDED)
52+
// Place update_config.json in your app's running directory
53+
// The Create method will automatically load from the file
54+
var config = ConfiginfoBuilder
55+
.Create("https://fallback.com/updates", "fallback-token", "https")
56+
.Build();
57+
// If update_config.json exists, those values are used instead of the parameters
2858
29-
// Method 2: Factory method (recommended)
59+
// Method 2: Using code configuration only (no JSON file)
3060
var config2 = ConfiginfoBuilder
3161
.Create("https://api.example.com/updates", "your-auth-token", "https")
3262
.Build();
3363

34-
// That's it! Application name, version, and all defaults are set automatically!
64+
// Method 3: Code configuration with custom overrides
65+
var config3 = ConfiginfoBuilder
66+
.Create("https://api.example.com/updates", "your-auth-token", "https")
67+
.SetAppName("MyApp.exe")
68+
.SetInstallPath("/custom/path")
69+
.Build();
3570
```
3671

3772
## Key Features
3873

74+
**JSON Configuration Support**: Load settings from `update_config.json` with highest priority
3975
**Minimal Parameters**: Only 3 required parameters (UpdateUrl, Token, Scheme)
4076
**Cross-Platform**: Automatically detects and adapts to Windows/Linux/macOS
4177
**Smart Defaults**: Platform-appropriate paths, separators, and configurations
42-
**Auto-Discovery**: Reads application name, version, and publisher from project file (.csproj)
4378
**Fluent API**: Clean, readable method chaining
4479
**Type-Safe**: Compile-time parameter validation
45-
**Well-Tested**: 37 comprehensive unit tests
80+
**Well-Tested**: 39 comprehensive unit tests including JSON configuration scenarios
4681

4782
## Platform Detection
4883

@@ -62,14 +97,35 @@ The builder automatically adapts based on your runtime environment:
6297

6398
## Examples
6499

65-
### Basic Usage
100+
### Using JSON Configuration File
66101
```csharp
67-
var config = new ConfiginfoBuilder(updateUrl, token, scheme).Build();
102+
// Create update_config.json in your app directory:
103+
// {
104+
// "UpdateUrl": "https://api.example.com/updates",
105+
// "Token": "my-token",
106+
// "Scheme": "https",
107+
// "AppName": "MyApp.exe",
108+
// "ClientVersion": "2.0.0"
109+
// }
110+
111+
// The builder will automatically load from the file
112+
var config = ConfiginfoBuilder
113+
.Create("ignored", "ignored", "ignored")
114+
.Build();
115+
// Values come from update_config.json!
116+
```
117+
118+
### Basic Usage (No JSON File)
119+
```csharp
120+
var config = ConfiginfoBuilder
121+
.Create(updateUrl, token, scheme)
122+
.Build();
68123
```
69124

70125
### Custom Configuration
71126
```csharp
72-
var config = new ConfiginfoBuilder(updateUrl, token, scheme)
127+
var config = ConfiginfoBuilder
128+
.Create(updateUrl, token, scheme)
73129
.SetAppName("MyApp.exe")
74130
.SetClientVersion("2.0.0")
75131
.SetInstallPath("/opt/myapp")
@@ -78,25 +134,35 @@ var config = new ConfiginfoBuilder(updateUrl, token, scheme)
78134

79135
### With File Filters
80136
```csharp
81-
var config = new ConfiginfoBuilder(updateUrl, token, scheme)
137+
var config = ConfiginfoBuilder
138+
.Create(updateUrl, token, scheme)
82139
.SetBlackFormats(new List<string> { ".log", ".tmp", ".cache" })
83140
.SetSkipDirectorys(new List<string> { "/temp", "/logs" })
84141
.Build();
85142
```
86143

87144
## Default Values
88145

89-
The builder provides these defaults automatically:
146+
The builder and `Configinfo` class provide these defaults:
147+
148+
### Configinfo Class Defaults (Property Initializers)
149+
- **AppName**: "Update.exe"
150+
- **InstallPath**: Current program's running directory (`AppDomain.CurrentDomain.BaseDirectory`)
90151

152+
### ConfiginfoBuilder Defaults (for Builder Pattern)
91153
- **ClientVersion**: "1.0.0"
92154
- **UpgradeClientVersion**: "1.0.0"
93155
- **AppSecretKey**: "default-secret-key"
94156
- **ProductId**: "default-product-id"
95-
- **BlackFormats**: `.log`, `.tmp` (via `ConfiginfoBuilder.DefaultBlackFormats`)
157+
- **MainAppName**: "App.exe"
158+
- **BlackFormats**: `.log`, `.tmp`, `.cache`, `.bak` (via `ConfiginfoBuilder.DefaultBlackFormats`)
96159
- **BlackFiles**: Empty list
97160
- **SkipDirectorys**: Empty list
98161

99-
All defaults can be overridden using the setter methods.
162+
All defaults can be overridden using:
163+
1. JSON configuration file (`update_config.json`) - highest priority
164+
2. Builder setter methods (`.SetXXX()`)
165+
3. Direct property assignment on `Configinfo` objects
100166

101167
## Error Handling
102168

@@ -117,12 +183,14 @@ try {
117183

118184
## Testing
119185

120-
The implementation includes 32 comprehensive unit tests covering:
186+
The implementation includes 39 comprehensive unit tests covering:
121187
- Constructor validation
122188
- Default value generation
123189
- Platform-specific behavior
124190
- Method chaining
125191
- Error handling
192+
- JSON configuration file loading
193+
- Fallback behavior when JSON is invalid
126194
- Complete integration scenarios
127195

128196
Run tests with:
@@ -158,9 +226,28 @@ var config = new Configinfo
158226
};
159227
```
160228

161-
**After:**
229+
**After (with JSON configuration):**
230+
```json
231+
// update_config.json
232+
{
233+
"UpdateUrl": "https://api.example.com/updates",
234+
"Token": "my-token",
235+
"Scheme": "https",
236+
"AppName": "MyApp.exe",
237+
"ClientVersion": "1.0.0"
238+
}
239+
```
240+
```csharp
241+
var config = ConfiginfoBuilder
242+
.Create("fallback", "fallback", "https")
243+
.Build();
244+
```
245+
246+
**After (with code only):**
162247
```csharp
163-
var config = new ConfiginfoBuilder(url, token, scheme).Build();
248+
var config = ConfiginfoBuilder
249+
.Create(url, token, scheme)
250+
.Build();
164251
```
165252

166253
## Contributing
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"UpdateUrl": "https://api.example.com/updates",
3+
"Token": "your-authentication-token-here",
4+
"Scheme": "https",
5+
"AppName": "Update.exe",
6+
"MainAppName": "MyApplication.exe",
7+
"ClientVersion": "1.0.0",
8+
"UpgradeClientVersion": "1.0.0",
9+
"AppSecretKey": "your-secret-key-here",
10+
"ProductId": "your-product-id",
11+
"InstallPath": "/path/to/installation",
12+
"UpdateLogUrl": "https://example.com/changelog",
13+
"ReportUrl": "https://api.example.com/report",
14+
"Bowl": "Bowl.exe",
15+
"Script": "#!/bin/bash\nchmod +x *",
16+
"DriverDirectory": "/path/to/drivers",
17+
"BlackFiles": [
18+
"config.json",
19+
"user.dat"
20+
],
21+
"BlackFormats": [
22+
".log",
23+
".tmp",
24+
".cache",
25+
".bak"
26+
],
27+
"SkipDirectorys": [
28+
"/temp",
29+
"/logs"
30+
]
31+
}

0 commit comments

Comments
 (0)