feat: 完成tool搭建

This commit is contained in:
橙子
2024-11-05 22:12:30 +08:00
parent c944bd3b0e
commit d83db53acb
8 changed files with 352 additions and 336 deletions

View File

@@ -23,22 +23,23 @@ namespace Yi.Abp.Tool
private void InitCommand() private void InitCommand()
{ {
Application.HelpOption("-h"); Application.HelpOption("-h|--help");
Application.VersionOption("-v|--versions","1.0.0");
foreach (var command in _commands) 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) public async Task InvokerAsync(string[] args)
{ {
//使用哪个命令根据第一参数来判断如果都不是打印help
// foreach (var commandLineApplication in Application.Commands)
// {
// commandLineApplication.Execute(args);
// }
Application.Execute(args); Application.Execute(args);
} }
} }

View File

@@ -1,121 +1,123 @@
// using System; using System;
// using System.Collections.Generic; using System.Collections.Generic;
// using System.Diagnostics; using System.Diagnostics;
// using System.Linq; using System.Linq;
// using System.Text; using System.Text;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Microsoft.Extensions.CommandLineUtils;
// namespace Yi.Abp.Tool.Commands
// { namespace Yi.Abp.Tool.Commands
// public class AddModuleCommand : ICommand {
// { public class AddModuleCommand : ICommand
// public List<string> CommandStrs => new List<string> { "add-module" }; {
// public string Command => "add-module";
// public async Task InvokerAsync(Dictionary<string, string> options, string[] args) public string? Description { get; }
// { public void CommandLineApplication(CommandLineApplication application)
// //只有一个add-module {
// if (args.Length <= 1) var modulePathOption= application.Option("-modulePath", "模块路径",CommandOptionType.SingleValue);
// { var solutionOption= application.Option("-s|--solution", "解决方案路径",CommandOptionType.SingleValue);
// throw new UserFriendlyException("命令错误add-module命令后必须添加 模块名"); var moduleNameArgument = application.Argument("moduleName", "模块名", null);
// }
// application.OnExecute(() =>
// //需要添加名称 {
// var moduleName = args[1]; var modulePath = "";
// options.TryGetValue("modulePath", out var modulePath); var moduleName = moduleNameArgument.Value;
// //模块路径默认按小写规则,当前路径
// //模块路径默认按小写规则,当前路径 if (modulePathOption.HasValue())
// if (string.IsNullOrEmpty(modulePath)) {
// { modulePath = moduleName.ToLower().Replace(".", "-");
// modulePath = moduleName.ToLower().Replace(".", "-"); }
// }
//
// //解决方案默认在模块文件夹上一级也可以通过s进行指定
// //解决方案默认在模块文件夹上一级也可以通过s进行指定 var slnPath = string.Empty;
// var slnPath = string.Empty;
// options.TryGetValue("s", out var slnPath1); if (!solutionOption.HasValue())
// options.TryGetValue("solution", out var slnPath2); {
// slnPath = string.IsNullOrEmpty(slnPath1) ? slnPath2 : slnPath1; slnPath = "../";
// if (string.IsNullOrEmpty(slnPath)) }
// {
// slnPath = "../"; CheckFirstSlnPath(slnPath);
// } var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" add \"{modulePath}\\{moduleName}.";
// var dotnetSlnCommandPart2 = new List<string>() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" };
// CheckFirstSlnPath(slnPath); var paths = dotnetSlnCommandPart2.Select(x => $@"{modulePath}\{moduleName}." + x).ToArray();
// var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" add \"{modulePath}\\{moduleName}."; CheckPathExist(paths);
// var dotnetSlnCommandPart2 = new List<string>() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" };
// var paths = dotnetSlnCommandPart2.Select(x => $@"{modulePath}\{moduleName}." + x).ToArray(); var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray();
// CheckPathExist(paths); StartCmd(cmdCommands);
//
// var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray(); Console.WriteLine("恭喜~模块添加成功!");
// StartCmd(cmdCommands); return 0;
// });
// await Console.Out.WriteLineAsync("恭喜~模块添加成功!");
// } }
//
// /// <summary> /// <summary>
// /// 获取一个sln解决方案多个将报错 /// 获取一个sln解决方案多个将报错
// /// </summary> /// </summary>
// /// <returns></returns> /// <returns></returns>
// private string CheckFirstSlnPath(string slnPath) private string CheckFirstSlnPath(string slnPath)
// { {
// string[] slnFiles = Directory.GetFiles(slnPath, "*.sln"); string[] slnFiles = Directory.GetFiles(slnPath, "*.sln");
// if (slnFiles.Length > 1) if (slnFiles.Length > 1)
// { {
// throw new UserFriendlyException("当前目录包含多个sln解决方案请只保留一个"); throw new UserFriendlyException("当前目录包含多个sln解决方案请只保留一个");
// } }
// if (slnFiles.Length == 0) if (slnFiles.Length == 0)
// { {
// throw new UserFriendlyException("当前目录未找到sln解决方案请检查"); throw new UserFriendlyException("当前目录未找到sln解决方案请检查");
// } }
//
// return slnFiles[0]; return slnFiles[0];
// } }
//
//
// /// <summary> /// <summary>
// /// 执行cmd命令 /// 执行cmd命令
// /// </summary> /// </summary>
// /// <param name="cmdCommands"></param> /// <param name="cmdCommands"></param>
// private void StartCmd(params string[] cmdCommands) private void StartCmd(params string[] cmdCommands)
// { {
// ProcessStartInfo psi = new ProcessStartInfo ProcessStartInfo psi = new ProcessStartInfo
// { {
// FileName = "cmd.exe", FileName = "cmd.exe",
// Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}", Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}",
// RedirectStandardInput = true, RedirectStandardInput = true,
// RedirectStandardOutput = true, RedirectStandardOutput = true,
// RedirectStandardError = true, RedirectStandardError = true,
// CreateNoWindow = true, CreateNoWindow = true,
// UseShellExecute = false UseShellExecute = false
// }; };
//
// Process proc = new Process Process proc = new Process
// { {
// StartInfo = psi StartInfo = psi
// }; };
//
// proc.Start(); proc.Start();
// string output = proc.StandardOutput.ReadToEnd(); string output = proc.StandardOutput.ReadToEnd();
// Console.WriteLine(output); Console.WriteLine(output);
//
// proc.WaitForExit(); proc.WaitForExit();
// } }
//
//
// /// <summary> /// <summary>
// /// 检查路径 /// 检查路径
// /// </summary> /// </summary>
// /// <param name="paths"></param> /// <param name="paths"></param>
// /// <exception cref="UserFriendlyException"></exception> /// <exception cref="UserFriendlyException"></exception>
// private void CheckPathExist(string[] paths) private void CheckPathExist(string[] paths)
// { {
// foreach (string path in paths) foreach (string path in paths)
// { {
// if (!Directory.Exists(path)) if (!Directory.Exists(path))
// { {
// throw new UserFriendlyException($"路径错误,请检查你的路径,找不到:{path}"); throw new UserFriendlyException($"路径错误,请检查你的路径,找不到:{path}");
// } }
// } }
// } }
// }
// }
}
}

