2024-06-05 00:33:54 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Abp.Tool.Commands
|
|
|
|
|
|
{
|
2024-06-05 18:38:43 +08:00
|
|
|
|
public class AddModuleCommand : ICommand
|
2024-06-05 00:33:54 +08:00
|
|
|
|
{
|
2024-06-05 18:38:43 +08:00
|
|
|
|
public List<string> CommandStrs => new List<string> { "add-module" };
|
2024-06-05 00:33:54 +08:00
|
|
|
|
|
2024-06-05 18:38:43 +08:00
|
|
|
|
public async Task InvokerAsync(Dictionary<string, string> options, string[] args)
|
2024-06-05 00:33:54 +08:00
|
|
|
|
{
|
2024-06-08 21:51:45 +08:00
|
|
|
|
//只有一个add-module
|
2024-06-05 00:33:54 +08:00
|
|
|
|
if (args.Length <= 1)
|
|
|
|
|
|
{
|
2024-06-05 18:38:43 +08:00
|
|
|
|
throw new UserFriendlyException("命令错误,add-module命令后必须添加 模块名");
|
2024-06-05 00:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//需要添加名称
|
2024-06-05 18:38:43 +08:00
|
|
|
|
var moduleName = args[1];
|
2024-06-05 00:33:54 +08:00
|
|
|
|
options.TryGetValue("modulePath", out var modulePath);
|
|
|
|
|
|
|
2024-06-08 21:51:45 +08:00
|
|
|
|
//模块路径默认按小写规则,当前路径
|
2024-06-05 18:38:43 +08:00
|
|
|
|
if (string.IsNullOrEmpty(modulePath))
|
2024-06-05 00:33:54 +08:00
|
|
|
|
{
|
2024-06-08 21:51:45 +08:00
|
|
|
|
modulePath = moduleName.ToLower().Replace(".", "-");
|
2024-06-05 00:33:54 +08:00
|
|
|
|
}
|
2024-06-05 18:38:43 +08:00
|
|
|
|
|
2024-06-08 21:51:45 +08:00
|
|
|
|
|
|
|
|
|
|
//解决方案默认在模块文件夹上一级,也可以通过s进行指定
|
2024-06-05 18:38:43 +08:00
|
|
|
|
var slnPath = string.Empty;
|
|
|
|
|
|
options.TryGetValue("s", out var slnPath1);
|
|
|
|
|
|
options.TryGetValue("solution", out var slnPath2);
|
|
|
|
|
|
slnPath = string.IsNullOrEmpty(slnPath1) ? slnPath2 : slnPath1;
|
|
|
|
|
|
if (string.IsNullOrEmpty(slnPath))
|
2024-06-05 00:33:54 +08:00
|
|
|
|
{
|
2024-06-08 21:51:45 +08:00
|
|
|
|
slnPath = "../";
|
2024-06-05 00:33:54 +08:00
|
|
|
|
}
|
2024-06-05 18:38:43 +08:00
|
|
|
|
|
|
|
|
|
|
CheckFirstSlnPath(slnPath);
|
|
|
|
|
|
var dotnetSlnCommandPart1 = $"dotnet sln \"{slnPath}\" 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);
|
|
|
|
|
|
|
|
|
|
|
|
await Console.Out.WriteLineAsync("恭喜~模块添加成功!");
|
2024-06-05 00:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取一个sln解决方案,多个将报错
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-06-05 18:38:43 +08:00
|
|
|
|
private string CheckFirstSlnPath(string slnPath)
|
2024-06-05 00:33:54 +08:00
|
|
|
|
{
|
2024-06-05 18:38:43 +08:00
|
|
|
|
string[] slnFiles = Directory.GetFiles(slnPath, "*.sln");
|
2024-06-05 00:33:54 +08:00
|
|
|
|
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",
|
2024-06-05 20:28:53 +08:00
|
|
|
|
Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}",
|
2024-06-05 00:33:54 +08:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-05 20:28:53 +08:00
|
|
|
|
|
2024-06-05 00:33:54 +08:00
|
|
|
|
/// <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}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|