2024-11-01 02:23:31 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public static class ExecuteCustomAction
|
|
|
|
|
{
|
|
|
|
|
public static void Run(string repo, string file, string args, Action<string> outputHandler)
|
|
|
|
|
{
|
|
|
|
|
var start = new ProcessStartInfo();
|
|
|
|
|
start.FileName = file;
|
|
|
|
|
start.Arguments = args;
|
|
|
|
|
start.UseShellExecute = false;
|
|
|
|
|
start.CreateNoWindow = true;
|
|
|
|
|
start.RedirectStandardOutput = true;
|
|
|
|
|
start.RedirectStandardError = true;
|
|
|
|
|
start.StandardOutputEncoding = Encoding.UTF8;
|
|
|
|
|
start.StandardErrorEncoding = Encoding.UTF8;
|
|
|
|
|
start.WorkingDirectory = repo;
|
|
|
|
|
|
|
|
|
|
// Force using en_US.UTF-8 locale to avoid GCM crash
|
|
|
|
|
if (OperatingSystem.IsLinux())
|
|
|
|
|
start.Environment.Add("LANG", "en_US.UTF-8");
|
|
|
|
|
|
|
|
|
|
// Fix macOS `PATH` env
|
|
|
|
|
if (OperatingSystem.IsMacOS() && !string.IsNullOrEmpty(Native.OS.CustomPathEnv))
|
|
|
|
|
start.Environment.Add("PATH", Native.OS.CustomPathEnv);
|
|
|
|
|
|
|
|
|
|
var proc = new Process() { StartInfo = start };
|
2024-11-04 20:15:20 -08:00
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
2024-11-01 02:23:31 -07:00
|
|
|
|
proc.OutputDataReceived += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.Data != null)
|
|
|
|
|
outputHandler?.Invoke(e.Data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proc.ErrorDataReceived += (_, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.Data != null)
|
2024-11-04 20:15:20 -08:00
|
|
|
|
{
|
2024-11-01 02:23:31 -07:00
|
|
|
|
outputHandler?.Invoke(e.Data);
|
2024-11-04 20:24:33 -08:00
|
|
|
|
builder.AppendLine(e.Data);
|
2024-11-10 20:16:20 -08:00
|
|
|
|
}
|
2024-11-01 02:23:31 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
proc.Start();
|
2024-11-03 01:00:17 -08:00
|
|
|
|
proc.BeginOutputReadLine();
|
|
|
|
|
proc.BeginErrorReadLine();
|
|
|
|
|
proc.WaitForExit();
|
2024-11-01 02:23:31 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
App.RaiseException(repo, e.Message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 20:15:20 -08:00
|
|
|
|
var exitCode = proc.ExitCode;
|
2024-11-01 02:23:31 -07:00
|
|
|
|
proc.Close();
|
2024-11-04 20:15:20 -08:00
|
|
|
|
|
|
|
|
|
if (exitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
var errMsg = builder.ToString();
|
|
|
|
|
Dispatcher.UIThread.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
App.RaiseException(repo, errMsg);
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-01 02:23:31 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|