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
7 changes: 7 additions & 0 deletions src/GitVersion.App/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ internal class ArgumentParser(IEnvironment environment,
IFileSystem fileSystem,
ICurrentBuildAgent buildAgent,
IConsole console,
IHelpWriter helpWriter,
IVersionWriter versionWriter,
IGlobbingResolver globbingResolver)
: IArgumentParser
{
private readonly IEnvironment environment = environment.NotNull();
private readonly IFileSystem fileSystem = fileSystem.NotNull();
private readonly ICurrentBuildAgent buildAgent = buildAgent.NotNull();
private readonly IConsole console = console.NotNull();
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
private readonly IGlobbingResolver globbingResolver = globbingResolver.NotNull();

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

if (firstArgument.IsHelp())
{
this.helpWriter.Write();
return new Arguments
{
IsHelp = true
Expand All @@ -61,6 +66,8 @@ public Arguments ParseArguments(string[] commandLineArguments)

if (firstArgument.IsSwitch("version"))
{
var assembly = Assembly.GetExecutingAssembly();
this.versionWriter.Write(assembly);
return new Arguments
{
IsVersion = true
Expand Down
17 changes: 14 additions & 3 deletions src/GitVersion.App/GitVersionApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ public Task RunAsync(CancellationToken _)
try
{
var gitVersionOptions = this.options.Value;
this.log.Verbosity = gitVersionOptions.Verbosity;
SysEnv.ExitCode = this.gitVersionExecutor.Execute(gitVersionOptions);

if (gitVersionOptions.IsHelp || gitVersionOptions.IsVersion)
{
SysEnv.ExitCode = 0;
}
else
{
this.log.Verbosity = gitVersionOptions.Verbosity;
SysEnv.ExitCode = this.gitVersionExecutor.Execute(gitVersionOptions);
}
}
catch (Exception exception)
{
Console.Error.WriteLine(exception.Message);
SysEnv.ExitCode = 1;
}
finally
{
this.applicationLifetime.StopApplication();
}

this.applicationLifetime.StopApplication();
return Task.CompletedTask;
}
}
65 changes: 20 additions & 45 deletions src/GitVersion.App/GitVersionExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ internal class GitVersionExecutor(
ILog log,
IFileSystem fileSystem,
IConsole console,
IVersionWriter versionWriter,
IHelpWriter helpWriter,
IConfigurationFileLocator configurationFileLocator,
IConfigurationProvider configurationProvider,
IConfigurationSerializer configurationSerializer,
Expand All @@ -25,8 +23,6 @@ internal class GitVersionExecutor(
private readonly ILog log = log.NotNull();
private readonly IFileSystem fileSystem = fileSystem.NotNull();
private readonly IConsole console = console.NotNull();
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
private readonly IHelpWriter helpWriter = helpWriter.NotNull();

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

public int Execute(GitVersionOptions gitVersionOptions)
{
if (!HandleNonMainCommand(gitVersionOptions, out var exitCode))
{
exitCode = RunGitVersionTool(gitVersionOptions);
}
Initialize(gitVersionOptions);

var exitCode = !VerifyAndDisplayConfiguration(gitVersionOptions)
? RunGitVersionTool(gitVersionOptions)
: 0;

if (exitCode != 0)
{
Expand Down Expand Up @@ -103,30 +100,23 @@ private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
return 0;
}

private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int exitCode)
private void Initialize(GitVersionOptions gitVersionOptions)
{
if (gitVersionOptions.IsVersion)
if (gitVersionOptions.Diag)
{
var assembly = Assembly.GetExecutingAssembly();
this.versionWriter.Write(assembly);
exitCode = 0;
return true;
gitVersionOptions.Settings.NoCache = true;
}

if (gitVersionOptions.IsHelp)
if (gitVersionOptions.Output.Contains(OutputType.BuildServer) || gitVersionOptions.LogFilePath == "console")
{
this.helpWriter.Write();
exitCode = 0;
return true;
this.log.AddLogAppender(new ConsoleAppender());
}

if (gitVersionOptions.Diag)
if (gitVersionOptions.LogFilePath != null && gitVersionOptions.LogFilePath != "console")
{
gitVersionOptions.Settings.NoCache = true;
this.log.AddLogAppender(new FileAppender(this.fileSystem, gitVersionOptions.LogFilePath));
}

ConfigureLogging(gitVersionOptions, this.log, this.fileSystem);

var workingDirectory = gitVersionOptions.WorkingDirectory;
if (gitVersionOptions.Diag)
{
Expand All @@ -141,34 +131,19 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
{
this.log.Info("Working directory: " + workingDirectory);
}

if (gitVersionOptions.ConfigurationInfo.ShowConfiguration)
{
if (gitVersionOptions.RepositoryInfo.TargetUrl.IsNullOrWhiteSpace())
{
this.configurationFileLocator.Verify(workingDirectory, this.repositoryInfo.ProjectRootDirectory);
}
var configuration = this.configurationProvider.Provide();
var configurationString = configurationSerializer.Serialize(configuration);
this.console.WriteLine(configurationString);
exitCode = 0;
return true;
}

exitCode = 0;
return false;
}

private static void ConfigureLogging(GitVersionOptions gitVersionOptions, ILog log, IFileSystem fileSystem)
private bool VerifyAndDisplayConfiguration(GitVersionOptions gitVersionOptions)
{
if (gitVersionOptions.Output.Contains(OutputType.BuildServer) || gitVersionOptions.LogFilePath == "console")
if (!gitVersionOptions.ConfigurationInfo.ShowConfiguration) return false;
if (gitVersionOptions.RepositoryInfo.TargetUrl.IsNullOrWhiteSpace())
{
log.AddLogAppender(new ConsoleAppender());
this.configurationFileLocator.Verify(gitVersionOptions.WorkingDirectory, this.repositoryInfo.ProjectRootDirectory);
}

if (gitVersionOptions.LogFilePath != null && gitVersionOptions.LogFilePath != "console")
{
log.AddLogAppender(new FileAppender(fileSystem, gitVersionOptions.LogFilePath));
}
var configuration = this.configurationProvider.Provide();
var configurationString = this.configurationSerializer.Serialize(configuration);
this.console.WriteLine(configurationString);
return true;
}
}
Loading