-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathConfiginfoBuilder.cs
More file actions
413 lines (371 loc) · 17.6 KB
/
Copy pathConfiginfoBuilder.cs
File metadata and controls
413 lines (371 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace GeneralUpdate.Core.Configuration
{
/// <summary>
/// Universal ConfigInfo builder class that simplifies creation of update configurations.
/// Only requires three essential parameters (UpdateUrl, Token, Scheme) while automatically
/// generating platform-appropriate defaults for all other configuration items.
/// Inspired by zero-configuration design patterns from projects like Velopack.
/// </summary>
public class ConfiginfoBuilder
{
// Configurable default values
// Note: AppName and InstallPath defaults are set in Configinfo class itself
// These are ConfiginfoBuilder-specific defaults to support the builder pattern
private string _updateUrl;
private string _token;
private string _scheme;
private string _appName = "Update.exe";
private string _mainAppName;
private string _clientVersion;
private string _upgradeClientVersion;
private string _appSecretKey;
private string _productId;
private string _installPath;
private string _updateLogUrl;
private string _reportUrl;
private string _bowl;
private string _script;
private string _driverDirectory;
private List<string> _blackFiles;
private List<string> _blackFormats;
private List<string> _skipDirectorys;
/// <summary>
/// Creates a new ConfiginfoBuilder instance by loading configuration from update_config.json file.
/// The configuration file must exist in the running directory and contain all required settings.
/// Configuration file has the highest priority - all settings must be specified in the JSON file.
/// </summary>
/// <returns>A new ConfiginfoBuilder instance with settings loaded from the configuration file.</returns>
/// <exception cref="FileNotFoundException">Thrown when update_config.json is not found.</exception>
/// <exception cref="InvalidOperationException">Thrown when the configuration file is invalid or cannot be loaded.</exception>
public static ConfiginfoBuilder Create()
{
// Try to load from configuration file
var configFromFile = LoadFromConfigFile();
if (configFromFile != null)
{
// Configuration file loaded successfully
return configFromFile;
}
// If no config file exists, throw an exception
throw new FileNotFoundException("Configuration file 'update_config.json' not found in the running directory. Please create this file with the required settings.");
}
/// <summary>
/// Loads configuration from update_config.json file in the running directory.
/// </summary>
/// <returns>ConfiginfoBuilder with settings from file, or null if file doesn't exist or is invalid.</returns>
private static ConfiginfoBuilder LoadFromConfigFile()
{
try
{
var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update_config.json");
if (!File.Exists(configPath))
{
return null;
}
var json = File.ReadAllText(configPath);
var config = JsonSerializer.Deserialize<Configinfo>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
if (config == null)
{
return null;
}
// Create a builder with the loaded configuration
var builder = new ConfiginfoBuilder();
// Apply all loaded settings
if (!string.IsNullOrWhiteSpace(config.UpdateUrl))
builder.SetUpdateUrl(config.UpdateUrl);
if (!string.IsNullOrWhiteSpace(config.Token))
builder.SetToken(config.Token);
if (!string.IsNullOrWhiteSpace(config.Scheme))
builder.SetScheme(config.Scheme);
if (!string.IsNullOrWhiteSpace(config.AppName))
builder.SetAppName(config.AppName);
if (!string.IsNullOrWhiteSpace(config.MainAppName))
builder.SetMainAppName(config.MainAppName);
if (!string.IsNullOrWhiteSpace(config.ClientVersion))
builder.SetClientVersion(config.ClientVersion);
if (!string.IsNullOrWhiteSpace(config.UpgradeClientVersion))
builder.SetUpgradeClientVersion(config.UpgradeClientVersion);
if (!string.IsNullOrWhiteSpace(config.AppSecretKey))
builder.SetAppSecretKey(config.AppSecretKey);
if (!string.IsNullOrWhiteSpace(config.ProductId))
builder.SetProductId(config.ProductId);
if (!string.IsNullOrWhiteSpace(config.InstallPath))
builder.SetInstallPath(config.InstallPath);
if (!string.IsNullOrWhiteSpace(config.UpdateLogUrl))
builder.SetUpdateLogUrl(config.UpdateLogUrl);
if (!string.IsNullOrWhiteSpace(config.ReportUrl))
builder.SetReportUrl(config.ReportUrl);
if (!string.IsNullOrWhiteSpace(config.Bowl))
builder.SetBowl(config.Bowl);
if (!string.IsNullOrWhiteSpace(config.Script))
builder.SetScript(config.Script);
if (!string.IsNullOrWhiteSpace(config.DriverDirectory))
builder.SetDriverDirectory(config.DriverDirectory);
if (config.BlackFiles != null)
builder.SetBlackFiles(config.BlackFiles);
if (config.BlackFormats != null)
builder.SetBlackFormats(config.BlackFormats);
if (config.SkipDirectorys != null)
builder.SetSkipDirectorys(config.SkipDirectorys);
builder.SetInstallPath(string.IsNullOrWhiteSpace(config.InstallPath) ? AppDomain.CurrentDomain.BaseDirectory : config.InstallPath);
return builder;
}
catch (System.Text.Json.JsonException)
{
// Invalid JSON format, fall back to parameters
return null;
}
catch (IOException)
{
// File read error, fall back to parameters
return null;
}
catch (UnauthorizedAccessException)
{
// Permission denied, fall back to parameters
return null;
}
catch
{
// Any other unexpected error, fall back to parameters
return null;
}
}
public ConfiginfoBuilder SetUpdateUrl(string updateUrl)
{
if (string.IsNullOrWhiteSpace(updateUrl))
throw new ArgumentException("updateUrl cannot be null or empty.", nameof(updateUrl));
_updateUrl = updateUrl;
return this;
}
public ConfiginfoBuilder SetToken(string token)
{
if (string.IsNullOrWhiteSpace(token))
throw new ArgumentException("token cannot be null or empty.", nameof(token));
_token = token;
return this;
}
public ConfiginfoBuilder SetScheme(string scheme)
{
if (string.IsNullOrWhiteSpace(scheme))
throw new ArgumentException("scheme cannot be null or empty.", nameof(scheme));
_scheme = scheme;
return this;
}
/// <summary>
/// Sets the application name (executable to start after update).
/// </summary>
/// <param name="appName">The name of the application executable.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetAppName(string appName)
{
if (string.IsNullOrWhiteSpace(appName))
throw new ArgumentException("AppName cannot be null or empty.", nameof(appName));
_appName = appName;
return this;
}
/// <summary>
/// Sets the main application name.
/// </summary>
/// <param name="mainAppName">The name of the main application without file extension.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetMainAppName(string mainAppName)
{
if (string.IsNullOrWhiteSpace(mainAppName))
throw new ArgumentException("MainAppName cannot be null or empty.", nameof(mainAppName));
_mainAppName = mainAppName;
return this;
}
/// <summary>
/// Sets the client version.
/// </summary>
/// <param name="clientVersion">The current version of the client application.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetClientVersion(string clientVersion)
{
if (string.IsNullOrWhiteSpace(clientVersion))
throw new ArgumentException("ClientVersion cannot be null or empty.", nameof(clientVersion));
_clientVersion = clientVersion;
return this;
}
/// <summary>
/// Sets the upgrade client version.
/// </summary>
/// <param name="upgradeClientVersion">The current version of the upgrade application.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetUpgradeClientVersion(string upgradeClientVersion)
{
if (string.IsNullOrWhiteSpace(upgradeClientVersion))
throw new ArgumentException("UpgradeClientVersion cannot be null or empty.", nameof(upgradeClientVersion));
_upgradeClientVersion = upgradeClientVersion;
return this;
}
/// <summary>
/// Sets the application secret key.
/// </summary>
/// <param name="appSecretKey">The secret key used for authentication.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetAppSecretKey(string appSecretKey)
{
if (string.IsNullOrWhiteSpace(appSecretKey))
throw new ArgumentException("AppSecretKey cannot be null or empty.", nameof(appSecretKey));
_appSecretKey = appSecretKey;
return this;
}
/// <summary>
/// Sets the product identifier.
/// </summary>
/// <param name="productId">The unique product identifier.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetProductId(string productId)
{
if (string.IsNullOrWhiteSpace(productId))
throw new ArgumentException("ProductId cannot be null or empty.", nameof(productId));
_productId = productId;
return this;
}
/// <summary>
/// Sets the installation path.
/// </summary>
/// <param name="installPath">The installation path where application files are located.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetInstallPath(string installPath)
{
if (string.IsNullOrWhiteSpace(installPath))
throw new ArgumentException("InstallPath cannot be null or empty.", nameof(installPath));
_installPath = installPath;
return this;
}
/// <summary>
/// Sets the update log URL.
/// </summary>
/// <param name="updateLogUrl">The URL address for the update log webpage.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetUpdateLogUrl(string updateLogUrl)
{
if (!string.IsNullOrWhiteSpace(updateLogUrl) && !Uri.IsWellFormedUriString(updateLogUrl, UriKind.Absolute))
throw new ArgumentException("UpdateLogUrl must be a valid absolute URI.", nameof(updateLogUrl));
_updateLogUrl = updateLogUrl;
return this;
}
/// <summary>
/// Sets the report URL.
/// </summary>
/// <param name="reportUrl">The API endpoint URL for reporting update status and results.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetReportUrl(string reportUrl)
{
if (!string.IsNullOrWhiteSpace(reportUrl) && !Uri.IsWellFormedUriString(reportUrl, UriKind.Absolute))
throw new ArgumentException("ReportUrl must be a valid absolute URI.", nameof(reportUrl));
_reportUrl = reportUrl;
return this;
}
/// <summary>
/// Sets the bowl process name.
/// </summary>
/// <param name="bowl">The process name that should be terminated before starting the update.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetBowl(string bowl)
{
_bowl = bowl;
return this;
}
/// <summary>
/// Sets the shell script content.
/// </summary>
/// <param name="script">Shell script content used to grant file permissions on Linux/Unix systems.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetScript(string script)
{
_script = script;
return this;
}
/// <summary>
/// Sets the driver directory.
/// </summary>
/// <param name="driverDirectory">The directory path containing driver files for driver update functionality.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetDriverDirectory(string driverDirectory)
{
_driverDirectory = driverDirectory;
return this;
}
/// <summary>
/// Sets the list of blacklisted files.
/// </summary>
/// <param name="blackFiles">List of specific files that should be excluded from the update process.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetBlackFiles(List<string> blackFiles)
{
_blackFiles = blackFiles ?? new List<string>();
return this;
}
/// <summary>
/// Sets the list of blacklisted file formats.
/// </summary>
/// <param name="blackFormats">List of file format extensions that should be excluded from the update process.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetBlackFormats(List<string> blackFormats)
{
_blackFormats = blackFormats ?? new List<string>();
return this;
}
/// <summary>
/// Sets the list of directories to skip.
/// </summary>
/// <param name="skipDirectorys">List of directory paths that should be skipped during the update process.</param>
/// <returns>The current ConfiginfoBuilder instance for method chaining.</returns>
public ConfiginfoBuilder SetSkipDirectorys(List<string> skipDirectorys)
{
_skipDirectorys = skipDirectorys ?? new List<string>();
return this;
}
/// <summary>
/// Builds and returns a complete Configinfo object with all configured and default values.
/// </summary>
/// <returns>A fully configured Configinfo instance.</returns>
/// <exception cref="InvalidOperationException">Thrown when the builder is in an invalid state.</exception>
public Configinfo Build()
{
// Create the Configinfo object with all values
var configinfo = new Configinfo
{
UpdateUrl = _updateUrl,
Token = _token,
Scheme = _scheme,
AppName = _appName,
MainAppName = _mainAppName,
ClientVersion = _clientVersion,
UpgradeClientVersion = _upgradeClientVersion,
AppSecretKey = _appSecretKey,
ProductId = _productId,
InstallPath = _installPath,
UpdateLogUrl = _updateLogUrl,
ReportUrl = _reportUrl,
Bowl = _bowl,
Script = _script,
DriverDirectory = _driverDirectory,
BlackFiles = _blackFiles ?? new List<string>(),
BlackFormats = _blackFormats ?? new List<string>(),
SkipDirectorys = _skipDirectorys ?? new List<string>()
};
// Validate the built configuration
try
{
configinfo.Validate();
}
catch (ArgumentException ex)
{
throw new InvalidOperationException($"Failed to build valid Configinfo: {ex.Message}", ex);
}
return configinfo;
}
}
}