@joebigelow / wix-1 / commits / 3fb889ab

Integrate change to TryParseCommand and other code cleanup

Rob Mensching committed Jun 6, 2020 at 15:49 UTC 3fb889ab7aa3cb0dfae23e0379e28552e919ad72
7 files changed +50 -93
src/WixToolset.Core.WindowsInstaller/Bind/BindDatabaseCommand.cs
+13 -14
@@ -403,13 +403,6 @@ namespace WixToolset.Core.WindowsInstaller.Bind
403
404 this.ValidateComponentGuids(output);
405
406 - // We can create instance transforms since Component Guids and Outputs are created.
407 - if (output.Type == OutputType.Product)
408 - {
409 - var command = new CreateInstanceTransformsCommand(section, output, tableDefinitions, this.BackendHelper);
410 - command.Execute();
411 - }
412 -
406 // Stop processing if an error previously occurred.
407 if (this.Messaging.EncounteredError)
408 {
@@ -452,19 +445,25 @@ namespace WixToolset.Core.WindowsInstaller.Bind
445 trackedFiles.AddRange(command.TrackedFiles);
446 }
447
455 - if (output.Type == OutputType.Patch)
456 - {
457 - // Copy output data back into the transforms.
458 - var command = new UpdateTransformsWithFileFacades(this.Messaging, output, this.SubStorages, tableDefinitions, fileFacades);
459 - command.Execute();
460 - }
461 -
448 // stop processing if an error previously occurred
449 if (this.Messaging.EncounteredError)
450 {
451 return null;
452 }
453
454 + // We can create instance transforms since Component Guids and Outputs are created.
455 + if (output.Type == OutputType.Product)
456 + {
457 + var command = new CreateInstanceTransformsCommand(section, output, tableDefinitions, this.BackendHelper);
458 + command.Execute();
459 + }
460 + else if (output.Type == OutputType.Patch)
461 + {
462 + // Copy output data back into the transforms.
463 + var command = new UpdateTransformsWithFileFacades(this.Messaging, output, this.SubStorages, tableDefinitions, fileFacades);
464 + command.Execute();
465 + }
466 +
467 // Generate database file.
468 this.Messaging.Write(VerboseMessages.GeneratingDatabase());
469
src/WixToolset.Core.WindowsInstaller/Bind/UpdateTransformsWithFileFacades.cs
-2
@@ -4,8 +4,6 @@ namespace WixToolset.Core.WindowsInstaller.Bind
4 {
5 using System;
6 using System.Collections.Generic;
7 - using System.Diagnostics;
8 - using System.IO;
7 using System.Linq;
8 using WixToolset.Core.Bind;
9 using WixToolset.Data;
src/WixToolset.Core/CommandLine/CommandLine.cs
+5 -33
@@ -54,35 +54,6 @@ namespace WixToolset.Core.CommandLine
54 }
55
56 return command;
57 - //switch (commandType)
58 - //{
59 - //case CommandTypes.Build:
60 - //{
61 - // var sourceFiles = GatherSourceFiles(files, outputFolder);
62 - // var variables = this.GatherPreprocessorVariables(defines);
63 - // var bindPathList = this.GatherBindPaths(bindPaths);
64 - // var filterCultures = CalculateFilterCultures(cultures);
65 - // var type = CalculateOutputType(outputType, outputFile);
66 - // var platform = CalculatePlatform(platformType);
67 - // return new BuildCommand(this.ServiceProvider, sourceFiles, variables, locFiles, libraryFiles, filterCultures, outputFile, type, platform, cabCachePath, bindFiles, bindPathList, includePaths, intermediateFolder, contentsFile, outputsFile, builtOutputsFile);
68 - //}
69 -
70 - //case CommandTypes.Compile:
71 - //{
72 - // var sourceFiles = GatherSourceFiles(files, outputFolder);
73 - // var variables = this.GatherPreprocessorVariables(defines);
74 - // var platform = CalculatePlatform(platformType);
75 - // return new CompileCommand(this.ServiceProvider, sourceFiles, variables, platform);
76 - //}
77 -
78 - //case CommandTypes.Decompile:
79 - //{
80 - // var sourceFiles = GatherSourceFiles(files, outputFolder);
81 - // return new DecompileCommand(this.ServiceProvider, sourceFiles, outputFile);
82 - //}
83 - //}
84 -
85 - //return null;
57 }
58
59 private ICommandLineCommand Parse(ICommandLineContext context)
@@ -109,7 +80,7 @@ namespace WixToolset.Core.CommandLine
80 // First argument must be the command or global switch (that creates a command).
81 if (command == null)
82 {
112 - if (!this.TryParseUnknownCommandArg(arg, parser, out command, extensions))
83 + if (!this.TryParseCommand(arg, parser, out command, extensions))
84 {
85 parser.ErrorArgument = arg;
86 }
@@ -121,7 +92,7 @@ namespace WixToolset.Core.CommandLine
92 parser.ErrorArgument = arg;
93 }
94 }
124 - else if (!TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && command?.TryParseArgument(parser, arg) == false)
95 + else if (!TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && !command.TryParseArgument(parser, arg))
96 {
97 parser.ErrorArgument = arg;
98 }
@@ -135,7 +106,7 @@ namespace WixToolset.Core.CommandLine
106 return command ?? new HelpCommand();
107 }
108
138 - private bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command, IEnumerable<IExtensionCommandLine> extensions)
109 + private bool TryParseCommand(string arg, ICommandLineParser parser, out ICommandLineCommand command, IEnumerable<IExtensionCommandLine> extensions)
110 {
111 command = null;
112
@@ -147,6 +118,7 @@ namespace WixToolset.Core.CommandLine
118 case "?":
119 case "h":
120 case "help":
121 + case "-help":
122 command = new HelpCommand();
123 break;
124
@@ -179,7 +151,7 @@ namespace WixToolset.Core.CommandLine
151 {
152 foreach (var extension in extensions)
153 {
182 - if (extension.TryParseCommand(parser, out command))
154 + if (extension.TryParseCommand(parser, arg, out command))
155 {
156 break;
157 }
src/WixToolset.Core/CommandLine/CommandLineArguments.cs
+24 -29
@@ -12,6 +12,11 @@ namespace WixToolset.Core.CommandLine
12
13 internal class CommandLineArguments : ICommandLineArguments
14 {
15 + public CommandLineArguments(IWixToolsetServiceProvider serviceProvider)
16 + {
17 + this.Messaging = serviceProvider.GetService<IMessaging>();
18 + }
19 +
20 public string[] OriginalArguments { get; set; }
21
22 public string[] Arguments { get; set; }
@@ -20,12 +25,7 @@ namespace WixToolset.Core.CommandLine
25
26 public string ErrorArgument { get; set; }
27
23 - private IWixToolsetServiceProvider ServiceProvider { get; }
24 -
25 - public CommandLineArguments(IWixToolsetServiceProvider serviceProvider)
26 - {
27 - this.ServiceProvider = serviceProvider;
28 - }
28 + private IMessaging Messaging { get; }
29
30 public void Populate(string commandLine)
31 {
@@ -41,27 +41,25 @@ namespace WixToolset.Core.CommandLine
41 this.ProcessArgumentsAndParseExtensions(this.OriginalArguments);
42 }
43
44 - public ICommandLineParser Parse()
45 - {
46 - var messaging = this.ServiceProvider.GetService<IMessaging>();
47 -
48 - return new CommandLineParser(messaging, this.Arguments, this.ErrorArgument);
49 - }
44 + public ICommandLineParser Parse() => new CommandLineParser(this.Messaging, this.Arguments, this.ErrorArgument);
45
46 private void FlattenArgumentsWithResponseFilesIntoOriginalArguments(string[] commandLineArguments)
47 {
53 - List<string> args = new List<string>();
48 + var args = new List<string>();
49
50 foreach (var arg in commandLineArguments)
51 {
57 - if ('@' == arg[0])
52 + if (arg != null)
53 {
59 - var responseFileArguments = CommandLineArguments.ParseResponseFile(arg.Substring(1));
60 - args.AddRange(responseFileArguments);
61 - }
62 - else
63 - {
64 - args.Add(arg);
54 + if ('@' == arg[0])
55 + {
56 + var responseFileArguments = CommandLineArguments.ParseResponseFile(arg.Substring(1));
57 + args.AddRange(responseFileArguments);
58 + }
59 + else
60 + {
61 + args.Add(arg);
62 + }
63 }
64 }
65
@@ -103,7 +101,7 @@ namespace WixToolset.Core.CommandLine
101 {
102 string arguments;
103
106 - using (StreamReader reader = new StreamReader(responseFile))
104 + using (var reader = new StreamReader(responseFile))
105 {
106 arguments = reader.ReadToEnd();
107 }
@@ -131,7 +129,7 @@ namespace WixToolset.Core.CommandLine
129 // The current argument string being built; when completed it will be added to the list.
130 var arg = new StringBuilder();
131
134 - for (int i = 0; i <= arguments.Length; i++)
132 + for (var i = 0; i <= arguments.Length; i++)
133 {
134 if (i == arguments.Length || (Char.IsWhiteSpace(arguments[i]) && !insideQuote))
135 {
@@ -182,10 +180,10 @@ namespace WixToolset.Core.CommandLine
180 var id = Environment.GetEnvironmentVariables();
181
182 var regex = new Regex("(?<=\\%)(?:[\\w\\.]+)(?=\\%)");
185 - MatchCollection matches = regex.Matches(arguments);
183 + var matches = regex.Matches(arguments);
184
187 - string value = String.Empty;
188 - for (int i = 0; i <= (matches.Count - 1); i++)
185 + var value = String.Empty;
186 + for (var i = 0; i <= (matches.Count - 1); i++)
187 {
188 try
189 {
@@ -204,9 +202,6 @@ namespace WixToolset.Core.CommandLine
202 return arguments;
203 }
204
207 - private static bool IsSwitchAt(string[] args, int index)
208 - {
209 - return args.Length > index && !String.IsNullOrEmpty(args[index]) && ('/' == args[index][0] || '-' == args[index][0]);
210 - }
205 + private static bool IsSwitchAt(string[] args, int index) => args.Length > index && !String.IsNullOrEmpty(args[index]) && ('/' == args[index][0] || '-' == args[index][0]);
206 }
207 }
src/WixToolset.Core/CommandLine/VersionCommand.cs
+2 -5
@@ -1,4 +1,4 @@
1 -// 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.
1 +// 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.
2
3 namespace WixToolset.Core.CommandLine
4 {
@@ -20,9 +20,6 @@ namespace WixToolset.Core.CommandLine
20 return 0;
21 }
22
23 - public bool TryParseArgument(ICommandLineParser parseHelper, string argument)
24 - {
25 - return true; // eat any arguments
26 - }
23 + public bool TryParseArgument(ICommandLineParser parseHelper, string argument) => true; // eat any arguments
24 }
25 }
src/WixToolset.Core/WixToolsetServiceProvider.cs
+4 -8
@@ -96,8 +96,7 @@ namespace WixToolset.Core
96 return service != null;
97 }
98
99 - public bool TryGetService<T>(out T service)
100 - where T : class
99 + public bool TryGetService<T>(out T service) where T : class
100 {
101 var success = this.TryGetService(typeof(T), out var untypedService);
102 service = (T)untypedService;
@@ -109,8 +108,7 @@ namespace WixToolset.Core
108 return this.TryGetService(serviceType, out var service) ? service : throw new ArgumentException($"Unknown service type: {serviceType.Name}", nameof(serviceType));
109 }
110
112 - public T GetService<T>()
113 - where T : class
111 + public T GetService<T>() where T : class
112 {
113 return (T)this.GetService(typeof(T));
114 }
@@ -120,14 +118,12 @@ namespace WixToolset.Core
118 this.CreationFunctions[serviceType] = creationFunction;
119 }
120
123 - public void AddService<T>(Func<IWixToolsetCoreServiceProvider, Dictionary<Type, object>, T> creationFunction)
124 - where T : class
121 + public void AddService<T>(Func<IWixToolsetCoreServiceProvider, Dictionary<Type, object>, T> creationFunction) where T : class
122 {
123 this.AddService(typeof(T), creationFunction);
124 }
125
129 - private static T AddSingleton<T>(Dictionary<Type, object> singletons, T service)
130 - where T : class
126 + private static T AddSingleton<T>(Dictionary<Type, object> singletons, T service) where T : class
127 {
128 singletons.Add(typeof(T), service);
129 return service;
src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs
+2 -2
@@ -34,7 +34,7 @@ namespace Example.Extension
34 return false;
35 }
36
37 - public bool TryParseCommand(ICommandLineParser parser, out ICommandLineCommand command)
37 + public bool TryParseCommand(ICommandLineParser parser, string argument, out ICommandLineCommand command)
38 {
39 command = null;
40 return false;
@@ -54,4 +54,4 @@ namespace Example.Extension
54 return null;
55 }
56 }
57 -}
\ No newline at end of file
57 +}