From 3fb889ab7aa3cb0dfae23e0379e28552e919ad72 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 6 Jun 2020 15:49:09 -0700 Subject: Integrate change to TryParseCommand and other code cleanup --- .../Bind/BindDatabaseCommand.cs | 27 ++++++----- .../Bind/UpdateTransformsWithFileFacades.cs | 2 - src/WixToolset.Core/CommandLine/CommandLine.cs | 38 ++-------------- .../CommandLine/CommandLineArguments.cs | 53 ++++++++++------------ src/WixToolset.Core/CommandLine/VersionCommand.cs | 7 +-- src/WixToolset.Core/WixToolsetServiceProvider.cs | 12 ++--- .../ExamplePreprocessorExtensionAndCommandLine.cs | 4 +- 7 files changed, 50 insertions(+), 93 deletions(-) diff --git a/src/WixToolset.Core.WindowsInstaller/Bind/BindDatabaseCommand.cs b/src/WixToolset.Core.WindowsInstaller/Bind/BindDatabaseCommand.cs index ffaf1479..bdb089ee 100644 --- a/src/WixToolset.Core.WindowsInstaller/Bind/BindDatabaseCommand.cs +++ b/src/WixToolset.Core.WindowsInstaller/Bind/BindDatabaseCommand.cs @@ -403,13 +403,6 @@ namespace WixToolset.Core.WindowsInstaller.Bind this.ValidateComponentGuids(output); - // We can create instance transforms since Component Guids and Outputs are created. - if (output.Type == OutputType.Product) - { - var command = new CreateInstanceTransformsCommand(section, output, tableDefinitions, this.BackendHelper); - command.Execute(); - } - // Stop processing if an error previously occurred. if (this.Messaging.EncounteredError) { @@ -452,19 +445,25 @@ namespace WixToolset.Core.WindowsInstaller.Bind trackedFiles.AddRange(command.TrackedFiles); } - if (output.Type == OutputType.Patch) - { - // Copy output data back into the transforms. - var command = new UpdateTransformsWithFileFacades(this.Messaging, output, this.SubStorages, tableDefinitions, fileFacades); - command.Execute(); - } - // stop processing if an error previously occurred if (this.Messaging.EncounteredError) { return null; } + // We can create instance transforms since Component Guids and Outputs are created. + if (output.Type == OutputType.Product) + { + var command = new CreateInstanceTransformsCommand(section, output, tableDefinitions, this.BackendHelper); + command.Execute(); + } + else if (output.Type == OutputType.Patch) + { + // Copy output data back into the transforms. + var command = new UpdateTransformsWithFileFacades(this.Messaging, output, this.SubStorages, tableDefinitions, fileFacades); + command.Execute(); + } + // Generate database file. this.Messaging.Write(VerboseMessages.GeneratingDatabase()); diff --git a/src/WixToolset.Core.WindowsInstaller/Bind/UpdateTransformsWithFileFacades.cs b/src/WixToolset.Core.WindowsInstaller/Bind/UpdateTransformsWithFileFacades.cs index af2e8f85..2af45e77 100644 --- a/src/WixToolset.Core.WindowsInstaller/Bind/UpdateTransformsWithFileFacades.cs +++ b/src/WixToolset.Core.WindowsInstaller/Bind/UpdateTransformsWithFileFacades.cs @@ -4,8 +4,6 @@ namespace WixToolset.Core.WindowsInstaller.Bind { using System; using System.Collections.Generic; - using System.Diagnostics; - using System.IO; using System.Linq; using WixToolset.Core.Bind; using WixToolset.Data; diff --git a/src/WixToolset.Core/CommandLine/CommandLine.cs b/src/WixToolset.Core/CommandLine/CommandLine.cs index 79f5d5bc..744e05b8 100644 --- a/src/WixToolset.Core/CommandLine/CommandLine.cs +++ b/src/WixToolset.Core/CommandLine/CommandLine.cs @@ -54,35 +54,6 @@ namespace WixToolset.Core.CommandLine } return command; - //switch (commandType) - //{ - //case CommandTypes.Build: - //{ - // var sourceFiles = GatherSourceFiles(files, outputFolder); - // var variables = this.GatherPreprocessorVariables(defines); - // var bindPathList = this.GatherBindPaths(bindPaths); - // var filterCultures = CalculateFilterCultures(cultures); - // var type = CalculateOutputType(outputType, outputFile); - // var platform = CalculatePlatform(platformType); - // return new BuildCommand(this.ServiceProvider, sourceFiles, variables, locFiles, libraryFiles, filterCultures, outputFile, type, platform, cabCachePath, bindFiles, bindPathList, includePaths, intermediateFolder, contentsFile, outputsFile, builtOutputsFile); - //} - - //case CommandTypes.Compile: - //{ - // var sourceFiles = GatherSourceFiles(files, outputFolder); - // var variables = this.GatherPreprocessorVariables(defines); - // var platform = CalculatePlatform(platformType); - // return new CompileCommand(this.ServiceProvider, sourceFiles, variables, platform); - //} - - //case CommandTypes.Decompile: - //{ - // var sourceFiles = GatherSourceFiles(files, outputFolder); - // return new DecompileCommand(this.ServiceProvider, sourceFiles, outputFile); - //} - //} - - //return null; } private ICommandLineCommand Parse(ICommandLineContext context) @@ -109,7 +80,7 @@ namespace WixToolset.Core.CommandLine // First argument must be the command or global switch (that creates a command). if (command == null) { - if (!this.TryParseUnknownCommandArg(arg, parser, out command, extensions)) + if (!this.TryParseCommand(arg, parser, out command, extensions)) { parser.ErrorArgument = arg; } @@ -121,7 +92,7 @@ namespace WixToolset.Core.CommandLine parser.ErrorArgument = arg; } } - else if (!TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && command?.TryParseArgument(parser, arg) == false) + else if (!TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && !command.TryParseArgument(parser, arg)) { parser.ErrorArgument = arg; } @@ -135,7 +106,7 @@ namespace WixToolset.Core.CommandLine return command ?? new HelpCommand(); } - private bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command, IEnumerable extensions) + private bool TryParseCommand(string arg, ICommandLineParser parser, out ICommandLineCommand command, IEnumerable extensions) { command = null; @@ -147,6 +118,7 @@ namespace WixToolset.Core.CommandLine case "?": case "h": case "help": + case "-help": command = new HelpCommand(); break; @@ -179,7 +151,7 @@ namespace WixToolset.Core.CommandLine { foreach (var extension in extensions) { - if (extension.TryParseCommand(parser, out command)) + if (extension.TryParseCommand(parser, arg, out command)) { break; } diff --git a/src/WixToolset.Core/CommandLine/CommandLineArguments.cs b/src/WixToolset.Core/CommandLine/CommandLineArguments.cs index 3f412611..456e19d7 100644 --- a/src/WixToolset.Core/CommandLine/CommandLineArguments.cs +++ b/src/WixToolset.Core/CommandLine/CommandLineArguments.cs @@ -12,6 +12,11 @@ namespace WixToolset.Core.CommandLine internal class CommandLineArguments : ICommandLineArguments { + public CommandLineArguments(IWixToolsetServiceProvider serviceProvider) + { + this.Messaging = serviceProvider.GetService(); + } + public string[] OriginalArguments { get; set; } public string[] Arguments { get; set; } @@ -20,12 +25,7 @@ namespace WixToolset.Core.CommandLine public string ErrorArgument { get; set; } - private IWixToolsetServiceProvider ServiceProvider { get; } - - public CommandLineArguments(IWixToolsetServiceProvider serviceProvider) - { - this.ServiceProvider = serviceProvider; - } + private IMessaging Messaging { get; } public void Populate(string commandLine) { @@ -41,27 +41,25 @@ namespace WixToolset.Core.CommandLine this.ProcessArgumentsAndParseExtensions(this.OriginalArguments); } - public ICommandLineParser Parse() - { - var messaging = this.ServiceProvider.GetService(); - - return new CommandLineParser(messaging, this.Arguments, this.ErrorArgument); - } + public ICommandLineParser Parse() => new CommandLineParser(this.Messaging, this.Arguments, this.ErrorArgument); private void FlattenArgumentsWithResponseFilesIntoOriginalArguments(string[] commandLineArguments) { - List args = new List(); + var args = new List(); foreach (var arg in commandLineArguments) { - if ('@' == arg[0]) + if (arg != null) { - var responseFileArguments = CommandLineArguments.ParseResponseFile(arg.Substring(1)); - args.AddRange(responseFileArguments); - } - else - { - args.Add(arg); + if ('@' == arg[0]) + { + var responseFileArguments = CommandLineArguments.ParseResponseFile(arg.Substring(1)); + args.AddRange(responseFileArguments); + } + else + { + args.Add(arg); + } } } @@ -103,7 +101,7 @@ namespace WixToolset.Core.CommandLine { string arguments; - using (StreamReader reader = new StreamReader(responseFile)) + using (var reader = new StreamReader(responseFile)) { arguments = reader.ReadToEnd(); } @@ -131,7 +129,7 @@ namespace WixToolset.Core.CommandLine // The current argument string being built; when completed it will be added to the list. var arg = new StringBuilder(); - for (int i = 0; i <= arguments.Length; i++) + for (var i = 0; i <= arguments.Length; i++) { if (i == arguments.Length || (Char.IsWhiteSpace(arguments[i]) && !insideQuote)) { @@ -182,10 +180,10 @@ namespace WixToolset.Core.CommandLine var id = Environment.GetEnvironmentVariables(); var regex = new Regex("(?<=\\%)(?:[\\w\\.]+)(?=\\%)"); - MatchCollection matches = regex.Matches(arguments); + var matches = regex.Matches(arguments); - string value = String.Empty; - for (int i = 0; i <= (matches.Count - 1); i++) + var value = String.Empty; + for (var i = 0; i <= (matches.Count - 1); i++) { try { @@ -204,9 +202,6 @@ namespace WixToolset.Core.CommandLine return arguments; } - private static bool IsSwitchAt(string[] args, int index) - { - return args.Length > index && !String.IsNullOrEmpty(args[index]) && ('/' == args[index][0] || '-' == args[index][0]); - } + private static bool IsSwitchAt(string[] args, int index) => args.Length > index && !String.IsNullOrEmpty(args[index]) && ('/' == args[index][0] || '-' == args[index][0]); } } diff --git a/src/WixToolset.Core/CommandLine/VersionCommand.cs b/src/WixToolset.Core/CommandLine/VersionCommand.cs index 1baee72d..50e90a93 100644 --- a/src/WixToolset.Core/CommandLine/VersionCommand.cs +++ b/src/WixToolset.Core/CommandLine/VersionCommand.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Core.CommandLine { @@ -20,9 +20,6 @@ namespace WixToolset.Core.CommandLine return 0; } - public bool TryParseArgument(ICommandLineParser parseHelper, string argument) - { - return true; // eat any arguments - } + public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments } } diff --git a/src/WixToolset.Core/WixToolsetServiceProvider.cs b/src/WixToolset.Core/WixToolsetServiceProvider.cs index 2cd097a4..1d475d00 100644 --- a/src/WixToolset.Core/WixToolsetServiceProvider.cs +++ b/src/WixToolset.Core/WixToolsetServiceProvider.cs @@ -96,8 +96,7 @@ namespace WixToolset.Core return service != null; } - public bool TryGetService(out T service) - where T : class + public bool TryGetService(out T service) where T : class { var success = this.TryGetService(typeof(T), out var untypedService); service = (T)untypedService; @@ -109,8 +108,7 @@ namespace WixToolset.Core return this.TryGetService(serviceType, out var service) ? service : throw new ArgumentException($"Unknown service type: {serviceType.Name}", nameof(serviceType)); } - public T GetService() - where T : class + public T GetService() where T : class { return (T)this.GetService(typeof(T)); } @@ -120,14 +118,12 @@ namespace WixToolset.Core this.CreationFunctions[serviceType] = creationFunction; } - public void AddService(Func, T> creationFunction) - where T : class + public void AddService(Func, T> creationFunction) where T : class { this.AddService(typeof(T), creationFunction); } - private static T AddSingleton(Dictionary singletons, T service) - where T : class + private static T AddSingleton(Dictionary singletons, T service) where T : class { singletons.Add(typeof(T), service); return service; diff --git a/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs b/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs index eddcf6e4..49f68de5 100644 --- a/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs +++ b/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs @@ -34,7 +34,7 @@ namespace Example.Extension return false; } - public bool TryParseCommand(ICommandLineParser parser, out ICommandLineCommand command) + public bool TryParseCommand(ICommandLineParser parser, string argument, out ICommandLineCommand command) { command = null; return false; @@ -54,4 +54,4 @@ namespace Example.Extension return null; } } -} \ No newline at end of file +} -- cgit v1.2.3-55-g6feb