From 5239ce966ecf728c39a061954e263cbeb982d49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=89=E4=BA=9E=E5=9D=A4?= <13140659949@163.com> Date: Mon, 9 Jan 2023 13:28:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=91=BD=E5=90=8D=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ShortIdGen.cs => ShortIdGenUtil.cs} | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) rename SageTools/Utils/{ShortIdGen.cs => ShortIdGenUtil.cs} (87%) diff --git a/SageTools/Utils/ShortIdGen.cs b/SageTools/Utils/ShortIdGenUtil.cs similarity index 87% rename from SageTools/Utils/ShortIdGen.cs rename to SageTools/Utils/ShortIdGenUtil.cs index af792d8..1be41f0 100644 --- a/SageTools/Utils/ShortIdGen.cs +++ b/SageTools/Utils/ShortIdGenUtil.cs @@ -8,23 +8,23 @@ namespace SageTools.Utils /// 短 ID 生成类 /// 代码参考自:https://github.com/MonkSoul/Furion /// - public static class ShortIdGen + public static class ShortIdGenUtil { /// /// 短 ID 生成器期初数据 /// private static Random _random = new Random(); - private const string BIG = "ABCDEFGHIJKLMNPQRSTUVWXY"; - private const string SMALL = "abcdefghjklmnopqrstuvwxyz"; - private const string NUMBER = "0123456789"; - private const string SPECIAL = "_-"; - private static string _pool = $"{SMALL}{BIG}"; + private const string Big = "ABCDEFGHIJKLMNPQRSTUVWXY"; + private const string Small = "abcdefghjklmnopqrstuvwxyz"; + private const string Number = "0123456789"; + private const string Special = "_-"; + private static string _pool = $"{Small}{Big}"; /// /// 线程安全锁 /// - private static readonly object _threadLock = new object(); + private static readonly object ThreadLock = new object(); /// /// 生成目前比较主流的短 ID @@ -61,19 +61,19 @@ public static string NextId(GenerationOptions options) throw new ArgumentException( $"The specified length of {options.Length} is less than the lower limit of {Constants.MINIMUM_AUTO_LENGTH} to avoid conflicts."); } - var characterPool = _pool; + var characterPool = ShortIdGenUtil._pool; var poolBuilder = new StringBuilder(characterPool); // 是否包含数字 if (options.UseNumbers) { - poolBuilder.Append(NUMBER); + poolBuilder.Append(Number); } // 是否包含特殊字符 if (options.UseSpecialCharacters) { - poolBuilder.Append(SPECIAL); + poolBuilder.Append(Special); } var pool = poolBuilder.ToString(); @@ -81,7 +81,7 @@ public static string NextId(GenerationOptions options) var output = new char[options.Length]; for (var i = 0; i < options.Length; i++) { - lock (_threadLock) + lock (ThreadLock) { var charIndex = _random.Next(0, pool.Length); output[i] = pool[charIndex]; @@ -110,7 +110,7 @@ public static void SetCharacters(string characters) throw new InvalidOperationException( $"The replacement characters must be at least {Constants.MINIMUM_CHARACTER_SET_LENGTH} letters in length and without whitespace."); } - lock (_threadLock) + lock (ThreadLock) { _pool = new string(charSet); } @@ -122,7 +122,7 @@ public static void SetCharacters(string characters) /// public static void SetSeed(int seed) { - lock (_threadLock) + lock (ThreadLock) { _random = new Random(seed); } @@ -133,10 +133,10 @@ public static void SetSeed(int seed) /// public static void Reset() { - lock (_threadLock) + lock (ThreadLock) { _random = new Random(); - _pool = $"{SMALL}{BIG}"; + _pool = $"{Small}{Big}"; } } From 497c42dfbe80fb89e07824f1968f7a871d824192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=89=E4=BA=9E=E5=9D=A4?= <13140659949@163.com> Date: Mon, 9 Jan 2023 13:47:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SageTools.Console/Extension.cs | 14 +++++++ SageTools.Console/Program.cs | 37 ++++++++++--------- .../Properties/launchSettings.json | 7 ++++ SageTools/SageTools.csproj | 6 +-- 4 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 SageTools.Console/Extension.cs create mode 100644 SageTools.Console/Properties/launchSettings.json diff --git a/SageTools.Console/Extension.cs b/SageTools.Console/Extension.cs new file mode 100644 index 0000000..4559b69 --- /dev/null +++ b/SageTools.Console/Extension.cs @@ -0,0 +1,14 @@ +using System.Linq; +using SageTools.Extension; + +namespace SageTools.Console; + +public static class Extension +{ + public static string GetArgValue(this string[] args, string argKey) + { + if (args.IsNullOrEmpty() || argKey.IsNullOrWhiteSpace()) return default; + argKey = '-' + argKey.TrimStart('-').TrimEnd('=') + '='; + return args.Where(x => x.Contains(argKey)).Select(x => x.Replace(argKey, "")).FirstOrDefault(); + } +} \ No newline at end of file diff --git a/SageTools.Console/Program.cs b/SageTools.Console/Program.cs index e19237b..71f51b2 100644 --- a/SageTools.Console/Program.cs +++ b/SageTools.Console/Program.cs @@ -1,14 +1,14 @@ using System; using System.IO; -using SageTools.Extension; using System.Linq; using System.Xml; +using SageTools.Extension; namespace SageTools.Console { internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { var help = args.Any(x => x.Contains("-h")); if (help) @@ -17,42 +17,45 @@ static void Main(string[] args) Environment.Exit(0); } System.Console.WriteLine("处理xml文件"); - var source = args.Where(x => x.Contains("-source=")).Select(x => x.Replace("-source=", "")).FirstOrDefault(); - var step = args.Where(x => x.Contains("-step=")).Select(x => x.Replace("-step=", "")).FirstOrDefault(); - var version = args.Where(x => x.Contains("-version=")).Select(x => x.Replace("-version=", "")).FirstOrDefault(); - if (source.IsNullOrEmpty()) - { - System.Console.WriteLine("执行错误:必须指定 -source"); - Environment.Exit(-1); - } + var source = args.GetArgValue("source"); + var step = args.GetArgValue("step"); + var version = args.GetArgValue("version"); + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, source); if (!File.Exists(path)) { System.Console.WriteLine($"执行错误:【{path}】文件不存在"); Environment.Exit(-1); } - var xmlDoc = new XmlDocument(); //新建XML文件 - xmlDoc.Load(path); //加载XML文件 + var xmlDoc = new XmlDocument(); + xmlDoc.Load(path); var versionNode = xmlDoc.DocumentElement?.SelectSingleNode("PropertyGroup")?.SelectSingleNode("Version"); - if (versionNode == null) + if (versionNode.IsNull()) { System.Console.WriteLine("执行错误:项目没有配置版本号"); Environment.Exit(-1); } if (version.IsNullOrEmpty()) { - version = versionNode.InnerText; - var lastNum = version.Substring(version.LastIndexOf(".", StringComparison.Ordinal) + 1); + version = versionNode!.InnerText; + var lastNum = version[(version.LastIndexOf(".", StringComparison.Ordinal) + 1)..]; var newNum = lastNum.ToInt32() + step; - var newVersion = version.Substring(0, version.LastIndexOf(".", StringComparison.Ordinal) + 1) + newNum; + var newVersion = version[..(version.LastIndexOf(".", StringComparison.Ordinal) + 1)] + newNum; versionNode.InnerText = newVersion; System.Console.WriteLine($"当前版本{version},末尾版本号{lastNum},新版本号{newVersion}"); } else { System.Console.WriteLine($"直接指定新版本号为 {version}"); - versionNode.InnerText = version; + versionNode!.InnerText = version; } + + var copyright = xmlDoc.DocumentElement?.SelectSingleNode("PropertyGroup")?.SelectSingleNode("Copyright"); + if (copyright.IsNotNull()) + { + copyright!.InnerText = $"Copyright © LiuDanK 2021-{DateTime.Now.Year}"; + } + xmlDoc.Save(path); System.Console.WriteLine("处理完毕!"); } diff --git a/SageTools.Console/Properties/launchSettings.json b/SageTools.Console/Properties/launchSettings.json new file mode 100644 index 0000000..e9aba79 --- /dev/null +++ b/SageTools.Console/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "SageTools.Console": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/SageTools/SageTools.csproj b/SageTools/SageTools.csproj index 91bf1fa..674fb39 100644 --- a/SageTools/SageTools.csproj +++ b/SageTools/SageTools.csproj @@ -1,5 +1,4 @@ - netstandard2.1 true @@ -7,7 +6,6 @@ LiuDanK 常用工具类、拓展方法等封装,目前还在实现中 Copyright © LiuDanK 2021 - 1.0.0 + 1.8.6 - - + \ No newline at end of file