From daaa3513aeac91c001f25e08ab3926552cc2a309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 2 Jun 2024 00:38:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Eyi-abp=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tool/Yi.Abp.Tool/CommandSelector.cs | 102 ++++++++++++++++++ .../tool/Yi.Abp.Tool/Commands/HelpCommand.cs | 37 +++++++ .../Yi.Abp.Tool/Commands/VersionCommand.cs | 14 +++ Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs | 23 ++++ Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs | 30 +++--- .../tool/Yi.Abp.Tool/Yi.Abp.Tool.csproj | 11 +- Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md | 1 + 7 files changed, 199 insertions(+), 19 deletions(-) create mode 100644 Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandSelector.cs create mode 100644 Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs create mode 100644 Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/VersionCommand.cs create mode 100644 Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs create mode 100644 Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandSelector.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandSelector.cs new file mode 100644 index 00000000..28844a62 --- /dev/null +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandSelector.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace Yi.Abp.Tool +{ + public class CommandSelector : ITransientDependency + { + private readonly IEnumerable _commands; + public CommandSelector(IEnumerable commands) + { + _commands = commands; + } + public async Task SelectorAsync(string[] args) + { + //不指定命令,默认给help + if (args.Length == 0) + { + await SelectorDefaultCommandAsync(); + return; + } + var commandStr = args[0]; + + var commandOrNull = _commands.Where(x => x.CommandStrs.Select(x => x.ToUpper()).Contains(commandStr.ToUpper())).FirstOrDefault(); + + //没有匹配到命令,,默认给help + if (commandOrNull == null) + { + await SelectorDefaultCommandAsync(); + return; + } + + var options = new Dictionary(); + + //去除命令,剩下进行参数装载 + string[] commonArgs = args.Skip(1).ToArray(); + for (var i = 0; i < commonArgs.Length; i++) + { + var currentArg = commonArgs[i]; + //命令参数以-或者--开头 + if (IsCommandArg(currentArg)) + { + string? commonValue = null; + //参数值在他的下一位 + if (i + 1 < commonArgs.Length) + { + var nextArg = commonArgs[i + 1]; + if (!IsCommandArg(nextArg)) + { + commonValue = nextArg; + } + + } + //删除-就是参数名 + options.Add(ArgToCommandMap(currentArg), commonValue); + } + + + + } + await commandOrNull.InvokerAsync(options); + + } + /// + /// 判断是否为命令参数 + /// + /// + private bool IsCommandArg(string arg) + { + if (arg.StartsWith("-") || arg.StartsWith("--")) + { + return true; + } + else + { + return false; + } + } + + /// + /// 参数到命令的转换 + /// + /// + private string ArgToCommandMap(string arg) + { + return arg.TrimStart('-').TrimStart('-'); + } + + /// + /// 选择默认命令 + /// + /// + private async Task SelectorDefaultCommandAsync() + { + await SelectorAsync(["-h", "-error"]); + } + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs new file mode 100644 index 00000000..0edf9947 --- /dev/null +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Abp.Tool.Commands +{ + public class HelpCommand : ICommand + { + public List CommandStrs => new List { "h", "help", "-h", "-help" }; + + public Task InvokerAsync(Dictionary options) + { + string? errorMsg = null; + if (options.TryGetValue("error", out _)) + { + errorMsg = "您输入的命令有误,请检查,以下帮助命令提示:"; + } + Console.WriteLine($""" + {errorMsg} + 使用: + + yi-abp [options] + + 命令列表: + + > v: 查看yi-abp工具版本号 + > help: 查看帮助列表,写下命令` yi-abp help ` + > new: 创建模板--(正在更新) + + """); + return Task.CompletedTask; + } + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/VersionCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/VersionCommand.cs new file mode 100644 index 00000000..b472cd45 --- /dev/null +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/VersionCommand.cs @@ -0,0 +1,14 @@ +namespace Yi.Abp.Tool.Commands +{ + public class VersionCommand : ICommand + { + public List CommandStrs => new List { "version", "v", "-version", "-v" }; + + public Task InvokerAsync(Dictionary options) + { + var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; + Console.WriteLine($"Yi-ABP TOOL {version}"); + return Task.CompletedTask; + } + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs new file mode 100644 index 00000000..577b299d --- /dev/null +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Yi.Abp.Tool +{ + public interface ICommand:ITransientDependency + { + /// + /// 命令串 + /// + public List CommandStrs { get; } + + /// + /// 执行 + /// + /// + public Task InvokerAsync(Dictionary options); + } +} diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs index 3b1995b8..96e8416f 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs @@ -5,9 +5,17 @@ using Yi.Abp.Tool; class Program { - static void Main(string[] args) + static async Task Main(string[] args) { +#if DEBUG + //args = ["v"]; + //args = ["-v"]; + //args = ["h"]; + //args = ["-h"]; + //args = []; + //args = ["12312"]; +#endif try { IHost host = Host.CreateDefaultBuilder() @@ -17,29 +25,15 @@ class Program }) .UseAutofac() .Build(); + + var commandSelector = host.Services.GetRequiredService(); + await commandSelector.SelectorAsync(args); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } - - if (args.Contains("-v")) - { - var version = Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; - Console.WriteLine($"Yi-ABP CLI {version}"); - } - else - { - Console.WriteLine(""" - Usage: - - yi-abp [options] - - Command List: - """); - } - } } \ No newline at end of file diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Yi.Abp.Tool.csproj b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Yi.Abp.Tool.csproj index 9f599f36..4ef7f100 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Yi.Abp.Tool.csproj +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Yi.Abp.Tool.csproj @@ -5,7 +5,7 @@ net8.0 enable enable - 0.1.0 + 0.2.0 橙子老哥 yi-framework框架配套工具 https://ccnetcore.com @@ -16,6 +16,7 @@ yi-abp ./nupkg logo.png + readme.md @@ -28,4 +29,12 @@ + + + + + True + \ + + diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md b/Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md new file mode 100644 index 00000000..a9507d7d --- /dev/null +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md @@ -0,0 +1 @@ +yi-framework框架配套工具 \ No newline at end of file