public Arguments ParseArguments(string[] commandLineArguments) { if (commandLineArguments.Length == 0) { var args = new Arguments { TargetPath = System.Environment.CurrentDirectory, }; args.Output.Add(OutputType.Json); return(args); } var firstArgument = commandLineArguments.First(); if (firstArgument.IsHelp()) { return(new Arguments { IsHelp = true, }); } if (firstArgument.IsInit()) { return(new Arguments { TargetPath = System.Environment.CurrentDirectory, Init = true, }); } var arguments = new Arguments(); AddAuthentication(arguments); var switchesAndValues = CollectSwitchesAndValuesFromArguments(commandLineArguments, out var firstArgumentIsSwitch); for (var i = 0; i < switchesAndValues.AllKeys.Length; i++) { ParseArguments(arguments, switchesAndValues, i); } if (arguments.Output.Count == 0) { arguments.Output.Add(OutputType.Json); } // If the first argument is a switch, it should already have been consumed in the above loop, // or else a WarningException should have been thrown and we wouldn't end up here. arguments.TargetPath ??= firstArgumentIsSwitch ? System.Environment.CurrentDirectory : firstArgument; arguments.NoFetch = arguments.NoFetch || buildAgent != null && buildAgent.PreventFetch(); return(arguments); }
public Arguments ParseArguments(string[] commandLineArguments) { if (commandLineArguments.Length == 0) { var args = new Arguments { TargetPath = System.Environment.CurrentDirectory, }; args.Output.Add(OutputType.Json); AddAuthentication(args); return(args); } var firstArgument = commandLineArguments.First(); if (firstArgument.IsInit()) { return(new Arguments { TargetPath = System.Environment.CurrentDirectory, Init = true, }); } if (firstArgument.IsHelp()) { return(new Arguments { IsHelp = true, }); } if (firstArgument.IsSwitch("version")) { return(new Arguments { IsVersion = true, }); } var arguments = new Arguments(); AddAuthentication(arguments); var switchesAndValues = CollectSwitchesAndValuesFromArguments(commandLineArguments, out var firstArgumentIsSwitch); for (var i = 0; i < switchesAndValues.AllKeys.Length; i++) { ParseSwitchArguments(arguments, switchesAndValues, i); } if (arguments.Output.Count == 0) { arguments.Output.Add(OutputType.Json); } if (arguments.Output.Contains(OutputType.File) && arguments.OutputFile == null) { arguments.OutputFile = defaultOutputFileName; } // If the first argument is a switch, it should already have been consumed in the above loop, // or else a WarningException should have been thrown and we wouldn't end up here. arguments.TargetPath ??= firstArgumentIsSwitch ? System.Environment.CurrentDirectory : firstArgument; arguments.TargetPath = arguments.TargetPath.TrimEnd('/', '\\'); if (!arguments.EnsureAssemblyInfo) { arguments.UpdateAssemblyInfoFileName = ResolveFiles(arguments.TargetPath, arguments.UpdateAssemblyInfoFileName).ToHashSet(); } arguments.NoFetch = arguments.NoFetch || buildAgent != null && buildAgent.PreventFetch(); return(arguments); }
public Arguments ParseArguments(string[] commandLineArguments) { if (commandLineArguments.Length == 0) { var args = new Arguments { TargetPath = System.Environment.CurrentDirectory, }; args.Output.Add(OutputType.Json); return(args); } var firstArgument = commandLineArguments.First(); if (firstArgument.IsHelp()) { return(new Arguments { IsHelp = true, }); } if (firstArgument.IsInit()) { return(new Arguments { TargetPath = System.Environment.CurrentDirectory, Init = true, }); } var arguments = new Arguments(); AddAuthentication(arguments); var switchesAndValues = CollectSwitchesAndValuesFromArguments(commandLineArguments, out var firstArgumentIsSwitch); for (var i = 0; i < switchesAndValues.AllKeys.Length; i++) { var name = switchesAndValues.AllKeys[i]; var values = switchesAndValues.GetValues(name); var value = values?.FirstOrDefault(); if (name.IsSwitch("version")) { EnsureArgumentValueCount(values); arguments.IsVersion = true; continue; } if (name.IsSwitch("l")) { EnsureArgumentValueCount(values); arguments.LogFilePath = value; continue; } if (name.IsSwitch("config")) { EnsureArgumentValueCount(values); arguments.ConfigFile = value; continue; } if (name.IsSwitch("targetpath")) { EnsureArgumentValueCount(values); arguments.TargetPath = value; if (!Directory.Exists(value)) { console.WriteLine($"The working directory '{value}' does not exist."); } continue; } if (name.IsSwitch("dynamicRepoLocation")) { EnsureArgumentValueCount(values); arguments.DynamicRepositoryClonePath = value; continue; } if (name.IsSwitch("url")) { EnsureArgumentValueCount(values); arguments.TargetUrl = value; continue; } if (name.IsSwitch("b")) { EnsureArgumentValueCount(values); arguments.TargetBranch = value; continue; } if (name.IsSwitch("u")) { EnsureArgumentValueCount(values); arguments.Authentication.Username = value; continue; } if (name.IsSwitch("p")) { EnsureArgumentValueCount(values); arguments.Authentication.Password = value; continue; } if (name.IsSwitch("c")) { EnsureArgumentValueCount(values); arguments.CommitId = value; continue; } if (name.IsSwitch("exec")) { EnsureArgumentValueCount(values); #pragma warning disable CS0612 // Type or member is obsolete arguments.Exec = value; #pragma warning restore CS0612 // Type or member is obsolete continue; } if (name.IsSwitch("execargs")) { EnsureArgumentValueCount(values); #pragma warning disable CS0612 // Type or member is obsolete arguments.ExecArgs = value; #pragma warning restore CS0612 // Type or member is obsolete continue; } if (name.IsSwitch("proj")) { EnsureArgumentValueCount(values); #pragma warning disable CS0612 // Type or member is obsolete arguments.Proj = value; #pragma warning restore CS0612 // Type or member is obsolete continue; } if (name.IsSwitch("projargs")) { EnsureArgumentValueCount(values); #pragma warning disable CS0612 // Type or member is obsolete arguments.ProjArgs = value; #pragma warning restore CS0612 // Type or member is obsolete continue; } if (name.IsSwitch("diag")) { if (value == null || value.IsTrue()) { arguments.Diag = true; } continue; } if (name.IsSwitch("updateAssemblyInfo")) { if (value.IsTrue()) { arguments.UpdateAssemblyInfo = true; } else if (value.IsFalse()) { arguments.UpdateAssemblyInfo = false; } else if (values != null && values.Length > 1) { arguments.UpdateAssemblyInfo = true; foreach (var v in values) { arguments.UpdateAssemblyInfoFileName.Add(v); } } else if (!value.IsSwitchArgument()) { arguments.UpdateAssemblyInfo = true; arguments.UpdateAssemblyInfoFileName.Add(value); } else { arguments.UpdateAssemblyInfo = true; } if (arguments.UpdateAssemblyInfoFileName.Count > 1 && arguments.EnsureAssemblyInfo) { 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"); } continue; } if (name.IsSwitch("assemblyversionformat")) { throw new WarningException("assemblyversionformat switch removed, use AssemblyVersioningScheme configuration value instead"); } if (name.IsSwitch("v") || name.IsSwitch("showvariable")) { string versionVariable = null; if (!string.IsNullOrWhiteSpace(value)) { versionVariable = VersionVariables.AvailableVariables.SingleOrDefault(av => av.Equals(value.Replace("'", ""), StringComparison.CurrentCultureIgnoreCase)); } if (versionVariable == null) { var message = $"{name} requires a valid version variable. Available variables are:{System.Environment.NewLine}" + string.Join(", ", VersionVariables.AvailableVariables.Select(x => string.Concat("'", x, "'"))); throw new WarningException(message); } arguments.ShowVariable = versionVariable; continue; } if (name.IsSwitch("showConfig")) { if (value.IsTrue()) { arguments.ShowConfig = true; } else if (value.IsFalse()) { arguments.UpdateAssemblyInfo = false; } else { arguments.ShowConfig = true; } continue; } if (name.IsSwitch("output")) { foreach (var v in values) { if (!Enum.TryParse(v, true, out OutputType outputType)) { throw new WarningException($"Value '{v}' cannot be parsed as output type, please use 'json' or 'buildserver'"); } arguments.Output.Add(outputType); } continue; } if (name.IsSwitch("nofetch")) { arguments.NoFetch = true; continue; } if (name.IsSwitch("nonormalize")) { arguments.NoNormalize = true; continue; } if (name.IsSwitch("ensureassemblyinfo")) { arguments.EnsureAssemblyInfo = true; if (value.IsFalse()) { arguments.EnsureAssemblyInfo = false; } if (arguments.UpdateAssemblyInfoFileName.Count > 1 && arguments.EnsureAssemblyInfo) { 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"); } continue; } if (name.IsSwitch("overrideconfig")) { var keyValueOptions = (value ?? "").Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (keyValueOptions.Length == 0) { continue; } arguments.OverrideConfig = new Config(); if (keyValueOptions.Length > 1) { throw new WarningException("Can't specify multiple /overrideconfig options: currently supported only 'tag-prefix' option"); } // key=value foreach (var keyValueOption in keyValueOptions) { var keyAndValue = keyValueOption.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (keyAndValue.Length != 2) { throw new WarningException($"Could not parse /overrideconfig option: {keyValueOption}. Ensure it is in format 'key=value'"); } var optionKey = keyAndValue[0].ToLowerInvariant(); arguments.OverrideConfig.TagPrefix = optionKey switch { "tag-prefix" => keyAndValue[1], _ => throw new WarningException($"Could not parse /overrideconfig option: {optionKey}. Currently supported only 'tag-prefix' option") }; } continue; } if (name.IsSwitch("nocache")) { arguments.NoCache = true; continue; } if (name.IsSwitch("verbosity")) { // first try the old version, this check will be removed in version 6.0.0, making it a breaking change if (Enum.TryParse(value, true, out LogLevel logLevel)) { arguments.Verbosity = LogExtensions.GetVerbosityForLevel(logLevel); } else if (!Enum.TryParse(value, true, out arguments.Verbosity)) { throw new WarningException($"Could not parse Verbosity value '{value}'"); } continue; } if (name.IsSwitch("updatewixversionfile")) { arguments.UpdateWixVersionFile = true; continue; } var couldNotParseMessage = $"Could not parse command line parameter '{name}'."; // If we've reached through all argument switches without a match, we can relatively safely assume that the first argument isn't a switch, but the target path. if (i == 0) { if (name.StartsWith("/")) { if (Path.DirectorySeparatorChar == '/' && name.IsValidPath()) { arguments.TargetPath = name; continue; } } else if (!name.IsSwitchArgument()) { arguments.TargetPath = name; continue; } couldNotParseMessage += " If it is the target path, make sure it exists."; } throw new WarningException(couldNotParseMessage); } if (arguments.Output.Count == 0) { arguments.Output.Add(OutputType.Json); } if (arguments.TargetPath == null) { // If the first argument is a switch, it should already have been consumed in the above loop, // or else a WarningException should have been thrown and we wouldn't end up here. arguments.TargetPath = firstArgumentIsSwitch ? System.Environment.CurrentDirectory : firstArgument; } arguments.NoFetch = arguments.NoFetch || buildAgent != null && buildAgent.PreventFetch(); return(arguments); }