diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddCommand.cs new file mode 100644 index 00000000..b32f489c --- /dev/null +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddCommand.cs @@ -0,0 +1,117 @@ +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 AddCommand : ICommand + { + public List CommandStrs => new List { "add" }; + + public Task InvokerAsync(Dictionary options, string[] args) + { + //只有一个add + if (args.Length <= 1) + { + throw new UserFriendlyException("命令错误,add命令后必须添加 类型"); + } + + //需要添加名称 + var addName = args[1]; + options.TryGetValue("modulePath", out var modulePath); + options.TryGetValue("moduleName", out var moduleName); + + + //添加的为模块 + if (addName == "module") + { + if (string.IsNullOrEmpty(modulePath) || string.IsNullOrEmpty(moduleName)) + { + throw new UserFriendlyException("命令错误,添加模块,必须指定模块路径及模块名称"); + } + GetFirstSlnPath(); + var dotnetSlnCommandPart1 = $@"dotnet sln 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); + + } + else + { + throw new UserFriendlyException("命令暂时只支持模块添加,请尝试使用 module"); + } + return Task.CompletedTask; + } + + /// + /// 获取一个sln解决方案,多个将报错 + /// + /// + private string GetFirstSlnPath() + { + string[] slnFiles = Directory.GetFiles("./", "*.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 {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/HelpCommand.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs index 777a4688..8364d61d 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs @@ -30,6 +30,8 @@ namespace Yi.Abp.Tool.Commands > help: 查看帮助列表,写下命令` yi-abp help ` > new: 创建模块模板` yi-abp new -t module -csf ` > new: 创建项目模板` yi-abp new -csf ` + > add: 将内容添加到当前解决方案` yi-abp add module -moduleName -modulePath ` + """); return Task.CompletedTask; } diff --git a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs index f842b16a..19e7001b 100644 --- a/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs +++ b/Yi.Abp.Net8/tool/Yi.Abp.Tool/Program.cs @@ -1,8 +1,6 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Yi.Abp.Tool; -using Yi.Abp.Tool.Application.Contracts; class Program { @@ -16,8 +14,9 @@ class Program //args = ["-h"]; //args = []; //args = ["12312"]; - args = ["new", "Acme.Book", "-t", "module", "-csf"]; + //args = ["new", "Acme.Book", "-t", "module", "-csf"]; //args = ["new", "Acme.Book", "-t", "module"]; + args = ["add","module", "-moduleName", "Acme.Demo", "-modulePath", "module\\acme-demo"]; #endif try { 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 3e66c00c..b5d3c2c3 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.5.0 + 0.6.0 橙子老哥 yi-framework框架配套工具 https://ccnetcore.com