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

GH-2127 - allow json and buildserver output at the same time #2128

Merged
merged 3 commits into from
Feb 26, 2020
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
2 changes: 1 addition & 1 deletion src/GitVersionCore/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Arguments
public bool NoCache;
public bool NoNormalize;

public OutputType Output = OutputType.Json;
public ISet<OutputType> Output = new HashSet<OutputType>();
public Verbosity Verbosity = Verbosity.Normal;

public bool UpdateAssemblyInfo;
Expand Down
34 changes: 30 additions & 4 deletions src/GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,54 @@ public void UnknownOutputShouldThrow()
public void OutputDefaultsToJson()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath");
arguments.Output.ShouldBe(OutputType.Json);
arguments.Output.ShouldContain(OutputType.Json);
}

[Test]
public void OutputJsonCanBeParsed()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output json");
arguments.Output.ShouldBe(OutputType.Json);
arguments.Output.ShouldContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.BuildServer);
}

[Test]
public void MultipleOutputJsonCanBeParsed()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output json -output json");
arguments.Output.ShouldContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.BuildServer);
}

[Test]
public void OutputBuildserverCanBeParsed()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver");
arguments.Output.ShouldBe(OutputType.BuildServer);
arguments.Output.ShouldContain(OutputType.BuildServer);
arguments.Output.ShouldNotContain(OutputType.Json);
}

[Test]
public void MultipleOutputBuildserverCanBeParsed()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver -output buildserver");
arguments.Output.ShouldContain(OutputType.BuildServer);
arguments.Output.ShouldNotContain(OutputType.Json);
}

[Test]
public void OutputBuildserverAndJsonCanBeParsed()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver -output json");
arguments.Output.ShouldContain(OutputType.BuildServer);
arguments.Output.ShouldContain(OutputType.Json);
}

[Test]
public void MultipleArgsAndFlag()
{
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver -updateAssemblyInfo");
arguments.Output.ShouldBe(OutputType.BuildServer);
arguments.Output.ShouldContain(OutputType.BuildServer);
}

[Test]
Expand Down
20 changes: 16 additions & 4 deletions src/GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ public Arguments ParseArguments(string[] commandLineArguments)
{
if (commandLineArguments.Length == 0)
{
return new Arguments
var args = new Arguments
{
TargetPath = System.Environment.CurrentDirectory,
};

args.Output.Add(OutputType.Json);
return args;
}

var firstArgument = commandLineArguments.First();
Expand Down Expand Up @@ -264,12 +267,16 @@ public Arguments ParseArguments(string[] commandLineArguments)

if (name.IsSwitch("output"))
{
if (!Enum.TryParse(value, true, out OutputType outputType))
foreach (var v in values)
{
throw new WarningException($"Value '{value}' cannot be parsed as output type, please use 'json' or 'buildserver'");
if (!Enum.TryParse(v, true, out OutputType outputType))
{
throw new WarningException($"Value '{value}' cannot be parsed as output type, please use 'json' or 'buildserver'");
}

arguments.Output.Add(outputType);
}

arguments.Output = outputType;
continue;
}

Expand Down Expand Up @@ -396,6 +403,11 @@ public Arguments ParseArguments(string[] commandLineArguments)
throw new WarningException(couldNotParseMessage);
}

if (arguments.Output.Count == 0)
{
arguments.Output.Add(OutputType.Json);
}

if (arguments.TargetPath == null)
{
// If the first argument is a switch, it should already have been consumed in the above loop,
Expand Down
46 changes: 21 additions & 25 deletions src/GitVersionExe/ExecCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,28 @@ public void Execute()

var arguments = options.Value;

switch (arguments.Output)
if (arguments.Output.Contains(OutputType.BuildServer))
{
case OutputType.BuildServer:
var buildServer = buildServerResolver.Resolve();
buildServer?.WriteIntegration(Console.WriteLine, variables);

break;
case OutputType.Json:
switch (arguments.ShowVariable)
{
case null:
Console.WriteLine(variables.ToString());
break;

default:
if (!variables.TryGetValue(arguments.ShowVariable, out var part))
{
throw new WarningException($"'{arguments.ShowVariable}' variable does not exist");
}
Console.WriteLine(part);
break;
}

break;
default:
throw new ArgumentOutOfRangeException();
var buildServer = buildServerResolver.Resolve();
buildServer?.WriteIntegration(Console.WriteLine, variables);
}
if (arguments.Output.Contains(OutputType.Json))
{
switch (arguments.ShowVariable)
{
case null:
Console.WriteLine(variables.ToString());
break;

default:
if (!variables.TryGetValue(arguments.ShowVariable, out var part))
{
throw new WarningException($"'{arguments.ShowVariable}' variable does not exist");
}

Console.WriteLine(part);
break;
}
}

if (arguments.UpdateWixVersionFile)
Expand Down
6 changes: 3 additions & 3 deletions src/GitVersionExe/GitVersionExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ private int VerifyArgumentsAndRun(Arguments arguments)
if (arguments.Diag)
{
arguments.NoCache = true;
arguments.Output = OutputType.BuildServer;
arguments.Output.Add(OutputType.BuildServer);
}

if (!string.IsNullOrEmpty(arguments.Proj) || !string.IsNullOrEmpty(arguments.Exec))
{
arguments.Output = OutputType.BuildServer;
arguments.Output.Add(OutputType.BuildServer);
}

var buildServer = buildServerResolver.Resolve();
Expand Down Expand Up @@ -154,7 +154,7 @@ private void VerifyConfiguration()

private static void ConfigureLogging(Arguments arguments, ILog log)
{
if (arguments.Output == OutputType.BuildServer || arguments.LogFilePath == "console" || arguments.Init)
if (arguments.Output.Contains(OutputType.BuildServer) || arguments.LogFilePath == "console" || arguments.Init)
{
log.AddLogAppender(new ConsoleAppender());
}
Expand Down