Skip to content
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/Core/GitVersionTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void OutputVariables(VersionVariables variables)

using (outputGenerator)
{
outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory));
outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore/Model/GitVersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public GitVersionOptions()

public string LogFilePath;
public string ShowVariable;
public string OutputFile;
public ISet<OutputType> Output = new HashSet<OutputType>();
public Verbosity Verbosity = Verbosity.Normal;

Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore/Model/OutputType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace GitVersion.Model
public enum OutputType
{
BuildServer,

Json
Json,
File
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ namespace GitVersion.VersionConverters.OutputGenerator
{
public readonly struct OutputContext : IConverterContext
{
public OutputContext(string workingDirectory)
public OutputContext(string workingDirectory, string outputFile)
{
WorkingDirectory = workingDirectory;
OutputFile = outputFile;
}

public string WorkingDirectory { get; }
public string OutputFile { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public interface IOutputGenerator : IVersionConverter<OutputContext>
public class OutputGenerator : IOutputGenerator
{
private readonly IConsole console;
private readonly IFileSystem fileSystem;
private readonly IOptions<GitVersionOptions> options;
private readonly ICurrentBuildAgent buildAgent;

public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IOptions<GitVersionOptions> options)
public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IFileSystem fileSystem, IOptions<GitVersionOptions> options)
{
this.console = console ?? throw new ArgumentNullException(nameof(console));
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.buildAgent = buildAgent;
}
Expand All @@ -30,6 +32,10 @@ public void Execute(VersionVariables variables, OutputContext context)
{
buildAgent?.WriteIntegration(console.WriteLine, variables);
}
if (gitVersionOptions.Output.Contains(OutputType.File))
{
fileSystem.WriteAllText(context.OutputFile, variables.ToString());
}
if (gitVersionOptions.Output.Contains(OutputType.Json))
{
switch (gitVersionOptions.ShowVariable)
Expand Down
46 changes: 45 additions & 1 deletion src/GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,16 @@ public void UsernameAndPasswordCanBeParsed()
public void UnknownOutputShouldThrow()
{
var exception = Assert.Throws<WarningException>(() => argumentParser.ParseArguments("targetDirectoryPath -output invalid_value"));
exception.Message.ShouldBe("Value 'invalid_value' cannot be parsed as output type, please use 'json' or 'buildserver'");
exception.Message.ShouldBe("Value 'invalid_value' cannot be parsed as output type, please use 'json', 'file' or 'buildserver'");
}

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

[Test]
Expand All @@ -171,6 +173,7 @@ public void OutputJsonCanBeParsed()
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output json");
arguments.Output.ShouldContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.BuildServer);
arguments.Output.ShouldNotContain(OutputType.File);
}

[Test]
Expand All @@ -179,6 +182,7 @@ public void MultipleOutputJsonCanBeParsed()
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output json -output json");
arguments.Output.ShouldContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.BuildServer);
arguments.Output.ShouldNotContain(OutputType.File);
}

[Test]
Expand All @@ -187,6 +191,7 @@ public void OutputBuildserverCanBeParsed()
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver");
arguments.Output.ShouldContain(OutputType.BuildServer);
arguments.Output.ShouldNotContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.File);
}

[Test]
Expand All @@ -195,6 +200,25 @@ public void MultipleOutputBuildserverCanBeParsed()
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver -output buildserver");
arguments.Output.ShouldContain(OutputType.BuildServer);
arguments.Output.ShouldNotContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.File);
}

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

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

[Test]
Expand All @@ -203,6 +227,16 @@ public void OutputBuildserverAndJsonCanBeParsed()
var arguments = argumentParser.ParseArguments("targetDirectoryPath -output buildserver -output json");
arguments.Output.ShouldContain(OutputType.BuildServer);
arguments.Output.ShouldContain(OutputType.Json);
arguments.Output.ShouldNotContain(OutputType.File);
}

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

[Test]
Expand All @@ -212,6 +246,16 @@ public void MultipleArgsAndFlag()
arguments.Output.ShouldContain(OutputType.BuildServer);
}

[TestCase("-output file", "GitVersion.json")]
[TestCase("-output file -outputfile version.json", "version.json")]
public void OutputFileArgumentCanBeParsed(string args, string outputFile)
{
var arguments = argumentParser.ParseArguments(args);

arguments.Output.ShouldContain(OutputType.File);
arguments.OutputFile.ShouldBe(outputFile);
}

