Creating Windows Installers : Generating Wix XML using Tallow
Now that our journey has begun and we have a basic Wix installer under our belt, we need to get the rest of our web application’s files into the installer. Creating all the <Directory> and <Component> and <File> elements by hand gets very tedious especially if you have a lot of directories and files in your projects. Thankfully there is a utility included with Wix called Tallow that takes care of most of the busy work for you. In this post I will show you how to use Tallow against your release folder to do a one time generation of Wix XML for your project. Don’t have a release folder yet? Let’s begin with automating the creation of one.
Build Your Release Folder
Here is some NAnt build automation that copies files to a release folder. The layout of the files in the release folder should match directory structure that we wish to deploy.
This is for a very simple project with just source code and documentation to copy. You will likely have a bit more going on. The <copy> task in NAnt is pretty powerful. Take a little time to learn all that it has to offer (like filesets andfilterchains.) Lets invoke the target and see what happens.
>cd (project root)
[copy] Copying 50 files to ‘C:\projects\pleats\build\Release’.
[mkdir] Creating directory ‘C:\projects\pleats\build\Release\docs’.
[copy] Copying 1 file to ‘C:\projects\pleats\build\Release\docs\pleats.chm’
I don’t really feel like like writing all the elements for the 51 files in the project, thankfully there is a better way.
Tallow is a tool that comes with the Wix binaries that is there to generate Wix content for existing directory structures It is not intended to be integrated with your build automation. You basically just run the tool against a directory and it will generate XML suitable for copy/paste into the pleats.wxs file.
(project root)>cd wix
..\tools\wix\tallow.exe -nologo -d ..\build\Release > pleats-release.wxs
Open the pleats-release.wxs file and you see:
Sprinkle with GUIDs and component names
Ah yes, PUT-GUID-HERE. Looks like we need to immortalize a couple of GUIDs to uniquely identify the two components in this project. I like to use the Create GUID tool in VS.Net : Tools -> Create GUID. For your own projects you should always use your own GUIDs not ones “immortalized” by me. Here is the same XML with GUIDs added and the components given more appropriate identifiers.
Cut out absolute paths
Next we need to remove that nasty absolute path from each Source attribute of the File elements. To decouple my build file from my development machine’s file paths I like to use a Wix preprocessor directive $(env.pleats.dir) to replace a path with an environment variable that I set using the NAnt <setenv> task. We already saw an example of doing this in the previous post.
Grab what we need and put it into pleats.wxs
You probably noticed that the XML generated is encapsulated in a Wix Fragment. Wix elements with Identifiers can be referenced from other .wxs files (see DirectoryRef for an example of this.) To keep things simple we are just going to cut and paste the bit we need into pleats.wxs and update the Feature element to reference to two components in our project.
Here is updated pleats.wxs file:
Now all there is to do is build the installer and make sure all the files and documentation are getting installed.
(project root)>.\tools\nant\nant build-installer
After running the installer with all defaults I see this directory on my system.
Tallow is great for when your application is all ready to go and you want to generate all the XML you need like we did above. Trouble comes knocking when you are in a more iterative process with an existing installer being updated with components, directories, and files being removed or added. When updating an existing installer I have always just made note of what files needed to be added or removed and manually edited the XML to suit. If you are are running a large project whose files change a lot there is a Tallow like tool from John Robbins called Paraffin that I have not used but advertises some change management features.