Update version number in vbproj file in NAnt build

Today, I got an exact same challenge Scot Hanselman faced 8 years ago. 

Copied his script into my build file, error said: Regex does not exist.

Added system.dll reference in NAnt according to this post, no help.

I stopped, why bother using Regex for such a small task.

I turn back to native NAnt tasks. Strategy is,

  1. Find the hardcoded version number in vdproj file, e.g., 1.1.0, notify team member don’t touch it, this will be my constant, or token to be replaced later.
  2. Replace this 1.1.0 on the fly during the build, build msi. Create backup vdproj file before replacing.
  3. When build finished, restore original vdproj.

Simple and clean solution, job accomplished.

Only goofy part is, if I run this build script when Visual Studio opened, VS kept saving memory cached content to disk, causing file conflict.

Not a big deal, this build target only runs on our CI server anyway.


  <property name="orig.version.number.in.vdproj" value="1.1.0" /> <!--ensure this matches vdproj-->
  <property name="path.vdproj.file" value="src\EmailProcessorNTServiceSetup\EmailProcessorNTServiceSetup.vdproj" />
  <property name="path.vdproj.file.orig" value="${path.vdproj.file}.orig" />

  <target name="update.vdproj.version" depends="">
    <loadfile file="${path.vdproj.file}" property="file.contents" />
	<!-- build.number in format of xx.xx.xx.xx, need to shorten to xx.xx.xx to satisfy vdproj -->
    <property name="version.number" value="${string::substring(build.number, 0, string::last-index-of(build.number, '.'))}"/>
	<echo message="shorteded version.number: ${version.number}" />
    <echo file="${path.vdproj.file}" message="${string::replace(file.contents, orig.version.number.in.vdproj, version.number)}" />
  </target>  
 
  </target>

  <target name="restore.vdproj.file" depends="">
    <delete file="${path.vdproj.file}" />
	<echo message="${path.vdproj.file} could not be deleted" if="${file::exists(path.vdproj.file)}" />

	<if test="${not file::exists(path.vdproj.file)}">
		<copy file="${path.vdproj.file.orig}"
          tofile="${path.vdproj.file}"

          />
		<delete file="${path.vdproj.file.orig}" />
	</if>
<!-- can also replace it back, same result, but this potentially still mess up svn flag.    <echo file="${path.vdproj.file}" message="${string::replace(file.contents, version.number, orig.version.number.in.vdproj)}" />-->
  </target>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s