Skip to content

Commit

Permalink
#49 Refactor CommandHelper - Take2
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Jan 31, 2019
1 parent 101bb1a commit 8ab783b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 123 deletions.
24 changes: 12 additions & 12 deletions TGit/Commands/ContextMenuCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public ContextMenuCommands(OleMenuCommandService mcs, DTE dte,

public void AddCommands()
{
CommandHelper.AddCommand(_mcs, ShowLogContextCommand, PkgCmdIDList.ShowLogContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, DiskBrowserContextCommand, PkgCmdIDList.DiskBrowserContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, RepoBrowserContextCommand, PkgCmdIDList.RepoBrowserContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, RevGraphContextCommand, PkgCmdIDList.RevGraphContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.ShowLogContext, ShowLogContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.DiskBrowserContext, DiskBrowserContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.RepoBrowserContext, RepoBrowserContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.RevGraphContext, RevGraphContextCommand, ActiveDocument_BeforeQueryStatus);

CommandHelper.AddCommand(_mcs, BlameContextCommand, PkgCmdIDList.BlameContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.BlameContext, BlameContextCommand, ActiveDocument_BeforeQueryStatus);

CommandHelper.AddCommand(_mcs, MergeContextCommand, PkgCmdIDList.MergeContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.MergeContext, MergeContextCommand, ActiveDocument_BeforeQueryStatus);

CommandHelper.AddCommand(_mcs, PullContextCommand, PkgCmdIDList.PullContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, FetchContextCommand, PkgCmdIDList.FetchContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, CommitContextCommand, PkgCmdIDList.CommitContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, RevertContextCommand, PkgCmdIDList.RevertContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, DiffContextCommand, PkgCmdIDList.DiffContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PrefDiffContextCommand, PkgCmdIDList.PrefDiffContext, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.PullContext, PullContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.FetchContext, FetchContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.CommitContext, CommitContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.RevertContext, RevertContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.DiffContext, DiffContextCommand, ActiveDocument_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.PrefDiffContext, PrefDiffContextCommand, ActiveDocument_BeforeQueryStatus);
}

private void ActiveDocument_BeforeQueryStatus(object sender, EventArgs e)
Expand Down
37 changes: 9 additions & 28 deletions TGit/Commands/GitFlowMenuCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,24 @@ public void AddCommands()
{
//GitFlow Commands
//Start/Finish Feature
var startFeature = CommandHelper.CreateCommand(StartFeatureCommand, PkgCmdIDList.StartFeature);
startFeature.BeforeQueryStatus += CommandHelper.GitFlow_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, startFeature);

var finishFeature = CommandHelper.CreateCommand(FinishFeatureCommand, PkgCmdIDList.FinishFeature);
finishFeature.BeforeQueryStatus += CommandHelper.Feature_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, finishFeature);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.StartFeature, StartFeatureCommand, CommandHelper.GitFlow_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.FinishFeature, FinishFeatureCommand, CommandHelper.Feature_BeforeQueryStatus);

//Start/Finish Release
var startRelease = CommandHelper.CreateCommand(StartReleaseCommand, PkgCmdIDList.StartRelease);
startRelease.BeforeQueryStatus += CommandHelper.GitFlow_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, startRelease);

var finishRelease = CommandHelper.CreateCommand(FinishReleaseCommand, PkgCmdIDList.FinishRelease);
finishRelease.BeforeQueryStatus += CommandHelper.Release_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, finishRelease);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.StartRelease, StartReleaseCommand, CommandHelper.GitFlow_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.FinishRelease, FinishReleaseCommand, CommandHelper.Release_BeforeQueryStatus);

//Start/Finish Hotfix
var startHotfix = CommandHelper.CreateCommand(StartHotfixCommand, PkgCmdIDList.StartHotfix);
startHotfix.BeforeQueryStatus += CommandHelper.GitFlow_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, startHotfix);

var finishHotfix = CommandHelper.CreateCommand(FinishHotfixCommand, PkgCmdIDList.FinishHotfix);
finishHotfix.BeforeQueryStatus += CommandHelper.Hotfix_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, finishHotfix);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.StartHotfix, StartHotfixCommand, CommandHelper.GitFlow_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.FinishHotfix, FinishHotfixCommand, CommandHelper.Hotfix_BeforeQueryStatus);

//Init
var init = CommandHelper.CreateCommand(InitCommand, PkgCmdIDList.Init);
init.BeforeQueryStatus += CommandHelper.GitHubFlow_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, init);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Init, InitCommand, CommandHelper.GitHubFlow_BeforeQueryStatus);

//GitHubFlow Commands
//Start/Finish Feature
CommandHelper.AddCommand(_mcs, StartFeatureGitHubCommand, PkgCmdIDList.StartFeatureGitHub);
var finishFeatureGitHub = CommandHelper.CreateCommand(FinishFeatureGitHubCommand, PkgCmdIDList.FinishFeatureGitHub);
finishFeatureGitHub.BeforeQueryStatus += CommandHelper.Feature_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, finishFeatureGitHub);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.StartFeatureGitHub, StartFeatureGitHubCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.FinishFeatureGitHub, FinishFeatureGitHubCommand, CommandHelper.Feature_BeforeQueryStatus);
}

