From d83db53acb28e2127f3d60a17cade8afa8fb8906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Tue, 5 Nov 2024 22:12:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90tool=E6=90=AD?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tool/Yi.Abp.Tool/CommandInvoker.cs | 19 +- .../Yi.Abp.Tool/Commands/AddModuleCommand.cs | 244 +++++++++--------- .../tool/Yi.Abp.Tool/Commands/ClearCommand.cs | 110 ++++---- .../tool/Yi.Abp.Tool/Commands/CloneCommand.cs | 110 ++++---- .../tool/Yi.Abp.Tool/Commands/NewCommand.cs | 174 +++++++------ .../tool/Yi.Abp.Tool/Commands/TestCommand.cs | 20 -- Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs | 3 +- Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs | 8 +- 8 files changed, 352 insertions(+), 336 deletions(-) delete mode 100644 Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/TestCommand.cs diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandInvoker.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandInvoker.cs index 5972ad5f..affa99d6 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandInvoker.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandInvoker.cs @@ -23,22 +23,23 @@ namespace Yi.Abp.Tool private void InitCommand() { - Application.HelpOption("-h"); - + Application.HelpOption("-h|--help"); + Application.VersionOption("-v|--versions","1.0.0"); foreach (var command in _commands) { - Application.Command(command.Command, con => command.CommandLineApplicationAsync(con).Wait()); + CommandLineApplication childrenCommandLineApplication = new CommandLineApplication(true) + { + Name = command.Command, + Parent = Application, + Description =command.Description + }; + Application.Commands.Add(childrenCommandLineApplication); + command.CommandLineApplication(childrenCommandLineApplication); } } public async Task InvokerAsync(string[] args) { - //使用哪个命令,根据第一参数来判断,如果都不是,打印help - // foreach (var commandLineApplication in Application.Commands) - // { - // commandLineApplication.Execute(args); - // } - Application.Execute(args); } } diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddModuleCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddModuleCommand.cs index 16c6d07f..a1d92984 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddModuleCommand.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddModuleCommand.cs @@ -1,121 +1,123 @@ -// using System; -// using System.Collections.Generic; -// using System.Diagnostics; -// using System.Linq; -// using System.Text; -// using System.Threading.Tasks; -// -// namespace Yi.Abp.Tool.Commands -// { -// public class AddModuleCommand : ICommand -// { -// public List CommandStrs => new List { "add-module" }; -// -// public async Task InvokerAsync(Dictionary options, string[] args) -// { -// //只有一个add-module -// if (args.Length <= 1) -// { -// throw new UserFriendlyException("命令错误,add-module命令后必须添加 模块名"); -// } -// -// //需要添加名称 -// var moduleName = args[1]; -// options.TryGetValue("modulePath", out var modulePath); -// -// //模块路径默认按小写规则,当前路径 -// if (string.IsNullOrEmpty(modulePath)) -// { -// modulePath = moduleName.ToLower().Replace(".", "-"); -// } -// -// -// //解决方案默认在模块文件夹上一级,也可以通过s进行指定 -// var slnPath = string.Empty; -// options.TryGetValue("s", out var slnPath1); -// options.TryGetValue("solution", out var slnPath2); -// slnPath = string.IsNullOrEmpty(slnPath1) ? slnPath2 : slnPath1; -// if (string.IsNullOrEmpty(slnPath)) -// { -// slnPath = "../"; -// } -// -// CheckFirstSlnPath(slnPath); -// var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" add \"{modulePath}\\{moduleName}."; -// var dotnetSlnCommandPart2 = new List() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" }; -// var paths = dotnetSlnCommandPart2.Select(x => $@"{modulePath}\{moduleName}." + x).ToArray(); -// CheckPathExist(paths); -// -// var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray(); -// StartCmd(cmdCommands); -// -// await Console.Out.WriteLineAsync("恭喜~模块添加成功!"); -// } -// -// /// -// /// 获取一个sln解决方案,多个将报错 -// /// -// /// -// private string CheckFirstSlnPath(string slnPath) -// { -// string[] slnFiles = Directory.GetFiles(slnPath, "*.sln"); -// if (slnFiles.Length > 1) -// { -// throw new UserFriendlyException("当前目录包含多个sln解决方案,请只保留一个"); -// } -// if (slnFiles.Length == 0) -// { -// throw new UserFriendlyException("当前目录未找到sln解决方案,请检查"); -// } -// -// return slnFiles[0]; -// } -// -// -// /// -// /// 执行cmd命令 -// /// -// /// -// private void StartCmd(params string[] cmdCommands) -// { -// ProcessStartInfo psi = new ProcessStartInfo -// { -// FileName = "cmd.exe", -// Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}", -// RedirectStandardInput = true, -// RedirectStandardOutput = true, -// RedirectStandardError = true, -// CreateNoWindow = true, -// UseShellExecute = false -// }; -// -// Process proc = new Process -// { -// StartInfo = psi -// }; -// -// proc.Start(); -// string output = proc.StandardOutput.ReadToEnd(); -// Console.WriteLine(output); -// -// proc.WaitForExit(); -// } -// -// -// /// -// /// 检查路径 -// /// -// /// -// /// -// private void CheckPathExist(string[] paths) -// { -// foreach (string path in paths) -// { -// if (!Directory.Exists(path)) -// { -// throw new UserFriendlyException($"路径错误,请检查你的路径,找不到:{path}"); -// } -// } -// } -// } -// } +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.CommandLineUtils; + +namespace Yi.Abp.Tool.Commands +{ + public class AddModuleCommand : ICommand + { + public string Command => "add-module"; + public string? Description { get; } + public void CommandLineApplication(CommandLineApplication application) + { + var modulePathOption= application.Option("-modulePath", "模块路径",CommandOptionType.SingleValue); + var solutionOption= application.Option("-s|--solution", "解决方案路径",CommandOptionType.SingleValue); + var moduleNameArgument = application.Argument("moduleName", "模块名", null); + + application.OnExecute(() => + { + var modulePath = ""; + var moduleName = moduleNameArgument.Value; + //模块路径默认按小写规则,当前路径 + if (modulePathOption.HasValue()) + { + modulePath = moduleName.ToLower().Replace(".", "-"); + } + + + //解决方案默认在模块文件夹上一级,也可以通过s进行指定 + var slnPath = string.Empty; + + if (!solutionOption.HasValue()) + { + slnPath = "../"; + } + + CheckFirstSlnPath(slnPath); + var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" add \"{modulePath}\\{moduleName}."; + var dotnetSlnCommandPart2 = new List() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" }; + var paths = dotnetSlnCommandPart2.Select(x => $@"{modulePath}\{moduleName}." + x).ToArray(); + CheckPathExist(paths); + + var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray(); + StartCmd(cmdCommands); + + Console.WriteLine("恭喜~模块添加成功!"); + return 0; + }); + + } + + /// + /// 获取一个sln解决方案,多个将报错 + /// + /// + private string CheckFirstSlnPath(string slnPath) + { + string[] slnFiles = Directory.GetFiles(slnPath, "*.sln"); + if (slnFiles.Length > 1) + { + throw new UserFriendlyException("当前目录包含多个sln解决方案,请只保留一个"); + } + if (slnFiles.Length == 0) + { + throw new UserFriendlyException("当前目录未找到sln解决方案,请检查"); + } + + return slnFiles[0]; + } + + + /// + /// 执行cmd命令 + /// + /// + private void StartCmd(params string[] cmdCommands) + { + ProcessStartInfo psi = new ProcessStartInfo + { + FileName = "cmd.exe", + Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}", + RedirectStandardInput = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true, + UseShellExecute = false + }; + + Process proc = new Process + { + StartInfo = psi + }; + + proc.Start(); + string output = proc.StandardOutput.ReadToEnd(); + Console.WriteLine(output); + + proc.WaitForExit(); + } + + + /// + /// 检查路径 + /// + /// + /// + private void CheckPathExist(string[] paths) + { + foreach (string path in paths) + { + if (!Directory.Exists(path)) + { + throw new UserFriendlyException($"路径错误,请检查你的路径,找不到:{path}"); + } + } + } + + + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/ClearCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/ClearCommand.cs index 5bdc5a13..5c49d851 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/ClearCommand.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/ClearCommand.cs @@ -1,50 +1,60 @@ -// using System; -// using System.Collections.Generic; -// using System.Linq; -// using System.Text; -// using System.Threading.Tasks; -// -// namespace Yi.Abp.Tool.Commands -// { -// public class ClearCommand : ICommand -// { -// public List CommandStrs => ["clear"]; -// -// public Task InvokerAsync(Dictionary options, string[] args) -// { -// List delDirBlacklist = ["obj", "bin"]; -// options.TryGetValue("path", out var path); -// -// if (string.IsNullOrEmpty(path)) -// { -// path = "./"; -// } -// DeleteObjBinFolders(path, delDirBlacklist); -// return Task.CompletedTask; -// } -// -// -// private static void DeleteObjBinFolders(string directory, List delDirBlacklist) -// { -// try -// { -// foreach (string subDir in Directory.GetDirectories(directory)) -// { -// if (delDirBlacklist.Contains(Path.GetFileName( subDir))) -// { -// Directory.Delete(subDir, true); -// Console.WriteLine($"已删除文件夹:{subDir}"); -// } -// else -// { -// DeleteObjBinFolders(subDir, delDirBlacklist); -// } -// } -// } -// catch (Exception ex) -// { -// Console.WriteLine($"无法删除文件夹:{directory},错误信息: {ex.Message}"); -// } -// } -// } -// } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.CommandLineUtils; + +namespace Yi.Abp.Tool.Commands +{ + public class ClearCommand : ICommand + { + public List CommandStrs => ["clear"]; + + + public string Command => "clear"; + public string? Description => "清除当前目录及子目录下的obj、bin文件夹` yi-abp clear `"; + + public void CommandLineApplication(CommandLineApplication application) + { + List delDirBlacklist = ["obj", "bin"]; + var pathOption= application.Option("-path", "路径",CommandOptionType.SingleValue); + + + application.OnExecute(() => + { + var path = "./"; + if (pathOption.HasValue()) + { + path = pathOption.Value(); + } + DeleteObjBinFolders(path, delDirBlacklist); + return 0; + }); + } + + + private static void DeleteObjBinFolders(string directory, List delDirBlacklist) + { + try + { + foreach (string subDir in Directory.GetDirectories(directory)) + { + if (delDirBlacklist.Contains(Path.GetFileName( subDir))) + { + Directory.Delete(subDir, true); + Console.WriteLine($"已删除文件夹:{subDir}"); + } + else + { + DeleteObjBinFolders(subDir, delDirBlacklist); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"无法删除文件夹:{directory},错误信息: {ex.Message}"); + } + } + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/CloneCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/CloneCommand.cs index 900b0da0..206eab7f 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/CloneCommand.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/CloneCommand.cs @@ -1,50 +1,60 @@ -// using System; -// using System.Collections.Generic; -// using System.Diagnostics; -// using System.Linq; -// using System.Text; -// using System.Threading.Tasks; -// -// namespace Yi.Abp.Tool.Commands -// { -// public class CloneCommand : ICommand -// { -// public List CommandStrs => new List { "clone"}; -// -// private const string cloneAddress= "https://gitee.com/ccnetcore/Yi"; -// public Task InvokerAsync(Dictionary options, string[] args) -// { -// StartCmd($"git clone {cloneAddress}"); -// return Task.CompletedTask; -// } -// -// /// -// /// 执行cmd命令 -// /// -// /// -// private void StartCmd(params string[] cmdCommands) -// { -// ProcessStartInfo psi = new ProcessStartInfo -// { -// FileName = "cmd.exe", -// Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}", -// RedirectStandardInput = true, -// RedirectStandardOutput = true, -// RedirectStandardError = true, -// CreateNoWindow = true, -// UseShellExecute = false -// }; -// -// Process proc = new Process -// { -// StartInfo = psi -// }; -// -// proc.Start(); -// string output = proc.StandardOutput.ReadToEnd(); -// Console.WriteLine(output); -// -// proc.WaitForExit(); -// } -// } -// } +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.CommandLineUtils; + +namespace Yi.Abp.Tool.Commands +{ + public class CloneCommand : ICommand + { + public List CommandStrs => new List { "clone"}; + + private const string cloneAddress= "https://gitee.com/ccnetcore/Yi"; + + + public string Command => "clone"; + public string? Description => "克隆最新YiFramework源代码"; + + public void CommandLineApplication(CommandLineApplication application) + { + application.OnExecute(() => + { + StartCmd($"git clone {cloneAddress}"); + return 0; + }); + } + + + /// + /// 执行cmd命令 + /// + /// + private void StartCmd(params string[] cmdCommands) + { + ProcessStartInfo psi = new ProcessStartInfo + { + FileName = "cmd.exe", + Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}", + RedirectStandardInput = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true, + UseShellExecute = false + }; + + Process proc = new Process + { + StartInfo = psi + }; + + proc.Start(); + string output = proc.StandardOutput.ReadToEnd(); + Console.WriteLine(output); + + proc.WaitForExit(); + } + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/NewCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/NewCommand.cs index 4830a319..6dfa057d 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/NewCommand.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/NewCommand.cs @@ -1,84 +1,90 @@ -// using System; -// using System.Collections.Generic; -// using System.IO.Compression; -// using System.Linq; -// using System.Text; -// using System.Threading.Tasks; -// using Microsoft.AspNetCore.Mvc; -// using Yi.Abp.Tool.Application.Contracts; -// using Yi.Abp.Tool.Application.Contracts.Dtos; -// -// namespace Yi.Abp.Tool.Commands -// { -// public class NewCommand : ICommand -// { -// private readonly ITemplateGenService _templateGenService; -// public NewCommand(ITemplateGenService templateGenService) -// { -// _templateGenService = templateGenService; -// } -// -// public List CommandStrs => new List() { "new" }; -// -// -// public async Task InvokerAsync(Dictionary options, string[] args) -// { -// var id = Guid.NewGuid().ToString("N"); -// //只有一个new -// if (args.Length <= 1) -// { -// throw new UserFriendlyException("命令错误,new命令后必须添加 名称"); -// } -// string name = args[1]; -// -// #region 处理生成类型 -// -// options.TryGetValue("t", out var templateType); -// var zipPath = string.Empty; -// byte[] fileByteArray; -// if (templateType == "module") -// { -// //代表模块生成 -// fileByteArray = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto -// { -// Name = name, -// }); -// } -// else -// { -// //代表模块生成 -// fileByteArray = await _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto -// { -// Name = name, -// }); -// } -// zipPath = $"{id}.zip"; -// await File.WriteAllBytesAsync(zipPath, fileByteArray); -// -// #endregion -// -// #region 处理解决方案文件夹 -// //默认是当前目录 -// var unzipDirPath = "./"; -// //如果创建解决方案文件夹 -// if (options.TryGetValue("csf", out _)) -// { -// var moduleName = name.ToLower().Replace(".", "-"); -// -// if (Directory.Exists(moduleName)) -// { -// throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试"); -// } -// Directory.CreateDirectory(moduleName); -// unzipDirPath = moduleName; -// } -// #endregion -// ZipFile.ExtractToDirectory(zipPath, unzipDirPath); -// //创建压缩包后删除临时目录 -// File.Delete(zipPath); -// -// -// await Console.Out.WriteLineAsync("恭喜~模块已生成!"); -// } -// } -// } +using System; +using System.Collections.Generic; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.CommandLineUtils; +using Yi.Abp.Tool.Application.Contracts; +using Yi.Abp.Tool.Application.Contracts.Dtos; + +namespace Yi.Abp.Tool.Commands +{ + public class NewCommand : ICommand + { + private readonly ITemplateGenService _templateGenService; + + public NewCommand(ITemplateGenService templateGenService) + { + _templateGenService = templateGenService; + } + + + public string Command => "new"; + public string? Description => "创建一个模板"; + + public void CommandLineApplication(CommandLineApplication application) + { + var templateTypeOption = application.Option("-t", "模板类型", CommandOptionType.SingleValue); + var csfOption = application.Option("csf", "是否创建解决方案", CommandOptionType.SingleValue); + var moduleNameArgument = application.Argument("moduleName", "模块名", null); + + + #region 处理生成类型 + + var id = Guid.NewGuid().ToString("N"); + var zipPath = string.Empty; + byte[] fileByteArray; + + var templateType = templateTypeOption.HasValue() ? templateTypeOption.Value() : "module"; + if (templateType == "module") + { + //代表模块生成 + fileByteArray = (_templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto + { + Name = moduleNameArgument.Value, + }).Result); + } + else + { + //代表模块生成 + fileByteArray = _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto + { + Name = moduleNameArgument.Value, + }).Result; + } + + zipPath = $"{id}.zip"; + File.WriteAllBytes(zipPath, fileByteArray); + + #endregion + + #region 处理解决方案文件夹 + + //默认是当前目录 + var unzipDirPath = "./"; + //如果创建解决方案文件夹 + if (csfOption.HasValue()) + { + var moduleName = moduleNameArgument.Value.ToLower().Replace(".", "-"); + + if (Directory.Exists(moduleName)) + { + throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试"); + } + + Directory.CreateDirectory(moduleName); + unzipDirPath = moduleName; + } + + #endregion + + ZipFile.ExtractToDirectory(zipPath, unzipDirPath); + //创建压缩包后删除临时目录 + File.Delete(zipPath); + + Console.WriteLine("恭喜~模块已生成!"); + } + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/TestCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/TestCommand.cs deleted file mode 100644 index d779295b..00000000 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/TestCommand.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.Extensions.CommandLineUtils; - -namespace Yi.Abp.Tool.Commands; - -public class TestCommand:ICommand -{ - public string Command => "clear"; - - public Task CommandLineApplicationAsync(CommandLineApplication application) - { - var sss= application.Option("-i| --id|-l ","内容id",CommandOptionType.SingleValue); - - application.OnExecute(() => - { - Console.WriteLine($"你好,---{sss.Value()}"); - return 0; - }); - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs index 9f5a3447..6757a03b 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs @@ -12,7 +12,8 @@ namespace Yi.Abp.Tool { public string Command { get; } - Task CommandLineApplicationAsync(CommandLineApplication application); + public string? Description { get; } + void CommandLineApplication(CommandLineApplication application); } } diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs index 5275e7d4..fc550bc9 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs @@ -18,7 +18,13 @@ class Program //args = ["new", "Acme.Book", "-t", "module"]; //args = ["add-module", "Acme.Demo", "-s", "D:\\code\\csharp\\source\\Yi\\Yi.Abp.Net8", "-modulePath", "D:\\code\\csharp\\source\\Yi\\Yi.Abp.Net8\\module\\acme-demo"]; // args = ["clear", "-path", "D:\\code\\csharp\\source\\Yi\\Yi.Abp.Net8\\src"]; - args = ["clear","-i","888"]; + + //帮助 + args = ["-h"]; + //版本 + // args = ["-v"]; + //清理 + // args = ["clear"]; #endif try {