[Test]
public void UrlAndBranchNameCanBeParsed()
{
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionExe.Tests/HelpWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void AllArgsAreInHelp()
{ nameof(Arguments.Init), "init" },
{ nameof(Arguments.TargetBranch), "/b" },
{ nameof(Arguments.LogFilePath) , "/l" },
{ nameof(Arguments.OutputFile) , "/outputfile" },
{ nameof(Arguments.DynamicRepositoryClonePath), "/dynamicRepoLocation" },
{ nameof(Arguments.IsHelp), "/?" },
{ nameof(Arguments.IsVersion), "/version" },
Expand Down
29 changes: 29 additions & 0 deletions src/GitVersionExe.Tests/JsonOutputOnBuildServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.Generic;
using System.IO;
using GitTools.Testing;
using GitVersion.BuildAgents;
using GitVersion.OutputVariables;
using Newtonsoft.Json;
using NUnit.Framework;
using Shouldly;

Expand Down Expand Up @@ -41,5 +44,31 @@ public void BeingOnBuildServerWithOutputJsonDoesNotFail()
result.OutputVariables.ShouldNotBeNull();
result.OutputVariables.FullSemVer.ShouldBeEquivalentTo(version);
}

[TestCase("", "GitVersion.json")]
[TestCase("version.json", "version.json")]
public void BeingOnBuildServerWithOutputJsonAndOutputFileDoesNotFail(string outputFile, string fileName)
{
using var fixture = new RemoteRepositoryFixture();
fixture.Repository.MakeATaggedCommit("1.2.3");
fixture.Repository.MakeACommit();

var env = new KeyValuePair<string, string>(TeamCity.EnvironmentVariableName, "8.0.0");

var result = GitVersionHelper.ExecuteIn(fixture.LocalRepositoryFixture.RepositoryPath, arguments: $" /output json /output buildserver /output file /outputfile {outputFile}", environments: env);

result.ExitCode.ShouldBe(0);
const string version = "0.1.0+4";
result.Output.ShouldContain($"##teamcity[buildNumber '{version}']");
result.OutputVariables.ShouldNotBeNull();
result.OutputVariables.FullSemVer.ShouldBeEquivalentTo(version);

var filePath = Path.Combine(fixture.LocalRepositoryFixture.RepositoryPath, fileName);
var json = File.ReadAllText(filePath);

var outputVariables = VersionVariables.FromDictionary(JsonConvert.DeserializeObject<Dictionary<string, string>>(json));
outputVariables.ShouldNotBeNull();
outputVariables.FullSemVer.ShouldBeEquivalentTo(version);
}
}
}
15 changes: 14 additions & 1 deletion src/GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ArgumentParser : IArgumentParser
private readonly ICurrentBuildAgent buildAgent;
private readonly IConsole console;
private readonly IGlobbingResolver globbingResolver;
private const string defaultOutputFileName = "GitVersion.json";

public ArgumentParser(IEnvironment environment, ICurrentBuildAgent buildAgent, IConsole console, IGlobbingResolver globbingResolver)
{
Expand Down Expand Up @@ -91,6 +92,11 @@ public Arguments ParseArguments(string[] commandLineArguments)
arguments.Output.Add(OutputType.Json);
}

if (arguments.Output.Contains(OutputType.File) && arguments.OutputFile == null)
{
arguments.OutputFile = defaultOutputFileName;
}

// If the first argument is a switch, it should already have been consumed in the above loop,
// or else a WarningException should have been thrown and we wouldn't end up here.
arguments.TargetPath ??= firstArgumentIsSwitch
Expand Down Expand Up @@ -223,6 +229,13 @@ private static bool ParseSwitches(Arguments arguments, string name, string[] val
return true;
}

if (name.IsSwitch("outputfile"))
{
EnsureArgumentValueCount(values);
arguments.OutputFile = value;
return true;
}

if (name.IsSwitch("nofetch"))
{
arguments.NoFetch = true;
Expand Down Expand Up @@ -417,7 +430,7 @@ private static void ParseOutput(Arguments arguments, string[] values)
{
if (!Enum.TryParse(v, true, out OutputType outputType))
{
throw new WarningException($"Value '{v}' cannot be parsed as output type, please use 'json' or 'buildserver'");
throw new WarningException($"Value '{v}' cannot be parsed as output type, please use 'json', 'file' or 'buildserver'");
}

arguments.Output.Add(outputType);
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersionExe/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Arguments

public string LogFilePath;
public string ShowVariable;
public string OutputFile;
public ISet<OutputType> Output = new HashSet<OutputType>();
public Verbosity Verbosity = Verbosity.Normal;

Expand Down Expand Up @@ -108,6 +109,7 @@ public GitVersionOptions ToOptions()
ShowVariable = ShowVariable,
Verbosity = Verbosity,
Output = Output,
OutputFile = OutputFile,

// TODO obsolete to be removed in version 6.0.0
Proj = Proj,
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersionExe/HelpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ path The directory containing .git. If not defined current directory
/h or /? Shows Help

/targetpath Same as 'path', but not positional
/output Determines the output to the console. Can be either 'json' or 'buildserver', will default to 'json'.
/output Determines the output to the console. Can be either 'json', 'file' or 'buildserver', will default to 'json'.
/outputfile Path to output file. It is used in combination with /output 'file'.
/showvariable Used in conjuntion with /output json, will output just a particular variable.
eg /output json /showvariable SemVer - will output `1.2.3+beta.4`
/l Path to logfile.
Expand Down