| 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.Harvesters |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Harvesters.Data; |
| 10 | using WixToolset.Harvesters.Extensibility; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// A utility heat extension for the WiX Toolset Harvester application. |
| 14 | /// </summary> |
| 15 | public sealed class UtilHeatExtension : BaseHeatExtension |
| 16 | { |
| 17 | public UtilHeatExtension(IServiceProvider serviceProvider) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Gets the supported command line types for this extension. |
| 23 | /// </summary> |
| 24 | /// <value>The supported command line types for this extension.</value> |
| 25 | public override HeatCommandLineOption[] CommandLineTypes |
| 26 | { |
| 27 | get |
| 28 | { |
| 29 | return new HeatCommandLineOption[] |
| 30 | { |
| 31 | new HeatCommandLineOption("dir", "harvest a directory"), |
| 32 | new HeatCommandLineOption("file", "harvest a file"), |
| 33 | new HeatCommandLineOption("perf", "harvest performance counters"), |
| 34 | new HeatCommandLineOption("reg", "harvest a .reg file"), |
| 35 | new HeatCommandLineOption("-ag", "autogenerate component guids at compile time"), |
| 36 | new HeatCommandLineOption("-cg <ComponentGroupName>", "component group name (cannot contain spaces e.g -cg MyComponentGroup)"), |
| 37 | new HeatCommandLineOption("-dr <DirectoryName>", "directory reference to root directories (cannot contain spaces e.g. -dr MyAppDirRef)"), |
| 38 | new HeatCommandLineOption("-var <VariableName>", "substitute File/@Source=\"SourceDir\" with a preprocessor or a wix variable" + Environment.NewLine + |
| 39 | "(e.g. -var var.MySource will become File/@Source=\"$(var.MySource)\\myfile.txt\" and " + Environment.NewLine + |
| 40 | "-var wix.MySource will become File/@Source=\"!(wix.MySource)\\myfile.txt\""), |
| 41 | new HeatCommandLineOption("-gg", "generate guids now"), |
| 42 | new HeatCommandLineOption("-g1", "generated guids are not in brackets"), |
| 43 | new HeatCommandLineOption("-ke", "keep empty directories"), |
| 44 | new HeatCommandLineOption("-scom", "suppress COM elements"), |
| 45 | new HeatCommandLineOption("-sfrag", "suppress fragments"), |
| 46 | new HeatCommandLineOption("-srd", "suppress harvesting the root directory as an element"), |
| 47 | new HeatCommandLineOption("-svb6", "suppress VB6 COM elements"), |
| 48 | new HeatCommandLineOption("-sreg", "suppress registry harvesting"), |
| 49 | new HeatCommandLineOption("-suid", "suppress unique identifiers for files, components, & directories"), |
| 50 | new HeatCommandLineOption("-t", "transform harvested output with XSL file"), |
| 51 | new HeatCommandLineOption("-template", "use template, one of: fragment,module,product"), |
| 52 | }; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// <summary> |
| 57 | /// Parse the command line options for this extension. |
| 58 | /// </summary> |
| 59 | /// <param name="type">The active harvester type.</param> |
| 60 | /// <param name="args">The option arguments.</param> |
| 61 | public override void ParseOptions(string type, string[] args) |
| 62 | { |
| 63 | var active = false; |
| 64 | IHarvesterExtension harvesterExtension = null; |
| 65 | var suppressHarvestingRegistryValues = false; |
| 66 | var utilFinalizeHarvesterMutator = new UtilFinalizeHarvesterMutator(); |
| 67 | var utilMutator = new UtilMutator(); |
| 68 | var transformMutators = new List<UtilTransformMutator>(); |
| 69 | var generateType = GenerateType.Components; |
| 70 | |
| 71 | // select the harvester |
| 72 | switch (type) |
| 73 | { |
| 74 | case "dir": |
| 75 | harvesterExtension = new DirectoryHarvester(); |
| 76 | active = true; |
| 77 | break; |
| 78 | case "file": |
| 79 | harvesterExtension = new FileHarvester(); |
| 80 | active = true; |
| 81 | break; |
| 82 | case "perf": |
| 83 | harvesterExtension = new PerformanceCategoryHarvester(); |
| 84 | active = true; |
| 85 | break; |
| 86 | case "reg": |
| 87 | harvesterExtension = new RegFileHarvester(); |
| 88 | active = true; |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | // set default settings |
| 93 | utilMutator.CreateFragments = true; |
| 94 | utilMutator.SetUniqueIdentifiers = true; |
| 95 | |
| 96 | // parse the options |
| 97 | for (var i = 0; i < args.Length; i++) |
| 98 | { |
| 99 | var commandSwitch = args[i]; |
| 100 | |
| 101 | if (null == commandSwitch || 0 == commandSwitch.Length) // skip blank arguments |
| 102 | { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if ('-' == commandSwitch[0] || '/' == commandSwitch[0]) |
| 107 | { |
| 108 | var truncatedCommandSwitch = commandSwitch.Substring(1); |
| 109 | |
| 110 | if ("ag" == truncatedCommandSwitch) |
| 111 | { |
| 112 | utilMutator.AutogenerateGuids = true; |
| 113 | } |
| 114 | else if ("cg" == truncatedCommandSwitch) |
| 115 | { |
| 116 | utilMutator.ComponentGroupName = this.GetArgumentParameter(args, i); |
| 117 | |
| 118 | if (this.Core.Messaging.EncounteredError) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | else if ("dr" == truncatedCommandSwitch) |
| 124 | { |
| 125 | var dr = this.GetArgumentParameter(args, i); |
| 126 | |
| 127 | if (this.Core.Messaging.EncounteredError) |
| 128 | { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if (harvesterExtension is DirectoryHarvester directoryHarvester) |
| 133 | { |
| 134 | directoryHarvester.RootedDirectoryRef = dr; |
| 135 | } |
| 136 | else if (harvesterExtension is FileHarvester fileHarvester) |
| 137 | { |
| 138 | fileHarvester.RootedDirectoryRef = dr; |
| 139 | } |
| 140 | } |
| 141 | else if ("gg" == truncatedCommandSwitch) |
| 142 | { |
| 143 | utilMutator.GenerateGuids = true; |
| 144 | } |
| 145 | else if ("g1" == truncatedCommandSwitch) |
| 146 | { |
| 147 | utilMutator.GuidFormat = "D"; |
| 148 | } |
| 149 | else if ("ke" == truncatedCommandSwitch) |
| 150 | { |
| 151 | if (harvesterExtension is DirectoryHarvester) |
| 152 | { |
| 153 | ((DirectoryHarvester)harvesterExtension).KeepEmptyDirectories = true; |
| 154 | } |
| 155 | else if (active) |
| 156 | { |
| 157 | // TODO: error message - not applicable to file harvester |
| 158 | } |
| 159 | } |
| 160 | else if ("scom" == truncatedCommandSwitch) |
| 161 | { |
| 162 | if (active) |
| 163 | { |
| 164 | utilFinalizeHarvesterMutator.SuppressCOMElements = true; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | // TODO: error message - not applicable |
| 169 | } |
| 170 | } |
| 171 | else if ("svb6" == truncatedCommandSwitch) |
| 172 | { |
| 173 | if (active) |
| 174 | { |
| 175 | utilFinalizeHarvesterMutator.SuppressVB6COMElements = true; |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | // TODO: error message - not applicable |
| 180 | } |
| 181 | } |
| 182 | else if ("sfrag" == truncatedCommandSwitch) |
| 183 | { |
| 184 | utilMutator.CreateFragments = false; |
| 185 | } |
| 186 | else if ("srd" == truncatedCommandSwitch) |
| 187 | { |
| 188 | if (harvesterExtension is DirectoryHarvester) |
| 189 | { |
| 190 | ((DirectoryHarvester)harvesterExtension).SuppressRootDirectory = true; |
| 191 | } |
| 192 | else if (harvesterExtension is FileHarvester) |
| 193 | { |
| 194 | ((FileHarvester)harvesterExtension).SuppressRootDirectory = true; |
| 195 | } |
| 196 | } |
| 197 | else if ("sreg" == truncatedCommandSwitch) |
| 198 | { |
| 199 | suppressHarvestingRegistryValues = true; |
| 200 | } |
| 201 | else if ("suid" == truncatedCommandSwitch) |
| 202 | { |
| 203 | utilMutator.SetUniqueIdentifiers = false; |
| 204 | |
| 205 | if (harvesterExtension is DirectoryHarvester) |
| 206 | { |
| 207 | ((DirectoryHarvester)harvesterExtension).SetUniqueIdentifiers = false; |
| 208 | } |
| 209 | else if (harvesterExtension is FileHarvester) |
| 210 | { |
| 211 | ((FileHarvester)harvesterExtension).SetUniqueIdentifiers = false; |
| 212 | } |
| 213 | } |
| 214 | else if (truncatedCommandSwitch.StartsWith("t:", StringComparison.Ordinal) || "t" == truncatedCommandSwitch) |
| 215 | { |
| 216 | string xslFile; |
| 217 | if (truncatedCommandSwitch.StartsWith("t:", StringComparison.Ordinal)) |
| 218 | { |
| 219 | this.Core.Messaging.Write(WarningMessages.DeprecatedCommandLineSwitch("t:", "t")); |
| 220 | xslFile = truncatedCommandSwitch.Substring(2); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | xslFile = this.GetArgumentParameter(args, i, true); |
| 225 | } |
| 226 | |
| 227 | if (0 <= xslFile.IndexOf('\"')) |
| 228 | { |
| 229 | this.Core.Messaging.Write(ErrorMessages.PathCannotContainQuote(xslFile)); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | try |
| 234 | { |
| 235 | xslFile = Path.GetFullPath(xslFile); |
| 236 | } |
| 237 | catch (Exception e) |
| 238 | { |
| 239 | this.Core.Messaging.Write(ErrorMessages.InvalidCommandLineFileName(xslFile, e.Message)); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | transformMutators.Add(new UtilTransformMutator(xslFile, transformMutators.Count)); |
| 244 | } |
| 245 | else if (truncatedCommandSwitch.StartsWith("template:", StringComparison.Ordinal) || "template" == truncatedCommandSwitch) |
| 246 | { |
| 247 | string template; |
| 248 | if(truncatedCommandSwitch.StartsWith("template:", StringComparison.Ordinal)) |
| 249 | { |
| 250 | this.Core.Messaging.Write(WarningMessages.DeprecatedCommandLineSwitch("template:", "template")); |
| 251 | template = truncatedCommandSwitch.Substring(9); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | template = this.GetArgumentParameter(args, i); |
| 256 | } |
| 257 | |
| 258 | switch (template) |
| 259 | { |
| 260 | case "fragment": |
| 261 | utilMutator.TemplateType = TemplateType.Fragment; |
| 262 | break; |
| 263 | case "module": |
| 264 | utilMutator.TemplateType = TemplateType.Module; |
| 265 | break; |
| 266 | case "package": |
| 267 | case "product": |
| 268 | utilMutator.TemplateType = TemplateType.Package ; |
| 269 | break; |
| 270 | default: |
| 271 | // TODO: error |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | else if ("var" == truncatedCommandSwitch) |
| 276 | { |
| 277 | if (active) |
| 278 | { |
| 279 | utilFinalizeHarvesterMutator.PreprocessorVariable = this.GetArgumentParameter(args, i); |
| 280 | |
| 281 | if (this.Core.Messaging.EncounteredError) |
| 282 | { |
| 283 | return; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | else if ("generate" == truncatedCommandSwitch) |
| 288 | { |
| 289 | if (harvesterExtension is DirectoryHarvester) |
| 290 | { |
| 291 | var genType = this.GetArgumentParameter(args, i).ToUpperInvariant(); |
| 292 | switch (genType) |
| 293 | { |
| 294 | case "COMPONENTS": |
| 295 | generateType = GenerateType.Components; |
| 296 | break; |
| 297 | case "PAYLOADGROUP": |
| 298 | generateType = GenerateType.PayloadGroup; |
| 299 | break; |
| 300 | default: |
| 301 | throw new WixException(HarvesterErrors.InvalidDirectoryOutputType(genType)); |
| 302 | } |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | // TODO: error message - not applicable |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // set the appropriate harvester extension |
| 313 | if (active) |
| 314 | { |
| 315 | this.Core.Harvester.Extension = harvesterExtension; |
| 316 | |
| 317 | if (!suppressHarvestingRegistryValues) |
| 318 | { |
| 319 | this.Core.Mutator.AddExtension(new UtilHarvesterMutator()); |
| 320 | } |
| 321 | |
| 322 | this.Core.Mutator.AddExtension(utilFinalizeHarvesterMutator); |
| 323 | |
| 324 | if (harvesterExtension is DirectoryHarvester directoryHarvester) |
| 325 | { |
| 326 | directoryHarvester.GenerateType = generateType; |
| 327 | this.Core.Harvester.Core.RootDirectory = this.Core.Harvester.Core.ExtensionArgument; |
| 328 | } |
| 329 | else if (harvesterExtension is FileHarvester) |
| 330 | { |
| 331 | if (((FileHarvester)harvesterExtension).SuppressRootDirectory) |
| 332 | { |
| 333 | this.Core.Harvester.Core.RootDirectory = Path.GetDirectoryName(Path.GetFullPath(this.Core.Harvester.Core.ExtensionArgument)); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | this.Core.Harvester.Core.RootDirectory = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(this.Core.Harvester.Core.ExtensionArgument))); |
| 338 | |
| 339 | // GetDirectoryName() returns null for root paths such as "c:\", so make sure to support that as well |
| 340 | if (null == this.Core.Harvester.Core.RootDirectory) |
| 341 | { |
| 342 | this.Core.Harvester.Core.RootDirectory = Path.GetPathRoot(Path.GetDirectoryName(Path.GetFullPath(this.Core.Harvester.Core.ExtensionArgument))); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // set the mutator |
| 349 | this.Core.Mutator.AddExtension(utilMutator); |
| 350 | |
| 351 | // add the transforms |
| 352 | foreach (var transformMutator in transformMutators) |
| 353 | { |
| 354 | this.Core.Mutator.AddExtension(transformMutator); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private string GetArgumentParameter(string[] args, int index) |
| 359 | { |
| 360 | return this.GetArgumentParameter(args, index, false); |
| 361 | } |
| 362 | |
| 363 | private string GetArgumentParameter(string[] args, int index, bool allowSpaces) |
| 364 | { |
| 365 | var truncatedCommandSwitch = args[index]; |
| 366 | var commandSwitchValue = args[index + 1]; |
| 367 | |
| 368 | //increment the index to the switch value |
| 369 | index++; |
| 370 | |
| 371 | if (IsValidArg(args, index) && !String.IsNullOrEmpty(commandSwitchValue.Trim())) |
| 372 | { |
| 373 | if (!allowSpaces && commandSwitchValue.Contains(" ")) |
| 374 | { |
| 375 | this.Core.Messaging.Write(HarvesterErrors.SpacesNotAllowedInArgumentValue(truncatedCommandSwitch, commandSwitchValue)); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | return commandSwitchValue; |
| 380 | } |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | this.Core.Messaging.Write(HarvesterErrors.ArgumentRequiresValue(truncatedCommandSwitch)); |
| 385 | } |
| 386 | |
| 387 | return null; |
| 388 | } |
| 389 | } |
| 390 | } |