refactor(tool): 使工具支持跨平台运行

- 在 AddModuleCommand 和 CloneCommand 中增加了对操作系统类型的判断
- 为 Windows、macOS 和 Linux 系统分别设置了不同的进程启动信息
-优化了路径组合方式,使用 Path.Combine 以确保跨平台兼容性
This commit is contained in:
易昊弘
2025-01-21 19:35:30 +08:00
parent d1c1eed52e
commit 350e4a5753
2 changed files with 27 additions and 7 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils; using Microsoft.Extensions.CommandLineUtils;
@@ -41,7 +42,7 @@ namespace Yi.Abp.Tool.Commands
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 => Path.Combine(modulePath, $"{moduleName}.{x}")).ToArray();
CheckPathExist(paths); CheckPathExist(paths);
var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray(); var cmdCommands = dotnetSlnCommandPart2.Select(x => dotnetSlnCommandPart1 + x+"\"").ToArray();
@@ -81,15 +82,24 @@ namespace Yi.Abp.Tool.Commands
{ {
ProcessStartInfo psi = new ProcessStartInfo ProcessStartInfo psi = new ProcessStartInfo
{ {
FileName = "cmd.exe",
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
}; };
// 判断操作系统
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)}\"";
}
Process proc = new Process Process proc = new Process
{ {
StartInfo = psi StartInfo = psi

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils; using Microsoft.Extensions.CommandLineUtils;
@@ -35,15 +36,24 @@ namespace Yi.Abp.Tool.Commands
{ {
ProcessStartInfo psi = new ProcessStartInfo ProcessStartInfo psi = new ProcessStartInfo
{ {
FileName = "cmd.exe",
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
}; };
// 判断操作系统
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)}\"";
}
Process proc = new Process Process proc = new Process
{ {
StartInfo = psi StartInfo = psi