Skip to content

Commit

Permalink
Distribute user scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
RussKie committed Oct 9, 2023
1 parent df7d030 commit 4f05dca
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 298 deletions.
6 changes: 3 additions & 3 deletions GitCommands/Settings/ConfigFileSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ public ConfigFileSettings(ConfigFileSettings? lowerPriority, ConfigFileSettingsC
SettingLevel = settingLevel;
}

public static ConfigFileSettings CreateEffective(GitModule module)
public static ConfigFileSettings CreateEffective(IGitModule module)
{
return CreateLocal(module,
CreateGlobal(CreateSystemWide()),
SettingLevel.Effective);
}

public static ConfigFileSettings CreateLocal(GitModule module, bool useSharedCache = true)
public static ConfigFileSettings CreateLocal(IGitModule module, bool useSharedCache = true)
{
return CreateLocal(module, lowerPriority: null, SettingLevel.Local, useSharedCache);
}

private static ConfigFileSettings CreateLocal(GitModule module, ConfigFileSettings? lowerPriority, SettingLevel settingLevel, bool useSharedCache = true)
private static ConfigFileSettings CreateLocal(IGitModule module, ConfigFileSettings? lowerPriority, SettingLevel settingLevel, bool useSharedCache = true)
{
return new ConfigFileSettings(lowerPriority,
ConfigFileSettingsCache.Create(Path.Combine(module.GitCommonDirectory, "config"), useSharedCache),
Expand Down
8 changes: 4 additions & 4 deletions GitCommands/Settings/DistributedSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ public static DistributedSettings CreateEffective(GitModule module)
return CreateLocal(module, CreateDistributed(module, CreateGlobal()), SettingLevel.Effective);
}

private static DistributedSettings CreateLocal(GitModule module, DistributedSettings? lowerPriority, SettingLevel settingLevel, bool useSharedCache = true)
private static DistributedSettings CreateLocal(IGitModule module, DistributedSettings? lowerPriority, SettingLevel settingLevel, bool useSharedCache = true)
{
return new DistributedSettings(lowerPriority,
GitExtSettingsCache.Create(Path.Combine(module.GitCommonDirectory, AppSettings.SettingsFileName), useSharedCache),
settingLevel);
}

public static DistributedSettings CreateLocal(GitModule module, bool useSharedCache = true)
public static DistributedSettings CreateLocal(IGitModule module, bool useSharedCache = true)
{
return CreateLocal(module, lowerPriority: null, SettingLevel.Local, useSharedCache);
}

private static DistributedSettings CreateDistributed(GitModule module, DistributedSettings? lowerPriority, bool useSharedCache = true)
private static DistributedSettings CreateDistributed(IGitModule module, DistributedSettings? lowerPriority, bool useSharedCache = true)
{
return new DistributedSettings(lowerPriority,
GitExtSettingsCache.Create(Path.Combine(module.WorkingDir, AppSettings.SettingsFileName), useSharedCache),
SettingLevel.Distributed);
}

public static DistributedSettings CreateDistributed(GitModule module, bool useSharedCache = true)
public static DistributedSettings CreateDistributed(IGitModule module, bool useSharedCache = true)
{
return CreateDistributed(module, lowerPriority: null, useSharedCache);
}
Expand Down
23 changes: 23 additions & 0 deletions GitExtensions.settings
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@
<string>en-US</string>
</value>
</item>
<item>
<key>
<string>ownScripts</string>
</key>
<value>
<string>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfScriptInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;ScriptInfo&gt;
&lt;Enabled&gt;true&lt;/Enabled&gt;
&lt;Name&gt;T&lt;/Name&gt;
&lt;Command&gt;cmd&lt;/Command&gt;
&lt;Arguments&gt;/c start cmd /k dotnet build /p:RunTranslationApp=true&lt;/Arguments&gt;
&lt;AddToRevisionGridContextMenu&gt;false&lt;/AddToRevisionGridContextMenu&gt;
&lt;OnEvent&gt;ShowInUserMenuBar&lt;/OnEvent&gt;
&lt;AskConfirmation&gt;false&lt;/AskConfirmation&gt;
&lt;RunInBackground&gt;true&lt;/RunInBackground&gt;
&lt;IsPowerShell&gt;false&lt;/IsPowerShell&gt;
&lt;HotkeyCommandIdentifier&gt;10001&lt;/HotkeyCommandIdentifier&gt;
&lt;Icon&gt;FileStatusRenamed&lt;/Icon&gt;
&lt;/ScriptInfo&gt;
&lt;/ArrayOfScriptInfo&gt;</string>
</value>
</item>
<item>
<key>
<string>RevisionLinkDefs</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ private class ScriptInfoProxy
[Browsable(false)]
public ImageList? ImageList { get; set; }

#if DEBUG
[Category(ScriptCategory)]
[PropertyOrder(0)]
[ReadOnly(true)]
#else
[Browsable(false)]
#endif
public int HotkeyCommandIdentifier { get; set; }

[Category(ScriptCategory)]
Expand Down
41 changes: 25 additions & 16 deletions GitUI/CommandsDialogs/SettingsDialog/Pages/ScriptsSettingsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
using GitExtUtils.GitUI.Theming;
using GitUI.ScriptsEngine;
using GitUIPluginInterfaces;
using Microsoft;
using ResourceManager;

namespace GitUI.CommandsDialogs.SettingsDialog.Pages
{
public partial class ScriptsSettingsPage : SettingsPageWithHeader
public partial class ScriptsSettingsPage : DistributedSettingsPage
{
private readonly TranslationString _scriptSettingsPageHelpDisplayArgumentsHelp = new("Arguments help");
private readonly TranslationString _scriptSettingsPageHelpDisplayContent = new(@"Use {option} for normal replacement.
Expand Down Expand Up @@ -79,8 +80,8 @@ public partial class ScriptsSettingsPage : SettingsPageWithHeader
ColorDepth = ColorDepth.Depth32Bit
};

private readonly IScriptsManager? _scriptsManager;
private readonly BindingList<ScriptInfoProxy> _scripts = new();
private readonly IScriptsManager _scriptsManager;
private SimpleHelpDisplayDialog? _argumentsCheatSheet;
private bool _handlingCheck;

Expand Down Expand Up @@ -158,39 +159,40 @@ public override void OnPageShown()
lvScripts.LargeImageList = lvScripts.SmallImageList = EmbeddedIcons;
_imagsLoaded = true;

BindScripts(_scripts, null);
BindScripts(_scripts, selectedScript: null);
}

protected override void SettingsToPage()
{
Validates.NotNull(CurrentSettings);
_scriptsManager.Initialize(CurrentSettings);

_scripts.Clear();

foreach (var script in _scriptsManager.GetScripts())
foreach (ScriptInfo script in _scriptsManager.GetScripts())
{
_scripts.Add(script);
}

if (_imagsLoaded)
{
BindScripts(_scripts, null);
BindScripts(_scripts, selectedScript: null);
}

base.SettingsToPage();
}

protected override void PageToSettings()
{
// TODO: this is an abomination, the whole script persistence must be scorched and rewritten

BindingList<ScriptInfo> scripts = _scriptsManager.GetScripts();
scripts.Clear();

foreach (ScriptInfoProxy proxy in _scripts)
// Update the currently edited script
ScriptInfo? selectedScript = SelectedScript;
if (selectedScript is null)
{
scripts.Add(proxy);
return;
}

AppSettings.OwnScripts = _scriptsManager.SerializeIntoXml();
_scriptsManager.Update(selectedScript);
_scriptsManager.Save();

base.PageToSettings();
}
Expand All @@ -206,6 +208,8 @@ private void BindScripts(IList<ScriptInfoProxy> scripts, ScriptInfoProxy? select

if (scripts.Count < 1)
{
SelectedScript = null;
propertyGrid1.SelectedObject = null;
btnAdd.Focus();
return;
}
Expand Down Expand Up @@ -261,11 +265,12 @@ private void BindScripts(IList<ScriptInfoProxy> scripts, ScriptInfoProxy? select
private void btnAdd_Click(object sender, EventArgs e)
{
ScriptInfoProxy script = _scripts.AddNew();
script.HotkeyCommandIdentifier = Math.Max(GitUI.ScriptsEngine.ScriptsManager.MinimumUserScriptID, _scripts.Max(s => s.HotkeyCommandIdentifier)) + 1;
script.Name = "<New Script>";
script.Enabled = true;

BindScripts(_scripts, script);
_scriptsManager.Add(script);

BindScripts(_scripts, selectedScript: script);
}

private void btnArgumentsHelp_Click(object sender, EventArgs e)
Expand All @@ -292,8 +297,11 @@ private void btnDelete_Click(object sender, EventArgs e)
return;
}

// This will save us from iterating over the saved collection later
_scriptsManager.Remove(SelectedScript);

_scripts.Remove(SelectedScript);
BindScripts(_scripts, null);
BindScripts(_scripts, selectedScript: null);
}

private void btnMoveDown_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -345,6 +353,7 @@ private void lvScripts_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvScripts.SelectedItems.Count < 1 || !(lvScripts.SelectedItems[0].Tag is ScriptInfoProxy script))
{
SelectedScript = null;
propertyGrid1.SelectedObject = null;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/SettingsDialog/SettingsPageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected ISettingsPageHost PageHost

public virtual bool IsInstantSavePage => false;

protected GitModule? Module => CommonLogic.Module;
protected IGitModule Module => CommonLogic.Module;

public virtual SettingsPageReference PageReference => new SettingsPageReferenceByType(GetType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace GitUI.ScriptsEngine
{
partial class ScriptsManager
partial class DistributedScriptsManager
{
/// <summary>Runs scripts.</summary>
internal static class ScriptRunner
Expand Down
Loading

0 comments on commit 4f05dca

Please sign in to comment.