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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
// 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 WixToolset.Extensibility;
using WixToolset.Extensibility.Data;
using WixToolset.Extensibility.Services;
internal enum CommandTypes
{
Unknown,
Build,
Preprocess,
Compile,
Link,
Bind,
Decompile,
}
internal class CommandLine : ICommandLine
{
private static readonly char[] BindPathSplit = { '=' };
public CommandLine(IServiceProvider serviceProvider)
{
this.ServiceProvider = serviceProvider;
this.Messaging = this.ServiceProvider.GetService<IMessaging>();
}
private IServiceProvider ServiceProvider { get; }
private IMessaging Messaging { get; set; }
public IExtensionManager ExtensionManager { get; set; }
public ICommandLineArguments Arguments { get; set; }
public static string ExpectedArgument { get; } = "expected argument";
public bool ShowHelp { get; private set; }
public ICommandLineCommand ParseStandardCommandLine()
{
var context = this.ServiceProvider.GetService<ICommandLineContext>();
context.ExtensionManager = this.ExtensionManager ?? this.ServiceProvider.GetService<IExtensionManager>();
context.Arguments = this.Arguments;
var command = this.Parse(context);
if (command.ShowLogo)
{
AppCommon.DisplayToolHeader();
}
return command;
//switch (commandType)
//{
//case CommandTypes.Build:
//{
// var sourceFiles = GatherSourceFiles(files, outputFolder);
// var variables = this.GatherPreprocessorVariables(defines);
// var bindPathList = this.GatherBindPaths(bindPaths);
// var filterCultures = CalculateFilterCultures(cultures);
// var type = CalculateOutputType(outputType, outputFile);
// var platform = CalculatePlatform(platformType);
// return new BuildCommand(this.ServiceProvider, sourceFiles, variables, locFiles, libraryFiles, filterCultures, outputFile, type, platform, cabCachePath, bindFiles, bindPathList, includePaths, intermediateFolder, contentsFile, outputsFile, builtOutputsFile);
//}
//case CommandTypes.Compile:
//{
// var sourceFiles = GatherSourceFiles(files, outputFolder);
// var variables = this.GatherPreprocessorVariables(defines);
// var platform = CalculatePlatform(platformType);
// return new CompileCommand(this.ServiceProvider, sourceFiles, variables, platform);
//}
//case CommandTypes.Decompile:
//{
// var sourceFiles = GatherSourceFiles(files, outputFolder);
// return new DecompileCommand(this.ServiceProvider, sourceFiles, outputFile);
//}
//}
//return null;
}
private ICommandLineCommand Parse(ICommandLineContext context)
{
var extensions = this.ExtensionManager.Create<IExtensionCommandLine>();
foreach (var extension in extensions)
{
extension.PreParse(context);
}
ICommandLineCommand command = null;
var parser = context.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, extensions))
{
parser.ErrorArgument = arg;
}
}
else if (parser.IsSwitch(arg))
{
if (!command.TryParseArgument(parser, arg) && !TryParseCommandLineArgumentWithExtension(arg, parser, extensions))
{
parser.ErrorArgument = arg;
}
}
else if (!TryParseCommandLineArgumentWithExtension(arg, parser, extensions) && command?.TryParseArgument(parser, arg) == false)
{
parser.ErrorArgument = arg;
}
}
foreach (var extension in extensions)
{
extension.PostParse();
}
return command ?? new HelpCommand();
}
private bool TryParseUnknownCommandArg(string arg, ICommandLineParser parser, out ICommandLineCommand command, IEnumerable<IExtensionCommandLine> extensions)
{
command = null;
if (parser.IsSwitch(arg))
{
var parameter = arg.Substring(1);
switch (parameter.ToLowerInvariant())
{
case "?":
case "h":
case "help":
command = new HelpCommand();
break;
case "version":
case "-version":
command = new VersionCommand();
break;
}
}
else
{
if (Enum.TryParse(arg, true, out CommandTypes commandType))
{
switch (commandType)
{
case CommandTypes.Build:
command = new BuildCommand(this.ServiceProvider);
break;
case CommandTypes.Compile:
command = new CompileCommand(this.ServiceProvider);
break;
case CommandTypes.Decompile:
command = new DecompileCommand(this.ServiceProvider);
break;
}
}
else
{
foreach (var extension in extensions)
{
if (extension.TryParseCommand(parser, out command))
{
break;
}
command = null;
}
}
}
return command != null;
}
private static bool TryParseCommandLineArgumentWithExtension(string arg, ICommandLineParser parse, IEnumerable<IExtensionCommandLine> extensions)
{
foreach (var extension in extensions)
{
if (extension.TryParseArgument(parse, arg))
{
return true;
}
}
return false;
}
}
}
|