/// <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> /// 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); } } }