This post originated from an RSS feed registered with .NET Buzz
by Scott Hanselman.
Original Post: Building MSI files from NAnt and Updating the VDProj's version information and other sins on Tuesday
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
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 <exec>
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