feat: 搭建tool
This commit is contained in:
45
Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandInvoker.cs
Normal file
45
Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandInvoker.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||||
|
|
||||||
|
namespace Yi.Abp.Tool
|
||||||
|
{
|
||||||
|
public class CommandInvoker : ISingletonDependency
|
||||||
|
{
|
||||||
|
private readonly IEnumerable<ICommand> _commands;
|
||||||
|
private CommandLineApplication Application { get; }
|
||||||
|
|
||||||
|
public CommandInvoker(IEnumerable<ICommand> commands)
|
||||||
|
{
|
||||||
|
_commands = commands;
|
||||||
|
Application = new CommandLineApplication();
|
||||||
|
InitCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitCommand()
|
||||||
|
{
|
||||||
|
Application.HelpOption("-h");
|
||||||
|
|
||||||
|
foreach (var command in _commands)
|
||||||
|
{
|
||||||
|
Application.Command(command.Command, con => command.CommandLineApplicationAsync(con).Wait());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InvokerAsync(string[] args)
|
||||||
|
{
|
||||||
|
//使用哪个命令,根据第一参数来判断,如果都不是,打印help
|
||||||
|
// foreach (var commandLineApplication in Application.Commands)
|
||||||
|
// {
|
||||||
|
// commandLineApplication.Execute(args);
|
||||||
|
// }
|
||||||
|
|
||||||
|
Application.Execute(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.CommandLineUtils;
|
|
||||||
using Volo.Abp.DependencyInjection;
|
|
||||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
||||||
|
|
||||||
namespace Yi.Abp.Tool
|
|
||||||
{
|
|
||||||
public class CommandSelector : ITransientDependency
|
|
||||||
{
|
|
||||||
private readonly IEnumerable<ICommand> _commands;
|
|
||||||
public CommandSelector(IEnumerable<ICommand> commands)
|
|
||||||
{
|
|
||||||
_commands = commands;
|
|
||||||
}
|
|
||||||
public async Task SelectorAsync(string[] args)
|
|
||||||
{
|
|
||||||
var app = new CommandLineApplication();
|
|
||||||
|
|
||||||
// //1-设置命令
|
|
||||||
// app.Command();
|
|
||||||
// //2-各自命令内,设置选项
|
|
||||||
// app.Options();
|
|
||||||
// //3-执行,暴露方法
|
|
||||||
// app.Execute(args);
|
|
||||||
|
|
||||||
//
|
|
||||||
// //不指定命令,默认给help
|
|
||||||
// if (args.Length == 0)
|
|
||||||
// {
|
|
||||||
// await SelectorDefaultCommandAsync();
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// var app = new CommandLineApplication();
|
|
||||||
//
|
|
||||||
// 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 command = commandOrNull;
|
|
||||||
//
|
|
||||||
// var options = new Dictionary<string, string?>();
|
|
||||||
//
|
|
||||||
// //去除命令,剩下进行参数装载
|
|
||||||
// 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 command.InvokerAsync(options,args);
|
|
||||||
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 判断是否为命令参数
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private bool IsCommandArg(string arg)
|
|
||||||
{
|
|
||||||
if (arg.StartsWith("-") || arg.StartsWith("--"))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 参数到命令的转换
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private string ArgToCommandMap(string arg)
|
|
||||||
{
|
|
||||||
return arg.TrimStart('-').TrimStart('-');
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 选择默认命令
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task SelectorDefaultCommandAsync()
|
|
||||||
{
|
|
||||||
await SelectorAsync(["-h", "-error"]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,121 +1,121 @@
|
|||||||
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;
|
||||||
|
//
|
||||||
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 List<string> CommandStrs => new List<string> { "add-module" };
|
||||||
|
//
|
||||||
public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
// public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
||||||
{
|
// {
|
||||||
//只有一个add-module
|
// //只有一个add-module
|
||||||
if (args.Length <= 1)
|
// if (args.Length <= 1)
|
||||||
{
|
// {
|
||||||
throw new UserFriendlyException("命令错误,add-module命令后必须添加 模块名");
|
// throw new UserFriendlyException("命令错误,add-module命令后必须添加 模块名");
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
//需要添加名称
|
// //需要添加名称
|
||||||
var moduleName = args[1];
|
// var moduleName = args[1];
|
||||||
options.TryGetValue("modulePath", out var modulePath);
|
// options.TryGetValue("modulePath", out var modulePath);
|
||||||
|
//
|
||||||
//模块路径默认按小写规则,当前路径
|
// //模块路径默认按小写规则,当前路径
|
||||||
if (string.IsNullOrEmpty(modulePath))
|
// 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);
|
// options.TryGetValue("s", out var slnPath1);
|
||||||
options.TryGetValue("solution", out var slnPath2);
|
// options.TryGetValue("solution", out var slnPath2);
|
||||||
slnPath = string.IsNullOrEmpty(slnPath1) ? slnPath2 : slnPath1;
|
// slnPath = string.IsNullOrEmpty(slnPath1) ? slnPath2 : slnPath1;
|
||||||
if (string.IsNullOrEmpty(slnPath))
|
// if (string.IsNullOrEmpty(slnPath))
|
||||||
{
|
// {
|
||||||
slnPath = "../";
|
// slnPath = "../";
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
CheckFirstSlnPath(slnPath);
|
// CheckFirstSlnPath(slnPath);
|
||||||
var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" add \"{modulePath}\\{moduleName}.";
|
// var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" add \"{modulePath}\\{moduleName}.";
|
||||||
var dotnetSlnCommandPart2 = new List<string>() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" };
|
// var dotnetSlnCommandPart2 = new List<string>() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" };
|
||||||
var paths = dotnetSlnCommandPart2.Select(x => $@"{modulePath}\{moduleName}." + x).ToArray();
|
// var paths = dotnetSlnCommandPart2.Select(x => $@"{modulePath}\{moduleName}." + x).ToArray();
|
||||||
CheckPathExist(paths);
|
// CheckPathExist(paths);
|
||||||
|
//
|
||||||
var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray();
|
// var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray();
|
||||||
StartCmd(cmdCommands);
|
// StartCmd(cmdCommands);
|
||||||
|
//
|
||||||
await Console.Out.WriteLineAsync("恭喜~模块添加成功!");
|
// 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}");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -1,50 +1,50 @@
|
|||||||
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;
|
||||||
|
//
|
||||||
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)
|
// public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
||||||
{
|
// {
|
||||||
List<string> delDirBlacklist = ["obj", "bin"];
|
// List<string> delDirBlacklist = ["obj", "bin"];
|
||||||
options.TryGetValue("path", out var path);
|
// options.TryGetValue("path", out var path);
|
||||||
|
//
|
||||||
if (string.IsNullOrEmpty(path))
|
// if (string.IsNullOrEmpty(path))
|
||||||
{
|
// {
|
||||||
path = "./";
|
// path = "./";
|
||||||
}
|
// }
|
||||||
DeleteObjBinFolders(path, delDirBlacklist);
|
// DeleteObjBinFolders(path, delDirBlacklist);
|
||||||
return Task.CompletedTask;
|
// return Task.CompletedTask;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
private static void DeleteObjBinFolders(string directory, List<string> delDirBlacklist)
|
// private static void DeleteObjBinFolders(string directory, List<string> delDirBlacklist)
|
||||||
{
|
// {
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
foreach (string subDir in Directory.GetDirectories(directory))
|
// foreach (string subDir in Directory.GetDirectories(directory))
|
||||||
{
|
// {
|
||||||
if (delDirBlacklist.Contains(Path.GetFileName( subDir)))
|
// if (delDirBlacklist.Contains(Path.GetFileName( subDir)))
|
||||||
{
|
// {
|
||||||
Directory.Delete(subDir, true);
|
// Directory.Delete(subDir, true);
|
||||||
Console.WriteLine($"已删除文件夹:{subDir}");
|
// Console.WriteLine($"已删除文件夹:{subDir}");
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
DeleteObjBinFolders(subDir, delDirBlacklist);
|
// DeleteObjBinFolders(subDir, delDirBlacklist);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
catch (Exception ex)
|
// catch (Exception ex)
|
||||||
{
|
// {
|
||||||
Console.WriteLine($"无法删除文件夹:{directory},错误信息: {ex.Message}");
|
// Console.WriteLine($"无法删除文件夹:{directory},错误信息: {ex.Message}");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -1,50 +1,50 @@
|
|||||||
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;
|
||||||
|
//
|
||||||
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";
|
// private const string cloneAddress= "https://gitee.com/ccnetcore/Yi";
|
||||||
public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
// public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
||||||
{
|
// {
|
||||||
StartCmd($"git clone {cloneAddress}");
|
// StartCmd($"git clone {cloneAddress}");
|
||||||
return Task.CompletedTask;
|
// return Task.CompletedTask;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/// <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();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -1,40 +1,40 @@
|
|||||||
using System;
|
// using System;
|
||||||
using System.Collections.Generic;
|
// using System.Collections.Generic;
|
||||||
using System.Linq;
|
// using System.Linq;
|
||||||
using System.Reflection;
|
// using System.Reflection;
|
||||||
using System.Text;
|
// using System.Text;
|
||||||
using System.Threading.Tasks;
|
// using System.Threading.Tasks;
|
||||||
|
//
|
||||||
namespace Yi.Abp.Tool.Commands
|
// namespace Yi.Abp.Tool.Commands
|
||||||
{
|
// {
|
||||||
public class HelpCommand : ICommand
|
// public class HelpCommand : ICommand
|
||||||
{
|
// {
|
||||||
public List<string> CommandStrs => new List<string> { "h", "help", "-h", "-help" };
|
// public List<string> CommandStrs => new List<string> { "h", "help", "-h", "-help" };
|
||||||
|
//
|
||||||
public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
// public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
||||||
{
|
// {
|
||||||
string? errorMsg = null;
|
// string? errorMsg = null;
|
||||||
if (options.TryGetValue("error", out _))
|
// if (options.TryGetValue("error", out _))
|
||||||
{
|
// {
|
||||||
errorMsg = "您输入的命令有误,请检查,以下帮助命令提示:";
|
// errorMsg = "您输入的命令有误,请检查,以下帮助命令提示:";
|
||||||
}
|
// }
|
||||||
Console.WriteLine($"""
|
// Console.WriteLine($"""
|
||||||
{errorMsg}
|
// {errorMsg}
|
||||||
使用:
|
// 使用:
|
||||||
|
//
|
||||||
yi-abp <command> <target> [options]
|
// yi-abp <command> <target> [options]
|
||||||
|
//
|
||||||
命令列表:
|
// 命令列表:
|
||||||
|
//
|
||||||
> v: 查看yi-abp工具版本号
|
// > v: 查看yi-abp工具版本号
|
||||||
> help: 查看帮助列表,写下命令` yi-abp help <command> `
|
// > help: 查看帮助列表,写下命令` yi-abp help <command> `
|
||||||
> new: 创建模块模板` yi-abp new <name> -t module -csf `
|
// > new: 创建模块模板` yi-abp new <name> -t module -csf `
|
||||||
> new: 创建项目模板` yi-abp new <name> -csf `
|
// > new: 创建项目模板` yi-abp new <name> -csf `
|
||||||
> add-module: 将内容添加到当前解决方案` yi-abp add-module <moduleName> [-modulePath <path>] [-s <slnPath>] `
|
// > add-module: 将内容添加到当前解决方案` yi-abp add-module <moduleName> [-modulePath <path>] [-s <slnPath>] `
|
||||||
> clear: 清除当前目录及子目录下的obj、bin文件夹` yi-abp clear `
|
// > clear: 清除当前目录及子目录下的obj、bin文件夹` yi-abp clear `
|
||||||
|
//
|
||||||
""");
|
// """);
|
||||||
return Task.CompletedTask;
|
// return Task.CompletedTask;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -1,84 +1,84 @@
|
|||||||
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 Yi.Abp.Tool.Application.Contracts;
|
||||||
using Yi.Abp.Tool.Application.Contracts.Dtos;
|
// 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;
|
// private readonly ITemplateGenService _templateGenService;
|
||||||
public NewCommand(ITemplateGenService templateGenService)
|
// public NewCommand(ITemplateGenService templateGenService)
|
||||||
{
|
// {
|
||||||
_templateGenService = templateGenService;
|
// _templateGenService = templateGenService;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public List<string> CommandStrs => new List<string>() { "new" };
|
// public List<string> CommandStrs => new List<string>() { "new" };
|
||||||
|
//
|
||||||
|
//
|
||||||
public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
// public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
||||||
{
|
// {
|
||||||
var id = Guid.NewGuid().ToString("N");
|
// var id = Guid.NewGuid().ToString("N");
|
||||||
//只有一个new
|
// //只有一个new
|
||||||
if (args.Length <= 1)
|
// if (args.Length <= 1)
|
||||||
{
|
// {
|
||||||
throw new UserFriendlyException("命令错误,new命令后必须添加 名称");
|
// throw new UserFriendlyException("命令错误,new命令后必须添加 名称");
|
||||||
}
|
// }
|
||||||
string name = args[1];
|
// string name = args[1];
|
||||||
|
//
|
||||||
#region 处理生成类型
|
// #region 处理生成类型
|
||||||
|
//
|
||||||
options.TryGetValue("t", out var templateType);
|
// options.TryGetValue("t", out var templateType);
|
||||||
var zipPath = string.Empty;
|
// var zipPath = string.Empty;
|
||||||
byte[] fileByteArray;
|
// byte[] fileByteArray;
|
||||||
if (templateType == "module")
|
// if (templateType == "module")
|
||||||
{
|
// {
|
||||||
//代表模块生成
|
// //代表模块生成
|
||||||
fileByteArray = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
|
// fileByteArray = await _templateGenService.CreateModuleAsync(new TemplateGenCreateInputDto
|
||||||
{
|
// {
|
||||||
Name = name,
|
// Name = name,
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
//代表模块生成
|
// //代表模块生成
|
||||||
fileByteArray = await _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto
|
// fileByteArray = await _templateGenService.CreateProjectAsync(new TemplateGenCreateInputDto
|
||||||
{
|
// {
|
||||||
Name = name,
|
// Name = name,
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
zipPath = $"{id}.zip";
|
// zipPath = $"{id}.zip";
|
||||||
await File.WriteAllBytesAsync(zipPath, fileByteArray);
|
// await File.WriteAllBytesAsync(zipPath, fileByteArray);
|
||||||
|
//
|
||||||
#endregion
|
// #endregion
|
||||||
|
//
|
||||||
#region 处理解决方案文件夹
|
// #region 处理解决方案文件夹
|
||||||
//默认是当前目录
|
// //默认是当前目录
|
||||||
var unzipDirPath = "./";
|
// var unzipDirPath = "./";
|
||||||
//如果创建解决方案文件夹
|
// //如果创建解决方案文件夹
|
||||||
if (options.TryGetValue("csf", out _))
|
// if (options.TryGetValue("csf", out _))
|
||||||
{
|
// {
|
||||||
var moduleName = name.ToLower().Replace(".", "-");
|
// var moduleName = name.ToLower().Replace(".", "-");
|
||||||
|
//
|
||||||
if (Directory.Exists(moduleName))
|
// if (Directory.Exists(moduleName))
|
||||||
{
|
// {
|
||||||
throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试");
|
// throw new UserFriendlyException($"文件夹[{moduleName}]已存在,请删除后重试");
|
||||||
}
|
// }
|
||||||
Directory.CreateDirectory(moduleName);
|
// Directory.CreateDirectory(moduleName);
|
||||||
unzipDirPath = moduleName;
|
// unzipDirPath = moduleName;
|
||||||
}
|
// }
|
||||||
#endregion
|
// #endregion
|
||||||
ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
|
// ZipFile.ExtractToDirectory(zipPath, unzipDirPath);
|
||||||
//创建压缩包后删除临时目录
|
// //创建压缩包后删除临时目录
|
||||||
File.Delete(zipPath);
|
// File.Delete(zipPath);
|
||||||
|
//
|
||||||
|
//
|
||||||
await Console.Out.WriteLineAsync("恭喜~模块已生成!");
|
// await Console.Out.WriteLineAsync("恭喜~模块已生成!");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
20
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/TestCommand.cs
Normal file
20
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/TestCommand.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
namespace Yi.Abp.Tool.Commands
|
// namespace Yi.Abp.Tool.Commands
|
||||||
{
|
// {
|
||||||
public class VersionCommand : ICommand
|
// public class VersionCommand : ICommand
|
||||||
{
|
// {
|
||||||
public List<string> CommandStrs => new List<string> { "version", "v", "-version", "-v" };
|
// public List<string> CommandStrs => new List<string> { "version", "v", "-version", "-v" };
|
||||||
|
//
|
||||||
public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
// public Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
||||||
{
|
// {
|
||||||
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
// var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
||||||
Console.WriteLine($"Yi-ABP TOOL {version}");
|
// Console.WriteLine($"Yi-ABP TOOL {version}");
|
||||||
return Task.CompletedTask;
|
// return Task.CompletedTask;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -3,21 +3,16 @@ 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;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.DependencyInjection;
|
||||||
|
|
||||||
namespace Yi.Abp.Tool
|
namespace Yi.Abp.Tool
|
||||||
{
|
{
|
||||||
public interface ICommand:ITransientDependency
|
public interface ICommand:ISingletonDependency
|
||||||
{
|
{
|
||||||
/// <summary>
|
public string Command { get; }
|
||||||
/// 命令串
|
|
||||||
/// </summary>
|
Task CommandLineApplicationAsync(CommandLineApplication application);
|
||||||
public List<string> CommandStrs { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 执行
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public Task InvokerAsync(Dictionary<string,string> options, string[] args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ class Program
|
|||||||
//args = ["new", "Acme.Book", "-t", "module", "-csf"];
|
//args = ["new", "Acme.Book", "-t", "module", "-csf"];
|
||||||
//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"];
|
||||||
#endif
|
#endif
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -32,8 +33,8 @@ class Program
|
|||||||
//})
|
//})
|
||||||
.UseAutofac()
|
.UseAutofac()
|
||||||
.Build();
|
.Build();
|
||||||
var commandSelector = host.Services.GetRequiredService<CommandSelector>();
|
var commandSelector = host.Services.GetRequiredService<CommandInvoker>();
|
||||||
await commandSelector.SelectorAsync(args);
|
await commandSelector.InvokerAsync(args);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user