Intellisense for editing NAnt build scripts

This post is inspired by a thread on the Alt.Net yahoo group complaining that authoring NAnt build scripts is a pain. I agree to a point but until a better build system is ready I want to help fellow NAnt disciples by sharing one of my favorite tricks for boosting NAnt scripting productivity. This is a quick guide on getting Intellisense working for NAnt under VS.Net 2005 and better yet some automation to do it for you.



The basics


Included with each NAnt release is a schema file that includes all the legal elements and attributes annotated with documentation on how to use them. After copying this file into your VS.Net XML schemas directory you get Intellisense when editing XML files with appropriate the NAnt namespace declared (the NAnt FAQ).



Automate it


While you could look for the schema file and copy it to the correct VS.Net directory. A better way to do this is to use the<nantschema/>  task to generate the schema file for you. As a bonus any 3rd party or custom task assemblies loaded by NAnt (how this works) will get included into the schema file that is generated.


We have this great tool called NAnt for automating manual tasks like this so let’s just create a little NAnt target to automate generating the schema and copying it to the VS.Net schemas folder.




Paste this target into the build file of your choice. I like to copy this to the bottom of the build file for my current project de jour. Note, if you manually load your custom task assemblies you should use <loadtasks/> to make sure they are included in the schema that is generated. Now you just need to invoke this target and the next time you open a NAnt script in VS.Net you should have Intellisense.


C:\projects\fcSDK>.\tools\NAnt\NAnt build-nant-schema-for-vs2005
NAnt 0.85 (Build 0.85.1932.0; rc3; 4/16/2005)
Copyright (C) 2001-2005 Gerry Shaw


Buildfile: file:///C:/projects/fcSDK/
Target framework: Microsoft .NET Framework 2.0
Target(s) specified: build-nant-schema-for-vs2005




[nantschema] Wrote schema to ‘C:\projects\fcSDK\NAnt.xsd’.
        [copy] Copying 1 file to ‘C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas’.
    [delete] Deleting file C:\projects\fcSDK\NAnt.xsd.





Total time: 1.4 seconds.