Files
Yi.Framework/Yi.Abp.Net8/tool/Yi.Abp.Tool/Commands/CloneCommand.cs
易昊弘 350e4a5753 refactor(tool): 使工具支持跨平台运行
- 在 AddModuleCommand 和 CloneCommand 中增加了对操作系统类型的判断
- 为 Windows、macOS 和 Linux 系统分别设置了不同的进程启动信息
-优化了路径组合方式,使用 Path.Combine 以确保跨平台兼容性
2025-01-21 19:35:30 +08:00

70 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils;
namespace Yi.Abp.Tool.Commands
{
public class CloneCommand : ICommand
{
private const string CloneAddress= "https://gitee.com/ccnetcore/Yi";
public string Command => "clone";
public string? Description => "克隆最新YiFramework源代码需依赖git";
public void CommandLineApplication(CommandLineApplication application)
{
application.OnExecute(() =>
{
Console.WriteLine("正在克隆,请耐心等待");
StartCmd($"git clone {CloneAddress}");
return 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
};
// 判断操作系统
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
{
StartInfo = psi
};
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine(output);
proc.WaitForExit();
}
}
}