View File

@@ -1,50 +1,60 @@
// using System; using System;
// using System.Collections.Generic; using System.Collections.Generic;
// using System.Linq; using System.Linq;
// using System.Text; using System.Text;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Microsoft.Extensions.CommandLineUtils;
// namespace Yi.Abp.Tool.Commands
// { namespace Yi.Abp.Tool.Commands
// public class ClearCommand : ICommand {
// { public class ClearCommand : ICommand
// public List<string> CommandStrs => ["clear"]; {
// public List<string> CommandStrs => ["clear"];
// public Task InvokerAsync(Dictionary<string, string> options, string[] args)
// {
// List<string> delDirBlacklist = ["obj", "bin"]; public string Command => "clear";
// options.TryGetValue("path", out var path); public string? Description => "清除当前目录及子目录下的obj、bin文件夹` yi-abp clear `";
//
// if (string.IsNullOrEmpty(path)) public void CommandLineApplication(CommandLineApplication application)
// { {
// path = "./"; List<string> delDirBlacklist = ["obj", "bin"];
// } var pathOption= application.Option("-path", "路径",CommandOptionType.SingleValue);
// DeleteObjBinFolders(path, delDirBlacklist);
// return Task.CompletedTask;
// } application.OnExecute(() =>
// {
// var path = "./";
// private static void DeleteObjBinFolders(string directory, List<string> delDirBlacklist) if (pathOption.HasValue())
// { {
// try path = pathOption.Value();
// { }
// foreach (string subDir in Directory.GetDirectories(directory)) DeleteObjBinFolders(path, delDirBlacklist);
// { return 0;
// if (delDirBlacklist.Contains(Path.GetFileName( subDir))) });
// { }
// Directory.Delete(subDir, true);
// Console.WriteLine($"已删除文件夹:{subDir}");
// } private static void DeleteObjBinFolders(string directory, List<string> delDirBlacklist)
// else {
// { try
// DeleteObjBinFolders(subDir, delDirBlacklist); {
// } foreach (string subDir in Directory.GetDirectories(directory))
// } {
// } if (delDirBlacklist.Contains(Path.GetFileName( subDir)))
// catch (Exception ex) {
// { Directory.Delete(subDir, true);
// Console.WriteLine($"无法删除文件夹:{directory},错误信息: {ex.Message}"); Console.WriteLine($"已删除文件夹:{subDir}");
// } }
// } else
// } {
// } DeleteObjBinFolders(subDir, delDirBlacklist);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"无法删除文件夹:{directory},错误信息: {ex.Message}");
}
}
}
}

