/// <summary> /// Mutate the components. /// </summary> private void MutateComponents() { IdentifierGenerator identifierGenerator = new IdentifierGenerator("Component", this.Core); if (TemplateType.Module == this.templateType) { identifierGenerator.MaxIdentifierLength = IdentifierGenerator.MaxModuleIdentifierLength; } foreach (Wix.Component component in this.components) { if (null == component.Id) { string firstFileId = string.Empty; // attempt to create a possible identifier from the first file identifier in the component foreach (Wix.File file in component[typeof(Wix.File)]) { firstFileId = file.Id; break; } if (string.IsNullOrEmpty(firstFileId)) { firstFileId = this.GetGuid(); } component.Id = identifierGenerator.GetIdentifier(firstFileId); } if (null == component.Guid) { if (this.AutogenerateGuids) { component.Guid = "*"; } else { component.Guid = this.GetGuid(); } } if (this.createFragments && component.ParentElement is Wix.Directory) { Wix.Directory directory = (Wix.Directory)component.ParentElement; // parent directory must have an identifier to create a reference to it if (null == directory.Id) { break; } if (this.rootElement is Wix.Module) { // add a ComponentRef for the Component Wix.ComponentRef componentRef = new Wix.ComponentRef(); componentRef.Id = component.Id; this.rootElement.AddChild(componentRef); } // create a new Fragment Wix.Fragment fragment = new Wix.Fragment(); this.fragments.Add(String.Concat("Component:", (null != component.Id ? component.Id : this.fragments.Count.ToString())), fragment); // create a new DirectoryRef Wix.DirectoryRef directoryRef = new Wix.DirectoryRef(); directoryRef.Id = directory.Id; fragment.AddChild(directoryRef); // move the Component from the the Directory to the DirectoryRef directory.RemoveChild(component); directoryRef.AddChild(component); } } }
/// <summary> /// Mutate the directories. /// </summary> private void MutateDirectories() { if (!this.setUniqueIdentifiers) { // assign all identifiers before fragmenting (because fragmenting requires them all to be present) IdentifierGenerator identifierGenerator = new IdentifierGenerator("Directory", this.Core); if (TemplateType.Module == this.templateType) { identifierGenerator.MaxIdentifierLength = IdentifierGenerator.MaxModuleIdentifierLength; } foreach (Wix.Directory directory in this.directories) { if (null == directory.Id) { directory.Id = identifierGenerator.GetIdentifier(directory.Name); } } } if (this.createFragments) { foreach (Wix.Directory directory in this.directories) { if (directory.ParentElement is Wix.Directory) { Wix.Directory parentDirectory = (Wix.Directory)directory.ParentElement; // parent directory must have an identifier to create a reference to it if (null == parentDirectory.Id) { return; } // create a new Fragment Wix.Fragment fragment = new Wix.Fragment(); this.fragments.Add(String.Concat("Directory:", ("TARGETDIR" == directory.Id ? null : (null != directory.Id ? directory.Id : this.fragments.Count.ToString()))), fragment); // create a new DirectoryRef Wix.DirectoryRef directoryRef = new Wix.DirectoryRef(); directoryRef.Id = parentDirectory.Id; fragment.AddChild(directoryRef); // move the Directory from the parent Directory to DirectoryRef parentDirectory.RemoveChild(directory); directoryRef.AddChild(directory); } else if (directory.ParentElement is Wix.Fragment) { // When creating fragments, remove any top-level Directory elements; // the fragments should be pulled in by their DirectoryRefs instead. Wix.Fragment parent = (Wix.Fragment)directory.ParentElement; parent.RemoveChild(directory); // Remove the fragment if it is empty. if (parent.Children.GetEnumerator().Current == null && parent.ParentElement != null) { ((Wix.IParentElement)parent.ParentElement).RemoveChild(parent); } } else if (directory.ParentElement == this.rootElement) { // create a new Fragment Wix.Fragment fragment = new Wix.Fragment(); this.fragments.Add(String.Concat("Directory:", ("TARGETDIR" == directory.Id ? null : (null != directory.Id ? directory.Id : this.fragments.Count.ToString()))), fragment); // move the Directory from the root element to the Fragment this.rootElement.RemoveChild(directory); fragment.AddChild(directory); } } } }
/// <summary> /// Creates a component group with a given name. /// </summary> /// <param name="wix">The Wix document element.</param> private void CreateComponentGroup(Wix.Wix wix) { Wix.ComponentGroup componentGroup = new Wix.ComponentGroup(); componentGroup.Id = this.componentGroupName; this.componentGroups.Add(componentGroup); Wix.Fragment cgFragment = new Wix.Fragment(); cgFragment.AddChild(componentGroup); wix.AddChild(cgFragment); int componentCount = 0; for (; componentCount < this.components.Count; componentCount++) { Wix.Component c = this.components[componentCount] as Wix.Component; if (this.createFragments) { if (c.ParentElement is Wix.Directory) { Wix.Directory parentDirectory = c.ParentElement as Wix.Directory; componentGroup.AddChild(c); c.Directory = parentDirectory.Id; parentDirectory.RemoveChild(c); } else if (c.ParentElement is Wix.DirectoryRef) { Wix.DirectoryRef parentDirectory = c.ParentElement as Wix.DirectoryRef; componentGroup.AddChild(c); c.Directory = parentDirectory.Id; parentDirectory.RemoveChild(c); // Remove whole fragment if moving the component to the component group just leaves an empty DirectoryRef if (0 < this.fragments.Count && parentDirectory.ParentElement is Wix.Fragment) { Wix.Fragment parentFragment = parentDirectory.ParentElement as Wix.Fragment; int childCount = 0; foreach (Wix.ISchemaElement element in parentFragment.Children) { childCount++; } // Component should always have an Id but the SortedList creation allows for null and bases the name on the fragment count which we cannot reverse engineer here. if (1 == childCount && !String.IsNullOrEmpty(c.Id)) { int removeIndex = this.fragments.IndexOfKey(String.Concat("Component:", c.Id)); if (0 <= removeIndex) { this.fragments.RemoveAt(removeIndex); } } } } } else { Wix.ComponentRef componentRef = new Wix.ComponentRef(); componentRef.Id = c.Id; componentGroup.AddChild(componentRef); } } }