Skip to content

Commit

Permalink
Refactor logic for actions you can take on repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
Scepheo committed Jun 4, 2019
1 parent af45957 commit 2d8290a
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 109 deletions.
73 changes: 73 additions & 0 deletions src/GitMan/Actions/RepositoryAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace GitMan.Actions
{
internal class RepositoryAction
{
public bool UseShellExecute { get; set; }
public string NameTemplate { get; set; }
public string CommandTemplate { get; set; }
public string SearchFilter { get; set; }

public IEnumerable<MenuItem> GetMenuItems(DirectoryInfo directoryInfo)
{
return EnumerateMatches(directoryInfo).Select(GetMenuItem);
}

private IEnumerable<FileSystemInfo> EnumerateMatches(DirectoryInfo directoryInfo)
{
return directoryInfo.EnumerateFileSystemInfos(SearchFilter, SearchOption.AllDirectories);
}

private static string Substitute(string value, FileSystemInfo info)
{
var name = info.Name;
var path = info.FullName;
var directory = Path.GetDirectoryName(path);

var result = value;
result = result.Replace("{name}", name);
result = result.Replace("{path}", path);
result = result.Replace("{directory}", directory);
return result;
}

private string GetName(FileSystemInfo fileSystemInfo)
{
var name = Substitute(NameTemplate, fileSystemInfo);
return name;
}

private ProcessStartInfo GetProcessStartInfo(FileSystemInfo fileSystemInfo)
{
var command = Substitute(CommandTemplate, fileSystemInfo);

var startInfo = new ProcessStartInfo
{
FileName = command,
UseShellExecute = UseShellExecute,
};

return startInfo;
}

private MenuItem GetMenuItem(FileSystemInfo fileSystemInfo)
{
var name = GetName(fileSystemInfo);

void onClick(object sender, EventArgs eventArgs)
{
var startInfo = GetProcessStartInfo(fileSystemInfo);
Process.Start(startInfo);
}

var menuItem = new MenuItem(name, onClick);
return menuItem;
}
}
}
96 changes: 96 additions & 0 deletions src/GitMan/Actions/RepositoryActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using GitMan.Config;
using System.Collections.Generic;
using System.IO;

namespace GitMan.Actions
{
internal static class RepositoryActions
{
public static IEnumerable<RepositoryAction> GetDefaults(Settings settings)
{
yield return MakeFolderAction();

if (GitBashExists(settings))
{
yield return MakeGitBashAction(settings);
}

if (VsCodeExists(settings))
{
yield return MakeVsCodeAction(settings);
}

yield return MakeSolutionAction();
}

private static RepositoryAction MakeSolutionAction()
{
var action = new RepositoryAction
{
CommandTemplate = "{path}",
NameTemplate = "Open {name}",
SearchFilter = "*.sln",
UseShellExecute = true,
};

return action;
}

private static RepositoryAction MakeFolderAction()
{
var action = new RepositoryAction
{
CommandTemplate = "{directory}",
NameTemplate = "Open folder",
SearchFilter = ".git",
UseShellExecute = true,
};

return action;
}

private static RepositoryAction MakeGitBashAction(Settings settings)
{
var gitBashPath = settings.GitBashPath;
var commandTemplate = $"\"{gitBashPath}\" --cd=\"{{directory}}\"";

var action = new RepositoryAction
{
CommandTemplate = commandTemplate,
NameTemplate = "Git Bash",
SearchFilter = ".git",
UseShellExecute = false,
};

return action;
}

private static RepositoryAction MakeVsCodeAction(Settings settings)
{
var vsCodePath = settings.VsCodePath;
var commandTemplate = $"\"{vsCodePath}\" \"{{directory}}\"";

var action = new RepositoryAction
{
CommandTemplate = commandTemplate,
NameTemplate = "VS Code",
SearchFilter = ".git",
UseShellExecute = false,
};

return action;
}

private static bool GitBashExists(Settings settings)
{
var gitBashExists = File.Exists(settings.GitBashPath);
return gitBashExists;
}

private static bool VsCodeExists(Settings settings)
{
var vsCodeExists = File.Exists(settings.VsCodePath);
return vsCodeExists;
}
}
}
117 changes: 8 additions & 109 deletions src/GitMan/Context.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitMan.Clients;
using GitMan.Actions;
using GitMan.Clients;
using GitMan.Config;
using System;
using System.Collections.Generic;
Expand All @@ -16,6 +17,7 @@ public class Context : ApplicationContext
private readonly NotifyIcon _icon;
private readonly Main _main;
private readonly Settings _settings;
private readonly RepositoryAction[] _repositoryActions;

