aboutsummaryrefslogtreecommitdiff
path: root/src/tools/heat/HeatCommandLine.cs
blob: f299266d2bb2dd9a6646cd2b695094a2fd653b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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.Harvesters
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using WixToolset.Data;
    using WixToolset.Extensibility.Data;
    using WixToolset.Extensibility.Services;
    using WixToolset.Harvesters.Data;
    using WixToolset.Harvesters.Extensibility;

    internal class HeatCommandLine : IHeatCommandLine
    {
        private readonly IServiceProvider serviceProvider;
        private readonly List<IHeatExtension> extensions;

        public HeatCommandLine(IServiceProvider serviceProvider, IEnumerable<IHeatExtension> heatExtensions)
        {
            this.serviceProvider = serviceProvider;
            this.extensions = new List<IHeatExtension> { new IIsHeatExtension(), new UtilHeatExtension(serviceProvider), new VSHeatExtension() };
            if (heatExtensions != null)
            {
                this.extensions.AddRange(heatExtensions);
            }
        }

        public ICommandLineCommand ParseStandardCommandLine(ICommandLineArguments arguments)
        {
            ICommandLineCommand command = null;
            var parser = arguments.Parse();

            while (command?.StopParsing != true &&
                   String.IsNullOrEmpty(parser.ErrorArgument) &&
                   parser.TryGetNextSwitchOrArgument(out var arg))
            {
                if (String.IsNullOrWhiteSpace(arg)) // skip blank arguments.
                {
                    continue;
                }

                // First argument must be the command or global switch (that creates a command).
                if (command == null)
                {
                    if (!this.TryParseUnknownCommandArg(arg, parser, out command))
                    {
                        parser.ReportErrorArgument(arg, ErrorMessages.HarvestTypeNotFound(arg));
                    }
                }
                else if (!command.TryParseArgument(parser, arg))
                {
                    parser.ReportErrorArgument(arg);
                }
            }

            return command ?? new HelpCommand(this.extensions);
        }

        public bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command)
        {
            command = null;

            if (parser.IsSwitch(arg))
            {
                var parameter = arg.Substring(1);
                switch (parameter.ToLowerInvariant())
                {
                    case "?":
                    case "h":
                    case "help":
                        command = new HelpCommand(this.extensions);
                        return true;
                }
            }

            foreach (var heatExtension in this.extensions)
            {
                if (heatExtension.CommandLineTypes.Any(o => o.Option == arg))
                {
                    command = new HeatCommand(arg, this.extensions, this.serviceProvider);
                    return true;
                }
            }

            return false;
        }
    }
}