View File

@@ -1,50 +1,60 @@
// using System; using System;
// using System.Collections.Generic; using System.Collections.Generic;
// using System.Diagnostics; using System.Diagnostics;
// using System.Linq; using System.Linq;
// using System.Text; using System.Text;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Microsoft.Extensions.CommandLineUtils;
// namespace Yi.Abp.Tool.Commands
// { namespace Yi.Abp.Tool.Commands
// public class CloneCommand : ICommand {
// { public class CloneCommand : ICommand
// public List<string> CommandStrs => new List<string> { "clone"}; {
// public List<string> CommandStrs => new List<string> { "clone"};
// private const string cloneAddress= "https://gitee.com/ccnetcore/Yi";
// public Task InvokerAsync(Dictionary<string, string> options, string[] args) private const string cloneAddress= "https://gitee.com/ccnetcore/Yi";
// {
// StartCmd($"git clone {cloneAddress}");
// return Task.CompletedTask; public string Command => "clone";
// } public string? Description => "克隆最新YiFramework源代码";
//
// /// <summary> public void CommandLineApplication(CommandLineApplication application)
// /// 执行cmd命令 {
// /// </summary> application.OnExecute(() =>
// /// <param name="cmdCommands"></param> {
// private void StartCmd(params string[] cmdCommands) StartCmd($"git clone {cloneAddress}");
// { return 0;
// ProcessStartInfo psi = new ProcessStartInfo });
// { }
// FileName = "cmd.exe",
// Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}",
// RedirectStandardInput = true, /// <summary>
// RedirectStandardOutput = true, /// 执行cmd命令
// RedirectStandardError = true, /// </summary>
// CreateNoWindow = true, /// <param name="cmdCommands"></param>
// UseShellExecute = false private void StartCmd(params string[] cmdCommands)
// }; {
// ProcessStartInfo psi = new ProcessStartInfo
// Process proc = new Process {
// { FileName = "cmd.exe",
// StartInfo = psi Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}",
// }; RedirectStandardInput = true,
// RedirectStandardOutput = true,
// proc.Start(); RedirectStandardError = true,
// string output = proc.StandardOutput.ReadToEnd(); CreateNoWindow = true,
// Console.WriteLine(output); UseShellExecute = false
// };
// proc.WaitForExit();
// } Process proc = new Process
// } {
// } StartInfo = psi
};
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine(output);
proc.WaitForExit();
}
}
}

View File

