Skip to content

Commit a83dba8

Browse files
committed
Add backup option to update process
Introduces a new UpdateOption.BackUp flag to control whether backups are performed during updates. Backup logic is now conditional on this option, and related documentation is updated. Also simplifies error handling in custom option execution and hash middleware.
1 parent 9bcb424 commit a83dba8

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

src/c#/GeneralUpdate.Client/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static async Task Main(string[] args)
5252
.Option(UpdateOption.DownloadTimeOut, 60)
5353
.Option(UpdateOption.Encoding, Encoding.UTF8)
5454
.Option(UpdateOption.Patch, false)
55+
.Option(UpdateOption.BackUp, false)
5556
.LaunchAsync();
5657
Console.WriteLine($"主程序已启动,{DateTime.Now}!");
5758
}

src/c#/GeneralUpdate.ClientCore/GeneralClientBootstrap.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,13 @@ private async Task ExecuteWorkflowAsync()
214214
JsonSerializer.Serialize(processInfo, ProcessInfoJsonContext.Default.ProcessInfo);
215215
}
216216

217-
StorageManager.Backup(_configInfo.InstallPath
218-
, _configInfo.BackupDirectory
219-
, BlackListManager.Instance.SkipDirectorys);
220-
217+
if (GetOption(UpdateOption.BackUp) ?? false)
218+
{
219+
StorageManager.Backup(_configInfo.InstallPath
220+
, _configInfo.BackupDirectory
221+
, BlackListManager.Instance.SkipDirectorys);
222+
}
223+
221224
StrategyFactory();
222225
switch (_configInfo.IsUpgradeUpdate)
223226
{
@@ -386,19 +389,10 @@ private void ExecuteCustomOptions()
386389

387390
foreach (var option in _customOptions)
388391
{
389-
try
390-
{
391-
if (!option.Invoke())
392-
{
393-
var exception = new Exception($"{nameof(option)}Execution failure!");
394-
var args = new ExceptionEventArgs(exception, exception.Message);
395-
EventManager.Instance.Dispatch(this, args);
396-
}
397-
}
398-
catch (Exception exception)
392+
if (!option.Invoke())
399393
{
400-
Debug.WriteLine(exception.Message);
401-
var args = new ExceptionEventArgs(exception, $"{nameof(option)}Execution failure!");
394+
var exception = new Exception($"{nameof(option)}Execution failure!");
395+
var args = new ExceptionEventArgs(exception, exception.Message);
402396
EventManager.Instance.Dispatch(this, args);
403397
}
404398
}

src/c#/GeneralUpdate.ClientCore/Pipeline/HashMiddleware.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,9 @@ private Task<bool> VerifyFileHash(string path, string hash)
2323
{
2424
return Task.Run(() =>
2525
{
26-
try
27-
{
28-
var hashAlgorithm = new Sha256HashAlgorithm();
29-
var hashSha256 = hashAlgorithm.ComputeHash(path);
30-
return string.Equals(hash, hashSha256, StringComparison.OrdinalIgnoreCase);
31-
}
32-
catch (Exception exception)
33-
{
34-
Debug.WriteLine(exception.Message);
35-
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(exception, exception.Message));
36-
}
37-
return false;
26+
var hashAlgorithm = new Sha256HashAlgorithm();
27+
var hashSha256 = hashAlgorithm.ComputeHash(path);
28+
return string.Equals(hash, hashSha256, StringComparison.OrdinalIgnoreCase);
3829
});
3930
}
4031
}

src/c#/GeneralUpdate.Common/Internal/Bootstrap/UpdateOption.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ private class UpdateOptionPool : ConstantPool
3838
public static readonly UpdateOption<bool?> Drive = ValueOf<bool?>("DRIVE");
3939

4040
/// <summary>
41-
/// Whether to enable the patch upgrade function.
41+
/// Whether to enable the patch function.
4242
/// </summary>
4343
public static readonly UpdateOption<bool?> Patch = ValueOf<bool?>("PATCH");
4444

45+
/// <summary>
46+
/// Whether to enable the backup function.
47+
/// </summary>
48+
public static readonly UpdateOption<bool?> BackUp = ValueOf<bool?>("BACKUP");
49+
4550
internal UpdateOption(int id, string name)
4651
: base(id, name) { }
4752

0 commit comments

Comments
 (0)