private void InitCommand(object sender, EventArgs e)
Expand Down
6 changes: 3 additions & 3 deletions TGit/Commands/GitSVNMenuCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public GitSVNMenuCommands(OleMenuCommandService mcs, DTE dte,

public void AddCommands()
{
CommandHelper.AddCommand(_mcs, SvnDCommitCommand, PkgCmdIDList.SvnDCommit);
CommandHelper.AddCommand(_mcs, SvnFetchCommand, PkgCmdIDList.SvnFetch);
CommandHelper.AddCommand(_mcs, SvnRebaseCommand, PkgCmdIDList.SvnRebase);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.SvnDCommit, SvnDCommitCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.SvnFetch, SvnFetchCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.SvnRebase, SvnRebaseCommand);
}

private void SvnDCommitCommand(object sender, EventArgs e)
Expand Down
54 changes: 26 additions & 28 deletions TGit/Commands/MainMenuCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,32 @@ public MainMenuCommands(OleMenuCommandService mcs, DTE dte, OptionPageGrid gener

public void AddCommands()
{
CommandHelper.AddCommand(_mcs, ShowChangesCommand, PkgCmdIDList.ShowChanges);
CommandHelper.AddCommand(_mcs, PullCommand, PkgCmdIDList.Pull);
CommandHelper.AddCommand(_mcs, FetchCommand, PkgCmdIDList.Fetch);
CommandHelper.AddCommand(_mcs, CommitCommand, PkgCmdIDList.Commit);
CommandHelper.AddCommand(_mcs, PushCommand, PkgCmdIDList.Push);

CommandHelper.AddCommand(_mcs, ShowLogCommand, PkgCmdIDList.ShowLog);
CommandHelper.AddCommand(_mcs, DiskBrowserCommand, PkgCmdIDList.DiskBrowser);
CommandHelper.AddCommand(_mcs, RepoBrowserCommand, PkgCmdIDList.RepoBrowser);
CommandHelper.AddCommand(_mcs, RevGraphCommand, PkgCmdIDList.RevGraph);

CommandHelper.AddCommand(_mcs, CreateStashCommand, PkgCmdIDList.CreateStash);
var applyStash = CommandHelper.CreateCommand(ApplyStashCommand, PkgCmdIDList.ApplyStash);
applyStash.BeforeQueryStatus += CommandHelper.ApplyStash_BeforeQueryStatus;
CommandHelper.AddCommand(_mcs, applyStash);

CommandHelper.AddCommand(_mcs, BranchCommand, PkgCmdIDList.Branch);
CommandHelper.AddCommand(_mcs, SwitchCommand, PkgCmdIDList.Switch);
CommandHelper.AddCommand(_mcs, MergeCommand, PkgCmdIDList.Merge);
CommandHelper.AddCommand(_mcs, RebaseCommand, PkgCmdIDList.Rebase);

CommandHelper.AddCommand(_mcs, RevertCommand, PkgCmdIDList.Revert);
CommandHelper.AddCommand(_mcs, ResolveCommand, PkgCmdIDList.Resolve);
CommandHelper.AddCommand(_mcs, TagCommand, PkgCmdIDList.Tag);
CommandHelper.AddCommand(_mcs, SyncCommand, PkgCmdIDList.Sync);
CommandHelper.AddCommand(_mcs, CleanupCommand, PkgCmdIDList.Cleanup);
CommandHelper.AddCommand(_mcs, BrowseRefCommand, PkgCmdIDList.BrowseRef);
CommandHelper.AddCommand(_mcs, AbortMergeCommand, PkgCmdIDList.AbortMerge);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.ShowChanges, ShowChangesCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Pull, PullCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Fetch, FetchCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Commit, CommitCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Push, PushCommand);

CommandHelper.AddCommand(_mcs, PkgCmdIDList.ShowLog, ShowLogCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.DiskBrowser, DiskBrowserCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.RepoBrowser, RepoBrowserCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.RevGraph, RevGraphCommand);

CommandHelper.AddCommand(_mcs, PkgCmdIDList.CreateStash, CreateStashCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.ApplyStash, ApplyStashCommand, CommandHelper.ApplyStash_BeforeQueryStatus);

CommandHelper.AddCommand(_mcs, PkgCmdIDList.Branch, BranchCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Switch, SwitchCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Merge, MergeCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Rebase, RebaseCommand);

CommandHelper.AddCommand(_mcs, PkgCmdIDList.Revert, RevertCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Resolve, ResolveCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Tag, TagCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Sync, SyncCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.Cleanup, CleanupCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.BrowseRef, BrowseRefCommand);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.AbortMerge, AbortMergeCommand);

}

