2024-11-05 22:12:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
2025-01-21 19:35:30 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-11-05 22:12:30 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.Extensions.CommandLineUtils;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Abp.Tool.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AddModuleCommand : ICommand
|
|
|
|
|
|
{
|
|
|
|
|
|
public string Command => "add-module";
|
2024-11-08 12:35:54 +08:00
|
|
|
|
public string? Description => "将内容添加到当前解决方案` yi-abp add-module <moduleName> [-p <path>] [-s <solution>] ";
|
2024-11-05 22:12:30 +08:00
|
|
|
|
public void CommandLineApplication(CommandLineApplication application)
|
|
|
|
|
|
{
|
2024-11-08 12:35:54 +08:00
|
|
|
|
application.HelpOption("-h|--help");
|
2024-11-05 22:22:42 +08:00
|
|
|
|
var modulePathOption= application.Option("-p|--modulePath", "模块路径",CommandOptionType.SingleValue);
|
2024-11-05 22:12:30 +08:00
|
|
|
|
var solutionOption= application.Option("-s|--solution", "解决方案路径",CommandOptionType.SingleValue);
|
2024-11-05 22:22:42 +08:00
|
|
|
|
var moduleNameArgument = application.Argument("moduleName", "模块名", (_) => { });
|
2024-11-05 22:12:30 +08:00
|
|
|
|
application.OnExecute(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var moduleName = moduleNameArgument.Value;
|
2024-11-08 12:35:54 +08:00
|
|
|
|
|
|
|
|
|
|
//模块路径默认按小写规则,默认在模块路径下一层
|
|
|
|
|
|
var modulePath =moduleName.ToLower().Replace(".", "-");
|
2024-11-05 22:12:30 +08:00
|
|
|
|
if (modulePathOption.HasValue())
|
|
|
|
|
|
{
|
2024-11-08 12:35:54 +08:00
|
|
|
|
modulePath = modulePathOption.Value();
|
2024-11-05 22:12:30 +08:00
|
|
|
|
}
|
2024-11-05 22:22:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-05 22:12:30 +08:00
|
|
|
|
//解决方案默认在模块文件夹上一级,也可以通过s进行指定
|
2024-11-08 12:35:54 +08:00
|
|
|
|
var slnPath = "../";
|
2024-11-05 22:22:42 +08:00
|
|
|
|
|
2024-11-08 12:35:54 +08:00
|
|
|
|
if (solutionOption.HasValue())
|
2024-11-05 22:12:30 +08:00
|
|
|
|
{
|
2024-11-08 12:35:54 +08:00
|
|
|
|
slnPath = solutionOption.Value();
|
2024-11-05 22:12:30 +08:00
|
|
|
|
}
|
2024-11-05 22:22:42 +08:00
|
|
|
|
|
2024-11-05 22:12:30 +08:00
|
|
|
|
CheckFirstSlnPath(slnPath);
|
2025-01-22 17:58:46 +08:00
|
|
|
|
var dotnetSlnCommandPart = new List<string>() { "Application", "Application.Contracts", "Domain", "Domain.Shared", "SqlSugarCore" };
|
|
|
|
|
|
var paths = dotnetSlnCommandPart.Select(x => Path.Combine(modulePath, $"{moduleName}.{x}")).ToArray();
|
2024-11-05 22:12:30 +08:00
|
|
|
|
CheckPathExist(paths);
|
2025-01-22 17:58:46 +08:00
|
|
|
|
|
|
|
|
|
|
var cmdCommands = dotnetSlnCommandPart.Select(x => $"dotnet sln \"{slnPath}\" add \"{Path.Combine(modulePath, $"{moduleName}.{x}")}\"").ToArray();
|
2024-11-05 22:12:30 +08:00
|
|
|
|
StartCmd(cmdCommands);
|
2024-11-05 22:22:42 +08:00
|
|
|
|
|
2024-11-05 22:12:30 +08:00
|
|
|
|
Console.WriteLine("恭喜~模块添加成功!");
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取一个sln解决方案,多个将报错
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private string CheckFirstSlnPath(string slnPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] slnFiles = Directory.GetFiles(slnPath, "*.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
|
|
|
|
|
|
{
|
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
|
UseShellExecute = false
|
|
|
|
|
|
};
|
2025-01-21 19:35:30 +08:00
|
|
|
|
// 判断操作系统
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
|
{
|
|
|
|
|
|
psi.FileName = "cmd.exe";
|
|
|
|
|
|
psi.Arguments = $"/c chcp 65001&{string.Join("&", cmdCommands)}";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
|
|
|
|
{
|
|
|
|
|
|
psi.FileName = "/bin/bash";
|
|
|
|
|
|
psi.Arguments = $"-c \"{string.Join("; ", cmdCommands)}\"";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-05 22:12:30 +08:00
|
|
|
|
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}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|