11using System . IO . Abstractions ;
2- using GitVersion . Agents ;
32using GitVersion . Extensions ;
43using GitVersion . FileSystemGlobbing ;
54using GitVersion . Helpers ;
5+ using GitVersion . Logging ;
66using GitVersion . OutputVariables ;
7+ using Serilog . Core ;
8+ using Serilog . Events ;
79
810namespace GitVersion ;
911
10- internal class ArgumentParser ( IEnvironment environment ,
11- IFileSystem fileSystem ,
12- ICurrentBuildAgent buildAgent ,
13- IConsole console ,
14- IHelpWriter helpWriter ,
15- IVersionWriter versionWriter ,
16- IGlobbingResolver globbingResolver )
12+ internal class ArgumentParser (
13+ IEnvironment environment ,
14+ IFileSystem fileSystem ,
15+ IConsole console ,
16+ IHelpWriter helpWriter ,
17+ IVersionWriter versionWriter ,
18+ IGlobbingResolver globbingResolver ,
19+ LoggingLevelSwitch loggingLevelSwitch
20+ )
1721 : IArgumentParser
1822{
1923 private readonly IEnvironment environment = environment . NotNull ( ) ;
2024 private readonly IFileSystem fileSystem = fileSystem . NotNull ( ) ;
21- private readonly ICurrentBuildAgent buildAgent = buildAgent . NotNull ( ) ;
2225 private readonly IConsole console = console . NotNull ( ) ;
2326 private readonly IHelpWriter helpWriter = helpWriter . NotNull ( ) ;
2427 private readonly IVersionWriter versionWriter = versionWriter . NotNull ( ) ;
2528 private readonly IGlobbingResolver globbingResolver = globbingResolver . NotNull ( ) ;
29+ private readonly LoggingLevelSwitch loggingLevelSwitch = loggingLevelSwitch . NotNull ( ) ;
2630
2731 private const string defaultOutputFileName = "GitVersion.json" ;
2832 private static readonly IEnumerable < string > availableVariables = GitVersionVariables . AvailableVariables ;
2933
34+ private static readonly Dictionary < Verbosity , LogEventLevel > VerbosityMaps = new ( )
35+ {
36+ { Verbosity . Verbose , LogEventLevel . Verbose } ,
37+ { Verbosity . Diagnostic , LogEventLevel . Debug } ,
38+ { Verbosity . Normal , LogEventLevel . Information } ,
39+ { Verbosity . Minimal , LogEventLevel . Warning } ,
40+ { Verbosity . Quiet , LogEventLevel . Error }
41+ } ;
42+
3043 public Arguments ParseArguments ( string commandLineArguments )
3144 {
3245 var arguments = QuotedStringHelpers . SplitUnquoted ( commandLineArguments , ' ' ) ;
@@ -47,8 +60,6 @@ public Arguments ParseArguments(string[] commandLineArguments)
4760
4861 AddAuthentication ( args ) ;
4962
50- args . NoFetch = this . buildAgent . PreventFetch ( ) ;
51-
5263 return args ;
5364 }
5465
@@ -57,20 +68,14 @@ public Arguments ParseArguments(string[] commandLineArguments)
5768 if ( firstArgument . IsHelp ( ) )
5869 {
5970 this . helpWriter . Write ( ) ;
60- return new Arguments
61- {
62- IsHelp = true
63- } ;
71+ return new Arguments { IsHelp = true } ;
6472 }
6573
6674 if ( firstArgument . IsSwitch ( "version" ) )
6775 {
6876 var assembly = Assembly . GetExecutingAssembly ( ) ;
6977 this . versionWriter . Write ( assembly ) ;
70- return new Arguments
71- {
72- IsVersion = true
73- } ;
78+ return new Arguments { IsVersion = true } ;
7479 }
7580
7681 var arguments = new Arguments ( ) ;
@@ -103,7 +108,6 @@ public Arguments ParseArguments(string[] commandLineArguments)
103108 arguments . TargetPath = arguments . TargetPath . TrimEnd ( '/' , '\\ ' ) ;
104109
105110 if ( ! arguments . EnsureAssemblyInfo ) arguments . UpdateAssemblyInfoFileName = ResolveFiles ( arguments . TargetPath , arguments . UpdateAssemblyInfoFileName ) . ToHashSet ( ) ;
106- arguments . NoFetch = arguments . NoFetch || this . buildAgent . PreventFetch ( ) ;
107111
108112 ValidateConfigurationFile ( arguments ) ;
109113
@@ -203,7 +207,7 @@ private void ParseTargetPath(Arguments arguments, string? name, IReadOnlyList<st
203207 throw new WarningException ( couldNotParseMessage ) ;
204208 }
205209
206- private static bool ParseSwitches ( Arguments arguments , string ? name , IReadOnlyList < string > ? values , string ? value )
210+ private bool ParseSwitches ( Arguments arguments , string ? name , IReadOnlyList < string > ? values , string ? value )
207211 {
208212 if ( name . IsSwitch ( "l" ) )
209213 {
@@ -291,7 +295,8 @@ private static bool ParseSwitches(Arguments arguments, string? name, IReadOnlyLi
291295
292296 if ( name . IsSwitch ( "verbosity" ) )
293297 {
294- ParseVerbosity ( arguments , value ) ;
298+ var verbosity = ParseVerbosity ( value ) ;
299+ loggingLevelSwitch . MinimumLevel = VerbosityMaps [ verbosity ] ;
295300 return true ;
296301 }
297302
@@ -436,12 +441,14 @@ private static void ParseOutput(Arguments arguments, IEnumerable<string>? values
436441 }
437442 }
438443
439- private static void ParseVerbosity ( Arguments arguments , string ? value )
444+ internal static Verbosity ParseVerbosity ( string ? value )
440445 {
441- if ( ! Enum . TryParse ( value , true , out arguments . Verbosity ) )
446+ if ( ! Enum . TryParse ( value , true , out Verbosity verbosity ) )
442447 {
443448 throw new WarningException ( $ "Could not parse Verbosity value '{ value } '") ;
444449 }
450+
451+ return verbosity ;
445452 }
446453
447454 private static void ParseOverrideConfig ( Arguments arguments , IReadOnlyCollection < string > ? values )
@@ -465,8 +472,10 @@ private static void ParseOverrideConfig(Arguments arguments, IReadOnlyCollection
465472 {
466473 throw new WarningException ( $ "Could not parse /overrideconfig option: { keyValueOption } . Unsupported 'key'.") ;
467474 }
475+
468476 parser . SetValue ( optionKey , keyAndValue [ 1 ] ) ;
469477 }
478+
470479 arguments . OverrideConfiguration = parser . GetOverrideConfiguration ( ) ;
471480 }
472481
@@ -505,6 +514,7 @@ private static void ParseUpdateAssemblyInfo(Arguments arguments, string? value,
505514 {
506515 throw new WarningException ( "Cannot specify both updateprojectfiles and updateassemblyinfo in the same run. Please rerun GitVersion with only one parameter" ) ;
507516 }
517+
508518 if ( arguments . UpdateAssemblyInfoFileName . Count > 1 && arguments . EnsureAssemblyInfo )
509519 {
510520 throw new WarningException ( "Can't specify multiple assembly info files when using -ensureassemblyinfo switch, either use a single assembly info file or do not specify -ensureassemblyinfo and create assembly info files manually" ) ;
@@ -546,6 +556,7 @@ private static void ParseUpdateProjectInfo(Arguments arguments, string? value, I
546556 {
547557 throw new WarningException ( "Cannot specify both updateassemblyinfo and updateprojectfiles in the same run. Please rerun GitVersion with only one parameter" ) ;
548558 }
559+
549560 if ( arguments . EnsureAssemblyInfo )
550561 {
551562 throw new WarningException ( "Cannot specify -ensureassemblyinfo with updateprojectfiles: please ensure your project file exists before attempting to update it" ) ;
0 commit comments