Expand Down
55 changes: 14 additions & 41 deletions TGit/Helpers/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ public static class CommandHelper
{
public static EnvHelper EnvHelper;

public static void AddCommand(OleMenuCommandService mcs, EventHandler handler, uint commandId, EventHandler eventHandler)
{
mcs.AddCommand(CreateCommand(handler, commandId, eventHandler));
}

public static void AddCommand(OleMenuCommandService mcs, EventHandler handler, uint commandId)
{
mcs.AddCommand(CreateCommand(handler, commandId));
}
public static void AddCommand(OleMenuCommandService mcs, uint commandId, EventHandler commandHandler = null, EventHandler eventHandler = null)
=> mcs.AddCommand(CreateCommand(commandId, commandHandler, eventHandler));

public static void AddCommand(OleMenuCommandService mcs, MenuCommand command)
{
mcs.AddCommand(command);
}

public static OleMenuCommand CreateCommand(EventHandler handler, uint commandId, EventHandler eventHandler = null)
public static OleMenuCommand CreateCommand(uint commandId, EventHandler commandHandler = null, EventHandler eventHandler = null)
{
var menuCommandId = new CommandID(GuidList.GuidTgitCmdSet, (int)commandId);
var menuItem = new OleMenuCommand(handler, menuCommandId);
var menuItem = new OleMenuCommand(commandHandler, menuCommandId);

if (eventHandler != null)
{
Expand All @@ -36,15 +24,8 @@ public static OleMenuCommand CreateCommand(EventHandler handler, uint commandId,
return menuItem;
}

public static OleMenuCommand CreateCommand(uint commandId)
{
return CreateCommand(null, commandId, Solution_BeforeQueryStatus);
}

public static void ApplyStash_BeforeQueryStatus(object sender, EventArgs e)
{
((OleMenuCommand) sender).Enabled = EnvHelper.HasStash();
}
public static void ApplyStash_BeforeQueryStatus(object sender, EventArgs e)
=> ((OleMenuCommand)sender).Enabled = EnvHelper.HasStash();

public static void Feature_BeforeQueryStatus(object sender, EventArgs e)
{
Expand All @@ -67,25 +48,17 @@ public static void Release_BeforeQueryStatus(object sender, EventArgs e)
((OleMenuCommand)sender).Enabled = EnvHelper.HasSolutionDir() && EnvHelper.BranchNameStartsWith(gitConfig.ReleasePrefix);
}

public static void Solution_BeforeQueryStatus(object sender, EventArgs e)
{
((OleMenuCommand) sender).Enabled = EnvHelper.HasSolutionDir();
}
public static void Solution_BeforeQueryStatus(object sender, EventArgs e)
=> ((OleMenuCommand)sender).Enabled = EnvHelper.HasSolutionDir();

public static void SolutionVisibility_BeforeQueryStatus(object sender, EventArgs e)
{
((OleMenuCommand) sender).Visible = EnvHelper.HasSolutionDir();
}
public static void SolutionVisibility_BeforeQueryStatus(object sender, EventArgs e)
=> ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir();

public static void GitFlow_BeforeQueryStatus(object sender, EventArgs e)
{
((OleMenuCommand) sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitFlow();
}
public static void GitFlow_BeforeQueryStatus(object sender, EventArgs e)
=> ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitFlow();

public static void GitHubFlow_BeforeQueryStatus(object sender, EventArgs e)
{
((OleMenuCommand) sender).Visible = EnvHelper.HasSolutionDir() && !EnvHelper.IsGitFlow();
}
public static void GitHubFlow_BeforeQueryStatus(object sender, EventArgs e)
=> ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir() && !EnvHelper.IsGitFlow();

public static void GitSvn_BeforeQueryStatus(object sender, EventArgs e)
=> ((OleMenuCommand)sender).Visible = EnvHelper.HasSolutionDir() && EnvHelper.IsGitSvn();
Expand Down
14 changes: 3 additions & 11 deletions TGit/TGITPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,9 @@ private void MainThreadInitialization()
_mcs.AddCommand(tgitMenu);
_mcs.AddCommand(tgitContextMenu);

var tgitGitFlowMenu = CommandHelper.CreateCommand(PkgCmdIDList.TGitGitFlowMenu);
tgitGitFlowMenu.BeforeQueryStatus += CommandHelper.SolutionVisibility_BeforeQueryStatus;
_mcs.AddCommand(tgitGitFlowMenu);

var tgitGitHubFlowMenu = CommandHelper.CreateCommand(PkgCmdIDList.TGitGitHubFlowMenu);
tgitGitHubFlowMenu.BeforeQueryStatus += CommandHelper.GitHubFlow_BeforeQueryStatus;
_mcs.AddCommand(tgitGitHubFlowMenu);

var tgitGitSvnMenu = CommandHelper.CreateCommand(PkgCmdIDList.TGitSvnMenu);
tgitGitSvnMenu.BeforeQueryStatus += CommandHelper.GitSvn_BeforeQueryStatus;
_mcs.AddCommand(tgitGitSvnMenu);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.TGitGitFlowMenu, eventHandler: CommandHelper.SolutionVisibility_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.TGitGitHubFlowMenu, eventHandler: CommandHelper.GitHubFlow_BeforeQueryStatus);
CommandHelper.AddCommand(_mcs, PkgCmdIDList.TGitSvnMenu, eventHandler: CommandHelper.GitSvn_BeforeQueryStatus);
}
}
}

0 comments on commit 8ab783b

Please sign in to comment.