Skip to content

Commit 0d25ac2

Browse files
authored
Merge pull request #4838 from arturcic/feature/refactor-help-version
Refactor help and version command handling
2 parents 819cebc + 5eea7e4 commit 0d25ac2

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed

src/GitVersion.App/ArgumentParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ internal class ArgumentParser(IEnvironment environment,
1212
IFileSystem fileSystem,
1313
ICurrentBuildAgent buildAgent,
1414
IConsole console,
15+
IHelpWriter helpWriter,
16+
IVersionWriter versionWriter,
1517
IGlobbingResolver globbingResolver)
1618
: IArgumentParser
1719
{
1820
private readonly IEnvironment environment = environment.NotNull();
1921
private readonly IFileSystem fileSystem = fileSystem.NotNull();
2022
private readonly ICurrentBuildAgent buildAgent = buildAgent.NotNull();
2123
private readonly IConsole console = console.NotNull();
24+
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
25+
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
2226
private readonly IGlobbingResolver globbingResolver = globbingResolver.NotNull();
2327

2428
private const string defaultOutputFileName = "GitVersion.json";
@@ -53,6 +57,7 @@ public Arguments ParseArguments(string[] commandLineArguments)
5357

5458
if (firstArgument.IsHelp())
5559
{
60+
this.helpWriter.Write();
5661
return new Arguments
5762
{
5863
IsHelp = true
@@ -61,6 +66,8 @@ public Arguments ParseArguments(string[] commandLineArguments)
6166

6267
if (firstArgument.IsSwitch("version"))
6368
{
69+
var assembly = Assembly.GetExecutingAssembly();
70+
this.versionWriter.Write(assembly);
6471
return new Arguments
6572
{
6673
IsVersion = true

src/GitVersion.App/GitVersionApp.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,27 @@ public Task RunAsync(CancellationToken _)
1919
try
2020
{
2121
var gitVersionOptions = this.options.Value;
22-
this.log.Verbosity = gitVersionOptions.Verbosity;
23-
SysEnv.ExitCode = this.gitVersionExecutor.Execute(gitVersionOptions);
22+
23+
if (gitVersionOptions.IsHelp || gitVersionOptions.IsVersion)
24+
{
25+
SysEnv.ExitCode = 0;
26+
}
27+
else
28+
{
29+
this.log.Verbosity = gitVersionOptions.Verbosity;
30+
SysEnv.ExitCode = this.gitVersionExecutor.Execute(gitVersionOptions);
31+
}
2432
}
2533
catch (Exception exception)
2634
{
2735
Console.Error.WriteLine(exception.Message);
2836
SysEnv.ExitCode = 1;
2937
}
38+
finally
39+
{
40+
this.applicationLifetime.StopApplication();
41+
}
3042

31-
this.applicationLifetime.StopApplication();
3243
return Task.CompletedTask;
3344
}
3445
}

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ internal class GitVersionExecutor(
1111
ILog log,
1212
IFileSystem fileSystem,
1313
IConsole console,
14-
IVersionWriter versionWriter,
15-
IHelpWriter helpWriter,
1614
IConfigurationFileLocator configurationFileLocator,
1715
IConfigurationProvider configurationProvider,
1816
IConfigurationSerializer configurationSerializer,
@@ -25,8 +23,6 @@ internal class GitVersionExecutor(
2523
private readonly ILog log = log.NotNull();
2624
private readonly IFileSystem fileSystem = fileSystem.NotNull();
2725
private readonly IConsole console = console.NotNull();
28-
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
29-
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
3026

3127
private readonly IConfigurationFileLocator configurationFileLocator = configurationFileLocator.NotNull();
3228
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
@@ -39,10 +35,11 @@ internal class GitVersionExecutor(
3935

4036
public int Execute(GitVersionOptions gitVersionOptions)
4137
{
42-
if (!HandleNonMainCommand(gitVersionOptions, out var exitCode))
43-
{
44-
exitCode = RunGitVersionTool(gitVersionOptions);
45-
}
38+
Initialize(gitVersionOptions);
39+
40+
var exitCode = !VerifyAndDisplayConfiguration(gitVersionOptions)
41+
? RunGitVersionTool(gitVersionOptions)
42+
: 0;
4643

4744
if (exitCode != 0)
4845
{
@@ -103,30 +100,23 @@ private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
103100
return 0;
104101
}
105102

106-
private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int exitCode)
103+
private void Initialize(GitVersionOptions gitVersionOptions)
107104
{
108-
if (gitVersionOptions.IsVersion)
105+
if (gitVersionOptions.Diag)
109106
{
110-
var assembly = Assembly.GetExecutingAssembly();
111-
this.versionWriter.Write(assembly);
112-
exitCode = 0;
113-
return true;
107+
gitVersionOptions.Settings.NoCache = true;
114108
}
115109

116-
if (gitVersionOptions.IsHelp)
110+
if (gitVersionOptions.Output.Contains(OutputType.BuildServer) || gitVersionOptions.LogFilePath == "console")
117111
{
118-
this.helpWriter.Write();
119-
exitCode = 0;
120-
return true;
112+
this.log.AddLogAppender(new ConsoleAppender());
121113
}
122114

123-
if (gitVersionOptions.Diag)
115+
if (gitVersionOptions.LogFilePath != null && gitVersionOptions.LogFilePath != "console")
124116
{
125-
gitVersionOptions.Settings.NoCache = true;
117+
this.log.AddLogAppender(new FileAppender(this.fileSystem, gitVersionOptions.LogFilePath));
126118
}
127119

128-
ConfigureLogging(gitVersionOptions, this.log, this.fileSystem);
129-
130120
var workingDirectory = gitVersionOptions.WorkingDirectory;
131121
if (gitVersionOptions.Diag)
132122
{
@@ -141,34 +131,19 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
141131
{
142132
this.log.Info("Working directory: " + workingDirectory);
143133
}
144-
145-
if (gitVersionOptions.ConfigurationInfo.ShowConfiguration)
146-
{
147-
if (gitVersionOptions.RepositoryInfo.TargetUrl.IsNullOrWhiteSpace())
148-
{
149-
this.configurationFileLocator.Verify(workingDirectory, this.repositoryInfo.ProjectRootDirectory);
150-
}
151-
var configuration = this.configurationProvider.Provide();
152-
var configurationString = configurationSerializer.Serialize(configuration);
153-
this.console.WriteLine(configurationString);
154-
exitCode = 0;
155-
return true;
156-
}
157-
158-
exitCode = 0;
159-
return false;
160134
}
161135

162-
private static void ConfigureLogging(GitVersionOptions gitVersionOptions, ILog log, IFileSystem fileSystem)
136+
private bool VerifyAndDisplayConfiguration(GitVersionOptions gitVersionOptions)
163137
{
164-
if (gitVersionOptions.Output.Contains(OutputType.BuildServer) || gitVersionOptions.LogFilePath == "console")
138+
if (!gitVersionOptions.ConfigurationInfo.ShowConfiguration) return false;
139+
if (gitVersionOptions.RepositoryInfo.TargetUrl.IsNullOrWhiteSpace())
165140
{
166-
log.AddLogAppender(new ConsoleAppender());
141+
this.configurationFileLocator.Verify(gitVersionOptions.WorkingDirectory, this.repositoryInfo.ProjectRootDirectory);
167142
}
168143

169-
if (gitVersionOptions.LogFilePath != null && gitVersionOptions.LogFilePath != "console")
170-
{
171-
log.AddLogAppender(new FileAppender(fileSystem, gitVersionOptions.LogFilePath));
172-
}
144+
var configuration = this.configurationProvider.Provide();
145+
var configurationString = this.configurationSerializer.Serialize(configuration);
146+
this.console.WriteLine(configurationString);
147+
return true;
173148
}
174149
}

0 commit comments

Comments
 (0)