feature: supports custom actions (#638)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2024-11-01 17:23:31 +08:00
parent 7c5de7e48c
commit a36058ec51
No known key found for this signature in database
17 changed files with 478 additions and 2 deletions

View file

@ -0,0 +1,40 @@
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class ExecuteCustomAction : Popup
{
public Models.CustomAction CustomAction
{
get;
private set;
}
public ExecuteCustomAction(Repository repo, Models.CustomAction action, string sha)
{
_repo = repo;
_args = action.Arguments.Replace("${REPO}", _repo.FullPath);
if (!string.IsNullOrEmpty(sha))
_args = _args.Replace("${SHA}", sha);
CustomAction = action;
View = new Views.ExecuteCustomAction() { DataContext = this };
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = "Run custom action ...";
return Task.Run(() =>
{
Commands.ExecuteCustomAction.Run(_repo.FullPath, CustomAction.Executable, _args, SetProgressDescription);
CallUIThread(() => _repo.SetWatcherEnabled(true));
return true;
});
}
private readonly Repository _repo = null;
private string _args = string.Empty;
}
}

View file

@ -605,6 +605,39 @@ namespace SourceGit.ViewModels
menu.Items.Add(archive);
menu.Items.Add(new MenuItem() { Header = "-" });
var actions = new List<Models.CustomAction>();
foreach (var action in _repo.Settings.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Commit)
actions.Add(action);
}
if (actions.Count > 0)
{
var custom = new MenuItem();
custom.Header = App.Text("CommitCM.CustomAction");
custom.Icon = App.CreateMenuIcon("Icons.Action");
foreach (var action in actions)
{
var dup = action;
var item = new MenuItem();
item.Icon = App.CreateMenuIcon("Icons.Action");
item.Header = dup.Name;
item.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowAndStartPopup(new ExecuteCustomAction(_repo, action, commit.SHA));
e.Handled = true;
};
custom.Items.Add(item);
}
menu.Items.Add(custom);
menu.Items.Add(new MenuItem() { Header = "-" });
}
var copySHA = new MenuItem();
copySHA.Header = App.Text("CommitCM.CopySHA");
copySHA.Icon = App.CreateMenuIcon("Icons.Copy");

View file

@ -1287,6 +1287,45 @@ namespace SourceGit.ViewModels
return menu;
}
public ContextMenu CreateContextMenuForCustomAction()
{
var actions = new List<Models.CustomAction>();
foreach (var action in _settings.CustomActions)
{
if (action.Scope == Models.CustomActionScope.Repository)
actions.Add(action);
}
var menu = new ContextMenu();
menu.Placement = PlacementMode.BottomEdgeAlignedLeft;
if (actions.Count > 0)
{
foreach (var action in actions)
{
var dup = action;
var item = new MenuItem();
item.Icon = App.CreateMenuIcon("Icons.Action");
item.Header = dup.Name;
item.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowAndStartPopup(new ExecuteCustomAction(this, action, null));
e.Handled = true;
};
menu.Items.Add(item);
}
}
else
{
menu.Items.Add(new MenuItem() { Header = App.Text("Repository.CustomActions.Empty") });
}
return menu;
}
public ContextMenu CreateContextMenuForLocalBranch(Models.Branch branch)
{
var menu = new ContextMenu();

View file

@ -127,6 +127,17 @@ namespace SourceGit.ViewModels
set => _repo.Settings.PreferedOpenAIService = value;
}
public AvaloniaList<Models.CustomAction> CustomActions
{
get => _repo.Settings.CustomActions;
}
public Models.CustomAction SelectedCustomAction
{
get => _selectedCustomAction;
set => SetProperty(ref _selectedCustomAction, value);
}
public RepositoryConfigure(Repository repo)
{
_repo = repo;
@ -233,11 +244,21 @@ namespace SourceGit.ViewModels
public void RemoveSelectedIssueTracker()
{
if (_selectedIssueTrackerRule != null)
_repo.Settings.RemoveIssueTracker(_selectedIssueTrackerRule);
_repo.Settings.RemoveIssueTracker(_selectedIssueTrackerRule);
SelectedIssueTrackerRule = null;
}
public void AddNewCustomAction()
{
SelectedCustomAction = _repo.Settings.AddNewCustomAction();
}
public void RemoveSelectedCustomAction()
{
_repo.Settings.RemoveCustomAction(_selectedCustomAction);
SelectedCustomAction = null;
}
public void Save()
{
SetIfChanged("user.name", UserName, "");
@ -271,5 +292,6 @@ namespace SourceGit.ViewModels
private string _httpProxy;
private Models.CommitTemplate _selectedCommitTemplate = null;
private Models.IssueTrackerRule _selectedIssueTrackerRule = null;
private Models.CustomAction _selectedCustomAction = null;
}
}