Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More idiomatic CliWrap usage #546

Merged
merged 1 commit into from Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions MinVerTests.Infra/CommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CliWrap;
using CliWrap.Buffered;
using CliWrap.Builders;
using CliWrap.Exceptions;

namespace MinVerTests.Infra
Expand Down Expand Up @@ -64,5 +66,25 @@ public static async Task<BufferedCommandResult> ExecuteBufferedLoggedAsync(this

return result.ExitCode == 0 || validation == CommandResultValidation.None ? result : throw new CommandExecutionException(command, result.ExitCode, log);
}

public static EnvironmentVariablesBuilder SetFrom(this EnvironmentVariablesBuilder env, IEnumerable<KeyValuePair<string, string>> source)
{
foreach (var (key, value) in source)
{
env.Set(key, value);
}

return env;
}

public static ArgumentsBuilder AddIf(this ArgumentsBuilder args, string value, bool condition)
{
if (condition)
{
args.Add(value);
}

return args;
}
}
}
24 changes: 17 additions & 7 deletions MinVerTests.Infra/Git.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CliWrap;
Expand Down Expand Up @@ -32,8 +31,11 @@ public static async Task Init(string path)
}

public static async Task<string> GetGraph(string path) =>
(await Cli.Wrap("git").WithArguments("log --graph --pretty=format:'%d'").WithWorkingDirectory(path).ExecuteBufferedAsync())
.StandardOutput;
await Cli.Wrap("git")
.WithArguments("log --graph --pretty=format:'%d'")
.WithWorkingDirectory(path)
.ExecuteBufferedAsync()
.Select(r => r.StandardOutput);

public static Task Tag(string path, string tag) =>
Cli.Wrap("git").WithArguments($"tag {tag}").WithWorkingDirectory(path).ExecuteAsync();
Expand All @@ -44,10 +46,18 @@ public static Task Tag(string path, string tagName, string sha) =>
public static Task AnnotatedTag(string path, string tag, string message) =>
Cli.Wrap("git").WithArguments($"tag {tag} -a -m '{message}'").WithWorkingDirectory(path).ExecuteAsync();

public static async Task<IEnumerable<string>> GetCommitShas(string path) =>
(await Cli.Wrap("git").WithArguments("log --pretty=format:\"%H\"").WithWorkingDirectory(path).ExecuteBufferedAsync())
.StandardOutput
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
public static async Task<IEnumerable<string>> GetCommitShas(string path)
{
var stdOutLines = new List<string>();

await Cli.Wrap("git")
.WithArguments("log --pretty=format:\"%H\"")
.WithWorkingDirectory(path)
.WithStandardOutputPipe(PipeTarget.ToDelegate(stdOutLines.Add))
.ExecuteAsync();

return stdOutLines;
}

public static Task Checkout(string path, string sha) =>
Cli.Wrap("git").WithArguments($"checkout {sha}").WithWorkingDirectory(path).ExecuteAsync();
Expand Down
31 changes: 15 additions & 16 deletions MinVerTests.Infra/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task CreateSolution(string path, string[] projectNames, stri
CreateGlobalJsonIfRequired(path);

_ = await Cli.Wrap("dotnet").WithArguments($"new sln --name test --output {path}")
.WithEnvironmentVariables(builder => builder.SetSdk().Build())
.WithEnvironmentVariables(builder => builder.SetSdk())
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);

string previousProjectName = null;
Expand All @@ -47,12 +47,12 @@ public static async Task CreateSolution(string path, string[] projectNames, stri
var previousProjectFileName = Path.Combine(path, previousProjectName, $"{previousProjectName}.csproj");

_ = await Cli.Wrap("dotnet").WithArguments($"add {projectFileName} reference {previousProjectFileName}")
.WithEnvironmentVariables(builder => builder.SetSdk().Build())
.WithEnvironmentVariables(builder => builder.SetSdk())
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);
}

_ = await Cli.Wrap("dotnet").WithArguments($"sln add {projectName}")
.WithEnvironmentVariables(builder => builder.SetSdk().Build())
.WithEnvironmentVariables(builder => builder.SetSdk())
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);

previousProjectName = projectName;
Expand All @@ -75,15 +75,15 @@ private static async Task CreateProject(string path, string configuration, strin
var minVerPackageVersion = Path.GetFileNameWithoutExtension(Directory.EnumerateFiles(source, "*.nupkg").First()).Split("MinVer.", 2)[1];

_ = await Cli.Wrap("dotnet").WithArguments($"new classlib --name {name} --output {path}")
.WithEnvironmentVariables(builder => builder.SetSdk().Build())
.WithEnvironmentVariables(builder => builder.SetSdk())
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);

_ = await Cli.Wrap("dotnet").WithArguments($"add package MinVer --source {source} --version {minVerPackageVersion} --package-directory packages")
.WithEnvironmentVariables(builder => builder.SetSdk().Build())
.WithEnvironmentVariables(builder => builder.SetSdk())
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);

_ = await Cli.Wrap("dotnet").WithArguments($"restore --source {source} --packages packages")
.WithEnvironmentVariables(builder => builder.SetSdk().Build())
.WithEnvironmentVariables(builder => builder.SetSdk())
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);
}

Expand Down Expand Up @@ -118,16 +118,15 @@ private static void CreateGlobalJsonIfRequired(string path)
_ = environmentVariables.TryAdd("NoPackageAnalysis", "true");

var result = await Cli.Wrap("dotnet")
.WithArguments($"build --no-restore{((Version?.StartsWith("2.", StringComparison.Ordinal) ?? false) ? "" : " --nologo")}")
.WithEnvironmentVariables(builder =>
{
foreach (var pair in environmentVariables)
{
_ = builder.Set(pair.Key, pair.Value);
}

_ = builder.SetSdk().Build();
})
.WithArguments(args => args
.Add("build")
.Add("--no-restore")
.AddIf("--nologo", !(Version?.StartsWith("2.", StringComparison.Ordinal) ?? false))
)
.WithEnvironmentVariables(env => env
.SetFrom(environmentVariables)
.SetSdk()
)
.WithWorkingDirectory(path).ExecuteBufferedLoggedAsync().ConfigureAwait(false);

var matcher = new Matcher().AddInclude("**/bin/Debug/*.nupkg");
Expand Down