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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// 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.IO;
using WixToolset.Data;
using WixToolset.Extensibility.Services;
internal class ParseCommandLine : IParseCommandLine
{
private const string ExpectedArgument = "expected argument";
public string ErrorArgument { get; set; }
private Queue<string> RemainingArguments { get; }
private IMessaging Messaging { get; }
public ParseCommandLine(IMessaging messaging, string[] arguments, string errorArgument)
{
this.Messaging = messaging;
this.RemainingArguments = new Queue<string>(arguments);
this.ErrorArgument = errorArgument;
}
public bool IsSwitch(string arg) => !String.IsNullOrEmpty(arg) && ('/' == arg[0] || '-' == arg[0]);
public void GetArgumentAsFilePathOrError(string argument, string fileType, IList<string> paths)
{
foreach (var path in GetFiles(argument, fileType))
{
paths.Add(path);
}
}
public string GetNextArgumentOrError(string commandLineSwitch)
{
if (this.TryGetNextNonSwitchArgumentOrError(out var argument))
{
return argument;
}
this.Messaging.Write(ErrorMessages.ExpectedArgument(commandLineSwitch));
return null;
}
public bool GetNextArgumentOrError(string commandLineSwitch, IList<string> args)
{
if (this.TryGetNextNonSwitchArgumentOrError(out var arg))
{
args.Add(arg);
return true;
}
this.Messaging.Write(ErrorMessages.ExpectedArgument(commandLineSwitch));
return false;
}
public string GetNextArgumentAsDirectoryOrError(string commandLineSwitch)
{
if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && TryGetDirectory(commandLineSwitch, this.Messaging, arg, out var directory))
{
return directory;
}
this.Messaging.Write(ErrorMessages.ExpectedArgument(commandLineSwitch));
return null;
}
public bool GetNextArgumentAsDirectoryOrError(string commandLineSwitch, IList<string> directories)
{
if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && TryGetDirectory(commandLineSwitch, this.Messaging, arg, out var directory))
{
directories.Add(directory);
return true;
}
this.Messaging.Write(ErrorMessages.ExpectedArgument(commandLineSwitch));
return false;
}
public string GetNextArgumentAsFilePathOrError(string commandLineSwitch)
{
if (this.TryGetNextNonSwitchArgumentOrError(out var arg) && this.TryGetFile(commandLineSwitch, arg, out var path))
{
return path;
}
this.Messaging.Write(ErrorMessages.ExpectedArgument(commandLineSwitch));
return null;
}
public bool GetNextArgumentAsFilePathOrError(string commandLineSwitch, string fileType, IList<string> paths)
{
if (this.TryGetNextNonSwitchArgumentOrError(out var arg))
{
foreach (var path in GetFiles(arg, fileType))
{
paths.Add(path);
}
return true;
}
this.Messaging.Write(ErrorMessages.ExpectedArgument(commandLineSwitch));
return false;
}
public bool TryGetNextSwitchOrArgument(out string arg)
{
return TryDequeue(this.RemainingArguments, out arg);
}
private bool TryGetNextNonSwitchArgumentOrError(out string arg)
{
var result = this.TryGetNextSwitchOrArgument(out arg);
if (!result && !this.IsSwitch(arg))
{
this.ErrorArgument = arg ?? ParseCommandLine.ExpectedArgument;
}
return result;
}
private static bool IsValidArg(string arg) => !(String.IsNullOrEmpty(arg) || '/' == arg[0] || '-' == arg[0]);
private static bool TryDequeue(Queue<string> q, out string arg)
{
if (q.Count > 0)
{
arg = q.Dequeue();
return true;
}
arg = null;
return false;
}
private bool TryGetDirectory(string commandlineSwitch, IMessaging messageHandler, string arg, out string directory)
{
directory = null;
if (File.Exists(arg))
{
this.Messaging.Write(ErrorMessages.ExpectedDirectoryGotFile(commandlineSwitch, arg));
return false;
}
directory = this.VerifyPath(arg);
return directory != null;
}
private bool TryGetFile(string commandlineSwitch, string arg, out string path)
{
path = null;
if (!IsValidArg(arg))
{
this.Messaging.Write(ErrorMessages.FilePathRequired(commandlineSwitch));
}
else if (Directory.Exists(arg))
{
this.Messaging.Write(ErrorMessages.ExpectedFileGotDirectory(commandlineSwitch, arg));
}
else
{
path = this.VerifyPath(arg);
}
return path != null;
}
/// <summary>
/// Get a set of files that possibly have a search pattern in the path (such as '*').
/// </summary>
/// <param name="searchPath">Search path to find files in.</param>
/// <param name="fileType">Type of file; typically "Source".</param>
/// <returns>An array of files matching the search path.</returns>
/// <remarks>
/// This method is written in this verbose way because it needs to support ".." in the path.
/// It needs the directory path isolated from the file name in order to use Directory.GetFiles
/// or DirectoryInfo.GetFiles. The only way to get this directory path is manually since
/// Path.GetDirectoryName does not support ".." in the path.
/// </remarks>
/// <exception cref="WixFileNotFoundException">Throws WixFileNotFoundException if no file matching the pattern can be found.</exception>
private string[] GetFiles(string searchPath, string fileType)
{
if (null == searchPath)
{
throw new ArgumentNullException(nameof(searchPath));
}
// Convert alternate directory separators to the standard one.
string filePath = searchPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
int lastSeparator = filePath.LastIndexOf(Path.DirectorySeparatorChar);
var files = new string[0];
try
{
if (0 > lastSeparator)
{
files = Directory.GetFiles(".", filePath);
}
else // found directory separator
{
files = Directory.GetFiles(filePath.Substring(0, lastSeparator + 1), filePath.Substring(lastSeparator + 1));
}
}
catch (DirectoryNotFoundException)
{
// Don't let this function throw the DirectoryNotFoundException. This exception
// occurs for non-existant directories and invalid characters in the searchPattern.
}
catch (ArgumentException)
{
// Don't let this function throw the ArgumentException. This exception
// occurs in certain situations such as when passing a malformed UNC path.
}
catch (IOException)
{
}
if (0 == files.Length)
{
this.Messaging.Write(ErrorMessages.FileNotFound(null, searchPath, fileType));
}
return files;
}
private string VerifyPath(string path)
{
string fullPath;
if (0 <= path.IndexOf('\"'))
{
this.Messaging.Write(ErrorMessages.PathCannotContainQuote(path));
return null;
}
try
{
fullPath = Path.GetFullPath(path);
}
catch (Exception e)
{
this.Messaging.Write(ErrorMessages.InvalidCommandLineFileName(path, e.Message));
return null;
}
return fullPath;
}
}
}
|