public Context()
{
Expand All @@ -28,6 +30,8 @@ public Context()
_main = new Main();

_settings = Settings.Load();

_repositoryActions = RepositoryActions.GetDefaults(_settings).ToArray();
}

private void Icon_DoubleClick(object sender, EventArgs e)
Expand Down Expand Up @@ -81,124 +85,19 @@ protected override void Dispose(bool disposing)
private MenuItem MakeMenuItem(Repository repository)
{
var name = repository.Name;
var directoryInfo = new DirectoryInfo(repository.FullName);

var subItems = new List<MenuItem>();

var folderItem = MakeFolderItem(repository);
subItems.Add(folderItem);

if (GitBashExists())
{
var gitBashItem = MakeGitBashItem(repository);
subItems.Add(gitBashItem);
}

if (repository.IsVsCodeProject() && VsCodeExists())
foreach (var repositoryAction in _repositoryActions)
{
var vsCodeItem = MakeVsCodeItem(repository);
subItems.Add(vsCodeItem);
}

foreach (var solutionFile in repository.SolutionFiles)
{
var solutionItem = MakeSolutionItem(solutionFile);
subItems.Add(solutionItem);
subItems.AddRange(repositoryAction.GetMenuItems(directoryInfo));
}

var menuItem = new MenuItem(name, subItems.ToArray());
return menuItem;
}

private static MenuItem MakeSolutionItem(FileInfo solutionFile)
{
void onClick(object sender, EventArgs eventArgs)
{
var startInfo = new ProcessStartInfo
{
FileName = solutionFile.FullName,
UseShellExecute = true,
};

Process.Start(startInfo);
}

var name = $"Open {solutionFile.Name}";
var menuItem = new MenuItem(name, onClick);
return menuItem;
}

private bool GitBashExists()
{
var gitBashExists = File.Exists(_settings.GitBashPath);
return gitBashExists;
}

private MenuItem MakeGitBashItem(Repository repository)
{
void onClick(object sender, EventArgs eventArgs)
{
var fullName = repository.FullName;
var argument = $"--cd=\"{fullName}\"";

var startInfo = new ProcessStartInfo
{
FileName = _settings.GitBashPath,
Arguments = argument,
};

Process.Start(startInfo);
}

const string name = "Git bash";
var menuItem = new MenuItem(name, onClick);
return menuItem;
}

private static MenuItem MakeFolderItem(Repository repository)
{
void onClick(object sender, EventArgs eventArgs)
{
var startInfo = new ProcessStartInfo
{
FileName = repository.FullName,
UseShellExecute = true,
};

Process.Start(startInfo);
}

const string name = "Open folder";
var menuItem = new MenuItem(name, onClick);
return menuItem;
}

private bool VsCodeExists()
{
var vsCodeExists = File.Exists(_settings.VsCodePath);
return vsCodeExists;
}

private MenuItem MakeVsCodeItem(Repository repository)
{
void onClick(object sender, EventArgs eventArgs)
{
var vsCodePath = _settings.VsCodePath;
var fullName = repository.FullName;

var startInfo = new ProcessStartInfo
{
FileName = vsCodePath,
Arguments = fullName,
};

Process.Start(startInfo);
}

const string name = "VS Code";
var menuItem = new MenuItem(name, onClick);
return menuItem;
}

private MenuItem MakeExitItem()
{
void onClick(object sender, EventArgs eventArgs)
Expand Down

0 comments on commit 2d8290a

Please sign in to comment.