main
cs 127 lines 4.46 KB
Raw
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 {
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Threading;
9 using System.Threading.Tasks;
10 using WixToolset.Extensibility;
11 using WixToolset.Extensibility.Data;
12 using WixToolset.Extensibility.Services;
13
14 internal class HelpCommand : BaseCommandLineCommand
15 {
16 public HelpCommand(IEnumerable<IExtensionCommandLine> extensions, IWixBranding branding, ICommandLineCommand command)
17 {
18 this.Extensions = extensions;
19 this.Branding = branding;
20 this.Command = command;
21 }
22
23 public override bool ShowLogo => true;
24
25 private IEnumerable<IExtensionCommandLine> Extensions { get; }
26
27 private IWixBranding Branding { get; }
28
29 private ICommandLineCommand Command { get; }
30
31 public override CommandLineHelp GetCommandLineHelp()
32 {
33 return new CommandLineHelp(null, "[command] [options]")
34 {
35 Switches = new[]
36 {
37 new CommandLineHelpSwitch("--help", "-h", "Show command line help."),
38 new CommandLineHelpSwitch("--version", "Display WiX Toolset version in use."),
39 new CommandLineHelpSwitch("--nologo", "Suppress displaying the logo information."),
40 },
41 Commands = new[]
42 {
43 new CommandLineHelpCommand("build", "Build a wixlib, package, or bundle.")
44 },
45 Notes = "Run 'wix [command] -h' for more information on a command."
46 };
47 }
48
49 public override Task<int> ExecuteAsync(CancellationToken _)
50 {
51 var extensionsHelp = this.Extensions.Select(e => e.GetCommandLineHelp()).Where(h => h != null).ToList();
52
53 var help = this.Command?.GetCommandLineHelp() ?? this.GetCommandLineHelp();
54
55 var switches = new List<CommandLineHelpSwitch>();
56 if (help.Switches != null)
57 {
58 switches.AddRange(help.Switches);
59 }
60
61 switches.AddRange(extensionsHelp.Where(e => e.Switches != null).SelectMany(e => e.Switches));
62
63 var commands = new List<CommandLineHelpCommand>();
64 if (help.Commands != null)
65 {
66 commands.AddRange(help.Commands);
67 }
68
69 if (this.Command == null)
70 {
71 commands.AddRange(extensionsHelp.Where(e => e.Commands != null).SelectMany(e => e.Commands));
72 }
73
74 if (!String.IsNullOrEmpty(help.Description))
75 {
76 Console.WriteLine("Description:");
77 Console.WriteLine(" {0}", help.Description);
78 Console.WriteLine();
79 }
80
81 if (!String.IsNullOrEmpty(help.Usage))
82 {
83 Console.WriteLine("Usage:");
84 Console.WriteLine(" wix {0}", help.Usage);
85 Console.WriteLine();
86 }
87
88 if (switches.Count > 0)
89 {
90 Console.WriteLine("Options:");
91 foreach (var commandLineSwitch in help.Switches)
92 {
93 var switchName = String.IsNullOrEmpty(commandLineSwitch.ShortName) ? commandLineSwitch.Name : $"{commandLineSwitch.ShortName}|{commandLineSwitch.Name}";
94 Console.WriteLine(" {0,-17} {1}", switchName, commandLineSwitch.Description);
95 }
96
97 Console.WriteLine();
98 }
99
100 if (commands.Count > 0)
101 {
102 Console.WriteLine("Commands:");
103 foreach (var command in commands)
104 {
105 Console.WriteLine(" {0,-17} {1}", command.Name, command.Description);
106 }
107
108 Console.WriteLine();
109 }
110
111 if (!String.IsNullOrEmpty(help.Notes))
112 {
113 Console.WriteLine(help.Notes);
114 Console.WriteLine();
115 }
116
117 Console.WriteLine(this.Branding.ReplacePlaceholders("For more information see: [SupportUrl]"));
118
119 return Task.FromResult(-1);
120 }
121
122 public override bool TryParseArgument(ICommandLineParser parseHelper, string argument)
123 {
124 return true; // eat any arguments
125 }
126 }
127 }