Skip to content

Commit

Permalink
Refactory remote repository menu item creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Scepheo committed Jun 5, 2019
1 parent 9087910 commit 9bdaf25
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 68 deletions.
8 changes: 4 additions & 4 deletions src/GitMan/Clients/AzureClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
14 changes: 0 additions & 14 deletions src/GitMan/Clients/AzureRepository.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/GitMan/Clients/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
16 changes: 0 additions & 16 deletions src/GitMan/Clients/GitHubRepository.cs

This file was deleted.

20 changes: 20 additions & 0 deletions src/GitMan/Clients/RemoteRepository.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
36 changes: 6 additions & 30 deletions src/GitMan/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -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<string, string> 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
{
Expand Down

0 comments on commit 9bdaf25

Please sign in to comment.