Monday, December 10, 2007

Cryptic NAnt Scripts

I've only had the privilege of working with a few NAnt scripts but most of them are very cryptic. They use XML attribute names delimited by periods. The following property name is distinguishable but not very clear.

<property name="base.dir" value="${project::get-base-directory()}" />

With the advent of fluent interfaces and striving to make our code more readable lets make our NAnt build scripts readable. I'm sure that NAnt has a naming convention but who cares lets be pragmatic about this and make our build files easier to read. Let us think of those who are looking at NAnt for the first time. On a side note Resharpers NAnt support kicks ass. My only complaint is that I can not rename a file set ID.

Here is an example of the type of build script that I would like to introduce.

<?xml version="1.0"?>

<project name="ProtocolBuilder" default="test">

  <property name="debug" value="true" />


  <property name="baseDirectory" value="${project::get-base-directory()}" />

  <property name="buildDirectory" value="${baseDirectory}\build" />

  <property name="toolsDirectory" value="${baseDirectory}\tools" />

  <property name="libraryDirectory" value="${baseDirectory}\lib" />

  <property name="sourceDirectory" value="${baseDirectory}\src" />

  <property name="applicationSourceDirectory" value="${sourceDirectory}\app" />

  <property name="testSourceDirectory" value="${sourceDirectory}\test" />


  <property name="applicationLibraryName" value="${project::get-name()}.dll" />

  <property name="testLibraryName" value="${project::get-name()}.test.dll" />

  <property name="xunitConsoleArguments" value="${testLibraryName} /sr /report-type:Text /rf:${buildDirectory} /rnf:Report" />


  <fileset id="libraryFilesSet">

    <include name="${libraryDirectory}\**\*.dll" />



  <fileset id="toolsForTestingFileSet">

    <include name="${toolsDirectory}\mbunit\bin\**.dll" />

    <include name="${toolsDirectory}\rhino.mocks\Rhino.Mocks.dll" />



  <fileset id="testReferencesFileSet">

    <include name="${toolsDirectory}\mbunit\bin\MbUnit.Framework.dll" />

    <include name="${toolsDirectory}\rhino.mocks\Rhino.Mocks.dll" />   

    <include name="${buildDirectory}\${applicationLibraryName}" />

    <include name="${libraryDirectory}\**\*.dll" />



  <fileset id="applicationSourceFileSet">

    <exclude name="${applicationSourceDirectory}\**\AssemblyInfo.cs" />

    <include name="${applicationSourceDirectory}\**\*.cs" />



  <target name="killNotepad">







  <target name="initialize" depends="killNotepad">

    <delete dir="${buildDirectory}" />

    <mkdir dir="${buildDirectory}" />



  <target name="compileSourceCode" depends="initialize">

    <csc output="${buildDirectory}\${applicationLibraryName}" target="library" debug="${debug}">

      <sources refid="applicationSourceFileSet" />

      <references refid="libraryFilesSet" />




  <target name="compileSourceCodeAndTests" depends="compileSourceCode">   

    <csc output="${buildDirectory}\${testLibraryName}" target="library" debug="${debug}">


        <include name="${testSourceDirectory}\**\*.cs" />

        <exclude name="${testSourceDirectory}\**\AssemblyInfo.cs" />


      <references refid="testReferencesFileSet" />




  <target name="copyTestDependencies"

    <copy todir="${buildDirectory}" flatten="true">

      <fileset refid="toolsForTestingFileSet" />


    <copy todir="${buildDirectory}" flatten="true">

      <fileset refid="libraryFilesSet" />




  <target name="test" depends="compileSourceCodeAndTests, copyTestDependencies"

    <exec basedir="${toolsDirectory}\mbunit\bin"





      commandline="${xunitConsoleArguments}" />           





Scott said...

Hi Adam,

I wonder if it would be another good step to start using ruby style naming



definetly a good step, poor readability might not seem like a big deal but we end up unintentially obfuscating our code.

Sean said...

Hi Adam,

Nice idea. For me a dot-separated full word style would work as well. Camal cases might be not always practical in a script? S o something like: rather base.dir (sorry JP :)


Scott said...

another way I was thinking about was putting the type first like:


Adam Alinauskas said...

Awesome idea Scott.
Putting the type first would make it much more readable. The underscores are also a nice touch.

Anonymous said...

hi there people. I'm actually into shoes and I had been digging for the sake of that exact brand. The prices due to the fact that the velcros are approximately 350 bucks on every page. But completely I base this locate selling them for the benefit of half price. I really want these [url=]gucci sneakers[/url]. I will probably purchase those. what is your opinion?