feat: 新增yi-abp工具
This commit is contained in:
102
Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandSelector.cs
Normal file
102
Yi.Abp.Net8/tool/Yi.Abp.Tool/CommandSelector.cs
Normal file
@@ -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<ICommand> _commands;
|
||||
public CommandSelector(IEnumerable<ICommand> 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, 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 commandOrNull.InvokerAsync(options);
|
||||
|
||||
}
|
||||
/// <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"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs
Normal file
37
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/HelpCommand.cs
Normal file
@@ -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<string> CommandStrs => new List<string> { "h", "help", "-h", "-help" };
|
||||
|
||||
public Task InvokerAsync(Dictionary<string, string> options)
|
||||
{
|
||||
string? errorMsg = null;
|
||||
if (options.TryGetValue("error", out _))
|
||||
{
|
||||
errorMsg = "您输入的命令有误,请检查,以下帮助命令提示:";
|
||||
}
|
||||
Console.WriteLine($"""
|
||||
{errorMsg}
|
||||
使用:
|
||||
|
||||
yi-abp <command> <target> [options]
|
||||
|
||||
命令列表:
|
||||
|
||||
> v: 查看yi-abp工具版本号
|
||||
> help: 查看帮助列表,写下命令` yi-abp help <command> `
|
||||
> new: 创建模板--(正在更新)
|
||||
|
||||
""");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/VersionCommand.cs
Normal file
14
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/VersionCommand.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Yi.Abp.Tool.Commands
|
||||
{
|
||||
public class VersionCommand : ICommand
|
||||
{
|
||||
public List<string> CommandStrs => new List<string> { "version", "v", "-version", "-v" };
|
||||
|
||||
public Task InvokerAsync(Dictionary<string, string> options)
|
||||
{
|
||||
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
||||
Console.WriteLine($"Yi-ABP TOOL {version}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs
Normal file
23
Yi.Abp.Net8/tool/Yi.Abp.Tool/ICommand.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 命令串
|
||||
/// </summary>
|
||||
public List<string> CommandStrs { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 执行
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task InvokerAsync(Dictionary<string,string> options);
|
||||
}
|
||||
}
|
||||
@@ -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<CommandSelector>();
|
||||
await commandSelector.SelectorAsync(args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
Console.WriteLine(ex.StackTrace);
|
||||
}
|
||||
|
||||
if (args.Contains("-v"))
|
||||
{
|
||||
var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
||||
Console.WriteLine($"Yi-ABP CLI {version}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("""
|
||||
Usage:
|
||||
|
||||
yi-abp <command> <target> [options]
|
||||
|
||||
Command List:
|
||||
""");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>0.1.0</Version>
|
||||
<Version>0.2.0</Version>
|
||||
<Authors>橙子老哥</Authors>
|
||||
<Description>yi-framework框架配套工具</Description>
|
||||
<PackageProjectUrl>https://ccnetcore.com</PackageProjectUrl>
|
||||
@@ -16,6 +16,7 @@
|
||||
<ToolCommandName>yi-abp</ToolCommandName>
|
||||
<PackageOutputPath>./nupkg</PackageOutputPath>
|
||||
<PackageIcon>logo.png</PackageIcon>
|
||||
<PackageReadmeFile>readme.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\logo.png">
|
||||
@@ -28,4 +29,12 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="readme.md">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath>\</PackagePath>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
1
Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md
Normal file
1
Yi.Abp.Net8/tool/Yi.Abp.Tool/readme.md
Normal file
@@ -0,0 +1 @@
|
||||
yi-framework框架配套工具
|
||||
Reference in New Issue
Block a user