aboutsummaryrefslogtreecommitdiff
path: root/src/wix/WixToolset.Core/CommandLine/HelpCommand.cs
blob: e8218b588ac09233797583c33683fe1008fe147a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using WixToolset.Extensibility;
    using WixToolset.Extensibility.Data;
    using WixToolset.Extensibility.Services;

    internal class HelpCommand : BaseCommandLineCommand
    {
        public HelpCommand(IEnumerable<IExtensionCommandLine> extensions, IWixBranding branding, ICommandLineCommand command)
        {
            this.Extensions = extensions;
            this.Branding = branding;
            this.Command = command;
        }

        public override bool ShowLogo => true;

        private IEnumerable<IExtensionCommandLine> Extensions { get; }

        private IWixBranding Branding { get; }

        private ICommandLineCommand Command { get; }

        public override CommandLineHelp GetCommandLineHelp()
        {
            return new CommandLineHelp(null, "[command] [options]")
            {
                Switches = new[]
                {
                    new CommandLineHelpSwitch("--help", "-h", "Show command line help."),
                    new CommandLineHelpSwitch("--version", "Display WiX Toolset version in use."),
                    new CommandLineHelpSwitch("--nologo", "Suppress displaying the logo information."),
                },
                Commands = new[]
                {
                    new CommandLineHelpCommand("build", "Build a wixlib, package, or bundle.")
                },
                Notes = "Run 'wix [command] -h' for more information on a command."
            };
        }

        public override Task<int> ExecuteAsync(CancellationToken _)
        {
            var extensionsHelp = this.Extensions.Select(e => e.GetCommandLineHelp()).Where(h => h != null).ToList();

            var help = this.Command?.GetCommandLineHelp() ?? this.GetCommandLineHelp();

            var switches = new List<CommandLineHelpSwitch>();
            if (help.Switches != null)
            {
                switches.AddRange(help.Switches);
            }

            switches.AddRange(extensionsHelp.Where(e => e.Switches != null).SelectMany(e => e.Switches));

            var commands = new List<CommandLineHelpCommand>();
            if (help.Commands != null)
            {
                commands.AddRange(help.Commands);
            }

            if (this.Command == null)
            {
                commands.AddRange(extensionsHelp.Where(e => e.Commands != null).SelectMany(e => e.Commands));
            }

            if (!String.IsNullOrEmpty(help.Description))
            {
                Console.WriteLine("Description:");
                Console.WriteLine("  {0}", help.Description);
                Console.WriteLine();
            }

            if (!String.IsNullOrEmpty(help.Usage))
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("  wix {0}", help.Usage);
                Console.WriteLine();
            }

            if (switches.Count > 0)
            {
                Console.WriteLine("Options:");
                foreach (var commandLineSwitch in help.Switches)
                {
                    var switchName = String.IsNullOrEmpty(commandLineSwitch.ShortName) ? commandLineSwitch.Name : $"{commandLineSwitch.ShortName}|{commandLineSwitch.Name}";
                    Console.WriteLine("  {0,-17} {1}", switchName, commandLineSwitch.Description);
                }

                Console.WriteLine();
            }

            if (commands.Count > 0)
            {
                Console.WriteLine("Commands:");
                foreach (var command in commands)
                {
                    Console.WriteLine("  {0,-17} {1}", command.Name, command.Description);
                }

                Console.WriteLine();
            }

            if (!String.IsNullOrEmpty(help.Notes))
            {
                Console.WriteLine(help.Notes);
                Console.WriteLine();
            }

            Console.WriteLine(this.Branding.ReplacePlaceholders("For more information see: [SupportUrl]"));

            return Task.FromResult(-1);
        }

        public override bool TryParseArgument(ICommandLineParser parseHelper, string argument)
        {
            return true; // eat any arguments
        }
    }
}