feat: yi-abp准备支持add
This commit is contained in:
117
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddCommand.cs
Normal file
117
Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/AddCommand.cs
Normal file
@@ -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<string> CommandStrs => new List<string> { "add" };
|
||||||
|
|
||||||
|
public Task InvokerAsync(Dictionary<string, string> 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<string>() { "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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取一个sln解决方案,多个将报错
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行cmd命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cmdCommands"></param>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查路径
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="paths"></param>
|
||||||
|
/// <exception cref="UserFriendlyException"></exception>
|
||||||
|
private void CheckPathExist(string[] paths)
|
||||||
|
{
|
||||||
|
foreach (string path in paths)
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(path))
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"路径错误,请检查你的路径,找不到:{path}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,8 @@ namespace Yi.Abp.Tool.Commands
|
|||||||
> 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: 将内容添加到当前解决方案` yi-abp add module -moduleName <name> -modulePath <path> `
|
||||||
|
|
||||||
""");
|
""");
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Yi.Abp.Tool;
|
using Yi.Abp.Tool;
|
||||||
using Yi.Abp.Tool.Application.Contracts;
|
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
@@ -16,8 +14,9 @@ class Program
|
|||||||
//args = ["-h"];
|
//args = ["-h"];
|
||||||
//args = [];
|
//args = [];
|
||||||
//args = ["12312"];
|
//args = ["12312"];
|
||||||
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", "-moduleName", "Acme.Demo", "-modulePath", "module\\acme-demo"];
|
||||||
#endif
|
#endif
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Version>0.5.0</Version>
|
<Version>0.6.0</Version>
|
||||||
<Authors>橙子老哥</Authors>
|
<Authors>橙子老哥</Authors>
|
||||||
<Description>yi-framework框架配套工具</Description>
|
<Description>yi-framework框架配套工具</Description>
|
||||||
<PackageProjectUrl>https://ccnetcore.com</PackageProjectUrl>
|
<PackageProjectUrl>https://ccnetcore.com</PackageProjectUrl>
|
||||||
|
|||||||
Reference in New Issue
Block a user