Skip to content

Commit 3700f2b

Browse files
committed
added output file option
1 parent 3e2a3e3 commit 3700f2b

File tree

7 files changed

+24
-6
lines changed

7 files changed

+24
-6
lines changed

src/GitVersionCore/Core/GitVersionTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void OutputVariables(VersionVariables variables)
8585

8686
using (outputGenerator)
8787
{
88-
outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory));
88+
outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile));
8989
}
9090
}
9191

src/GitVersionCore/Model/GitVersionOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public GitVersionOptions()
3939

4040
public string LogFilePath;
4141
public string ShowVariable;
42+
public string OutputFile;
4243
public ISet<OutputType> Output = new HashSet<OutputType>();
4344
public Verbosity Verbosity = Verbosity.Normal;
4445

src/GitVersionCore/Model/OutputType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace GitVersion.Model
33
public enum OutputType
44
{
55
BuildServer,
6-
7-
Json
6+
Json,
7+
File
88
}
99
}

src/GitVersionCore/VersionConverters/OutputGenerator/OutputContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ namespace GitVersion.VersionConverters.OutputGenerator
22
{
33
public readonly struct OutputContext : IConverterContext
44
{
5-
public OutputContext(string workingDirectory)
5+
public OutputContext(string workingDirectory, string outputFile)
66
{
77
WorkingDirectory = workingDirectory;
8+
OutputFile = outputFile;
89
}
910

1011
public string WorkingDirectory { get; }
12+
public string OutputFile { get; }
1113
}
1214
}

src/GitVersionCore/VersionConverters/OutputGenerator/OutputGenerator.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public interface IOutputGenerator : IVersionConverter<OutputContext>
1313
public class OutputGenerator : IOutputGenerator
1414
{
1515
private readonly IConsole console;
16+
private readonly IFileSystem fileSystem;
1617
private readonly IOptions<GitVersionOptions> options;
1718
private readonly ICurrentBuildAgent buildAgent;
1819

19-
public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IOptions<GitVersionOptions> options)
20+
public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IFileSystem fileSystem, IOptions<GitVersionOptions> options)
2021
{
2122
this.console = console ?? throw new ArgumentNullException(nameof(console));
23+
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
2224
this.options = options ?? throw new ArgumentNullException(nameof(options));
2325
this.buildAgent = buildAgent;
2426
}
@@ -30,6 +32,10 @@ public void Execute(VersionVariables variables, OutputContext context)
3032
{
3133
buildAgent?.WriteIntegration(console.WriteLine, variables);
3234
}
35+
if (gitVersionOptions.Output.Contains(OutputType.File))
36+
{
37+
fileSystem.WriteAllText(context.OutputFile, variables.ToString());
38+
}
3339
if (gitVersionOptions.Output.Contains(OutputType.Json))
3440
{
3541
switch (gitVersionOptions.ShowVariable)

src/GitVersionExe/ArgumentParser.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ private static bool ParseSwitches(Arguments arguments, string name, string[] val
223223
return true;
224224
}
225225

226+
if (name.IsSwitch("outputfile"))
227+
{
228+
EnsureArgumentValueCount(values);
229+
arguments.OutputFile = value;
230+
return true;
231+
}
232+
226233
if (name.IsSwitch("nofetch"))
227234
{
228235
arguments.NoFetch = true;
@@ -417,7 +424,7 @@ private static void ParseOutput(Arguments arguments, string[] values)
417424
{
418425
if (!Enum.TryParse(v, true, out OutputType outputType))
419426
{
420-
throw new WarningException($"Value '{v}' cannot be parsed as output type, please use 'json' or 'buildserver'");
427+
throw new WarningException($"Value '{v}' cannot be parsed as output type, please use 'json', 'file' or 'buildserver'");
421428
}
422429

423430
arguments.Output.Add(outputType);

src/GitVersionExe/Arguments.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Arguments
3434

3535
public string LogFilePath;
3636
public string ShowVariable;
37+
public string OutputFile;
3738
public ISet<OutputType> Output = new HashSet<OutputType>();
3839
public Verbosity Verbosity = Verbosity.Normal;
3940

@@ -108,6 +109,7 @@ public GitVersionOptions ToOptions()
108109
ShowVariable = ShowVariable,
109110
Verbosity = Verbosity,
110111
Output = Output,
112+
OutputFile = OutputFile,
111113

112114
// TODO obsolete to be removed in version 6.0.0
113115
Proj = Proj,

0 commit comments

Comments
 (0)