faded picture of luke
a semi-random photo | click for the full photo gallery
click to browse photos
homepage navigation

Luke Melia

September 30, 2005

Wix fragment component generation

In order to get Wix to generate good MSIs, you can have no more than one DLL/EXE per component. In addition, each component needs it's own GUID. My team wanted a way to automate the definition of these components for our .NET apps as part of our automated build process. We tried using tallow/mallow, but that didn't really work out too well for us.

Here's the solution we came up, in a nutshell: a custom nant task runs against a build directory and catalogs all files in an XML file. Special logic applies for DLLs and EXEs, which get inspected and have relevant metadata added to thir elements in the XML. Once we have this XML, we apply an XSLT which transforms it into a Wix-friendly fragment file. We made one XSLT for executables, and another one for web services. We activate this XSLT as part of our nant script, and then included it in the call to Wix.

I'm including code listings for the relevant files here. It's available for use under a BSD license, which I'll include at the bottom.

The nant custom task: "BuildOutputToXmlTask.cs"

C#:
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Xml;
  11. using NAnt.Core;
  12. using NAnt.Core.Attributes;
  13. using NAnt.Core.Types;
  14.  
  15. namespace Oxygen.Build.Tasks
  16. {
  17.    /// <summary>
  18.    /// &amp;lt;buildoutputtoxml dir="build/app-client" output="build-output.xml" /&amp;gt;
  19.    /// </summary>
  20.    [TaskName("buildoutputtoxml")]
  21.    public class BuildOutputToXmlTask : Task
  22.    {
  23.       private string _dir;
  24.       private string _output = "build-output.xml";
  25.       private bool _debug;
  26.       private FileSet _fileset = new FileSet();
  27.       private const int MAX_PATH = 260;
  28.  
  29.       [TaskAttribute("dir", Required = true, ExpandProperties=true)]
  30.       [StringValidator(AllowEmpty = false)]
  31.       public string Dir
  32.       {
  33.          get { return _dir; }
  34.          set { _dir = value.Replace('/', '\\').TrimEnd(new char[] {'\\'}); }
  35.       }
  36.  
  37.       [TaskAttribute("output", Required = false)]
  38.       [StringValidator(AllowEmpty = false)]
  39.       public string Output
  40.       {
  41.          get { return _output; }
  42.          set { _output = value; }
  43.       }
  44.  
  45.       [TaskAttribute("debug", Required = false)]
  46.       [BooleanValidator]
  47.       public bool Debug
  48.       {
  49.          get { return _debug; }
  50.          set { _debug = value; }
  51.       }
  52.  
  53.       /// <summary>
  54.       /// Used to select the files to create XML for.
  55.       /// </summary>
  56.       [BuildElement("fileset")]
  57.       public virtual FileSet BuildOutputFileSet
  58.       {
  59.          get { return _fileset; }
  60.          set { _fileset = value; }
  61.       }
  62.  
  63.       protected override void ExecuteTask()
  64.       {
  65.          if (this.Debug) Debugger.Break();
  66.          //Check the directory exists
  67.          if (!Directory.Exists(this.Dir))
  68.             throw new BuildException("The directory specified does not exist");
  69.  
  70.          // ensure base directory is set, even if fileset was not initialized
  71.          // from XML
  72.          if (BuildOutputFileSet.BaseDirectory == null)
  73.          {
  74.             BuildOutputFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
  75.          }
  76.          if (BuildOutputFileSet.Includes.Count == 0)
  77.             BuildOutputFileSet.Includes.Add("**/*");
  78.  
  79.          XmlDocument xmlDoc = new XmlDocument();
  80.          XmlNode parentNode = xmlDoc.AppendChild(xmlDoc.CreateElement("buildOutput"));
  81.          AddFilesToXml(GetFileSetFilesInDir(this.Dir, false), xmlDoc, parentNode);
  82.          AddDirectoriesToXml(Directory.GetDirectories(this.Dir), xmlDoc, parentNode);
  83.          Project.Log(Level.Info, "Writing " + this.Output + " with contents of " + this.Dir);
  84.          XmlTextWriter writer = new XmlTextWriter(this.Output, Encoding.UTF8);
  85.          try
  86.          {
  87.             writer.Formatting = Formatting.Indented;
  88.             xmlDoc.WriteTo(writer);
  89.          }
  90.          finally
  91.          {
  92.             writer.Close();
  93.          }
  94.       }
  95.  
  96.       private void AddDirectoriesToXml(string[] directories, XmlDocument xmlDoc, XmlNode parentNode)
  97.       {
  98.          foreach (string subDir in directories)
  99.          {
  100.             if (DirectoryContainsFileSetFiles(subDir))
  101.             {
  102.                string directoryName = subDir.Replace(this.Dir + "\\", String.Empty);
  103.                string shortName = Path.GetFileName(GetShortPathName(subDir));
  104.                XmlElement directoryElement = xmlDoc.CreateElement("directory");
  105.                directoryElement.Attributes.Append(xmlDoc.CreateAttribute("shortName")).Value = shortName;
  106.                directoryElement.Attributes.Append(xmlDoc.CreateAttribute("longName")).Value = directoryName;
  107.                directoryElement.Attributes.Append(xmlDoc.CreateAttribute("processedName")).Value = ProcessDirectoryName(directoryName);
  108.                parentNode.AppendChild(directoryElement);
  109.                AddFilesToXml(GetFileSetFilesInDir(subDir, false), xmlDoc, directoryElement);
  110.                AddDirectoriesToXml(Directory.GetDirectories(subDir), xmlDoc, directoryElement);
  111.             }
  112.          }
  113.       }
  114.  
  115.       private void AddFilesToXml(StringCollection files, XmlDocument xmlDoc, XmlNode parentNode)
  116.       {
  117.          SortedList shortNames = new SortedList(files.Count);
  118.          foreach (string path in files)
  119.          {
  120.             string fileName = new FileInfo(path).Name;
  121.             string shortName = Path.GetFileName(GetShortPathName(path));
  122.             // Check if 8.3 name exists
  123.             // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem NtfsDisable8dot3NameCreation=1
  124.             if (!Regex.IsMatch(shortName, @"^\S{1,8}\.\S{0,3}$"))
  125.             {
  126.                shortName = GenerateShortName(fileName, shortNames);
  127.             }
  128.             shortNames.Add(shortName, null);
  129.  
  130.             XmlElement fileElement = xmlDoc.CreateElement("file");
  131.             fileElement.Attributes.Append(xmlDoc.CreateAttribute("name")).Value = shortName;
  132.             fileElement.Attributes.Append(xmlDoc.CreateAttribute("longName")).Value = Path.GetFileName(fileName);
  133.             fileElement.Attributes.Append(xmlDoc.CreateAttribute("extension")).Value = Path.GetExtension(fileName);
  134.             fileElement.Attributes.Append(xmlDoc.CreateAttribute("processedName")).Value = ProcessFilePath(path);
  135.             fileElement.Attributes.Append(xmlDoc.CreateAttribute("generatedId")).Value = Guid.NewGuid().ToString();
  136.             if (path.ToLower().EndsWith(".exe") || path.ToLower().EndsWith(".dll"))
  137.             {
  138.                try
  139.                {
  140.                   // LoadFile vs LoadFrom lets us load an assembly without resolving anything just
  141.                   // to examine it. So already loaded assemblies or duplicate assemblies don't matter
  142.                   AssemblyName assemblyName = AssemblyName.GetAssemblyName(Path.GetFullPath(path));
  143.                   XmlElement assemblyElement = xmlDoc.CreateElement("assembly");
  144.                   fileElement.AppendChild(assemblyElement);
  145.                   string[] nameParts = Regex.Split(assemblyName.FullName, @", *|=");
  146.                   assemblyElement.Attributes.Append(xmlDoc.CreateAttribute("fullName")).Value = assemblyName.FullName;
  147.                   assemblyElement.Attributes.Append(xmlDoc.CreateAttribute("name")).Value = nameParts[0];
  148.  
  149.                   for (int i = 1; i + 1 <nameParts.Length; i += 2)
  150.                   {
  151.                      string name = ToLowerFirstChar(nameParts[i]);
  152.                      string value = nameParts[i + 1];
  153.                      if (value != "null")
  154.                         assemblyElement.Attributes.Append(xmlDoc.CreateAttribute(name)).Value = value;
  155.                   }
  156.                }
  157.                catch (Exception ex)
  158.                {
  159.                   Console.Out.WriteLine(ex.ToString());
  160.                }
  161.             }
  162.             parentNode.AppendChild(fileElement);
  163.          }
  164.          Project.Log(Level.Info, "Writing " + this.Output + " with contents of " + this.Dir);
  165.          XmlTextWriter writer = new XmlTextWriter(this.Output, Encoding.UTF8);
  166.          try
  167.          {
  168.             writer.Formatting = Formatting.Indented;
  169.             xmlDoc.WriteTo(writer);
  170.          }
  171.          finally
  172.          {
  173.             writer.Close();
  174.          }
  175.       }
  176.  
  177.       private string ProcessFilePath(string fullPath)
  178.       {
  179.          string path = fullPath.Substring(fullPath.IndexOf(this.Dir) + this.Dir.Length + 1);
  180.          return StripDotsAndSlashesAndInterCap(path);
  181.       }
  182.  
  183.       private string ProcessDirectoryName(string directoryName)
  184.       {
  185.          return StripDotsAndSlashesAndInterCap(directoryName);
  186.       }
  187.  
  188.       private string StripDotsAndSlashesAndInterCap(string s)
  189.       {
  190.          StringBuilder buffer = new StringBuilder();
  191.          bool lastCharWasPeriodOrBackslash = false;
  192.          for (int i = 0; i <s.Length; i++)
  193.          {
  194.             char c = s[i];
  195.             if ((c == '.') || (c == '\\'))
  196.             {
  197.                lastCharWasPeriodOrBackslash = true;
  198.             }
  199.             else
  200.             {
  201.                if (lastCharWasPeriodOrBackslash || i == 0)
  202.                   buffer.Append(Char.ToUpper(c));
  203.                else
  204.                   buffer.Append(c);
  205.                lastCharWasPeriodOrBackslash = false;
  206.             }
  207.          }
  208.          return buffer.ToString();
  209.       }
  210.  
  211.       private static string ToLowerFirstChar(string input)
  212.       {
  213.          if (input == null) return null;
  214.          if (input.Length == 0) return input;
  215.          if (input.Length == 1) return input.ToLower();
  216.          char[] output = new char[input.Length];
  217.          output[0] = Char.ToLower(input[0]);
  218.          input.CopyTo(1, output, 1, input.Length - 1);
  219.          return new string(output);
  220.       }
  221.  
  222.       /// <summary>
  223.       /// Gets the short name for a file.
  224.       /// </summary>
  225.       /// <param name="fullPath">Fullpath to file on disk.</param>
  226.       /// <returns>Short name for file.</returns>
  227.       private static string GetShortPathName(string fullPath)
  228.       {
  229.          StringBuilder shortPath = new StringBuilder(MAX_PATH, MAX_PATH);
  230.  
  231.          uint result = GetShortPathName(fullPath, shortPath, MAX_PATH);
  232.  
  233.          if (0 == result)
  234.          {
  235.             int err = Marshal.GetLastWin32Error();
  236.             throw new COMException("Failed to get short path name", err);
  237.          }
  238.  
  239.          return shortPath.ToString();
  240.       }
  241.  
  242.       /// <summary>
  243.       /// Gets the short name for a file.
  244.       /// </summary>
  245.       /// <param name="longPath">Long path to convert to short path.</param>
  246.       /// <param name="shortPath">Short path from long path.</param>
  247.       /// <param name="buffer">Size of short path.</param>
  248.       /// <returns>zero if success.</returns>
  249.       [DllImport("kernel32.dll", EntryPoint="GetShortPathNameW", CharSet=CharSet.Unicode, ExactSpelling=true, SetLastError=true)]
  250.       internal static extern uint GetShortPathName(string longPath, StringBuilder shortPath, [MarshalAs(UnmanagedType.U4)] int buffer);
  251.  
  252.       private string GenerateShortName(string name, SortedList shortNames)
  253.       {
  254.          int i = 1;
  255.          string shortName;
  256.          do
  257.          {
  258.             string extension = Path.GetExtension(name).ToUpper();
  259.             if (extension.Length> 4)
  260.                extension = extension.Substring(0, 4);
  261.             string baseName = Path.GetFileNameWithoutExtension(name).ToUpper();
  262.             if (baseName.Length> 6)
  263.                baseName = baseName.Substring(0, 6);
  264.             if (i <5) // win 2003 this is <5 for speed
  265.             {
  266.                shortName = baseName + '~' + i + extension;
  267.                i++;
  268.             }
  269.             else
  270.             {
  271.                Random random = new Random();
  272.                int randomNumber = random.Next(0x0, 0xFFFF);
  273.                shortName = baseName.Substring(0, 2) + randomNumber.ToString("X4") + '~' + 1 + extension;
  274.             }
  275.          } while (shortNames.Contains(shortName));
  276.          return shortName;
  277.       }
  278.  
  279.  
  280.       private StringCollection GetFileSetFilesInDir(string dir, bool recursive)
  281.       {
  282.          DirectoryInfo dirInfo = new DirectoryInfo(dir);
  283.          StringCollection results = new StringCollection();
  284.          foreach (string fileName in this.BuildOutputFileSet.FileNames)
  285.          {
  286.             FileInfo fileInfo = new FileInfo(fileName);
  287.             if (fileInfo.FullName.IndexOf(dirInfo.FullName) == 0)
  288.             {
  289.                if (recursive)
  290.                {
  291.                   results.Add(fileName);
  292.                }
  293.                else
  294.                {
  295.                   if (fileInfo.DirectoryName == dirInfo.FullName)
  296.                      results.Add(fileName);
  297.                }
  298.             }
  299.          }
  300.          return results;
  301.       }
  302.  
  303.       private bool DirectoryContainsFileSetFiles(string dir)
  304.       {
  305.          if (GetFileSetFilesInDir(dir, true).Count> 0)
  306.             return true;
  307.  
  308.          return false;
  309.       }
  310.    }
  311. }

