| 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; |
| 7 | using System.Collections.Specialized; |
| 8 | using System.IO; |
| 9 | using WixToolset.Harvesters.Extensibility; |
| 10 | using IIs = Serialize.IIs; |
| 11 | using Wix = WixToolset.Harvesters.Serialize; |
| 12 | |
| 13 | /// <summary> |
| 14 | /// The harvester mutator for the WiX Toolset Internet Information Services Extension. |
| 15 | /// </summary> |
| 16 | public sealed class IIsHarvesterMutator : BaseMutatorExtension |
| 17 | { |
| 18 | private ArrayList components; |
| 19 | private DirectoryHarvester directoryHarvester; |
| 20 | private Hashtable directoryPaths; |
| 21 | private FileHarvester fileHarvester; |
| 22 | private Wix.IParentElement rootElement; |
| 23 | private bool setUniqueIdentifiers; |
| 24 | private ArrayList webAddresses; |
| 25 | private ArrayList webDirs; |
| 26 | private ArrayList webDirProperties; |
| 27 | private ArrayList webFilters; |
| 28 | private ArrayList webSites; |
| 29 | private ArrayList webVirtualDirs; |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Instantiate a new IIsHarvesterMutator. |
| 33 | /// </summary> |
| 34 | public IIsHarvesterMutator() |
| 35 | { |
| 36 | this.components = new ArrayList(); |
| 37 | this.directoryHarvester = new DirectoryHarvester(); |
| 38 | this.directoryPaths = CollectionsUtil.CreateCaseInsensitiveHashtable(); |
| 39 | this.fileHarvester = new FileHarvester(); |
| 40 | this.webAddresses = new ArrayList(); |
| 41 | this.webDirs = new ArrayList(); |
| 42 | this.webDirProperties = new ArrayList(); |
| 43 | this.webFilters = new ArrayList(); |
| 44 | this.webSites = new ArrayList(); |
| 45 | this.webVirtualDirs = new ArrayList(); |
| 46 | } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Gets the sequence of this mutator extension. |
| 50 | /// </summary> |
| 51 | /// <value>The sequence of this mutator extension.</value> |
| 52 | public override int Sequence |
| 53 | { |
| 54 | get { return 100; } |
| 55 | } |
| 56 | |
| 57 | /// <summary> |
| 58 | /// Gets of sets the option to set unique identifiers. |
| 59 | /// </summary> |
| 60 | /// <value>The option to set unique identifiers.</value> |
| 61 | public bool SetUniqueIdentifiers |
| 62 | { |
| 63 | get { return this.setUniqueIdentifiers; } |
| 64 | set { this.setUniqueIdentifiers = value; } |
| 65 | } |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Mutate a WiX document. |
| 69 | /// </summary> |
| 70 | /// <param name="wix">The Wix document element.</param> |
| 71 | public override void Mutate(Wix.Wix wix) |
| 72 | { |
| 73 | this.components.Clear(); |
| 74 | this.directoryPaths.Clear(); |
| 75 | this.webAddresses.Clear(); |
| 76 | this.webDirs.Clear(); |
| 77 | this.webDirProperties.Clear(); |
| 78 | this.webFilters.Clear(); |
| 79 | this.webSites.Clear(); |
| 80 | this.webVirtualDirs.Clear(); |
| 81 | this.rootElement = null; |
| 82 | |
| 83 | this.IndexElement(wix); |
| 84 | |
| 85 | this.MutateWebAddresses(); |
| 86 | |
| 87 | this.MutateWebDirs(); |
| 88 | |
| 89 | this.MutateWebDirProperties(); |
| 90 | |
| 91 | this.MutateWebSites(); |
| 92 | |
| 93 | this.MutateWebVirtualDirs(); |
| 94 | |
| 95 | // this must come after the web virtual dirs in case they harvest a directory containing a web filter file |
| 96 | this.MutateWebFilters(); |
| 97 | |
| 98 | // this must come after the web site identifiers are created |
| 99 | this.MutateComponents(); |
| 100 | } |
| 101 | |
| 102 | /// <summary> |
| 103 | /// Harvest a new directory or return one that was previously harvested. |
| 104 | /// </summary> |
| 105 | /// <param name="path">The path of the directory.</param> |
| 106 | /// <param name="harvestChildren">The option to harvest the children of the directory.</param> |
| 107 | /// <returns>The harvested directory.</returns> |
| 108 | private Wix.Directory HarvestUniqueDirectory(string path, bool harvestChildren) |
| 109 | { |
| 110 | if (this.directoryPaths.Contains(path)) |
| 111 | { |
| 112 | return (Wix.Directory)this.directoryPaths[path]; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | Wix.Directory directory = this.directoryHarvester.HarvestDirectory(path, harvestChildren); |
| 117 | |
| 118 | this.rootElement.AddChild(directory); |
| 119 | |
| 120 | // index this new directory and all of its children |
| 121 | this.IndexElement(directory); |
| 122 | |
| 123 | return directory; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// <summary> |
| 128 | /// Index an element. |
| 129 | /// </summary> |
| 130 | /// <param name="element">The element to index.</param> |
| 131 | private void IndexElement(Wix.ISchemaElement element) |
| 132 | { |
| 133 | if (element is IIs.WebAddress) |
| 134 | { |
| 135 | this.webAddresses.Add(element); |
| 136 | } |
| 137 | else if (element is IIs.WebDir) |
| 138 | { |
| 139 | this.webDirs.Add(element); |
| 140 | } |
| 141 | else if (element is IIs.WebDirProperties) |
| 142 | { |
| 143 | this.webDirProperties.Add(element); |
| 144 | } |
| 145 | else if (element is IIs.WebFilter) |
| 146 | { |
| 147 | this.webFilters.Add(element); |
| 148 | } |
| 149 | else if (element is IIs.WebSite) |
| 150 | { |
| 151 | this.webSites.Add(element); |
| 152 | } |
| 153 | else if (element is IIs.WebVirtualDir) |
| 154 | { |
| 155 | this.webVirtualDirs.Add(element); |
| 156 | } |
| 157 | else if (element is Wix.Component) |
| 158 | { |
| 159 | this.components.Add(element); |
| 160 | } |
| 161 | else if (element is Wix.Directory) |
| 162 | { |
| 163 | Wix.Directory directory = (Wix.Directory)element; |
| 164 | |
| 165 | if (null != directory.FileSource) |
| 166 | { |
| 167 | this.directoryPaths.Add(directory.FileSource, directory); |
| 168 | } |
| 169 | } |
| 170 | else if (element is Wix.Fragment || element is Wix.Module || element is Wix.PatchCreation || element is Wix.Package) |
| 171 | { |
| 172 | this.rootElement = (Wix.IParentElement)element; |
| 173 | } |
| 174 | |
| 175 | // index the child elements |
| 176 | if (element is Wix.IParentElement) |
| 177 | { |
| 178 | foreach (Wix.ISchemaElement childElement in ((Wix.IParentElement)element).Children) |
| 179 | { |
| 180 | this.IndexElement(childElement); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// <summary> |
| 186 | /// Mutate the Component elements. |
| 187 | /// </summary> |
| 188 | private void MutateComponents() |
| 189 | { |
| 190 | if (this.setUniqueIdentifiers) |
| 191 | { |
| 192 | IdentifierGenerator identifierGenerator = new IdentifierGenerator("Component", this.Core); |
| 193 | |
| 194 | // index all the existing identifiers |
| 195 | foreach (Wix.Component component in this.components) |
| 196 | { |
| 197 | if (null != component.Id) |
| 198 | { |
| 199 | identifierGenerator.IndexExistingIdentifier(component.Id); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // index all the web site identifiers |
| 204 | foreach (IIs.WebSite webSite in this.webSites) |
| 205 | { |
| 206 | if (webSite.ParentElement is Wix.Component) |
| 207 | { |
| 208 | identifierGenerator.IndexName(webSite.Id); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // create an identifier for each component based on its child web site identifier |
| 213 | foreach (IIs.WebSite webSite in this.webSites) |
| 214 | { |
| 215 | Wix.Component component = webSite.ParentElement as Wix.Component; |
| 216 | |
| 217 | if (null != component) |
| 218 | { |
| 219 | component.Id = identifierGenerator.GetIdentifier(webSite.Id); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /// <summary> |
| 226 | /// Mutate the WebAddress elements. |
| 227 | /// </summary> |
| 228 | private void MutateWebAddresses() |
| 229 | { |
| 230 | if (this.setUniqueIdentifiers) |
| 231 | { |
| 232 | IdentifierGenerator identifierGenerator = new IdentifierGenerator("WebAddress", this.Core); |
| 233 | |
| 234 | // index all the existing identifiers and names |
| 235 | foreach (IIs.WebAddress webAddress in this.webAddresses) |
| 236 | { |
| 237 | if (null != webAddress.Id) |
| 238 | { |
| 239 | identifierGenerator.IndexExistingIdentifier(webAddress.Id); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | identifierGenerator.IndexName(String.Concat(webAddress.IP, "_", webAddress.Port)); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | foreach (IIs.WebAddress webAddress in this.webAddresses) |
| 248 | { |
| 249 | if (null == webAddress.Id) |
| 250 | { |
| 251 | webAddress.Id = identifierGenerator.GetIdentifier(String.Concat(webAddress.IP, "_", webAddress.Port)); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /// <summary> |
| 258 | /// Mutate the WebDir elements. |
| 259 | /// </summary> |
| 260 | private void MutateWebDirs() |
| 261 | { |
| 262 | if (this.setUniqueIdentifiers) |
| 263 | { |
| 264 | IdentifierGenerator identifierGenerator = new IdentifierGenerator("WebDir", this.Core); |
| 265 | |
| 266 | // index all the existing identifiers and names |
| 267 | foreach (IIs.WebDir webDir in this.webDirs) |
| 268 | { |
| 269 | if (null != webDir.Id) |
| 270 | { |
| 271 | identifierGenerator.IndexExistingIdentifier(webDir.Id); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | identifierGenerator.IndexName(webDir.Path); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | foreach (IIs.WebDir webDir in this.webDirs) |
| 280 | { |
| 281 | if (null == webDir.Id) |
| 282 | { |
| 283 | webDir.Id = identifierGenerator.GetIdentifier(webDir.Path); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// <summary> |
| 290 | /// Mutate the WebDirProperties elements. |
| 291 | /// </summary> |
| 292 | private void MutateWebDirProperties() |
| 293 | { |
| 294 | if (this.setUniqueIdentifiers) |
| 295 | { |
| 296 | IdentifierGenerator identifierGenerator = new IdentifierGenerator("WebDirProperties", this.Core); |
| 297 | |
| 298 | // index all the existing identifiers and names |
| 299 | foreach (IIs.WebDirProperties webDirProperties in this.webDirProperties) |
| 300 | { |
| 301 | if (null != webDirProperties.Id) |
| 302 | { |
| 303 | identifierGenerator.IndexExistingIdentifier(webDirProperties.Id); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | foreach (IIs.WebDirProperties webDirProperties in this.webDirProperties) |
| 308 | { |
| 309 | if (null == webDirProperties.Id) |
| 310 | { |
| 311 | webDirProperties.Id = identifierGenerator.GetIdentifier(String.Empty); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /// <summary> |
| 318 | /// Mutate the WebFilter elements. |
| 319 | /// </summary> |
| 320 | private void MutateWebFilters() |
| 321 | { |
| 322 | IdentifierGenerator identifierGenerator = null; |
| 323 | |
| 324 | if (this.setUniqueIdentifiers) |
| 325 | { |
| 326 | identifierGenerator = new IdentifierGenerator("WebFilter", this.Core); |
| 327 | |
| 328 | // index all the existing identifiers and names |
| 329 | foreach (IIs.WebFilter webFilter in this.webFilters) |
| 330 | { |
| 331 | if (null != webFilter.Id) |
| 332 | { |
| 333 | identifierGenerator.IndexExistingIdentifier(webFilter.Id); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | identifierGenerator.IndexName(webFilter.Name); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | foreach (IIs.WebFilter webFilter in this.webFilters) |
| 343 | { |
| 344 | if (this.setUniqueIdentifiers && null == webFilter.Id) |
| 345 | { |
| 346 | webFilter.Id = identifierGenerator.GetIdentifier(webFilter.Name); |
| 347 | } |
| 348 | |
| 349 | // harvest the file for this WebFilter |
| 350 | Wix.Directory directory = this.HarvestUniqueDirectory(Path.GetDirectoryName(webFilter.Path), false); |
| 351 | |
| 352 | Wix.Component component = new Wix.Component(); |
| 353 | directory.AddChild(component); |
| 354 | |
| 355 | Wix.File file = this.fileHarvester.HarvestFile(webFilter.Path); |
| 356 | component.AddChild(file); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /// <summary> |
| 361 | /// Mutate the WebSite elements. |
| 362 | /// </summary> |
| 363 | private void MutateWebSites() |
| 364 | { |
| 365 | if (this.setUniqueIdentifiers) |
| 366 | { |
| 367 | IdentifierGenerator identifierGenerator = new IdentifierGenerator("WebSite", this.Core); |
| 368 | |
| 369 | // index all the existing identifiers and names |
| 370 | foreach (IIs.WebSite webSite in this.webSites) |
| 371 | { |
| 372 | if (null != webSite.Id) |
| 373 | { |
| 374 | identifierGenerator.IndexExistingIdentifier(webSite.Id); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | identifierGenerator.IndexName(webSite.Description); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | foreach (IIs.WebSite webSite in this.webSites) |
| 383 | { |
| 384 | if (null == webSite.Id) |
| 385 | { |
| 386 | webSite.Id = identifierGenerator.GetIdentifier(webSite.Description); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /// <summary> |
| 393 | /// Mutate the WebVirtualDir elements. |
| 394 | /// </summary> |
| 395 | private void MutateWebVirtualDirs() |
| 396 | { |
| 397 | IdentifierGenerator identifierGenerator = null; |
| 398 | |
| 399 | if (this.setUniqueIdentifiers) |
| 400 | { |
| 401 | identifierGenerator = new IdentifierGenerator("WebVirtualDir", this.Core); |
| 402 | |
| 403 | // index all the existing identifiers and names |
| 404 | foreach (IIs.WebVirtualDir webVirtualDir in this.webVirtualDirs) |
| 405 | { |
| 406 | if (null != webVirtualDir.Id) |
| 407 | { |
| 408 | identifierGenerator.IndexExistingIdentifier(webVirtualDir.Id); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | identifierGenerator.IndexName(webVirtualDir.Alias); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | foreach (IIs.WebVirtualDir webVirtualDir in this.webVirtualDirs) |
| 418 | { |
| 419 | if (this.setUniqueIdentifiers && null == webVirtualDir.Id) |
| 420 | { |
| 421 | webVirtualDir.Id = identifierGenerator.GetIdentifier(webVirtualDir.Alias); |
| 422 | } |
| 423 | |
| 424 | // harvest the directory for this WebVirtualDir |
| 425 | this.HarvestUniqueDirectory(webVirtualDir.Directory, true); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |