Invoking different NAnt targets on different versions of Windows

A friend just asked me how to invoke different targets for different versions of Windows using NAnt 0.84. At first I told him to start using NAnt 0.85 so he could use the new string comparison functions, but as I thought about it more, I realized that it's not hard to do without resorting to functions, the following will work fine in either version of NAnt:

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

  <target name="go">
    <readregistry
     
property="os.version"
     
key="SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion"
      hive="LocalMachine" />

    <call target="dothis.${os.version}"/>
  </target>

  <target name="dothis.5.0">
    <echo message="Hello Windows 2000"/>
  </target>

  <target name="dothis.5.1">
    <echo message="Hello Windows XP"/>
  </target>

  <target name="dothis.5.2">
    <echo message="Hello Windows 2003"/>
  </target>

</project>

Simple.

No Comments