The XSLT for transforming a Windows executable project: "OutputToComponents.xslt":

XML:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.microsoft.com/wix/2003/01/wi">
  3.    <xsl:output indent="yes" method="xml" />
  4.    <xsl:param name="applicationName" select="MyApplication" />
  5.    <xsl:template match="/">
  6.       <Wix>
  7.          <Fragment>
  8.             <Media Id="1" Cabinet="ProductFeature.cab" EmbedCab="yes" />
  9.             <Directory Id="TARGETDIR" Name="SourceDir">
  10.                <xsl:for-each select="//file">
  11.                   <Component Id="{@processedName}" Guid="{@generatedId}">
  12.                      <File Id="{@processedName}File" Name="{@name}" LongName="{@longName}" src="{@longName}"
  13.                         Vital="yes" KeyPath="yes" DiskId="1">
  14.                         <xsl:if test="count(assembly)=1">
  15.                            <xsl:attribute name="Assembly">.net</xsl:attribute>
  16.                            <xsl:attribute name="AssemblyApplication"><xsl:value-of select="@processedName" />File</xsl:attribute>
  17.                            <xsl:attribute name="AssemblyManifest"><xsl:value-of select="@processedName" />File</xsl:attribute>
  18.                         </xsl:if>
  19.                         <xsl:if test="@extension='.exe'">
  20.                            <Shortcut Id="ApplicationShortcut" Advertise="yes" Icon="AppIcon.exe" Directory="OxygenMenuFolder"
  21.                               Name="{substring($applicationName,0,7)}~1" LongName="{$applicationName}" />
  22.                         </xsl:if>
  23.                      </File>
  24.                   </Component>
  25.                </xsl:for-each>
  26.                <Directory Id="ProgramMenuFolder" Name="USER'S~1" LongName="User's Programs Menu">
  27.                   <Directory Id="OxygenMenuFolder" Name='OXYGEN~1' LongName="Oxygen Media" />
  28.                </Directory>
  29.                <Component Id="RemoveOxygenMenuFolderComponent" Guid="A7A96ACE-AA12-4503-A02C-AF0F55D6E194">
  30.                   <RemoveFolder Id="RemoveOxygenMenuFolder" On="uninstall" Directory="OxygenMenuFolder" />
  31.                </Component>
  32.             </Directory>
  33.             <Icon Id="AppIcon.exe" src="app.ico" />
  34.             <Feature Id="ProductFeature" Title="{$applicationName}" Level="1">
  35.                <xsl:for-each select="//file">
  36.                   <ComponentRef Id="{@processedName}" />
  37.                </xsl:for-each>
  38.                <ComponentRef Id="RemoveOxygenMenuFolderComponent" />
  39.             </Feature>
  40.          </Fragment>
  41.       </Wix>
  42.    </xsl:template>
  43. </xsl:stylesheet>
  44. </code></pre>
  45.  
  46. The XSLT for transforming a web service project:
  47.  
  48. <pre><code allow="none">
  49. <?xml version="1.0" encoding="UTF-8" ?>
  50. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.microsoft.com/wix/2003/01/wi">
  51.    <xsl:output indent="yes" method="xml" />
  52.    <xsl:param name="applicationName" select="MyApplication" />
  53.    <xsl:template match="/">
  54.       <Wix>
  55.          <Fragment>
  56.             <Media Id="1" Cabinet="ProductFeature.cab" EmbedCab="yes" />
  57.             <Directory Id="TARGETDIR" Name="SourceDir">
  58.                <xsl:apply-templates />
  59.                <Component Id="WixWebVirtualDirComponent" Guid="INSERT_GUID_HERE">
  60.                   <WebVirtualDir Id="WebVirtualDir" Alias="[VIRTUALDIRECTORYNAME]" Directory="TARGETDIR" WebSite="DefaultWebSite">
  61.                      <WebApplication Id="WebApplication" Name="[VIRTUALDIRECTORYNAME]" />
  62.                   </WebVirtualDir>
  63.                </Component>
  64.             </Directory>
  65.             <WebSite Id="DefaultWebSite" Description="Default Web Site">
  66.                <WebAddress Id="AllUnassigned" Port="80" />
  67.             </WebSite>
  68.             <Feature Id="ProductFeature" Title="{$applicationName}" Level="1">
  69.                <xsl:for-each select="//file">
  70.                   <ComponentRef Id="{@processedName}" />
  71.                </xsl:for-each>
  72.                <ComponentRef Id="WixWebVirtualDirComponent" />
  73.             </Feature>
  74.          </Fragment>
  75.       </Wix>
  76.    </xsl:template>
  77.    <xsl:template name="directoryTemplate" match="directory">
  78.       <Directory Id="{@processedName}" Name="{@shortName}">
  79.             <xsl:if test="@shortName != @longname">
  80.                <xsl:attribute name="LongName"><xsl:value-of select="@longName" /></xsl:attribute>
  81.             </xsl:if>
  82.          <xsl:apply-templates />
  83.       </Directory>
  84.    </xsl:template>
  85.    <xsl:template name="fileTemplate" match="file">
  86.       <Component Id="{@processedName}" Guid="{@generatedId}">
  87.          <File Id="{@processedName}File" Name="{@name}" LongName="{@longName}"
  88.             Vital="yes" KeyPath="yes" DiskId="1">
  89.             <xsl:if test="count(parent::directory)=0">
  90.                <xsl:attribute name="src"><xsl:value-of select="@longName" /></xsl:attribute>
  91.             </xsl:if>
  92.             <xsl:choose>
  93.                <xsl:when test="count(parent::directory)=1">
  94.                   <xsl:attribute name="src"><xsl:value-of select="parent::directory/@longName" />/<xsl:value-of select="@longName" /></xsl:attribute>
  95.                </xsl:when>
  96.                <xsl:otherwise>
  97.                   <xsl:attribute name="src"><xsl:value-of select="@longName" /></xsl:attribute>
  98.                </xsl:otherwise>
  99.             </xsl:choose>
  100.          </File>
  101.       </Component>
  102.    </xsl:template>
  103. </xsl:stylesheet>
  104. </code></pre>
  105.  
  106. A snippet of a nant build file showing the use of this code:
  107.  
  108. <pre><code allow="none">
  109.    <target name="client-installer" depends="init, version">
  110.       <buildoutputtoxml dir="${client.build.dir}" output="${client.build.dir}/buildoutput.xml" debug="false" />
  111.       <style in="${client.build.dir}/buildoutput.xml" out="${client.build.dir}/Component.wxs" style="lib/OutputToComponents.xslt" verbose="true" failonerror="true">
  112.          <parameters>
  113.             <parameter name="applicationName" namespaceuri="" value="HelloWorldApp" />
  114.          </parameters>
  115.       </style>
  116.  
  117.       <copy todir="${client.build.dir}" flatten="true" overwrite="true" failonerror="true">
  118.          <fileset basedir="wix">
  119.             <include name="ClientInstaller.wxs" />
  120.          </fileset>
  121.       </copy>
  122.  
  123.       <copy file="src/ClientUI/app.ico" tofile="${client.build.dir}/app.ico" failonerror="true" overwrite="true" />
  124.  
  125.       <exec workingdir="${client.build.dir}"
  126.          program="tools\wix\candle.exe"
  127.          commandline="Component.wxs ClientInstaller.wxs" />
  128.       <exec workingdir="${client.build.dir}"
  129.          program="tools\wix\light.exe"
  130.          commandline="Component.wixobj ClientInstaller.wixobj -out ..\HelloWorldApp.msi" />
  131.       <delete failonerror="false">
  132.          <fileset basedir="${client.build.dir}">
  133.             <include name="buildoutput.xml" />
  134.             <include name="*.wxs" />
  135.             <include name="*.wixobj" />
  136.             <include name="app.ico" />
  137.          </fileset>
  138.       </delete>
  139.    </target>

Copyright and open source license:

Copyright (c) 2005, Oxygen Media, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the Oxygen Media nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
September 26, 2005

CSS maintainability

My new software development blog

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!
Luke Melia on software development freelance web development how to contact me Luke Melia, Software Developer letters and more from my travels photo gallery personal philosophy