From 9bdaf25509f4dc5daefa10f4573d5c37a49ec93f Mon Sep 17 00:00:00 2001 From: Scepheo Date: Wed, 5 Jun 2019 16:31:40 +0200 Subject: [PATCH] Refactory remote repository menu item creation --- src/GitMan/Clients/AzureClient.cs | 8 +++--- src/GitMan/Clients/AzureRepository.cs | 14 ---------- src/GitMan/Clients/GitHubClient.cs | 8 +++--- src/GitMan/Clients/GitHubRepository.cs | 16 ------------ src/GitMan/Clients/RemoteRepository.cs | 20 ++++++++++++++ src/GitMan/Context.cs | 36 +++++--------------------- 6 files changed, 34 insertions(+), 68 deletions(-) delete mode 100644 src/GitMan/Clients/AzureRepository.cs delete mode 100644 src/GitMan/Clients/GitHubRepository.cs create mode 100644 src/GitMan/Clients/RemoteRepository.cs diff --git a/src/GitMan/Clients/AzureClient.cs b/src/GitMan/Clients/AzureClient.cs index 0f96295..8e1f9d9 100644 --- a/src/GitMan/Clients/AzureClient.cs +++ b/src/GitMan/Clients/AzureClient.cs @@ -94,20 +94,20 @@ private JsonDocument GetResponse(string path) } } - public AzureRepository[] GetRepositories() + public RemoteRepository[] GetRepositories() { var document = GetResponse("git/repositories"); var repositories = document.RootElement.GetProperty("value"); var count = repositories.GetArrayLength(); - var azureRepos = new AzureRepository[count]; + var azureRepos = new RemoteRepository[count]; var index = 0; foreach (var repository in repositories.EnumerateArray()) { var name = repository.GetProperty("name").GetString(); - var remoteUrl = repository.GetProperty("remoteUrl").GetString(); - var azureRepo = new AzureRepository(name, remoteUrl); + var cloneUrl = repository.GetProperty("remoteUrl").GetString(); + var azureRepo = new RemoteRepository(name, cloneUrl); azureRepos[index] = azureRepo; index++; } diff --git a/src/GitMan/Clients/AzureRepository.cs b/src/GitMan/Clients/AzureRepository.cs deleted file mode 100644 index 14220cb..0000000 --- a/src/GitMan/Clients/AzureRepository.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace GitMan.Clients -{ - public class AzureRepository - { - public string Name { get; } - public string RemoteUrl { get; } - - public AzureRepository(string name, string remoteUrl) - { - Name = name; - RemoteUrl = remoteUrl; - } - } -} diff --git a/src/GitMan/Clients/GitHubClient.cs b/src/GitMan/Clients/GitHubClient.cs index 832c666..a95d3c5 100644 --- a/src/GitMan/Clients/GitHubClient.cs +++ b/src/GitMan/Clients/GitHubClient.cs @@ -66,21 +66,21 @@ private JsonDocument GetResponse(string path) } } - public GitHubRepository[] GetRepositories() + public RemoteRepository[] GetRepositories() { var document = GetResponse("user/repos"); var repositories = document.RootElement; var count = repositories.GetArrayLength(); - var GitHubRepos = new GitHubRepository[count]; + var GitHubRepos = new RemoteRepository[count]; var index = 0; foreach (var repository in repositories.EnumerateArray()) { var name = repository.GetProperty("name").GetString(); var fullName = repository.GetProperty("full_name").GetString(); - var remoteUrl = repository.GetProperty("clone_url").GetString(); - var GitHubRepo = new GitHubRepository(name, fullName, remoteUrl); + var cloneUrl = repository.GetProperty("clone_url").GetString(); + var GitHubRepo = new RemoteRepository(name, fullName, cloneUrl); GitHubRepos[index] = GitHubRepo; index++; } diff --git a/src/GitMan/Clients/GitHubRepository.cs b/src/GitMan/Clients/GitHubRepository.cs deleted file mode 100644 index 9dfe551..0000000 --- a/src/GitMan/Clients/GitHubRepository.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace GitMan.Clients -{ - public class GitHubRepository - { - public string Name { get; } - public string FullName { get; } - public string RemoteUrl { get; } - - public GitHubRepository(string name, string fullName, string remoteUrl) - { - Name = name; - FullName = fullName; - RemoteUrl = remoteUrl; - } - } -} diff --git a/src/GitMan/Clients/RemoteRepository.cs b/src/GitMan/Clients/RemoteRepository.cs new file mode 100644 index 0000000..ebf3b97 --- /dev/null +++ b/src/GitMan/Clients/RemoteRepository.cs @@ -0,0 +1,20 @@ +namespace GitMan.Clients +{ + internal class RemoteRepository + { + public string Name { get; set; } + public string DisplayName { get; set; } + public string CloneUrl { get; set; } + + public RemoteRepository(string name, string cloneUrl) + : this(name, name, cloneUrl) + { } + + public RemoteRepository(string name, string displayName, string cloneUrl) + { + Name = name; + DisplayName = displayName; + CloneUrl = cloneUrl; + } + } +} diff --git a/src/GitMan/Context.cs b/src/GitMan/Context.cs index c533686..813fb0f 100644 --- a/src/GitMan/Context.cs +++ b/src/GitMan/Context.cs @@ -166,7 +166,7 @@ void onPopup(object sender, EventArgs eventArgs) .OrderBy(repository => repository.Name); var menuItems = repositories - .Select(repository => MakeAzureRepositoryItem(repository, provider)) + .Select(repository => MakeRemoteRepositoryItem(repository, provider.DefaultConfig)) .ToArray(); menuItem.MenuItems.Clear(); @@ -190,30 +190,6 @@ void onSelect(object sender, EventArgs eventArgs) { } return menuItem; } - private MenuItem MakeAzureRepositoryItem(AzureRepository repository, AzureProvider provider) - { - var name = repository.Name; - - void onClick(object sender, EventArgs eventArgs) - { - var configs = provider.DefaultConfig.Select(pair => $"--config \"{pair.Key}={pair.Value}\""); - var argument = $"clone \"{repository.RemoteUrl}\" {string.Join(' ', configs)}"; - - var startInfo = new ProcessStartInfo - { - FileName = "git", - Arguments = argument, - UseShellExecute = true, - WorkingDirectory = _settings.RepositoryFolder, - }; - - Process.Start(startInfo); - } - - var menuItem = new MenuItem(name, onClick); - return menuItem; - } - private MenuItem MakeGitHubProviderItem( RepositoryList existingRepositories, GitHubProvider provider) @@ -248,7 +224,7 @@ void onPopup(object sender, EventArgs eventArgs) .OrderBy(repository => repository.Name); var menuItems = repositories - .Select(repository => MakeGitHubRepositoryItem(repository, provider)) + .Select(repository => MakeRemoteRepositoryItem(repository, provider.DefaultConfig)) .ToArray(); menuItem.MenuItems.Clear(); @@ -272,14 +248,14 @@ void onSelect(object sender, EventArgs eventArgs) { } return menuItem; } - private MenuItem MakeGitHubRepositoryItem(GitHubRepository repository, GitHubProvider provider) + private MenuItem MakeRemoteRepositoryItem(RemoteRepository repository, Dictionary defaultConfig) { - var name = repository.Name; + var name = repository.DisplayName; void onClick(object sender, EventArgs eventArgs) { - var configs = provider.DefaultConfig.Select(pair => $"--config \"{pair.Key}={pair.Value}\""); - var argument = $"clone \"{repository.RemoteUrl}\" {string.Join(' ', configs)}"; + var configs = defaultConfig.Select(pair => $"--config \"{pair.Key}={pair.Value}\""); + var argument = $"clone \"{repository.CloneUrl}\" {string.Join(' ', configs)}"; var startInfo = new ProcessStartInfo {