Ejemplo n.º 1
0
        /// <summary>
        /// Mutate a Wix element.
        /// </summary>
        /// <param name="wix">The Wix element to mutate.</param>
        private void MutateWix(Wix.Wix wix)
        {
            if (TemplateType.Fragment != this.templateType)
            {
                if (null != this.rootElement || 0 != this.features.Count)
                {
                    throw new Exception("The template option cannot be used with Feature, Product, or Module elements present.");
                }

                // create a package element although it won't always be used
                Wix.Package package = new Wix.Package();
                if (TemplateType.Module == this.templateType)
                {
                    package.Id = this.GetGuid();
                }
                else
                {
                    package.Compressed = Wix.YesNoType.yes;
                }

                package.InstallerVersion = 200;

                Wix.Directory targetDir = new Wix.Directory();
                targetDir.Id   = "TARGETDIR";
                targetDir.Name = "SourceDir";

                foreach (Wix.DirectoryRef directoryRef in this.directoryRefs)
                {
                    if (String.Equals(directoryRef.Id, "TARGETDIR", StringComparison.OrdinalIgnoreCase))
                    {
                        Wix.IParentElement parent = directoryRef.ParentElement as Wix.IParentElement;

                        foreach (Wix.ISchemaElement element in directoryRef.Children)
                        {
                            targetDir.AddChild(element);
                        }

                        parent.RemoveChild(directoryRef);

                        if (null != ((Wix.ISchemaElement)parent).ParentElement)
                        {
                            int i = 0;

                            foreach (Wix.ISchemaElement element in parent.Children)
                            {
                                i++;
                            }

                            if (0 == i)
                            {
                                Wix.IParentElement supParent = (Wix.IParentElement)((Wix.ISchemaElement)parent).ParentElement;
                                supParent.RemoveChild((Wix.ISchemaElement)parent);
                            }
                        }

                        break;
                    }
                }

                if (TemplateType.Module == this.templateType)
                {
                    Wix.Module module = new Wix.Module();
                    module.Id       = "PUT-MODULE-NAME-HERE";
                    module.Language = "1033";
                    module.Version  = "1.0.0.0";

                    package.Manufacturer = "PUT-COMPANY-NAME-HERE";
                    module.AddChild(package);
                    module.AddChild(targetDir);

                    wix.AddChild(module);
                    this.rootElement = module;
                }
                else // product
                {
                    Wix.Product product = new Wix.Product();
                    product.Id           = this.GetGuid();
                    product.Language     = "1033";
                    product.Manufacturer = "PUT-COMPANY-NAME-HERE";
                    product.Name         = "PUT-PRODUCT-NAME-HERE";
                    product.UpgradeCode  = this.GetGuid();
                    product.Version      = "1.0.0.0";
                    product.AddChild(package);
                    product.AddChild(targetDir);

                    Wix.Media media = new Wix.Media();
                    media.Id       = "1";
                    media.Cabinet  = "product.cab";
                    media.EmbedCab = Wix.YesNoType.yes;
                    product.AddChild(media);

                    Wix.Feature feature = new Wix.Feature();
                    feature.Id    = "ProductFeature";
                    feature.Title = "PUT-FEATURE-TITLE-HERE";
                    feature.Level = 1;
                    product.AddChild(feature);
                    this.features.Add(feature);

                    wix.AddChild(product);
                    this.rootElement = product;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decompile the Feature table.
        /// </summary>
        /// <param name="table">The table to decompile.</param>
        private void DecompileFeatureTable(Table table)
        {
            SortedList sortedFeatures = new SortedList();

            foreach (Row row in table.Rows)
            {
                Wix.Feature feature = new Wix.Feature();

                feature.Id = Convert.ToString(row[0]);

                if (null != row[2])
                {
                    feature.Title = Convert.ToString(row[2]);
                }

                if (null != row[3])
                {
                    feature.Description = Convert.ToString(row[3]);
                }

                if (null == row[4])
                {
                    feature.Display = "hidden";
                }
                else
                {
                    int display = Convert.ToInt32(row[4]);

                    if (0 == display)
                    {
                        feature.Display = "hidden";
                    }
                    else if (1 == display % 2)
                    {
                        feature.Display = "expand";
                    }
                }

                feature.Level = Convert.ToInt32(row[5]);

                if (null != row[6])
                {
                    feature.ConfigurableDirectory = Convert.ToString(row[6]);
                }

                int attributes = Convert.ToInt32(row[7]);

                if (MsiInterop.MsidbFeatureAttributesFavorSource == (attributes & MsiInterop.MsidbFeatureAttributesFavorSource) && MsiInterop.MsidbFeatureAttributesFollowParent == (attributes & MsiInterop.MsidbFeatureAttributesFollowParent))
                {
                    // TODO: display a warning for setting favor local and follow parent together
                }
                else if (MsiInterop.MsidbFeatureAttributesFavorSource == (attributes & MsiInterop.MsidbFeatureAttributesFavorSource))
                {
                    feature.InstallDefault = Wix.Feature.InstallDefaultType.source;
                }
                else if (MsiInterop.MsidbFeatureAttributesFollowParent == (attributes & MsiInterop.MsidbFeatureAttributesFollowParent))
                {
                    feature.InstallDefault = Wix.Feature.InstallDefaultType.followParent;
                }

                if (MsiInterop.MsidbFeatureAttributesFavorAdvertise == (attributes & MsiInterop.MsidbFeatureAttributesFavorAdvertise))
                {
                    feature.TypicalDefault = Wix.Feature.TypicalDefaultType.advertise;
                }

                if (MsiInterop.MsidbFeatureAttributesDisallowAdvertise == (attributes & MsiInterop.MsidbFeatureAttributesDisallowAdvertise) &&
                    MsiInterop.MsidbFeatureAttributesNoUnsupportedAdvertise == (attributes & MsiInterop.MsidbFeatureAttributesNoUnsupportedAdvertise))
                {
                    this.core.OnMessage(WixWarnings.InvalidAttributeCombination(row.SourceLineNumbers, "msidbFeatureAttributesDisallowAdvertise", "msidbFeatureAttributesNoUnsupportedAdvertise", "Feature.AllowAdvertiseType", "no"));
                    feature.AllowAdvertise = Wix.Feature.AllowAdvertiseType.no;
                }
                else if (MsiInterop.MsidbFeatureAttributesDisallowAdvertise == (attributes & MsiInterop.MsidbFeatureAttributesDisallowAdvertise))
                {
                    feature.AllowAdvertise = Wix.Feature.AllowAdvertiseType.no;
                }
                else if (MsiInterop.MsidbFeatureAttributesNoUnsupportedAdvertise == (attributes & MsiInterop.MsidbFeatureAttributesNoUnsupportedAdvertise))
                {
                    feature.AllowAdvertise = Wix.Feature.AllowAdvertiseType.system;
                }

                if (MsiInterop.MsidbFeatureAttributesUIDisallowAbsent == (attributes & MsiInterop.MsidbFeatureAttributesUIDisallowAbsent))
                {
                    feature.Absent = Wix.Feature.AbsentType.disallow;
                }

                this.core.IndexElement(row, feature);

                // sort the features by their display column (and append the identifier to ensure unique keys)
                sortedFeatures.Add(String.Format(CultureInfo.InvariantCulture, "{0:00000}|{1}", Convert.ToInt32(row[4], CultureInfo.InvariantCulture), row[0]), row);
            }

            // nest the features
            foreach (Row row in sortedFeatures.Values)
            {
                Wix.Feature feature = (Wix.Feature)this.core.GetIndexedElement("Feature", Convert.ToString(row[0]));

                if (null == row[1])
                {
                    this.core.RootElement.AddChild(feature);
                }
                else
                {
                    Wix.Feature parentFeature = (Wix.Feature)this.core.GetIndexedElement("Feature", Convert.ToString(row[1]));

                    if (null == parentFeature)
                    {
                        this.core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(DecompilerCore.PrimaryKeyDelimiter), "Feature_Parent", Convert.ToString(row[1]), "Feature"));
                    }
                    else if (parentFeature == feature)
                    {
                        // TODO: display a warning about self-nesting
                    }
                    else
                    {
                        parentFeature.AddChild(feature);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Mutate a WiX document.
        /// </summary>
        /// <param name="wix">The Wix document element.</param>
        public override void Mutate(Wix.Wix wix)
        {
            this.components.Clear();
            this.directories.Clear();
            this.directoryRefs.Clear();
            this.features.Clear();
            this.files.Clear();
            this.fragments.Clear();
            this.rootElement = null;

            // index elements in this wix document
            this.IndexElement(wix);

            this.MutateWix(wix);

            this.MutateFiles();

            this.MutateDirectories();

            this.MutateComponents();

            if (null != this.componentGroupName)
            {
                this.CreateComponentGroup(wix);
            }

            // add the components to the product feature after all the identifiers have been set
            if (TemplateType.Product == this.templateType)
            {
                Wix.Feature feature = (Wix.Feature) this.features[0];

                foreach (Wix.ComponentGroup group in this.componentGroups)
                {
                    Wix.ComponentGroupRef componentGroupRef = new Wix.ComponentGroupRef();
                    componentGroupRef.Id = group.Id;

                    feature.AddChild(componentGroupRef);
                }
            }
            else if (TemplateType.Module == this.templateType)
            {
                foreach (Wix.ISchemaElement element in wix.Children)
                {
                    if (element is Wix.Module)
                    {
                        foreach (Wix.ComponentGroup group in this.componentGroups)
                        {
                            Wix.ComponentGroupRef componentGroupRef = new Wix.ComponentGroupRef();
                            componentGroupRef.Id = group.Id;

                            ((Wix.IParentElement)element).AddChild(componentGroupRef);
                        }
                        break;
                    }
                }
            }

            //if(!this.createFragments && TemplateType.Product
            foreach (Wix.Fragment fragment in this.fragments.Values)
            {
                wix.AddChild(fragment);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Builds a setup package to the specified output path.
        /// </summary>
        /// <param name="outputPath">Location to build the setup package to.</param>
        /// <param name="outputSourcePath">Optional path where the package's .wxs file will be written.</param>
        public bool Build(string outputPath, string outputSourcePath)
        {
            this.buildError = null; // clear out any previous errors

            int currentProgress = 0;
            int totalProgress   = 7;

            // calculate the upper progress
            if (outputSourcePath != null)
            {
                ++totalProgress;
            }
            if (this.previousPackagePath != null)
            {
                ++totalProgress;
            }

            this.VerifyRequiredInformation();

            if (!this.OnProgress(currentProgress++, totalProgress, "Initialized package builder..."))
            {
                return(false);
            }

            // Calculate where everything is going
            string localSetupExe  = outputPath;
            string localSetupFeed = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileName(this.updateUrl.AbsolutePath));

            Uri urlSetupExe  = new Uri(this.updateUrl, Path.GetFileName(localSetupExe));
            Uri urlSetupFeed = new Uri(this.updateUrl, Path.GetFileName(localSetupFeed));

            Guid    previousUpgradeCode = Guid.Empty;
            Version previousVersion     = null;
            Uri     previousSetupFeed   = null;

            // if a previous package was provided, go read the key information out of it now
            if (this.previousPackagePath != null)
            {
                if (!this.OnProgress(currentProgress++, totalProgress, "Reading previous package..."))
                {
                    return(false);
                }

                this.ReadPreviousPackage(this.previousPackagePath, out previousUpgradeCode, out previousVersion, out previousSetupFeed);
            }

            //
            // if a upgrade code and/or version has not been specified use one
            // from the previous package or create new.
            //
            if (this.upgradeCode == Guid.Empty)
            {
                if (previousUpgradeCode == Guid.Empty)
                {
                    this.upgradeCode = Guid.NewGuid();
                }
                else
                {
                    this.upgradeCode = previousUpgradeCode;
                }
            }

            if (this.version == null)
            {
                if (previousVersion == null)
                {
                    FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(this.applicationRoot.Content, this.applicationEntry.Content));
                    this.version = new Version(fileVersionInfo.FileVersion);
                }
                else
                {
                    this.version = previousVersion;
                }
            }

            // verify that new data is okay when compared to previous package
            if (previousUpgradeCode != Guid.Empty && previousUpgradeCode != this.upgradeCode)
            {
                this.OnMessage(ClickThroughErrors.UpgradeCodeChanged(previousUpgradeCode, this.upgradeCode));
            }
            if (previousVersion != null && previousVersion >= this.version)
            {
                this.OnMessage(ClickThroughErrors.NewVersionIsNotGreater(previousVersion, this.version));
            }

            if (this.buildError != null)
            {
                throw new InvalidOperationException(String.Format(this.buildError.ResourceManager.GetString(this.buildError.ResourceName), this.buildError.MessageArgs));
            }
            else if (!this.OnProgress(currentProgress++, totalProgress, "Processing package information..."))
            {
                return(false);
            }

            // Product information
            Application application = new Application();

            application.Product.Id           = Guid.NewGuid().ToString();
            application.Product.Language     = "1033";
            application.Product.Manufacturer = this.manufacturerName.Content;
            application.Product.Name         = this.applicationName.Content;
            application.Package.Description  = this.description.Content;
            application.Product.UpgradeCode  = this.upgradeCode.ToString();
            application.Product.Version      = this.version.ToString();

            Wix.WixVariable variable = new Wix.WixVariable();
            variable       = new Wix.WixVariable();
            variable.Id    = "ProductName";
            variable.Value = application.Product.Name;
            application.Product.AddChild(variable);

            variable       = new Wix.WixVariable();
            variable.Id    = "ProductCode";
            variable.Value = application.Product.Id;
            application.Product.AddChild(variable);

            variable       = new Wix.WixVariable();
            variable.Id    = "ProductVersion";
            variable.Value = application.Product.Version;
            application.Product.AddChild(variable);

            variable       = new Wix.WixVariable();
            variable.Id    = "ShortcutFileId";
            variable.Value = "todoFileIdHere";
            application.Product.AddChild(variable);

            // Upgrade logic
            Wix.Upgrade upgrade = new Wix.Upgrade();
            upgrade.Id = application.Product.UpgradeCode;
            application.Product.AddChild(upgrade);

            Wix.UpgradeVersion minUpgrade = new Wix.UpgradeVersion();
            minUpgrade.Minimum    = application.Product.Version;
            minUpgrade.OnlyDetect = Wix.YesNoType.yes;
            minUpgrade.Property   = "NEWERVERSIONDETECTED";
            upgrade.AddChild(minUpgrade);

            Wix.UpgradeVersion maxUpgrade = new Wix.UpgradeVersion();
            maxUpgrade.Maximum        = application.Product.Version;
            maxUpgrade.IncludeMaximum = Wix.YesNoType.no;
            maxUpgrade.Property       = "OLDERVERSIONBEINGUPGRADED";
            upgrade.AddChild(maxUpgrade);

            // Update Feed
            Wix.Property property = new Wix.Property();
            property.Id    = "ARPURLUPDATEINFO";
            property.Value = urlSetupFeed.AbsoluteUri;
            application.Product.AddChild(property);

#if false
            // Directory tree
            Wix.DirectoryRef applicationCacheRef = new Wix.DirectoryRef();
            applicationCacheRef.Id = "ApplicationsCacheFolder";
            application.Product.AddChild(applicationCacheRef);
#endif

            Wix.DirectoryRef directoryRef = new Wix.DirectoryRef();
            directoryRef.Id = "ApplicationsFolder";
            application.Product.AddChild(directoryRef);

            this.applicationRootDirectory.Name = String.Concat(application.Product.UpgradeCode, "v", application.Product.Version);
            directoryRef.AddChild(this.applicationRootDirectory);

#if false
            // System registry keys
            Wix.Component registryComponent = new Wix.Component();
            registryComponent.Id   = "SystemVersionRegistryKeyComponent";
            registryComponent.Guid = Guid.NewGuid().ToString();
            directoryRef.AddChild(registryComponent);

            Wix.Registry productRegKey = new Wix.Registry();
            productRegKey.Root   = Wix.RegistryRootType.HKCU;
            productRegKey.Key    = @"Software\WiX\ClickThrough\Applications\[UpgradeCode]";
            productRegKey.Action = Wix.Registry.ActionType.createKeyAndRemoveKeyOnUninstall;
            registryComponent.AddChild(productRegKey);

            Wix.Registry versionRegKey = new Wix.Registry();
            versionRegKey.Name  = "Version";
            versionRegKey.Type  = Wix.Registry.TypeType.@string;
            versionRegKey.Value = "[ProductVersion]";
            productRegKey.AddChild(versionRegKey);

            Wix.Registry sourceRegKey = new Wix.Registry();
            sourceRegKey.Name  = "UpdateInfoSource";
            sourceRegKey.Type  = Wix.Registry.TypeType.@string;
            sourceRegKey.Value = "[ARPURLUPDATEINFO]";
            productRegKey.AddChild(sourceRegKey);

            // Shortcut
            Wix.DirectoryRef programMenuRef = new Wix.DirectoryRef();
            programMenuRef.Id = "ProgramMenuFolder";
            Wix.Directory shortcutsDirectory = new Wix.Directory();
            shortcutsDirectory.Id       = "ThisAppShortcuts";
            shortcutsDirectory.LongName = application.Product.Name;
            shortcutsDirectory.Name     = "AppSCDir";
            programMenuRef.AddChild(shortcutsDirectory);
            application.Product.AddChild(programMenuRef);

            Wix.Component shortcutsComponent = new Wix.Component();
            shortcutsComponent.Id      = "ThisApplicationShortcutComponent";
            shortcutsComponent.Guid    = Guid.NewGuid().ToString();
            shortcutsComponent.KeyPath = Wix.YesNoType.yes;
            shortcutsDirectory.AddChild(shortcutsComponent);

            Wix.CreateFolder shortcutsCreateFolder = new Wix.CreateFolder();
            shortcutsComponent.AddChild(shortcutsCreateFolder);

            Wix.Shortcut shortcut = this.GetShortcut(this.applicationEntry.Content, rootDirectory, shortcutsDirectory);
            shortcutsComponent.AddChild(shortcut);

            // Remove cached MSI file.
            Wix.Component removeComponent = new Wix.Component();
            removeComponent.Id      = "ThisApplicationRemoveComponent";
            removeComponent.Guid    = Guid.NewGuid().ToString();
            removeComponent.KeyPath = Wix.YesNoType.yes;
            applicationCacheRef.AddChild(removeComponent);

            Wix.RemoveFile cacheRemoveFile = new Wix.RemoveFile();
            cacheRemoveFile.Id        = "ThisApplicationRemoveCachedMsi";
            cacheRemoveFile.Directory = "ApplicationsCacheFolder";
            cacheRemoveFile.Name      = "unknown.msi";
            cacheRemoveFile.LongName  = String.Concat("{", application.Product.Id.ToUpper(CultureInfo.InvariantCulture), "}v", application.Version.ToString(), ".msi");
            cacheRemoveFile.On        = Wix.RemoveFile.OnType.uninstall;
            removeComponent.AddChild(cacheRemoveFile);

            Wix.RemoveFile cacheRemoveFolder = new Wix.RemoveFile();
            cacheRemoveFolder.Id        = "ThisApplicationRemoveCacheFolder";
            cacheRemoveFolder.Directory = "ApplicationsCacheFolder";
            cacheRemoveFolder.On        = Wix.RemoveFile.OnType.uninstall;
            removeComponent.AddChild(cacheRemoveFolder);

            Wix.RemoveFile applicationRemoveFolder = new Wix.RemoveFile();
            applicationRemoveFolder.Id        = "ThisApplicationRemoveApplicationsFolder";
            applicationRemoveFolder.Directory = "ApplicationsFolder";
            applicationRemoveFolder.On        = Wix.RemoveFile.OnType.uninstall;
            removeComponent.AddChild(applicationRemoveFolder);
#endif
            // Feature tree
            Wix.FeatureRef applicationFeatureRef = new Wix.FeatureRef();
            applicationFeatureRef.Id = "ApplicationFeature";
            application.Product.AddChild(applicationFeatureRef);

#if false
            Wix.Feature applicationFeature = new Wix.Feature();
            applicationFeature.Id             = "ApplicationFeature";
            applicationFeature.Display        = "expand";
            applicationFeature.Level          = 1;
            applicationFeature.Absent         = Wix.Feature.AbsentType.disallow;
            applicationFeature.AllowAdvertise = Wix.Feature.AllowAdvertiseType.yes;
            applicationFeature.InstallDefault = Wix.Feature.InstallDefaultType.local;
            applicationFeature.TypicalDefault = Wix.Feature.TypicalDefaultType.install;
            application.Product.AddChild(applicationFeature);

            Wix.ComponentRef shortcutsComponentRef = new Wix.ComponentRef();
            shortcutsComponentRef.Id = shortcutsComponent.Id;
            applicationFeature.AddChild(shortcutsComponentRef);

            Wix.ComponentRef removeComponentRef = new Wix.ComponentRef();
            removeComponentRef.Id = removeComponent.Id;
            applicationFeature.AddChild(removeComponentRef);
#endif

            Wix.ComponentRef[] componentRefs = this.GetComponentRefs(this.applicationRootDirectory);
            foreach (Wix.ComponentRef componentRef in componentRefs)
            {
                applicationFeatureRef.AddChild(componentRef);
            }

            if (!this.OnProgress(currentProgress++, totalProgress, "Serializing package information into XML..."))
            {
                return(false);
            }

            // serialize to an xml string
            string xml;
            using (StringWriter sw = new StringWriter())
            {
                XmlTextWriter writer = null;
                try
                {
                    writer = new XmlTextWriter(sw);

                    application.WixRoot.OutputXml(writer);

                    xml = sw.ToString();
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }

            // load the xml into a document
            XmlDocument sourceDoc = new XmlDocument();
            sourceDoc.LoadXml(xml);

            if (outputSourcePath != null)
            {
                if (!this.OnProgress(currentProgress++, totalProgress, "Saving .wxs file..."))
                {
                    return(false);
                }

                sourceDoc.Save(outputSourcePath);
            }

            // generate the MSI, create the setup.exe, and generate the RSS feed.
            string outputMsi = null;
            try
            {
                outputMsi = Path.GetTempFileName();

                if (!this.OnProgress(currentProgress++, totalProgress, "Generating .msi file..."))
                {
                    return(false);
                }

                this.GenerateMsi(sourceDoc, outputMsi);
                if (this.buildError != null)
                {
                    throw new InvalidOperationException(String.Format(this.buildError.ResourceManager.GetString(this.buildError.ResourceName), this.buildError.MessageArgs));
                }

                string assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

                if (!this.OnProgress(currentProgress++, totalProgress, "Generating setup bootstrapper..."))
                {
                    return(false);
                }

                /*
                 * NativeMethods.CREATE_SETUP_PACKAGE[] createSetup = new Microsoft.Tools.WindowsInstallerXml.ClickThrough.NativeMethods.CREATE_SETUP_PACKAGE[1];
                 * createSetup[0].fPrivileged = false;
                 * createSetup[0].fCache = true;
                 * createSetup[0].wzSourcePath = outputMsi;
                 *
                 * int hr = NativeMethods.CreateSetup(Path.Combine(assemblyPath, "setup.exe"), createSetup, createSetup.Length, localSetupExe);
                 */
                int hr = NativeMethods.CreateSimpleSetup(Path.Combine(assemblyPath, "setup.exe"), outputMsi, localSetupExe);
                if (hr != 0)
                {
                    this.OnMessage(ClickThroughErrors.FailedSetupExeCreation(Path.Combine(assemblyPath, "setup.exe"), localSetupExe));
                }

                if (!this.OnProgress(currentProgress++, totalProgress, "Generating update feed..."))
                {
                    return(false);
                }
                this.GenerateRssFeed(localSetupFeed, localSetupExe, urlSetupExe, application.Product.Id, application.Product.UpgradeCode, application.Product.Version);
            }
            finally
            {
                this.OnProgress(currentProgress++, totalProgress, "Cleaning up...");
                if (outputMsi != null)
                {
                    File.Delete(outputMsi);
                }
            }

            if (this.buildError != null)
            {
                throw new InvalidOperationException(String.Format(this.buildError.ResourceManager.GetString(this.buildError.ResourceName), this.buildError.MessageArgs));
            }
            else if (!this.OnProgress(currentProgress++, totalProgress, "Package build complete."))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Mutate a Wix element.
        /// </summary>
        /// <param name="wix">The Wix element to mutate.</param>
        private void MutateWix(Wix.Wix wix)
        {
            if (TemplateType.Fragment != this.templateType)
            {
                if (null != this.rootElement || 0 != this.features.Count)
                {
                    throw new Exception("The template option cannot be used with Feature, Product, or Module elements present.");
                }

                // create a package element although it won't always be used
                Wix.Package package = new Wix.Package();
                if (TemplateType.Module == this.templateType)
                {
                    package.Id = this.GetGuid();
                }
                else
                {
                    package.Compressed = Wix.YesNoType.yes;
                }

                package.InstallerVersion = 200;

                Wix.Directory targetDir = new Wix.Directory();
                targetDir.Id = "TARGETDIR";
                targetDir.Name = "SourceDir";

                foreach (Wix.DirectoryRef directoryRef in this.directoryRefs)
                {
                    if (String.Equals(directoryRef.Id, "TARGETDIR", StringComparison.OrdinalIgnoreCase))
                    {
                        Wix.IParentElement parent = directoryRef.ParentElement as Wix.IParentElement;

                        foreach (Wix.ISchemaElement element in directoryRef.Children)
                        {
                            targetDir.AddChild(element);
                        }

                        parent.RemoveChild(directoryRef);

                        if (null != ((Wix.ISchemaElement)parent).ParentElement)
                        {
                            int i = 0;

                            foreach (Wix.ISchemaElement element in parent.Children)
                            {
                                i++;
                            }

                            if (0 == i)
                            {
                                Wix.IParentElement supParent = (Wix.IParentElement)((Wix.ISchemaElement)parent).ParentElement;
                                supParent.RemoveChild((Wix.ISchemaElement)parent);
                            }
                        }

                        break;
                    }
                }

                if (TemplateType.Module == this.templateType)
                {
                    Wix.Module module = new Wix.Module();
                    module.Id = "PUT-MODULE-NAME-HERE";
                    module.Language = "1033";
                    module.Version = "1.0.0.0";

                    package.Manufacturer = "PUT-COMPANY-NAME-HERE";
                    module.AddChild(package);
                    module.AddChild(targetDir);

                    wix.AddChild(module);
                    this.rootElement = module;
                }
                else // product
                {
                    Wix.Product product = new Wix.Product();
                    product.Id = this.GetGuid();
                    product.Language = "1033";
                    product.Manufacturer = "PUT-COMPANY-NAME-HERE";
                    product.Name = "PUT-PRODUCT-NAME-HERE";
                    product.UpgradeCode = this.GetGuid();
                    product.Version = "1.0.0.0";
                    product.AddChild(package);
                    product.AddChild(targetDir);

                    Wix.Media media = new Wix.Media();
                    media.Id = "1";
                    media.Cabinet = "product.cab";
                    media.EmbedCab = Wix.YesNoType.yes;
                    product.AddChild(media);

                    Wix.Feature feature = new Wix.Feature();
                    feature.Id = "ProductFeature";
                    feature.Title = "PUT-FEATURE-TITLE-HERE";
                    feature.Level = 1;
                    product.AddChild(feature);
                    this.features.Add(feature);

                    wix.AddChild(product);
                    this.rootElement = product;
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Builds a setup package to the specified output path.
        /// </summary>
        /// <param name="outputPath">Location to build the setup package to.</param>
        /// <param name="outputSourcePath">Optional path where the package's .wxs file will be written.</param>
        public bool Build(string outputPath, string outputSourcePath)
        {
            this.buildError = null; // clear out any previous errors

            int currentProgress = 0;
            int totalProgress = 7;

            // calculate the upper progress
            if (outputSourcePath != null)
            {
                ++totalProgress;
            }
            if (this.previousPackagePath != null)
            {
                ++totalProgress;
            }

            this.VerifyRequiredInformation();

            if (!this.OnProgress(currentProgress++, totalProgress, "Initialized package builder..."))
            {
                return false;
            }

            // Calculate where everything is going
            string localSetupExe = outputPath;
            string localSetupFeed = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileName(this.updateUrl.AbsolutePath));

            Uri urlSetupExe = new Uri(this.updateUrl, Path.GetFileName(localSetupExe));
            Uri urlSetupFeed = new Uri(this.updateUrl, Path.GetFileName(localSetupFeed));

            Guid previousUpgradeCode = Guid.Empty;
            Version previousVersion = null;
            Uri previousSetupFeed = null;

            // if a previous package was provided, go read the key information out of it now
            if (this.previousPackagePath != null)
            {
                if (!this.OnProgress(currentProgress++, totalProgress, "Reading previous package..."))
                {
                    return false;
                }

                this.ReadPreviousPackage(this.previousPackagePath, out previousUpgradeCode, out previousVersion, out previousSetupFeed);
            }

            //
            // if a upgrade code and/or version has not been specified use one
            // from the previous package or create new.
            //
            if (this.upgradeCode == Guid.Empty)
            {
                if (previousUpgradeCode == Guid.Empty)
                {
                    this.upgradeCode = Guid.NewGuid();
                }
                else
                {
                    this.upgradeCode = previousUpgradeCode;
                }
            }

            if (this.version == null)
            {
                if (previousVersion == null)
                {
                    FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(this.applicationRoot.Content, this.applicationEntry.Content));
                    this.version = new Version(fileVersionInfo.FileVersion);
                }
                else
                {
                    this.version = previousVersion;
                }
            }

            // verify that new data is okay when compared to previous package
            if (previousUpgradeCode != Guid.Empty && previousUpgradeCode != this.upgradeCode)
            {
                this.OnMessage(ClickThroughErrors.UpgradeCodeChanged(previousUpgradeCode, this.upgradeCode));
            }
            if (previousVersion != null && previousVersion >= this.version)
            {
                this.OnMessage(ClickThroughErrors.NewVersionIsNotGreater(previousVersion, this.version));
            }

            if (this.buildError != null)
            {
                throw new InvalidOperationException(String.Format(this.buildError.ResourceManager.GetString(this.buildError.ResourceName), this.buildError.MessageArgs));
            }
            else if (!this.OnProgress(currentProgress++, totalProgress, "Processing package information..."))
            {
                return false;
            }

            // Product information
            Application application = new Application();
            application.Product.Id = Guid.NewGuid().ToString();
            application.Product.Language = "1033";
            application.Product.Manufacturer = this.manufacturerName.Content;
            application.Product.Name = this.applicationName.Content;
            application.Package.Description = this.description.Content;
            application.Product.UpgradeCode = this.upgradeCode.ToString();
            application.Product.Version = this.version.ToString();

            Wix.WixVariable variable = new Wix.WixVariable();
            variable = new Wix.WixVariable();
            variable.Id = "ProductName";
            variable.Value = application.Product.Name;
            application.Product.AddChild(variable);

            variable = new Wix.WixVariable();
            variable.Id = "ProductCode";
            variable.Value = application.Product.Id;
            application.Product.AddChild(variable);

            variable = new Wix.WixVariable();
            variable.Id = "ProductVersion";
            variable.Value = application.Product.Version;
            application.Product.AddChild(variable);

            variable = new Wix.WixVariable();
            variable.Id = "ShortcutFileId";
            variable.Value = "todoFileIdHere";
            application.Product.AddChild(variable);

            // Upgrade logic
            Wix.Upgrade upgrade = new Wix.Upgrade();
            upgrade.Id = application.Product.UpgradeCode;
            application.Product.AddChild(upgrade);

            Wix.UpgradeVersion minUpgrade = new Wix.UpgradeVersion();
            minUpgrade.Minimum = application.Product.Version;
            minUpgrade.OnlyDetect = Wix.YesNoType.yes;
            minUpgrade.Property = "NEWERVERSIONDETECTED";
            upgrade.AddChild(minUpgrade);

            Wix.UpgradeVersion maxUpgrade = new Wix.UpgradeVersion();
            maxUpgrade.Maximum = application.Product.Version;
            maxUpgrade.IncludeMaximum = Wix.YesNoType.no;
            maxUpgrade.Property = "OLDERVERSIONBEINGUPGRADED";
            upgrade.AddChild(maxUpgrade);

            // Update Feed
            Wix.Property property = new Wix.Property();
            property.Id = "ARPURLUPDATEINFO";
            property.Value = urlSetupFeed.AbsoluteUri;
            application.Product.AddChild(property);

#if false
            // Directory tree
            Wix.DirectoryRef applicationCacheRef = new Wix.DirectoryRef();
            applicationCacheRef.Id = "ApplicationsCacheFolder";
            application.Product.AddChild(applicationCacheRef);
#endif

            Wix.DirectoryRef directoryRef = new Wix.DirectoryRef();
            directoryRef.Id = "ApplicationsFolder";
            application.Product.AddChild(directoryRef);

            this.applicationRootDirectory.Name = String.Concat(application.Product.UpgradeCode, "v", application.Product.Version);
            directoryRef.AddChild(this.applicationRootDirectory);

#if false
            // System registry keys
            Wix.Component registryComponent = new Wix.Component();
            registryComponent.Id = "SystemVersionRegistryKeyComponent";
            registryComponent.Guid = Guid.NewGuid().ToString();
            directoryRef.AddChild(registryComponent);

            Wix.Registry productRegKey = new Wix.Registry();
            productRegKey.Root = Wix.RegistryRootType.HKCU;
            productRegKey.Key = @"Software\WiX\ClickThrough\Applications\[UpgradeCode]";
            productRegKey.Action = Wix.Registry.ActionType.createKeyAndRemoveKeyOnUninstall; 
            registryComponent.AddChild(productRegKey);

            Wix.Registry versionRegKey = new Wix.Registry();
            versionRegKey.Name = "Version";
            versionRegKey.Type = Wix.Registry.TypeType.@string;
            versionRegKey.Value = "[ProductVersion]";
            productRegKey.AddChild(versionRegKey);

            Wix.Registry sourceRegKey = new Wix.Registry();
            sourceRegKey.Name = "UpdateInfoSource";
            sourceRegKey.Type = Wix.Registry.TypeType.@string;
            sourceRegKey.Value = "[ARPURLUPDATEINFO]";
            productRegKey.AddChild(sourceRegKey);

            // Shortcut
            Wix.DirectoryRef programMenuRef = new Wix.DirectoryRef();
            programMenuRef.Id = "ProgramMenuFolder";
            Wix.Directory shortcutsDirectory = new Wix.Directory();
            shortcutsDirectory.Id = "ThisAppShortcuts";
            shortcutsDirectory.LongName = application.Product.Name;
            shortcutsDirectory.Name = "AppSCDir";
            programMenuRef.AddChild(shortcutsDirectory);
            application.Product.AddChild(programMenuRef);

            Wix.Component shortcutsComponent = new Wix.Component();
            shortcutsComponent.Id = "ThisApplicationShortcutComponent";
            shortcutsComponent.Guid = Guid.NewGuid().ToString();
            shortcutsComponent.KeyPath = Wix.YesNoType.yes;
            shortcutsDirectory.AddChild(shortcutsComponent);

            Wix.CreateFolder shortcutsCreateFolder = new Wix.CreateFolder();
            shortcutsComponent.AddChild(shortcutsCreateFolder);

            Wix.Shortcut shortcut = this.GetShortcut(this.applicationEntry.Content, rootDirectory, shortcutsDirectory);
            shortcutsComponent.AddChild(shortcut);

            // Remove cached MSI file.
            Wix.Component removeComponent = new Wix.Component();
            removeComponent.Id = "ThisApplicationRemoveComponent";
            removeComponent.Guid = Guid.NewGuid().ToString();
            removeComponent.KeyPath = Wix.YesNoType.yes;
            applicationCacheRef.AddChild(removeComponent);

            Wix.RemoveFile cacheRemoveFile = new Wix.RemoveFile();
            cacheRemoveFile.Id = "ThisApplicationRemoveCachedMsi";
            cacheRemoveFile.Directory = "ApplicationsCacheFolder";
            cacheRemoveFile.Name = "unknown.msi";
            cacheRemoveFile.LongName = String.Concat("{", application.Product.Id.ToUpper(CultureInfo.InvariantCulture), "}v", application.Version.ToString(), ".msi");
            cacheRemoveFile.On = Wix.RemoveFile.OnType.uninstall;
            removeComponent.AddChild(cacheRemoveFile);

            Wix.RemoveFile cacheRemoveFolder = new Wix.RemoveFile();
            cacheRemoveFolder.Id = "ThisApplicationRemoveCacheFolder";
            cacheRemoveFolder.Directory = "ApplicationsCacheFolder";
            cacheRemoveFolder.On = Wix.RemoveFile.OnType.uninstall;
            removeComponent.AddChild(cacheRemoveFolder);

            Wix.RemoveFile applicationRemoveFolder = new Wix.RemoveFile();
            applicationRemoveFolder.Id = "ThisApplicationRemoveApplicationsFolder";
            applicationRemoveFolder.Directory = "ApplicationsFolder";
            applicationRemoveFolder.On = Wix.RemoveFile.OnType.uninstall;
            removeComponent.AddChild(applicationRemoveFolder);
#endif
            // Feature tree
            Wix.FeatureRef applicationFeatureRef = new Wix.FeatureRef();
            applicationFeatureRef.Id = "ApplicationFeature";
            application.Product.AddChild(applicationFeatureRef);

#if false
            Wix.Feature applicationFeature = new Wix.Feature();
            applicationFeature.Id = "ApplicationFeature";
            applicationFeature.Display = "expand";
            applicationFeature.Level = 1;
            applicationFeature.Absent = Wix.Feature.AbsentType.disallow;
            applicationFeature.AllowAdvertise = Wix.Feature.AllowAdvertiseType.yes;
            applicationFeature.InstallDefault = Wix.Feature.InstallDefaultType.local;
            applicationFeature.TypicalDefault = Wix.Feature.TypicalDefaultType.install;
            application.Product.AddChild(applicationFeature);

            Wix.ComponentRef shortcutsComponentRef = new Wix.ComponentRef();
            shortcutsComponentRef.Id = shortcutsComponent.Id;
            applicationFeature.AddChild(shortcutsComponentRef);

            Wix.ComponentRef removeComponentRef = new Wix.ComponentRef();
            removeComponentRef.Id = removeComponent.Id;
            applicationFeature.AddChild(removeComponentRef);
#endif

            Wix.ComponentRef[] componentRefs = this.GetComponentRefs(this.applicationRootDirectory);
            foreach (Wix.ComponentRef componentRef in componentRefs)
            {
                applicationFeatureRef.AddChild(componentRef);
            }

            if (!this.OnProgress(currentProgress++, totalProgress, "Serializing package information into XML..."))
            {
                return false;
            }

            // serialize to an xml string
            string xml;
            using (StringWriter sw = new StringWriter())
            {
                XmlTextWriter writer = null;
                try
                {
                    writer = new XmlTextWriter(sw);

                    application.WixRoot.OutputXml(writer);

                    xml = sw.ToString();
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }

            // load the xml into a document
            XmlDocument sourceDoc = new XmlDocument();
            sourceDoc.LoadXml(xml);

            if (outputSourcePath != null)
            {
                if (!this.OnProgress(currentProgress++, totalProgress, "Saving .wxs file..."))
                {
                    return false;
                }

                sourceDoc.Save(outputSourcePath);
            }

            // generate the MSI, create the setup.exe, and generate the RSS feed.
            string outputMsi = null;
            try
            {
                outputMsi = Path.GetTempFileName();

                if (!this.OnProgress(currentProgress++, totalProgress, "Generating .msi file..."))
                {
                    return false;
                }

                this.GenerateMsi(sourceDoc, outputMsi);
                if (this.buildError != null)
                {
                    throw new InvalidOperationException(String.Format(this.buildError.ResourceManager.GetString(this.buildError.ResourceName), this.buildError.MessageArgs));
                }

                string assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

                if (!this.OnProgress(currentProgress++, totalProgress, "Generating setup bootstrapper..."))
                {
                    return false;
                }

                /*
                NativeMethods.CREATE_SETUP_PACKAGE[] createSetup = new Microsoft.Tools.WindowsInstallerXml.ClickThrough.NativeMethods.CREATE_SETUP_PACKAGE[1];
                createSetup[0].fPrivileged = false;
                createSetup[0].fCache = true;
                createSetup[0].wzSourcePath = outputMsi;

                int hr = NativeMethods.CreateSetup(Path.Combine(assemblyPath, "setup.exe"), createSetup, createSetup.Length, localSetupExe);
                */
                int hr = NativeMethods.CreateSimpleSetup(Path.Combine(assemblyPath, "setup.exe"), outputMsi, localSetupExe);
                if (hr != 0)
                {
                    this.OnMessage(ClickThroughErrors.FailedSetupExeCreation(Path.Combine(assemblyPath, "setup.exe"), localSetupExe));
                }

                if (!this.OnProgress(currentProgress++, totalProgress, "Generating update feed..."))
                {
                    return false;
                }
                this.GenerateRssFeed(localSetupFeed, localSetupExe, urlSetupExe, application.Product.Id, application.Product.UpgradeCode, application.Product.Version);
            }
            finally
            {
                this.OnProgress(currentProgress++, totalProgress, "Cleaning up...");
                if (outputMsi != null)
                {
                    File.Delete(outputMsi);
                }
            }

            if (this.buildError != null)
            {
                throw new InvalidOperationException(String.Format(this.buildError.ResourceManager.GetString(this.buildError.ResourceName), this.buildError.MessageArgs));
            }
            else if (!this.OnProgress(currentProgress++, totalProgress, "Package build complete."))
            {
                return false;
            }

            return true;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Mutate a Wix element.
        /// </summary>
        /// <param name="wix">The Wix element to mutate.</param>
        private void MutateWix(Wix.Wix wix)
        {
            if (TemplateType.Fragment != this.templateType)
            {
                if (null != this.rootElement || 0 != this.features.Count)
                {
                    throw new Exception("The template option cannot be used with Feature, Product, or Module elements present.");
                }

                // create a package element although it won't always be used
                Wix.Package package = new Wix.Package();
                if (TemplateType.Module == this.templateType)
                {
                    package.Id = this.GetGuid();
                }
                package.Compressed       = Wix.YesNoType.yes;
                package.InstallerVersion = 200;

                // create the root directory
                Wix.Directory targetDir = new Wix.Directory();
                targetDir.Id   = "TARGETDIR";
                targetDir.Name = "SourceDir";

                // add all previous root directories to the root directory
                foreach (Wix.Directory directory in this.directories)
                {
                    if (!(directory.ParentElement is Wix.Directory || directory.ParentElement is Wix.DirectoryRef))
                    {
                        ((Wix.IParentElement)directory.ParentElement).RemoveChild(directory);
                        targetDir.AddChild(directory);
                    }
                }

                // add children of DirectoryRef/@Id="TARGETROOT" elements to the root directory
                foreach (Wix.DirectoryRef directoryRef in this.directoryRefs)
                {
                    if ("TARGETDIR" == directoryRef.Id)
                    {
                        foreach (Wix.ISchemaElement element in directoryRef.Children)
                        {
                            targetDir.AddChild(element);
                        }
                        ((Wix.IParentElement)directoryRef.ParentElement).RemoveChild(directoryRef);
                    }
                }

                this.directories.Add(targetDir);

                if (TemplateType.Module == this.templateType)
                {
                    Wix.Module module = new Wix.Module();
                    module.Id       = "PUT-MODULE-NAME-HERE";
                    module.Language = "1033";
                    module.Version  = "1.0.0.0";

                    package.Manufacturer = "PUT-COMPANY-NAME-HERE";
                    module.AddChild(package);

                    // add the authoring from the fragments directly into the module
                    foreach (Wix.Fragment fragment in wix.Children)
                    {
                        foreach (Wix.ISchemaElement element in fragment.Children)
                        {
                            module.AddChild(element);
                        }
                    }

                    foreach (Wix.Fragment fragment in wix.Children)
                    {
                        wix.RemoveChild(fragment);
                    }

                    module.AddChild(targetDir);

                    wix.AddChild(module);
                    this.rootElement = module;
                }
                else // product
                {
                    Wix.Product product = new Wix.Product();
                    product.Id           = this.GetGuid();
                    product.Language     = "1033";
                    product.Manufacturer = "PUT-COMPANY-NAME-HERE";
                    product.Name         = "PUT-PRODUCT-NAME-HERE";
                    product.UpgradeCode  = this.GetGuid();
                    product.Version      = "1.0.0.0";

                    product.AddChild(package);

                    Wix.Media media = new Wix.Media();
                    media.Id       = 1;
                    media.Cabinet  = "product.cab";
                    media.EmbedCab = Wix.YesNoType.yes;
                    product.AddChild(media);

                    Wix.Feature feature = new Wix.Feature();
                    feature.Id    = "ProductFeature";
                    feature.Title = "PUT-FEATURE-TITLE-HERE";
                    feature.Level = 1;
                    product.AddChild(feature);
                    this.features.Add(feature);

                    // add the authoring from the fragments directly into the product
                    foreach (Wix.Fragment fragment in wix.Children)
                    {
                        foreach (Wix.ISchemaElement element in fragment.Children)
                        {
                            product.AddChild(element);
                        }
                    }

                    foreach (Wix.Fragment fragment in wix.Children)
                    {
                        wix.RemoveChild(fragment);
                    }

                    product.AddChild(targetDir);

                    wix.AddChild(product);
                    this.rootElement = product;
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Mutate a WiX document.
        /// </summary>
        /// <param name="wix">The Wix document element.</param>
        public override void Mutate(Wix.Wix wix)
        {
            this.components.Clear();
            this.directories.Clear();
            this.directoryRefs.Clear();
            this.features.Clear();
            this.files.Clear();
            this.fragments.Clear();
            this.rootElement = null;

            // index elements in this wix document
            this.IndexElement(wix);

            this.MutateWix(wix);

            this.MutateFiles();

            this.MutateDirectories();

            this.MutateComponents();

            // add the components to the product feature after all the identifiers have been set
            if (TemplateType.Product == this.templateType)
            {
                Wix.Feature feature = (Wix.Feature) this.features[0];

                foreach (Wix.Component component in this.components)
                {
                    if (null != component.Id)
                    {
                        Wix.ComponentRef componentRef = new Wix.ComponentRef();
                        componentRef.Id = component.Id;
                        feature.AddChild(componentRef);
                    }
                }
            }
            // create a ComponentGroup with all the components
            else if (TemplateType.Fragment == this.templateType && !this.createFragments)
            {
                Wix.ComponentGroup componentGroup = new Wix.ComponentGroup();
                componentGroup.Id = "ComponentGroup1";

                foreach (Wix.Component component in this.components)
                {
                    if (null != component.Id)
                    {
                        Wix.ComponentRef componentRef = new Wix.ComponentRef();
                        componentRef.Id = component.Id;
                        componentGroup.AddChild(componentRef);
                    }
                }

                Wix.Fragment fragment = new Wix.Fragment();
                this.fragments.Add("ComponentGroup:1", fragment);
                fragment.AddChild(componentGroup);
            }

            foreach (Wix.Fragment fragment in this.fragments.Values)
            {
                wix.AddChild(fragment);
            }
        }