From 0ede69ccc4d01c5a0677bbb02ad1b87465986a9b Mon Sep 17 00:00:00 2001 From: Matthias Langhard Date: Tue, 2 Nov 2021 12:21:27 +0100 Subject: [PATCH] chore: improvements --- README.md | 7 +++- src/Cli/AppRunner.cs | 22 +++++++++---- src/Core/Commands/PushCommitsToRemote.cs | 9 ++--- src/Core/Interfaces/IGitRepoReadService.cs | 4 ++- src/Core/Interfaces/IGitRepoWriteService.cs | 4 ++- src/Core/Queries/GetRepoBasePath.cs | 33 +++++++++++++++++++ src/Infrastructure/Infrastructure.csproj | 1 + .../Services/GitRepoReadService.cs | 12 +++++++ .../Services/GitRepoWriteService.cs | 11 +++++-- 9 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 src/Core/Queries/GetRepoBasePath.cs diff --git a/README.md b/README.md index 3b91cf9..be89c92 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # update-tag -Updates the tag of a repo to the next chosen version according the semver symantic. \ No newline at end of file +Updates the tag of a repo to the next chosen version according the semver symantic. + +## Install +``` +dotnet tool install --global Novaloop.UpdateTag +``` \ No newline at end of file diff --git a/src/Cli/AppRunner.cs b/src/Cli/AppRunner.cs index 72500d6..5abf7a6 100644 --- a/src/Cli/AppRunner.cs +++ b/src/Cli/AppRunner.cs @@ -19,13 +19,23 @@ namespace Cli _mediator = mediator; } - public async Task Run(string repoPath) + public async Task Run(string workingDir) { - // Check if git dir + var repoBasePath = ""; + try + { + repoBasePath = await _mediator.Send(new GetRepoBasePath.Query(workingDir)); + } + catch (CliWrap.Exceptions.CommandExecutionException) + { + AnsiConsole.Markup("[red]Error:[/] Unable to extract Versions. Are we running inside a git repository?\n\n"); + Environment.Exit(1); + } + var services = new List(); try { - services = await _mediator.Send(new GetServicesFromGitRepo.Query(repoPath)); + services = await _mediator.Send(new GetServicesFromGitRepo.Query(repoBasePath)); } catch (LibGit2Sharp.RepositoryNotFoundException) { @@ -45,7 +55,7 @@ namespace Cli } - var versionInfo = await _mediator.Send(new GetVersionInformationFromRepo.Query(repoPath, chosenService)); + var versionInfo = await _mediator.Send(new GetVersionInformationFromRepo.Query(repoBasePath, chosenService)); Selection selection; if (versionInfo != null) { @@ -82,7 +92,7 @@ namespace Cli try { - await _mediator.Send(new AddTagToGitRepo.Command(repoPath, selection.Version.ToString())); + await _mediator.Send(new AddTagToGitRepo.Command(workingDir, selection.Version.ToString())); } catch (Exception ex) { @@ -93,7 +103,7 @@ namespace Cli try { - await _mediator.Send(new PushCommitsToRemote.Command(repoPath)); + await _mediator.Send(new PushCommitsToRemote.Command(workingDir)); } catch (Exception ex) { diff --git a/src/Core/Commands/PushCommitsToRemote.cs b/src/Core/Commands/PushCommitsToRemote.cs index c21890d..80daed5 100644 --- a/src/Core/Commands/PushCommitsToRemote.cs +++ b/src/Core/Commands/PushCommitsToRemote.cs @@ -1,9 +1,11 @@ +using System.Threading; +using System.Threading.Tasks; using Application.Interfaces; using MediatR; namespace Application.Commands { - public class PushCommitsToRemote : RequestHandler + public class PushCommitsToRemote : AsyncRequestHandler { private readonly IGitRepoWriteService _gitRepoWriteService; @@ -22,10 +24,9 @@ namespace Application.Commands } } - - protected override void Handle(Command request) + protected override async Task Handle(Command request, CancellationToken cancellationToken) { - _gitRepoWriteService.Push(request.RepoPath); + await _gitRepoWriteService.Push(request.RepoPath); } } } \ No newline at end of file diff --git a/src/Core/Interfaces/IGitRepoReadService.cs b/src/Core/Interfaces/IGitRepoReadService.cs index b02dc8e..b1db0ba 100644 --- a/src/Core/Interfaces/IGitRepoReadService.cs +++ b/src/Core/Interfaces/IGitRepoReadService.cs @@ -1,10 +1,12 @@ using System.Collections.Generic; +using System.Threading.Tasks; using Application.Models; namespace Application.Interfaces { public interface IGitRepoReadService { - public IEnumerable GetAllVersions(string repoPath); + IEnumerable GetAllVersions(string repoPath); + Task GetRepoBasePath(string workingDir); } } \ No newline at end of file diff --git a/src/Core/Interfaces/IGitRepoWriteService.cs b/src/Core/Interfaces/IGitRepoWriteService.cs index 238e1f0..4fd4722 100644 --- a/src/Core/Interfaces/IGitRepoWriteService.cs +++ b/src/Core/Interfaces/IGitRepoWriteService.cs @@ -1,8 +1,10 @@ +using System.Threading.Tasks; + namespace Application.Interfaces { public interface IGitRepoWriteService { void AddTag(string repoPath, string tag); - void Push(string repoPath); + Task Push(string repoPath); } } \ No newline at end of file diff --git a/src/Core/Queries/GetRepoBasePath.cs b/src/Core/Queries/GetRepoBasePath.cs new file mode 100644 index 0000000..014620a --- /dev/null +++ b/src/Core/Queries/GetRepoBasePath.cs @@ -0,0 +1,33 @@ +using System.Threading; +using System.Threading.Tasks; +using Application.Interfaces; +using MediatR; + +namespace Application.Queries +{ + public class GetRepoBasePath : IRequestHandler + { + public class Query : IRequest + { + public Query(string workingDir) + { + WorkingDir = workingDir; + } + + public string WorkingDir { get; } + } + + private readonly IGitRepoReadService _gitRepoReadService; + + public GetRepoBasePath(IGitRepoReadService gitRepoReadService) + { + _gitRepoReadService = gitRepoReadService; + } + + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + return await _gitRepoReadService.GetRepoBasePath(request.WorkingDir); + } + } +} \ No newline at end of file diff --git a/src/Infrastructure/Infrastructure.csproj b/src/Infrastructure/Infrastructure.csproj index 6435cd8..7cffa82 100644 --- a/src/Infrastructure/Infrastructure.csproj +++ b/src/Infrastructure/Infrastructure.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Infrastructure/Services/GitRepoReadService.cs b/src/Infrastructure/Services/GitRepoReadService.cs index 2152576..6f8b99b 100644 --- a/src/Infrastructure/Services/GitRepoReadService.cs +++ b/src/Infrastructure/Services/GitRepoReadService.cs @@ -1,5 +1,8 @@ using System.Collections.Generic; +using System.Threading.Tasks; using Application.Interfaces; +using CliWrap; +using CliWrap.Buffered; using LibGit2Sharp; using Semver; using Version = Application.Models.Version; @@ -8,6 +11,15 @@ namespace Infrastructure.Services { public class GitRepoReadService : IGitRepoReadService { + public async Task GetRepoBasePath(string workingDir) + { + var result = await Cli.Wrap("git") + .WithArguments("rev-parse --show-toplevel") + .WithWorkingDirectory(workingDir) + .ExecuteBufferedAsync(); + return result.StandardOutput.Trim(); + } + public IEnumerable GetAllVersions(string repoPath) { using var repo = new Repository(repoPath); diff --git a/src/Infrastructure/Services/GitRepoWriteService.cs b/src/Infrastructure/Services/GitRepoWriteService.cs index 60012f8..dc7a1e8 100644 --- a/src/Infrastructure/Services/GitRepoWriteService.cs +++ b/src/Infrastructure/Services/GitRepoWriteService.cs @@ -1,4 +1,7 @@ +using System.Threading.Tasks; using Application.Interfaces; +using CliWrap; +using CliWrap.Buffered; using LibGit2Sharp; namespace Infrastructure.Services @@ -11,10 +14,12 @@ namespace Infrastructure.Services repo.ApplyTag(tag); } - public void Push(string repoPath) + public async Task Push(string repoPath) { - using var repo = new Repository(repoPath); - repo.Network.Push(repo.Head); + await Cli.Wrap("git") + .WithArguments("push --tags") + .WithWorkingDirectory(repoPath) + .ExecuteBufferedAsync(); } } } \ No newline at end of file