Files
sourcegit/src/ViewModels/ExecuteCustomAction.cs
leo 271f02b694 refactor: apply input control value from end to start
For example, this will avoid applying the first parameter to $10 - $19

Signed-off-by: leo <longshuang@msn.cn>
2025-06-26 09:28:39 +08:00

255 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public interface ICustomActionControlParameter
{
string GetValue();
}
public class CustomActionControlTextBox : ICustomActionControlParameter
{
public string Label { get; set; } = string.Empty;
public string Placeholder { get; set; } = string.Empty;
public string Text { get; set; } = string.Empty;
public CustomActionControlTextBox(string label, string placeholder, string defaultValue)
{
Label = label;
Placeholder = placeholder;
Text = defaultValue;
}
public string GetValue() => Text;
}
public class CustomActionControlPathSelector : ObservableObject, ICustomActionControlParameter
{
public string Label { get; set; } = string.Empty;
public string Placeholder { get; set; } = string.Empty;
public bool IsFolder { get; set; } = false;
public string Path
{
get => _path;
set => SetProperty(ref _path, value);
}
public CustomActionControlPathSelector(string label, string placeholder, bool isFolder, string defaultValue)
{
Label = label;
Placeholder = placeholder;
IsFolder = isFolder;
_path = defaultValue;
}
public string GetValue() => _path;
private string _path = string.Empty;
}
public class CustomActionControlCheckBox : ICustomActionControlParameter
{
public string Label { get; set; } = string.Empty;
public string ToolTip { get; set; } = string.Empty;
public string CheckedValue { get; set; } = string.Empty;
public bool IsChecked { get; set; }
public CustomActionControlCheckBox(string label, string tooltip, string checkedValue, bool isChecked)
{
Label = label;
ToolTip = string.IsNullOrEmpty(tooltip) ? null : tooltip;
CheckedValue = checkedValue;
IsChecked = isChecked;
}
public string GetValue() => IsChecked ? CheckedValue : string.Empty;
}
public class ExecuteCustomAction : Popup
{
public Models.CustomAction CustomAction
{
get;
}
public List<ICustomActionControlParameter> ControlParameters
{
get;
} = [];
public bool IsSimpleMode
{
get => ControlParameters.Count == 0;
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action)
{
_repo = repo;
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir());
CustomAction = action;
PrepareControlParameters();
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Branch branch)
{
_repo = repo;
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${BRANCH}", branch.FriendlyName);
CustomAction = action;
PrepareControlParameters();
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Commit commit)
{
_repo = repo;
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${SHA}", commit.SHA);
CustomAction = action;
PrepareControlParameters();
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Tag tag)
{
_repo = repo;
_commandline = action.Arguments.Replace("${REPO}", GetWorkdir()).Replace("${TAG}", tag.Name);
CustomAction = action;
PrepareControlParameters();
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = "Run custom action ...";
var cmdline = _commandline;
for (var i = ControlParameters.Count - 1; i >= 0; i--)
{
var param = ControlParameters[i];
cmdline = cmdline.Replace($"${i}", param.GetValue());
}
var log = _repo.CreateLog(CustomAction.Name);
Use(log);
return Task.Run(() =>
{
log.AppendLine($"$ {CustomAction.Executable} {cmdline}\n");
if (CustomAction.WaitForExit)
RunAndWait(cmdline, log);
else
Run(cmdline);
log.Complete();
CallUIThread(() => _repo.SetWatcherEnabled(true));
return true;
});
}
private void PrepareControlParameters()
{
foreach (var ctl in CustomAction.Controls)
{
switch (ctl.Type)
{
case Models.CustomActionControlType.TextBox:
ControlParameters.Add(new CustomActionControlTextBox(ctl.Label, ctl.Description, ctl.StringValue));
break;
case Models.CustomActionControlType.CheckBox:
ControlParameters.Add(new CustomActionControlCheckBox(ctl.Label, ctl.Description, ctl.StringValue, ctl.BoolValue));
break;
case Models.CustomActionControlType.PathSelector:
ControlParameters.Add(new CustomActionControlPathSelector(ctl.Label, ctl.Description, ctl.BoolValue, ctl.StringValue));
break;
}
}
}
private string GetWorkdir()
{
return OperatingSystem.IsWindows() ? _repo.FullPath.Replace("/", "\\") : _repo.FullPath;
}
private void Run(string args)
{
var start = new ProcessStartInfo();
start.FileName = CustomAction.Executable;
start.Arguments = args;
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.WorkingDirectory = _repo.FullPath;
try
{
Process.Start(start);
}
catch (Exception e)
{
CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message));
}
}
private void RunAndWait(string args, Models.ICommandLog log)
{
var start = new ProcessStartInfo();
start.FileName = CustomAction.Executable;
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.FullPath;
var proc = new Process() { StartInfo = start };
var builder = new StringBuilder();
proc.OutputDataReceived += (_, e) =>
{
if (e.Data != null)
log?.AppendLine(e.Data);
};
proc.ErrorDataReceived += (_, e) =>
{
if (e.Data != null)
{
log?.AppendLine(e.Data);
builder.AppendLine(e.Data);
}
};
try
{
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
var exitCode = proc.ExitCode;
if (exitCode != 0)
{
var errMsg = builder.ToString().Trim();
if (!string.IsNullOrEmpty(errMsg))
CallUIThread(() => App.RaiseException(_repo.FullPath, errMsg));
}
}
catch (Exception e)
{
CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message));
}
proc.Close();
}
private readonly Repository _repo = null;
private readonly string _commandline = string.Empty;
}
}