| 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.Harvesters |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Extensibility.Data; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | using WixToolset.Harvesters.Data; |
| 12 | using WixToolset.Harvesters.Extensibility; |
| 13 | |
| 14 | internal class HeatCommandLine : IHeatCommandLine |
| 15 | { |
| 16 | private readonly IServiceProvider serviceProvider; |
| 17 | private readonly List<IHeatExtension> extensions; |
| 18 | |
| 19 | public HeatCommandLine(IServiceProvider serviceProvider, IEnumerable<IHeatExtension> heatExtensions) |
| 20 | { |
| 21 | this.serviceProvider = serviceProvider; |
| 22 | this.extensions = new List<IHeatExtension> { new IIsHeatExtension(), new UtilHeatExtension(serviceProvider), new VSHeatExtension() }; |
| 23 | if (heatExtensions != null) |
| 24 | { |
| 25 | this.extensions.AddRange(heatExtensions); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public ICommandLineCommand ParseStandardCommandLine(ICommandLineArguments arguments) |
| 30 | { |
| 31 | ICommandLineCommand command = null; |
| 32 | var parser = arguments.Parse(); |
| 33 | |
| 34 | while (command?.StopParsing != true && |
| 35 | String.IsNullOrEmpty(parser.ErrorArgument) && |
| 36 | parser.TryGetNextSwitchOrArgument(out var arg)) |
| 37 | { |
| 38 | if (String.IsNullOrWhiteSpace(arg)) // skip blank arguments. |
| 39 | { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | // First argument must be the command or global switch (that creates a command). |
| 44 | if (command == null) |
| 45 | { |
| 46 | if (!this.TryParseUnknownCommandArg(arg, parser, out command)) |
| 47 | { |
| 48 | parser.ReportErrorArgument(arg, ErrorMessages.HarvestTypeNotFound(arg)); |
| 49 | } |
| 50 | } |
| 51 | else if (!command.TryParseArgument(parser, arg)) |
| 52 | { |
| 53 | parser.ReportErrorArgument(arg); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return command ?? new HelpCommand(this.extensions); |
| 58 | } |
| 59 | |
| 60 | public bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command) |
| 61 | { |
| 62 | command = null; |
| 63 | |
| 64 | if (parser.IsSwitch(arg)) |
| 65 | { |
| 66 | var parameter = arg.Substring(1); |
| 67 | switch (parameter.ToLowerInvariant()) |
| 68 | { |
| 69 | case "?": |
| 70 | case "h": |
| 71 | case "help": |
| 72 | command = new HelpCommand(this.extensions); |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | foreach (var heatExtension in this.extensions) |
| 78 | { |
| 79 | if (heatExtension.CommandLineTypes.Any(o => o.Option == arg)) |
| 80 | { |
| 81 | command = new HeatCommand(arg, this.extensions, this.serviceProvider); |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | } |