From a40656b48b0208cc22fc4c6a60eda0436c862f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=89=E4=BA=9E=E5=9D=A4?= <13140659949@163.com> Date: Fri, 14 Jan 2022 10:19:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E5=8F=91=E5=B8=83=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- SageTools.Console/Program.cs | 61 +++++++++++++++++++++++++------ SageTools/Extension/Collection.cs | 28 +++++++++++++- SageTools/Extension/DateTime.cs | 2 +- SageTools/Extension/Number.cs | 1 + SageTools/SageTools.csproj | 2 +- 6 files changed, 81 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index d0b1b78..c7e09d4 100644 --- a/.gitignore +++ b/.gitignore @@ -369,4 +369,5 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd +/pack diff --git a/SageTools.Console/Program.cs b/SageTools.Console/Program.cs index 5444795..e85501a 100644 --- a/SageTools.Console/Program.cs +++ b/SageTools.Console/Program.cs @@ -1,5 +1,8 @@ using System; +using System.IO; using SageTools.Extension; +using System.Linq; +using System.Xml; namespace SageTools.Console { @@ -7,16 +10,52 @@ internal class Program { static void Main(string[] args) { - - System.Console.WriteLine(DateTime.Now.AddDays(-1).EndOfDay()); - - var time1 = new DateTime(DateTime.Now.Year, 1, 1); - System.Console.WriteLine(time1.EndOfDay()); - System.Console.WriteLine(DateTime.Now.IsAfternoon()); - System.Console.WriteLine(DateTime.Now.IsMorning()); - System.Console.WriteLine(DateTime.Now.IsNow()); - System.Console.ReadKey(); + var help = args.Any(x => x.Contains("-h")); + if (help) + { + System.Console.WriteLine($"-source= 文件相对路径 {Environment.NewLine}-step= 版本号步长值(整数){Environment.NewLine}-version= 直接指定版本,忽略-step"); + 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 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 versionNode = xmlDoc.DocumentElement?.SelectSingleNode("PropertyGroup")?.SelectSingleNode("Version"); + if (versionNode == null) + { + System.Console.WriteLine("执行错误:项目没有配置版本号"); + Environment.Exit(-1); + } + if (version.IsNullOrEmpty()) + { + version = versionNode.InnerText; + var lastNum = version.Substring(version.LastIndexOf(".", StringComparison.Ordinal) + 1); + var newNum = lastNum.ToInt32() + step; + var newVersion = version.Substring(0, version.LastIndexOf(".", StringComparison.Ordinal) + 1) + newNum; + versionNode.InnerText = newVersion; + System.Console.WriteLine($"当前版本{version},末尾版本号{lastNum},新版本号{newVersion}"); + } + else + { + System.Console.WriteLine($"直接指定新版本号为 {version}"); + versionNode.InnerText = version; + } + xmlDoc.Save(path); + System.Console.WriteLine("处理完毕!"); + System.Console.ReadKey(); } } - -} +} \ No newline at end of file diff --git a/SageTools/Extension/Collection.cs b/SageTools/Extension/Collection.cs index a145e09..f3aadff 100644 --- a/SageTools/Extension/Collection.cs +++ b/SageTools/Extension/Collection.cs @@ -73,13 +73,13 @@ public static IList ToPagedList(this ICollection allItems, int pageInde /// 每页大小 public static void PagingToOperate(this ICollection list, Action> action, int pageSize = 1000) { - if (list.IsNullOrEmpty()) + if (list.IsNullOrEmpty() || list.Count <= pageSize) { action(list); } else { - var pageCount = Math.Ceiling(list.Count * 1d / pageSize); + var pageCount = list.Count.ToPageCount(pageSize); for (var pageIndex = 0; pageIndex < pageCount; pageIndex++) { var items = list.Skip(pageIndex * pageSize).Take(pageSize).ToList(); @@ -87,5 +87,29 @@ public static void PagingToOperate(this ICollection list, Action + /// 分页执行操作,返回当前页码 + /// + /// + /// 源数据集合 + /// 要进行的操作 + /// 每页大小 + public static void PagingToOperate(this ICollection list, Action,int> action, int pageSize = 1000) + { + if (list.IsNullOrEmpty() || list.Count <= pageSize) + { + action(list,1); + } + else + { + var pageCount = list.Count.ToPageCount(pageSize); + for (var pageIndex = 0; pageIndex < pageCount; pageIndex++) + { + var items = list.Skip(pageIndex * pageSize).Take(pageSize).ToList(); + action(items, pageIndex + 1); + } + } + } } } \ No newline at end of file diff --git a/SageTools/Extension/DateTime.cs b/SageTools/Extension/DateTime.cs index 8bdda8d..b9f0d60 100644 --- a/SageTools/Extension/DateTime.cs +++ b/SageTools/Extension/DateTime.cs @@ -170,7 +170,7 @@ private static string ToRelativeTimeSimple(TimeSpan ts, string sign) /// /// /// - public static long GetTimeStamp(this DateTime @this) + public static long ToTimeStamp(this DateTime @this) { return (long)(@this - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; } diff --git a/SageTools/Extension/Number.cs b/SageTools/Extension/Number.cs index ba7df34..0e4495f 100644 --- a/SageTools/Extension/Number.cs +++ b/SageTools/Extension/Number.cs @@ -114,6 +114,7 @@ public static bool ToBool(this int num) public static int ToPageCount(this int totalCount,int pageSize) { if(totalCount==0 || pageSize==0)return 0; + if (totalCount <= pageSize) return 1; if (pageSize == 1) return totalCount; return (int)Math.Ceiling(1D * totalCount / pageSize); } diff --git a/SageTools/SageTools.csproj b/SageTools/SageTools.csproj index 82c8d8b..91bf1fa 100644 --- a/SageTools/SageTools.csproj +++ b/SageTools/SageTools.csproj @@ -7,7 +7,7 @@ LiuDanK 常用工具类、拓展方法等封装,目前还在实现中 Copyright © LiuDanK 2021 - 1.0.0 + 1.0.0