Scott Hanselman

Building MSI files from NAnt and Updating the VDProj's version information and other sins on Tuesday

March 30, '04 Comments [5] Posted in NUnit | Nant
Sponsored By

So, I commited a series of mortal sins today, but hey, it works, right? :)  Other people have felt my pain, so I thought I'd share.

We have a glorious automated build process with NAnt and NUnit and CruiseControl.  We also update all the AssemblyInfo files (we actually create 'AssemblyVersion.cs' files).  We zip up all the binaries and NDoc generated CHM files.

Additionally I want to create an MSI file at the end of it all. 

Given:

  • I don't want to mess with Installshield yet as the VS.NET stuff does what I need for now. 
  • I don't want to mess with the msitask because it's too freaky.

Therefore:

  • I update the versions inside of the setup.vdproj myself with some of the worse regex's and crappiest code I've done in the last month.

    <target name="updatemsisetupversion" depends="setup">
    <property name="short.project.version" value="0.0.0" />
    <script language="C#">
    <code>CDATA[
    public static void ScriptMain
    (Project project) {
    //Shorten the project string
    (like 1.3.4.5, to 1.3.4)
    string projectVersion = project.Properties["project.version"];
    projectVersion = projectVersion.Substring(0,projectVersion.LastIndexOf("."));
    project.Properties["short.project.version"] = projectVersion;
    string setupFileName = Path.Combine(project.BaseDirectory, "setup\\MySetup.vdproj");
    StreamReader reader = File.OpenText(setupFileName);
    string file = String.Empty;
    try {
    Regex expression1 = new Regex(@"(?:\""ProductName\"" = \""8.My Project  )(\d.\d.\d+)");
    Regex expression2 = new Regex(@"(?:\""ProductCode\"" = \""8.){([\d\w-]+)}");
    Regex expression3 = new Regex(@"(?:\""PackageCode\"" = \""8.){([\d\w-]+)}");
    Regex expression4 = new Regex(@"(?:\""ProductVersion\"" = \""8.)(\d.\d.\d+)");
    file = reader.ReadToEnd();
    file = expression1.Replace(file,"\"ProductName\" = \"8:My Project " + projectVersion);
    file = expression2.Replace(file,"\"ProductCode\" = \"8:{" + Guid.NewGuid().ToString().ToUpper() + "}");
    file = expression3.Replace(file,"\"PackageCode\" = \"8:{" + Guid.NewGuid().ToString().ToUpper() + "}");
    file = expression4.Replace(file,"\"ProductVersion\" = \"8:" + projectVersion);
    }
    finally {
    //
    must remember to close the file or the compile may not work
    reader.Close();
    }
    //
    create a writer and open the file
    TextWriter tw = new StreamWriter(setupFileName);
    try {
    // write a line of text to the file
    tw.WriteLine(file);
    }
    finally {
    //
    close the stream
    tw.Close();
    }
    }
    ]]>code>

  • Then, I shell out devenv.exe and run my new vdproj with

Note: YES, I know the RegEx's suck, and I'm loading the file into a single string, and blah blah blah, micro perf, optimizations, etc.  But, it's a build file. so Nyah! :P 

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Wednesday, March 31, 2004 6:03:13 PM UTC
Funny, I had a task today to figure out &lt;msitask&gt;, but I don't know much about MSI, and having been tagged as the "install expert" back in the pre-MSI days, that's one part of my skill set I don't want to keep up to date. It seems like the consensus is that shelling to VS.NET is the way to go.

BTW, whatever dasBlog is doing to create emoticons is having fun with your regexes.
Friday, August 27, 2004 6:15:57 PM UTC
So with this approach, do you even bother using the CSC or SOLUTION task to build your VS.NET solution?

The reason I ask is that when I shell out devenv.exe to build the .vdproj project, it runs a build of all the assemblies in the solution anyways. I'd rather it didn't, but then I guess it sort of has to unless there's a way to point it to the assemblies in the build output folder.
Wednesday, July 27, 2005 3:28:14 PM UTC
Nice tip (although your HTML doesn't show up correctly in either Opera or Mozilla properly making it an IE job to copy and paste).

I used it to change absolute references in my CSS files (tom images and such) when transfering from development to a staging server. What a time (and headache) saver!
Mayburne
Sunday, October 30, 2005 2:57:47 AM UTC
Thanks for the tip! I was trying to gather up the confidence to try to edit the .vdproj file myself but I was worried about doing that based off of everything I saw VS.Net modify. One update that I needed to make to your regular expression though: @"(?:\""ProductVersion\"" = \""8.)(\d{0,2}.\d{0,2}.\d{0,4})"

I needed to modify the expression to looking for XX.XX.XXXX and any combination there of.

Thanks again.
Chris Austin
Wednesday, November 23, 2005 6:07:30 AM UTC
Scott,

Along the same lines, you can remove those pesky 'only C#, J#, VB.NET projects supported' errors that show up in your CruiseControl.NET logs as warnings if you do something along these lines:

http://dotnetbkm.blogspot.com/2005/11/nant-solution-task-and-setupmerge.html

Cheers,
John
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.