Thursday, April 12, 2007

WiX

I got to work on something cool today. Windows Installer XML toolset (WiX) is a set of tools and specifications that help you build Windows installation packages (.msi and .msm) from an XML document. WiX is released by Microsoft under the Common Public License.
WiX brings into setups, the traditional compile and build model to create executables from a source code file. It is written in C# and requires the .NET Framework to be installed on your machine.


A google search on WiX yields a couple of good resouces that tell you what exactly it is, so I am not going to delve much into that. I spent quite a bit of time today trying to get this thing to create an installer that would execute a TSQL during the installation when I found that WiX has a built in support for executing TSQL scripts directly without you having to invoke the sqlcmd utility. (In normal circumstances you would include a custom action in your setup project that would execute scripts using sqlcmd). Obviously, you can do a lot more in WiX than just that.


A good practical scenario of this requirement is when you have to create patches and bugfixes to an existing hosted build of your application. The advantage of creating a patch in the form of an msi file instead of an exe is that an msi is an installer database file which is tracked by your operating system. Later on, you can easily find out what patches you have applied simply by going to you add/remove programs menu.

Anyway, coming back to my purpose of writing this post; there are hardly any good resources online that give you a complete working example and/or tell you what exactly to do when you have to deal with SQL scripts while using WiX. Here is a sample working .wxs file that creates an msi. When the msi is installed, the TSQL script in two .sql files are executed.




<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="
http://schemas.microsoft.com/wix/2003/01/wi
">
<Product Id="24B6D4E2-2FC2-448F-B4E3-C7FBC8790C50" Name="MyProduct" Language="1033" Version="1.0.0.0


" Manufacturer="kvsoft">

<Package Id="3B54E578-A304-437E-91A8-4299246848DE"
Description="Description of your product" Comments="This will appear in
the file summary stream." InstallerVersion="200" Compressed="yes" />
<Media Id="1" Cabinet="Product.cab" EmbedCab="yes" />
<User Id="SqlUser" Name="sa" Password="somepassword"></User>
<SqlDatabase Id="MySqlDatabase" Database="TryOuts" Server="krishnamurthyk" User="SqlUser" /><Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLLOCATION" Name="MyAppDir" LongName="My Application Directory">

<Component Id="ProductComponent" Guid="AA0225A7-F189-47CC-9D22-D84A577BB431">
<SqlScript Id="CreateTableScript" BinaryKey="DatabaseScriptBin"
ExecuteOnInstall="yes" SqlDb="MySqlDatabase" />
<SqlScript Id="SecondScript" BinaryKey="SecondScript" ExecuteOnInstall="yes" SqlDb="MySqlDatabase"><
/SqlScript>
</Component>
</Directory>

</Directory>
</Directory>
<Binary Id="DatabaseScriptBin" src="Test.sql" mce_src="Test.sql" />
<Binary Id="SecondScript" src="SecondScript.sql" mce_src="SecondScript.sql"/><Feature Id="ProductFeature" Title="Feature Title" Level="1">
<ComponentRef Id="ProductComponent" />
</Feature>
</Product>
</Wix>


Today I was repeatedly getting an error:



Unresolved reference to symbol 'CustomAction:ConfigureSql' in
section 'Product:0780EA99-9329-47AD-8CF5-C2274939FC85'.


This error disappears when I comment the SqlDatabase tag. The problem, as it turned out was funny: a missing reference. In order to get this working, you need to include a reference to sca.wixlib if you are using the Visual Studio WiX project.


If you are working with the command line utilities, then this is what you do:


> candle.exe Product.wxs
At this step, you will have an intermediate file called
Product.wixobj.


> light.exe -o MySetup.msi Product.wixobj "E:/WiX/sca.wixlib"


Voila, you have the require msi file. I hope this helps all the developers out there.
Maybe when I learn more of this, I would do a more descriptive tutorial.

WiX

I got to work on something cool today. Windows Installer XML toolset (WiX) is a set of tools and specifications that help you build Windows installation packages (.msi and .msm) from an XML document. WiX is released by Microsoft under the Common Public License.
WiX brings into setups, the traditional compile and build model to create executables from a source code file. It is written in C# and requires the .NET Framework to be installed on your machine.

A google search on WiX yields a couple of good resouces that tell you what exactly it is, so I am not going to delve much into that. I spent quite a bit of time today trying to get this thing to create an installer that would execute a TSQL during the installation when I found that WiX has a built in support for executing TSQL scripts directly without you having to invoke the sqlcmd utility. (In normal circumstances you would include a custom action in your setup project that would execute scripts using sqlcmd). Obviously, you can do a lot more in WiX than just that.

A good practical scenario of this requirement is when you have to create patches and bugfixes to an existing hosted build of your application. The advantage of creating a patch in the form of an msi file instead of an exe is that an msi is an installer database file which is tracked by your operating system. Later on, you can easily find out what patches you have applied simply by going to you add/remove programs menu.

Anyway, coming back to my purpose of writing this post; there are hardly any good resources online that give you a complete working example and/or tell you what exactly to do when you have to deal with SQL scripts while using WiX. Here is a sample working .wxs file that creates an msi. When the msi is installed, the TSQL script in two .sql files are executed.

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="
http://schemas.microsoft.com/wix/2003/01/wi
">
<Product Id="24B6D4E2-2FC2-448F-B4E3-C7FBC8790C50" Name="MyProduct" Language="1033" Version="1.0.0.0

" Manufacturer="kvsoft">

<Package Id="3B54E578-A304-437E-91A8-4299246848DE"
Description="Description of your product" Comments="This will appear in
the file summary stream." InstallerVersion="200" Compressed="yes" />
<Media Id="1" Cabinet="Product.cab" EmbedCab="yes" />
<User Id="SqlUser" Name="sa" Password="somepassword"></User>
<SqlDatabase Id="MySqlDatabase" Database="TryOuts" Server="krishnamurthyk" User="SqlUser" />

<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLLOCATION" Name="MyAppDir" LongName="My Application Directory">

<Component Id="ProductComponent" Guid="AA0225A7-F189-47CC-9D22-D84A577BB431">


<SqlScript Id="CreateTableScript" BinaryKey="DatabaseScriptBin"
ExecuteOnInstall="yes" SqlDb="MySqlDatabase" />
<SqlScript Id="SecondScript" BinaryKey="SecondScript" ExecuteOnInstall="yes" SqlDb="MySqlDatabase"><
/SqlScript>
</Component>


</Directory>

</Directory>
</Directory>
<Binary Id="DatabaseScriptBin" src="Test.sql" />
<Binary Id="SecondScript" src="SecondScript.sql"/>

<Feature Id="ProductFeature" Title="Feature Title" Level="1">
<ComponentRef Id="ProductComponent" />
</Feature>
</Product>
</Wix>

Today I was repeatedly getting an error:

Unresolved reference to symbol 'CustomAction:ConfigureSql' in
section 'Product:0780EA99-9329-47AD-8CF5-C2274939FC85'.

This error disappears when I comment the SqlDatabase tag. The problem, as it turned out was funny: a missing reference. In order to get this working, you need to include a reference to sca.wixlib if you are using the Visual Studio WiX project.

If you are working with the command line utilities, then this is what you do:

> candle.exe Product.wxs
At this step, you will have an intermediate file called
Product.wixobj.

> light.exe -o MySetup.msi Product.wixobj "E:/WiX/sca.wixlib"

Voila, you have the require msi file. I hope this helps all the developers out there.
Maybe when I learn more of this, I would do a more descriptive tutorial.