-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor logic for actions you can take on repositories
- Loading branch information
Showing
3 changed files
with
177 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters