diff options
Diffstat (limited to 'src/WixToolset.Core/CommandLine/CommandLineHelper.cs')
-rw-r--r-- | src/WixToolset.Core/CommandLine/CommandLineHelper.cs | 216 |
1 files changed, 0 insertions, 216 deletions
diff --git a/src/WixToolset.Core/CommandLine/CommandLineHelper.cs b/src/WixToolset.Core/CommandLine/CommandLineHelper.cs deleted file mode 100644 index 51ece0f7..00000000 --- a/src/WixToolset.Core/CommandLine/CommandLineHelper.cs +++ /dev/null | |||
@@ -1,216 +0,0 @@ | |||
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.IO; | ||
7 | using WixToolset.Data; | ||
8 | using WixToolset.Extensibility.Services; | ||
9 | |||
10 | public class CommandLineHelper | ||
11 | { | ||
12 | /// <summary> | ||
13 | /// Validates that a string is a valid directory name, and throws appropriate warnings/errors if not | ||
14 | /// </summary> | ||
15 | /// <param name="commandlineSwitch">The commandline switch we're parsing (for error display purposes).</param> | ||
16 | /// <param name="messageHandler">The messagehandler to report warnings/errors to.</param> | ||
17 | /// <param name="args">The list of strings to check.</param> | ||
18 | /// <param name="index">The index (in args) of the commandline parameter to be parsed.</param> | ||
19 | /// <returns>The string if it is valid, null if it is invalid.</returns> | ||
20 | public static string GetDirectory(string commandlineSwitch, IMessaging messageHandler, string[] args, int index) | ||
21 | { | ||
22 | return GetDirectory(commandlineSwitch, messageHandler, args, index, false); | ||
23 | } | ||
24 | |||
25 | /// <summary> | ||
26 | /// Validates that a string is a valid directory name, and throws appropriate warnings/errors if not | ||
27 | /// </summary> | ||
28 | /// <param name="commandlineSwitch">The commandline switch we're parsing (for error display purposes).</param> | ||
29 | /// <param name="messageHandler">The messagehandler to report warnings/errors to.</param> | ||
30 | /// <param name="args">The list of strings to check.</param> | ||
31 | /// <param name="index">The index (in args) of the commandline parameter to be parsed.</param> | ||
32 | /// <param name="allowPrefix">Indicates if a colon-delimited prefix is allowed.</param> | ||
33 | /// <returns>The string if it is valid, null if it is invalid.</returns> | ||
34 | public static string GetDirectory(string commandlineSwitch, IMessaging messageHandler, string[] args, int index, bool allowPrefix) | ||
35 | { | ||
36 | commandlineSwitch = String.Concat("-", commandlineSwitch); | ||
37 | |||
38 | if (!IsValidArg(args, index)) | ||
39 | { | ||
40 | messageHandler.Write(ErrorMessages.DirectoryPathRequired(commandlineSwitch)); | ||
41 | return null; | ||
42 | } | ||
43 | |||
44 | if (File.Exists(args[index])) | ||
45 | { | ||
46 | messageHandler.Write(ErrorMessages.ExpectedDirectoryGotFile(commandlineSwitch, args[index])); | ||
47 | return null; | ||
48 | } | ||
49 | |||
50 | return VerifyPath(messageHandler, args[index], allowPrefix); | ||
51 | } | ||
52 | |||
53 | /// <summary> | ||
54 | /// Validates that a string is a valid filename, and throws appropriate warnings/errors if not | ||
55 | /// </summary> | ||
56 | /// <param name="commandlineSwitch">The commandline switch we're parsing (for error display purposes).</param> | ||
57 | /// <param name="messageHandler">The messagehandler to report warnings/errors to.</param> | ||
58 | /// <param name="args">The list of strings to check.</param> | ||
59 | /// <param name="index">The index (in args) of the commandline parameter to be parsed.</param> | ||
60 | /// <returns>The string if it is valid, null if it is invalid.</returns> | ||
61 | public static string GetFile(string commandlineSwitch, IMessaging messageHandler, string[] args, int index) | ||
62 | { | ||
63 | commandlineSwitch = String.Concat("-", commandlineSwitch); | ||
64 | |||
65 | if (!IsValidArg(args, index)) | ||
66 | { | ||
67 | messageHandler.Write(ErrorMessages.FilePathRequired(commandlineSwitch)); | ||
68 | return null; | ||
69 | } | ||
70 | |||
71 | if (Directory.Exists(args[index])) | ||
72 | { | ||
73 | messageHandler.Write(ErrorMessages.ExpectedFileGotDirectory(commandlineSwitch, args[index])); | ||
74 | return null; | ||
75 | } | ||
76 | |||
77 | return VerifyPath(messageHandler, args[index]); | ||
78 | } | ||
79 | |||
80 | /// <summary> | ||
81 | /// Get a set of files that possibly have a search pattern in the path (such as '*'). | ||
82 | /// </summary> | ||
83 | /// <param name="searchPath">Search path to find files in.</param> | ||
84 | /// <param name="fileType">Type of file; typically "Source".</param> | ||
85 | /// <returns>An array of files matching the search path.</returns> | ||
86 | /// <remarks> | ||
87 | /// This method is written in this verbose way because it needs to support ".." in the path. | ||
88 | /// It needs the directory path isolated from the file name in order to use Directory.GetFiles | ||
89 | /// or DirectoryInfo.GetFiles. The only way to get this directory path is manually since | ||
90 | /// Path.GetDirectoryName does not support ".." in the path. | ||
91 | /// </remarks> | ||
92 | /// <exception cref="WixFileNotFoundException">Throws WixFileNotFoundException if no file matching the pattern can be found.</exception> | ||
93 | public static string[] GetFiles(string searchPath, string fileType) | ||
94 | { | ||
95 | if (null == searchPath) | ||
96 | { | ||
97 | throw new ArgumentNullException(nameof(searchPath)); | ||
98 | } | ||
99 | |||
100 | // Convert alternate directory separators to the standard one. | ||
101 | string filePath = searchPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); | ||
102 | int lastSeparator = filePath.LastIndexOf(Path.DirectorySeparatorChar); | ||
103 | string[] files = null; | ||
104 | |||
105 | try | ||
106 | { | ||
107 | if (0 > lastSeparator) | ||
108 | { | ||
109 | files = Directory.GetFiles(".", filePath); | ||
110 | } | ||
111 | else // found directory separator | ||
112 | { | ||
113 | files = Directory.GetFiles(filePath.Substring(0, lastSeparator + 1), filePath.Substring(lastSeparator + 1)); | ||
114 | } | ||
115 | } | ||
116 | catch (DirectoryNotFoundException) | ||
117 | { | ||
118 | // Don't let this function throw the DirectoryNotFoundException. This exception | ||
119 | // occurs for non-existant directories and invalid characters in the searchPattern. | ||
120 | } | ||
121 | catch (ArgumentException) | ||
122 | { | ||
123 | // Don't let this function throw the ArgumentException. This exception | ||
124 | // occurs in certain situations such as when passing a malformed UNC path. | ||
125 | } | ||
126 | catch (IOException) | ||
127 | { | ||
128 | throw new WixFileNotFoundException(searchPath, fileType); | ||
129 | } | ||
130 | |||
131 | if (null == files || 0 == files.Length) | ||
132 | { | ||
133 | throw new WixFileNotFoundException(searchPath, fileType); | ||
134 | } | ||
135 | |||
136 | return files; | ||
137 | } | ||
138 | |||
139 | /// <summary> | ||
140 | /// Validates that a valid string parameter (without "/" or "-"), and returns a bool indicating its validity | ||
141 | /// </summary> | ||
142 | /// <param name="args">The list of strings to check.</param> | ||
143 | /// <param name="index">The index (in args) of the commandline parameter to be validated.</param> | ||
144 | /// <returns>True if a valid string parameter exists there, false if not.</returns> | ||
145 | public static bool IsValidArg(string[] args, int index) | ||
146 | { | ||
147 | if (args.Length <= index || String.IsNullOrEmpty(args[index]) || '/' == args[index][0] || '-' == args[index][0]) | ||
148 | { | ||
149 | return false; | ||
150 | } | ||
151 | else | ||
152 | { | ||
153 | return true; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | /// <summary> | ||
158 | /// Validates that a commandline parameter is a valid file or directory name, and throws appropriate warnings/errors if not | ||
159 | /// </summary> | ||
160 | /// <param name="messageHandler">The messagehandler to report warnings/errors to.</param> | ||
161 | /// <param name="path">The path to test.</param> | ||
162 | /// <returns>The string if it is valid, null if it is invalid.</returns> | ||
163 | public static string VerifyPath(IMessaging messageHandler, string path) | ||
164 | { | ||
165 | return VerifyPath(messageHandler, path, false); | ||
166 | } | ||
167 | |||
168 | /// <summary> | ||
169 | /// Validates that a commandline parameter is a valid file or directory name, and throws appropriate warnings/errors if not | ||
170 | /// </summary> | ||
171 | /// <param name="messageHandler">The messagehandler to report warnings/errors to.</param> | ||
172 | /// <param name="path">The path to test.</param> | ||
173 | /// <param name="allowPrefix">Indicates if a colon-delimited prefix is allowed.</param> | ||
174 | /// <returns>The full path if it is valid, null if it is invalid.</returns> | ||
175 | public static string VerifyPath(IMessaging messageHandler, string path, bool allowPrefix) | ||
176 | { | ||
177 | string fullPath; | ||
178 | |||
179 | if (0 <= path.IndexOf('\"')) | ||
180 | { | ||
181 | messageHandler.Write(ErrorMessages.PathCannotContainQuote(path)); | ||
182 | return null; | ||
183 | } | ||
184 | |||
185 | try | ||
186 | { | ||
187 | string prefix = null; | ||
188 | if (allowPrefix) | ||
189 | { | ||
190 | int prefixLength = path.IndexOf('=') + 1; | ||
191 | if (0 != prefixLength) | ||
192 | { | ||
193 | prefix = path.Substring(0, prefixLength); | ||
194 | path = path.Substring(prefixLength); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | if (String.IsNullOrEmpty(prefix)) | ||
199 | { | ||
200 | fullPath = Path.GetFullPath(path); | ||
201 | } | ||
202 | else | ||
203 | { | ||
204 | fullPath = String.Concat(prefix, Path.GetFullPath(path)); | ||
205 | } | ||
206 | } | ||
207 | catch (Exception e) | ||
208 | { | ||
209 | messageHandler.Write(ErrorMessages.InvalidCommandLineFileName(path, e.Message)); | ||
210 | return null; | ||
211 | } | ||
212 | |||
213 | return fullPath; | ||
214 | } | ||
215 | } | ||
216 | } | ||