@@ -1,84 +1,90 @@
// using System; using System;
// using System.Collections.Generic; using System.Collections.Generic;
// using System.IO.Compression; using System.IO.Compression;
// using System.Linq; using System.Linq;
// using System.Text; using System.Text;
// using System.Threading.Tasks; using System.Threading.Tasks;
// using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
// using Yi.Abp.Tool.Application.Contracts; using Microsoft.Extensions.CommandLineUtils;
// using Yi.Abp.Tool.Application.Contracts.Dtos; using Yi.Abp.Tool.Application.Contracts;
// using Yi.Abp.Tool.Application.Contracts.Dtos;
// namespace Yi.Abp.Tool.Commands
// { namespace Yi.Abp.Tool.Commands
// public class NewCommand : ICommand {
// { public class NewCommand : ICommand
// private readonly ITemplateGenService _templateGenService; {
// public NewCommand(ITemplateGenService templateGenService) private readonly ITemplateGenService _templateGenService;
// {
// _templateGenService = templateGenService; public NewCommand(ITemplateGenService templateGenService)
// } {
// _templateGenService = templateGenService;
// public List<string> CommandStrs => new List<string>() { "new" }; }
//
//
// public async Task InvokerAsync(Dictionary<string, string> options, string[] args) public string Command => "new";
// { public string? Description => "创建一个模板";
// var id = Guid.NewGuid().ToString("N");
// //只有一个new public void CommandLineApplication(CommandLineApplication application)
// if (args.Length <= 1) {
// { var templateTypeOption = application.Option("-t", "模板类型", CommandOptionType.SingleValue);
// throw new UserFriendlyException("命令错误new命令后必须添加 名称"); var csfOption = application.Option("csf", "是否创建解决方案", CommandOptionType.SingleValue);
// } var moduleNameArgument = application.Argument("moduleName", "模块名", null);
// string name = args[1];
//
// #region 处理生成类 #region
//
// options.TryGetValue("t", out var templateType); var id = Guid.NewGuid().ToString("N");
// var zipPath = string.Empty; var zipPath = string.Empty;
// byte[] fileByteArray; byte[] fileByteArray;
// if (templateType == "module")
// { var templateType = templateTypeOption.HasValue() ? templateTypeOption.Value() : "module";
// //代表模块生成 if (templateType == "module")
// fileByteArray = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto {
// { //代表模块生成
// Name = name, fileByteArray = (_templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
// }); {
// } Name = moduleNameArgument.Value,
// else }).Result);
// { }
// //代表模块生成 else
// fileByteArray = await _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto {
// { //代表模块生成
// Name = name, fileByteArray = _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto
// }); {
// } Name = moduleNameArgument.Value,
// zipPath = $"{id}.zip"; }).Result;
// await File.WriteAllBytesAsync(zipPath, fileByteArray); }
//
// #endregion zipPath = $"{id}.zip";
// File.WriteAllBytes(zipPath, fileByteArray);
// #region 处理解决方案文件夹
// //默认是当前目录 #endregion
// var unzipDirPath = "./";
// //如果创建解决方案文件 #region
// if (options.TryGetValue("csf", out _))
// { //默认是当前目录
// var moduleName = name.ToLower().Replace(".", "-"); var unzipDirPath = "./";
// //如果创建解决方案文件夹
// if (Directory.Exists(moduleName)) if (csfOption.HasValue())
// { {
// throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试"); var moduleName = moduleNameArgument.Value.ToLower().Replace(".", "-");
// }
// Directory.CreateDirectory(moduleName); if (Directory.Exists(moduleName))
// unzipDirPath = moduleName; {
// } throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试");
// #endregion }
// ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
// //创建压缩包后删除临时目录 Directory.CreateDirectory(moduleName);
// File.Delete(zipPath); unzipDirPath = moduleName;
// }
//
// await Console.Out.WriteLineAsync("恭喜~模块已生成!"); #endregion
// }
// } ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
// } //创建压缩包后删除临时目录
File.Delete(zipPath);
Console.WriteLine("恭喜~模块已生成!");
}
}
}

View File

@@ -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>","内容id",CommandOptionType.SingleValue);
application.OnExecute(() =>
{
Console.WriteLine($"你好,---{sss.Value()}");
return 0;
});
return Task.CompletedTask;
}
}

View File

@@ -12,7 +12,8 @@ namespace Yi.Abp.Tool
{ {
public string Command { get; } public string Command { get; }
Task CommandLineApplicationAsync(CommandLineApplication application); public string? Description { get; }
void CommandLineApplication(CommandLineApplication application);
} }
} }

View File

@@ -18,7 +18,13 @@ class Program
//args = ["new", "Acme.Book", "-t", "module"]; //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 = ["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", "-path", "D:\\code\\csharp\\source\\Yi\\Yi.Abp.Net8\\src"];
args = ["clear","-i","888"];
//帮助
args = ["-h"];
//版本
// args = ["-v"];
//清理
// args = ["clear"];
#endif #endif
try try
{ {