/// <summary>
        /// Builds the WiX source file (*.wxs) from the specified <see cref="Bundle"/> instance.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <returns></returns>
        public static string BuildWxs(Bundle project)
        {
            lock (typeof(Compiler))
            {
                //very important to keep "ClientAssembly = " in all "public Build*" methods to ensure GetCallingAssembly
                //returns the build script assembly but not just another method of Compiler.
                if (ClientAssembly.IsEmpty())
                {
                    ClientAssembly = System.Reflection.Assembly.GetCallingAssembly().GetLocation();
                }

                project.Validate();

                lock (Compiler.AutoGeneration.WxsGenerationSynchObject)
                {
                    var oldAlgorithm = AutoGeneration.CustomIdAlgorithm;
                    try
                    {
                        WixEntity.ResetIdGenerator(false);
                        AutoGeneration.CustomIdAlgorithm = project.CustomIdAlgorithm ?? AutoGeneration.CustomIdAlgorithm;

                        string file = IO.Path.GetFullPath(IO.Path.Combine(project.OutDir, project.OutFileName) + ".wxs");

                        if (IO.File.Exists(file))
                        {
                            IO.File.Delete(file);
                        }

                        string extraNamespaces = project.WixNamespaces.Distinct()
                                                 .Select(x => x.StartsWith("xmlns:") ? x : "xmlns:" + x)
                                                 .ConcatItems(" ");

                        var wix3Namespace = "http://schemas.microsoft.com/wix/2006/wi";
                        var wix4Namespace = "http://wixtoolset.org/schemas/v4/wxs";

                        var wixNamespace = Compiler.IsWix4 ? wix4Namespace : wix3Namespace;

                        var doc = XDocument.Parse(
                            @"<?xml version=""1.0"" encoding=""utf-8""?>
                             " + $"<Wix xmlns=\"{wixNamespace}\" {extraNamespaces} " + @" >
                        </Wix>");

                        doc.Root.Add(project.ToXml());

                        AutoElements.NormalizeFilePaths(doc, project.SourceBaseDir, EmitRelativePaths);

                        project.InvokeWixSourceGenerated(doc);

                        AutoElements.ExpandCustomAttributes(doc, project);

                        if (WixSourceGenerated != null)
                        {
                            WixSourceGenerated(doc);
                        }

                        string xml = "";
                        using (IO.StringWriter sw = new StringWriterWithEncoding(Encoding.Default))
                        {
                            doc.Save(sw, SaveOptions.None);
                            xml = sw.ToString();
                        }

                        //of course you can use XmlTextWriter.WriteRaw but this is just a temporary quick'n'dirty solution
                        //http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2657663&SiteID=1
                        xml = xml.Replace("xmlns=\"\"", "");

                        DefaultWixSourceFormatedHandler(ref xml);

                        project.InvokeWixSourceFormated(ref xml);
                        if (WixSourceFormated != null)
                        {
                            WixSourceFormated(ref xml);
                        }

                        using (var sw = new IO.StreamWriter(file, false, Encoding.Default))
                            sw.WriteLine(xml);

                        Compiler.OutputWriteLine("\n----------------------------------------------------------\n");
                        Compiler.OutputWriteLine("Wix project file has been built: " + file + "\n");

                        project.InvokeWixSourceSaved(file);
                        if (WixSourceSaved != null)
                        {
                            WixSourceSaved(file);
                        }

                        return(file);
                    }
                    finally
                    {
                        AutoGeneration.CustomIdAlgorithm = oldAlgorithm;
                    }
                }
            }
        }
Example #2
0
 internal void ResetAutoIdGeneration(bool supressWarning)
 {
     ResetWixGuidStartValue();
     WixEntity.ResetIdGenerator(supressWarning);
 }