Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use IArgumentParser
  • Loading branch information
arturcic committed Oct 18, 2019
commit 63f81ee124843426269c59acc0dac5b2ba01efb4
3 changes: 2 additions & 1 deletion src/GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Shouldly;
using GitVersion.Exceptions;
using GitVersion.Helpers;
using GitVersion.Log;
using GitVersion.OutputFormatters;

namespace GitVersionExe.Tests
Expand All @@ -17,7 +18,7 @@ public class ArgumentParserTests
[SetUp]
public void SetUp()
{
argumentParser = new ArgumentParser();
argumentParser = new ArgumentParser(new NullLog());
}

[Test]
Expand Down
44 changes: 41 additions & 3 deletions src/GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,44 @@
using System.Linq;
using GitVersion.Configuration;
using GitVersion.Exceptions;
using GitVersion.Log;
using GitVersion.OutputVariables;
using GitVersion.OutputFormatters;

namespace GitVersion
{
public class ArgumentParser : IArgumentParser
{
private readonly ILog log;

public ArgumentParser(ILog log)
{
this.log = log;
}

public Arguments ParseArguments()
{
var argumentsWithoutExeName = GetArgumentsWithoutExeName();

Arguments arguments = null;
try
{
arguments = ParseArguments(argumentsWithoutExeName);
}
catch (Exception exception)
{
Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName));
if (!string.IsNullOrWhiteSpace(exception.Message))
{
log.Error(exception.Message);
}

return arguments;
}

return arguments;
}

public Arguments ParseArguments(string commandLineArguments)
{
var arguments = commandLineArguments
Expand Down Expand Up @@ -213,15 +244,15 @@ public Arguments ParseArguments(List<string> commandLineArguments)
{
string versionVariable = null;

if (!string.IsNullOrWhiteSpace(value))
if (!String.IsNullOrWhiteSpace(value))
{
versionVariable = VersionVariables.AvailableVariables.SingleOrDefault(av => av.Equals(value.Replace("'", ""), StringComparison.CurrentCultureIgnoreCase));
}

if (versionVariable == null)
{
var messageFormat = "{0} requires a valid version variable. Available variables are:\n{1}";
var message = string.Format(messageFormat, name, string.Join(", ", VersionVariables.AvailableVariables.Select(x => string.Concat("'", x, "'"))));
var message = String.Format(messageFormat, name, String.Join(", ", VersionVariables.AvailableVariables.Select(x => String.Concat("'", x, "'"))));
throw new WarningException(message);
}

Expand Down Expand Up @@ -417,7 +448,7 @@ private NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string>
else if (currentKey != null)
{
// And if the current switch does not have a value yet and the value is not itself a switch, set its value to this argument.
if (string.IsNullOrEmpty(switchesAndValues[currentKey]))
if (String.IsNullOrEmpty(switchesAndValues[currentKey]))
{
switchesAndValues[currentKey] = arg;
}
Expand All @@ -438,5 +469,12 @@ private NameValueCollection CollectSwitchesAndValuesFromArguments(IList<string>

return switchesAndValues;
}

private static List<string> GetArgumentsWithoutExeName()
{
return Environment.GetCommandLineArgs()
.Skip(1)
.ToList();
}
}
}
56 changes: 18 additions & 38 deletions src/GitVersionExe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using GitVersion.Configuration;
Expand Down Expand Up @@ -38,28 +37,17 @@ static void Main()

static int VerifyArgumentsAndRun()
{
var log = new Log.Log();
var fileSystem = new FileSystem();
var environment = new Environment();
var argumentParser = new ArgumentParser(log);
Arguments arguments = null;
try
{
var fileSystem = new FileSystem();
var environment = new Environment();
var argumentParser = new ArgumentParser();
var argumentsWithoutExeName = GetArgumentsWithoutExeName();
arguments = argumentParser.ParseArguments();

try
{
arguments = argumentParser.ParseArguments(argumentsWithoutExeName);
}
catch (Exception exception)
if (arguments == null)
{
Console.WriteLine("Failed to parse arguments: {0}", string.Join(" ", argumentsWithoutExeName));
if (!string.IsNullOrWhiteSpace(exception.Message))
{
Console.WriteLine();
Console.WriteLine(exception.Message);
Console.WriteLine();
}

HelpWriter.Write();
return 1;
}
Expand Down Expand Up @@ -128,20 +116,19 @@ static int VerifyArgumentsAndRun()
var error = $"An unexpected error occurred:\r\n{exception}";
Logger.Error(error);

if (arguments != null)
{
Logger.Info(string.Empty);
Logger.Info("Attempting to show the current git graph (please include in issue): ");
Logger.Info("Showing max of 100 commits");
if (arguments == null) return 1;

try
{
LibGitExtensions.DumpGraph(arguments.TargetPath, Logger.Info, 100);
}
catch (Exception dumpGraphException)
{
Logger.Error("Couldn't dump the git graph due to the following error: " + dumpGraphException);
}
Logger.Info(string.Empty);
Logger.Info("Attempting to show the current git graph (please include in issue): ");
Logger.Info("Showing max of 100 commits");

try
{
LibGitExtensions.DumpGraph(arguments.TargetPath, Logger.Info, 100);
}
catch (Exception dumpGraphException)
{
Logger.Error("Couldn't dump the git graph due to the following error: " + dumpGraphException);
}
return 1;
}
Expand Down Expand Up @@ -205,12 +192,5 @@ static void WriteLogEntry(Arguments arguments, string s)
var contents = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\t\t{s}\r\n";
File.AppendAllText(arguments.LogFilePath, contents);
}

static List<string> GetArgumentsWithoutExeName()
{
return System.Environment.GetCommandLineArgs()
.Skip(1)
.ToList();
}
}
}