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