From 4e2e60cb83a08a77318f8303185d10528400c414 Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Sat, 30 Dec 2023 23:20:40 +0800 Subject: [PATCH 1/2] test --- src/c#/GeneralUpdate.Client/MySample.cs | 71 ++++++++++++------------- src/c#/GeneralUpdate.Client/Program.cs | 1 + 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/c#/GeneralUpdate.Client/MySample.cs b/src/c#/GeneralUpdate.Client/MySample.cs index 26f4fde..d45be17 100644 --- a/src/c#/GeneralUpdate.Client/MySample.cs +++ b/src/c#/GeneralUpdate.Client/MySample.cs @@ -39,7 +39,7 @@ namespace GeneralUpdate.Client #region 常规更新 - public void Upgrade() + public async Task Upgrade() { //Task.Run(async () => //{ @@ -51,42 +51,39 @@ namespace GeneralUpdate.Client // await GeneralClientOSS.Start(@params); //}); - Task.Run(async () => - { - //ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。 - //generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C"). - var configinfo = GetWindowsConfigInfo(); - var generalClientBootstrap = await new GeneralClientBootstrap() - //单个或多个更新包下载通知事件 - .AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged) - //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件 - .AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics) - //单个或多个更新包下载完成 - .AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted) - //完成所有的下载任务通知 - .AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted) - //下载过程出现的异常通知 - .AddListenerMultiDownloadError(OnMultiDownloadError) - //整个更新过程出现的任何问题都会通过这个事件通知 - .AddListenerException(OnException) - .Config(configinfo) - .Option(UpdateOption.DownloadTimeOut, 60) - .Option(UpdateOption.Encoding, Encoding.Default) - .Option(UpdateOption.Format, Format.ZIP) - //开启驱动更新 - .Option(UpdateOption.Drive, true) - //开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。 - //.Option(UpdateOption.WillMessage, true) - .Strategy() - //注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效 - //.SetCustomSkipOption(ShowCustomOption) - //注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境) - //.AddCustomOption(new List>() { () => Check1(), () => Check2() }) - //默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" } - //如果不需要扩展,需要重新传入黑名单集合来覆盖。 - //.SetBlacklist(GetBlackFiles(), GetBlackFormats()) - .LaunchTaskAsync(); - }); + //ClientStrategy该更新策略将完成1.自动升级组件自更新 2.启动更新组件 3.配置好ClientParameter无需再像之前的版本写args数组进程通讯了。 + //generalClientBootstrap.Config(baseUrl, "B8A7FADD-386C-46B0-B283-C9F963420C7C"). + var configinfo = GetWindowsConfigInfo(); + var generalClientBootstrap = await new GeneralClientBootstrap() + //单个或多个更新包下载通知事件 + .AddListenerMultiDownloadProgress(OnMultiDownloadProgressChanged) + //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件 + .AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics) + //单个或多个更新包下载完成 + .AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted) + //完成所有的下载任务通知 + .AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted) + //下载过程出现的异常通知 + .AddListenerMultiDownloadError(OnMultiDownloadError) + //整个更新过程出现的任何问题都会通过这个事件通知 + .AddListenerException(OnException) + .Config(configinfo) + .Option(UpdateOption.DownloadTimeOut, 60) + .Option(UpdateOption.Encoding, Encoding.Default) + .Option(UpdateOption.Format, Format.ZIP) + //开启驱动更新 + .Option(UpdateOption.Drive, true) + //开启遗言功能,需要部署GeneralUpdate.SystemService Windows服务。 + .Option(UpdateOption.WillMessage, true) + .Strategy() + //注入一个func让用户决定是否跳过本次更新,如果是强制更新则不生效 + //.SetCustomSkipOption(ShowCustomOption) + //注入一个自定义方法集合,该集合会在更新启动前执行。执行自定义方法列表如果出现任何异常,将通过异常订阅通知。(推荐在更新之前检查当前软件环境) + //.AddCustomOption(new List>() { () => Check1(), () => Check2() }) + //默认黑名单文件: { "Newtonsoft.Json.dll" } 默认黑名单文件扩展名: { ".patch", ".7z", ".zip", ".rar", ".tar" , ".json" } + //如果不需要扩展,需要重新传入黑名单集合来覆盖。 + //.SetBlacklist(GetBlackFiles(), GetBlackFormats()) + .LaunchTaskAsync(); } private bool Check1() => true; diff --git a/src/c#/GeneralUpdate.Client/Program.cs b/src/c#/GeneralUpdate.Client/Program.cs index 3e37065..d2b5080 100644 --- a/src/c#/GeneralUpdate.Client/Program.cs +++ b/src/c#/GeneralUpdate.Client/Program.cs @@ -9,6 +9,7 @@ MySample sample = new MySample(); //await sample.TestDifferentialClean(); //await sample.TestDifferentialDirty(); + await sample.Upgrade(); }); Console.Read(); } -- Gitee From 04ae971a42cad8aa7a3af05828bd7c24b266c21f Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Tue, 2 Jan 2024 23:14:00 +0800 Subject: [PATCH 2/2] feature: add throw util --- .../Exceptions/ThrowExceptionUtility.cs | 48 +++++++++++++++++++ src/c#/GeneralUpdate.Upgrad/Program.cs | 10 ++-- 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs diff --git a/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs new file mode 100644 index 0000000..013a253 --- /dev/null +++ b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs @@ -0,0 +1,48 @@ +using GeneralUpdate.Core.Exceptions.CustomArgs; +using System; + +namespace GeneralUpdate.Core.Exceptions +{ + internal sealed class ThrowExceptionUtility + { + public static void ThrowGeneralUpdateException(ExceptionArgs args) + => Throw(args.ToString(), args); + + #region Common + + /// + /// Checks if an object is empty and throws an exception if it is + /// + /// + /// + /// + public static void ThrowIfNull(object obj, string paramName) + { + if (obj == null) + Throw(paramName); + } + + /// + /// Checks if the string is empty or blank, and throws an exception if it is. + /// + /// + /// + /// + public static void ThrowIfNullOrWhiteSpace(string str, string paramName) + { + if (string.IsNullOrWhiteSpace(str)) + Throw("Parameter cannot be null or whitespace", paramName); + } + + /// + /// Basic method of exception declaration. + /// + /// + /// + /// + public static void Throw(string message, params object[] args) where T : Exception, new() + => throw (T)Activator.CreateInstance(typeof(T), message, args); + + #endregion + } +} diff --git a/src/c#/GeneralUpdate.Upgrad/Program.cs b/src/c#/GeneralUpdate.Upgrad/Program.cs index 6189220..0a9e88a 100644 --- a/src/c#/GeneralUpdate.Upgrad/Program.cs +++ b/src/c#/GeneralUpdate.Upgrad/Program.cs @@ -50,11 +50,11 @@ namespace GeneralUpdate.Upgrad .AddListenerMultiDownloadError(OnMultiDownloadError) //整个更新过程出现的任何问题都会通过这个事件通知 .AddListenerException(OnException) - .Strategy(). - Option(UpdateOption.Encoding, Encoding.Default). - Option(UpdateOption.DownloadTimeOut, 60). - Option(UpdateOption.Format, Format.ZIP). - LaunchTaskAsync(); + .Strategy() + .Option(UpdateOption.Encoding, Encoding.Default) + .Option(UpdateOption.DownloadTimeOut, 60) + .Option(UpdateOption.Format, Format.ZIP) + .LaunchTaskAsync(); }); Console.Read(); } -- Gitee