Write C# Code in your NAnt Build File

When rockin' out the NAnt build process, I thought it was necessary to have a folder that accurately states the build version/number of the enclosed assembly. I'm sure I could have done this w/code, but eff that... I wanted to have a bit of fun, trying the <code> tags from within the build file itself.

Given a path to an assembly, ${build.dir} will echo something like:

build-38372-383748--2010-10-10

I've found it handy for:

  • Doing something at build time wo/another console app.
  • Keeping track of builds (duh)
  • Impressing the ladies.

(Sorry about the whacked formatting!)

<property name="build.dir" value="build" />
<property name="pathToAssembly" value="C:\your\moms\house\nice.dll" />
<target name="getbuildnum">
<script language="C#" prefix="method" >
<imports>
<import namespace="System.Reflection" />
</imports>
<code>
<![CDATA[
[Function("get-version-from-assembly")]
public string GetVersionFromAssembly(string assPath) {
Assembly ass = Assembly.LoadFrom(assPath);
return "build-"+ass.GetName().Version.ToString()
.Replace(".","-")+"--"+DateTime.Now.ToString("yyyy-MM-dd");
}
]]>
</code>
</script>
<property name='build.dir' value='${method::get-version-from-assembly(pathToAssembly)}' />
<echo message='${build.dir}' />
</target>

kick it on DotNetKicks.com

Tags:

About Brian


profile for bluevoodoo1 on Stack Exchange, a network of free, community-driven Q&A sites

Brian Canzanella brings you nifty tips and tricks for most things .NET. read more...

Readers / Stuff