Skip to content

Commit

Permalink
Make repository actions configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Scepheo committed Jun 18, 2019
1 parent 49a5626 commit 6927977
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 116 deletions.
38 changes: 29 additions & 9 deletions src/GitMan/Actions/RepositoryAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GitMan.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -9,10 +10,29 @@ 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; }
private readonly bool _useShellExecute;
private readonly string _nameTemplate;
private readonly string _commandTemplate;
private readonly string _searchFilter;

public RepositoryAction(ActionSettings settings)
{
_useShellExecute = settings.Shell.GetValueOrDefault(false);
_nameTemplate = settings.Name;
_commandTemplate = GetCommand(settings.Program, settings.Args);
_searchFilter = settings.SearchFilter;
}

private static string GetCommand(string program, string[] args)
{
program ??= string.Empty;
args ??= Array.Empty<string>();

var items = Enumerable.Repeat(program, 1).Concat(args);
var quoted = items.Select(item => $"\"{item}\"");
var command = string.Join(' ', quoted);
return command;
}

public IEnumerable<MenuItem> GetMenuItems(DirectoryInfo directoryInfo)
{
Expand All @@ -21,7 +41,7 @@ public IEnumerable<MenuItem> GetMenuItems(DirectoryInfo directoryInfo)

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

private static string Substitute(string value, FileSystemInfo info)
Expand All @@ -39,18 +59,18 @@ private static string Substitute(string value, FileSystemInfo info)

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

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

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

return startInfo;
Expand Down
96 changes: 0 additions & 96 deletions src/GitMan/Actions/RepositoryActions.cs

This file was deleted.

11 changes: 11 additions & 0 deletions src/GitMan/Config/ActionSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace GitMan.Config
{
internal class ActionSettings
{
public string Name { get; set; }
public string Program { get; set; }
public string[] Args { get; set; }
public string SearchFilter { get; set; }
public bool? Shell { get; set; }
}
}
92 changes: 92 additions & 0 deletions src/GitMan/Config/DefaultActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace GitMan.Config
{
internal static class DefaultActions
{
public static ActionSettings[] Get()
{
var actions = new List<ActionSettings>();

AddFolderAction(actions);
AddGitBashAction(actions);
AddVsCodeAction(actions);
AddSolutionAction(actions);

return actions.ToArray();
}

private static void AddSolutionAction(List<ActionSettings> actions)
{
var action = new ActionSettings
{
Program = "{path}",
Name = "Open {name}",
SearchFilter = "*.sln",
Shell = true,
};

actions.Add(action);
}

private static void AddFolderAction(List<ActionSettings> actions)
{
var action = new ActionSettings
{
Program = "{directory}",
Name = "Open folder",
SearchFilter = ".git",
Shell = true,
};

actions.Add(action);
}

private static void AddGitBashAction(List<ActionSettings> actions)
{
const string gitBashPath = "C:\\Program Files\\Git\\git-bash.exe";

var gitBashExists = File.Exists(gitBashPath);

if (gitBashExists)
{
var args = new [] { "--cd={directory}" };

var action = new ActionSettings
{
Program = gitBashPath,
Args = args,
Name = "Git Bash",
SearchFilter = ".git",
};

actions.Add(action);
}
}

private static void AddVsCodeAction(List<ActionSettings> actions)
{
var userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
var vsCodePath = Path.Combine(userProfile, "AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe");

var vsCodeExists = File.Exists(vsCodePath);

if (vsCodeExists)
{
var args = new [] { "{directory}" };

var action = new ActionSettings
{
Program = vsCodePath,
Args = args,
Name = "VS Code",
SearchFilter = ".git",
};

actions.Add(action);
}
}
}
}
15 changes: 9 additions & 6 deletions src/GitMan/Config/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ internal class Settings
public string GitBashPath { get; set; }
public AzureProviderSettings[] AzureProviders { get; set; }
public GitHubProviderSettings[] GitHubProviders { get; set; }
public ActionSettings[] Actions { get; set; }

private static Settings CreateDefault()
{
var userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
var repositoryFolder = Path.Combine(userProfile, "./Source/Repos");
var vsCodePath = Path.Combine(userProfile, "./AppData/Local/Programs/Microsoft VS Code/Code.exe");
const string gitBashPath = "C:/Program Files/Git/git-bash.exe";
var repositoryFolder = Path.Combine(userProfile, "Source\\Repos");

var actions = DefaultActions.Get();

var azureProviders = Array.Empty<AzureProviderSettings>();
var gitHubProviders = Array.Empty<GitHubProviderSettings>();

var settings = new Settings
{
RepositoryFolder = repositoryFolder,
VsCodePath = vsCodePath,
GitBashPath = gitBashPath,
AzureProviders = azureProviders,
GitHubProviders = gitHubProviders,
Actions = actions,
};

return settings;
Expand All @@ -50,6 +51,7 @@ public static Settings Load()

settings.AzureProviders ??= Array.Empty<AzureProviderSettings>();
settings.GitHubProviders ??= Array.Empty<GitHubProviderSettings>();
settings.Actions ??= Array.Empty<ActionSettings>();

return settings;
}
Expand All @@ -58,10 +60,11 @@ public void Save()
{
var options = new JsonSerializerOptions
{
IgnoreNullValues = true,
WriteIndented = true,
};

var json = JsonSerializer.ToString(this, options);
var json = JsonSerializer.ToString( this, options);
File.WriteAllText("config.json", json);
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/GitMan/Context.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GitMan.Actions;
using GitMan.Providers;
using GitMan.Config;
using GitMan.Providers;
using System;
using System.Collections.Generic;
using System.Drawing;
Expand All @@ -16,7 +16,6 @@ public class Context : ApplicationContext
private readonly NotifyIcon _icon;
private readonly Main _main;
private readonly Settings _settings;
private readonly RepositoryAction[] _repositoryActions;
private readonly RemoteProvider[] _remoteProviders;
private readonly RepositoryDirectory _repositoryDirectory;

Expand All @@ -28,8 +27,6 @@ public Context()

_settings = Settings.Load();

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

var azureProviders = _settings.AzureProviders
.Select(provider => (RemoteProvider)new AzureProvider(provider));
var gitHubProviders = _settings.GitHubProviders
Expand Down Expand Up @@ -137,8 +134,9 @@ private MenuItem MakeMenuItem(Repository repository)
var directoryInfo = new DirectoryInfo(repository.FullName);

var subItems = new List<MenuItem>();
var actions = _settings.Actions.Select(settings => new RepositoryAction(settings));

foreach (var repositoryAction in _repositoryActions)
foreach (var repositoryAction in actions)
{
subItems.AddRange(repositoryAction.GetMenuItems(directoryInfo));
}
Expand Down

0 comments on commit 6927977

Please sign in to comment.