Thursday, January 04, 2007

NAnt is great.  I have a script that compiles all my source files, builds the standalone installation packages, fires off Virtual PC for testing, and it can even FTP the installation package to my client's site.  It seems like there's nothing NAnt can't do.  But I needed to peel off the version number from the executable, and I couldn't find any easy way to do this in NAnt.  Fortunately, I discovered that it's really not that hard to create a custom task in NAnt in C#, then use it from the target.

Basically, you have to create a <script> block (as shown below) and create a class that inherits from Task.  The TaskName attribute is what you will use in the main NAnt script to call it.  Any property with a TaskAttribute attribute can be used as an XML attribute in the script, and you can set Required=true to require it.  The Task object has the Project property, which can be used to access anything at all to do with the build process.  I use it here to set a NAnt property and log to StdOut.  NAnt will compile the C# code on the fly (if you did everything correctly) and voila, anything you can think of is now possible.

Here is a sample from the build script I created. 

<project name="SomeNAntProject" default="DefaultTarget">

    <property name="version.number" value="0.0.0.0" />

    <target name="DefaultTarget">
        <getversioninfo filename="C:\Folder\MyExecutable.exe" />
        <echo message="${version.number}" />
    </target>

    <script language="C#">
        <code>
        <![CDATA[
        [TaskName("getversioninfo")]
        public class GetVersionInfoTask: Task {
            
        private string _fileName;

           [TaskAttribute("filename", Required=true)]
            public string FileName {
                get {return _fileName; }
                set {_fileName = value; }
            }
            
            protected override void ExecuteTask() {
            
                System.Diagnostics.FileVersionInfo fvi = 
                    System.Diagnostics.FileVersionInfo.GetVersionInfo(_fileName);
                    
                Project.Properties["version.number"] = fvi.ProductVersion;
                
                Project.Log(Level.Info, 
                    "   [script] Got version info for " + _fileName);
            }
        }
        ]]>
        </code>
    </script>
    
</project>
posted on Thursday, January 04, 2007 7:58:31 PM (Central Standard Time, UTC-06:00) by Christopher S. Velazquez
digg it!     del.icio.us     Live it!     Newsvine     Facebook #    Comments [1] Trackback
Related posts:
Running .NET Code from Microsoft Office 2003
Dictionary of Delegates as an Alternative to the "switch" statement
Why Office 2007 Really Does Rock
Developing Software that Doesn't Suck
Resharper 2.5 is now available
SubSonic - The Zero Code DAL
Tracked by:
http://negator.startlogic.com/sitemap1.html [Pingback]
http://guga.readyhosting.com/sitemap1.html [Pingback]
http://khcrlyw.net/toys/sitemap1.html [Pingback]
http://ac7a7zx.info/post/sitemap1.html [Pingback]
http://freewebs.com/ursaler/boat/sitemap1.html [Pingback]
http://dl5czga.info/sitemap1.html [Pingback]
http://freewebs.com/sinkopa/00/sitemap1.html [Pingback]
http://sinkopa.webs.com/02/sitemap2.html [Pingback]
http://host264.hostmonster.com/~battery1/sitemap2.html [Pingback]
http://www.google.com/search?q=pqagdcsj [Pingback]
http://www.ciaobambino.com/profile_commReview.asp?id=399629506 [Pingback]
http://www.canarias.org/aytomogan/asp/hoyen/nota.asp?idhoyen=689 [Pingback]
http://www.bot.org/news/prior/event.asp?id=906 [Pingback]
http://www.the411mag.com/print.cfm?content_id=2693 [Pingback]
http://www.the411mag.com/print.cfm?content_id=2695 [Pingback]
http://www.autonav2000.com/mall/anProdDetail.asp?ProdID=877 [Pingback]
http://www.autonav2000.com/mall/anProdDetail.asp?ProdID=876 [Pingback]
http://www.observatoriodoalgarve.com/cna/noticias_ver.asp?noticia=24937 [Pingback]
http://www.autonav2000.com/mall/anProdDetail.asp?ProdID=851 [Pingback]
http://www.autonav2000.com/mall/anProdDetail.asp?ProdID=870 [Pingback]
http://www.inhabitliving.com/detail.aspx?ID=1483 [Pingback]
http://www.jerryjazzmusician.com/shop/detail.cfm?Books__RecordNumber=3899 [Pingback]
http://www.jerryjazzmusician.com/shop/detail.cfm?Books__RecordNumber=3888 [Pingback]
http://www.autonav2000.com/mall/anProdDetail.asp?ProdID=864 [Pingback]
http://www.jerryjazzmusician.com/shop/detail.cfm?Books__RecordNumber=3885 [Pingback]
http://www.videolar.com.br/paginas/noticias.asp?noticia_v=282 [Pingback]
http://www.videolar.com.br/paginas/noticias.asp?noticia_v=340 [Pingback]
http://www.google.com/search?q=eqjqobah [Pingback]
http://www.google.com/search?q=ccowgocm [Pingback]
http://www.offshore.no/linker/details.aspx?id=1612 [Pingback]
http://www.pipers.ie/classifiedsview.asp?key=232 [Pingback]
http://www.pipers.ie/classifiedsview.asp?key=236 [Pingback]
http://www.offshore.no/linker/details.aspx?id=1610 [Pingback]
http://www.inweekly.net/directory.asp?dirID=313 [Pingback]
http://www.inweekly.net/directory.asp?dirID=320 [Pingback]
http://www.offshore.no/linker/details.aspx?id=1649 [Pingback]
http://www.offshore.no/nyheter/sak.asp?Id=22407 [Pingback]
http://www.offshore.no/nyheter/sak.asp?Id=22404 [Pingback]
http://www.odod.state.oh.us/calendar/detail.asp?PID=939 [Pingback]
http://www.currentforeclosures.com/articles/view_article.asp?id=1420 [Pingback]
http://www.nzgg.org.nz/default_events/dsp_event_details.cfm?EventID=786823914 [Pingback]
http://www.chooseyourevent.com/MoreDetails.asp?ID=44769 [Pingback]
http://www.chooseyourevent.com/MoreDetails.asp?ID=44767 [Pingback]
http://www.migraine.org.uk/weblogcomments.aspx?blogentry_id=237 [Pingback]
http://www.nativeplantnetwork.org/network/view.asp?protocol_id=3583 [Pingback]
http://www.chooseyourevent.com/MoreDetails.asp?ID=44770 [Pingback]
http://www.nativeplantnetwork.org/network/view.asp?protocol_id=3595 [Pingback]
http://www.avinc.com/careers_detail1.asp?uID=475 [Pingback]
http://www.google.com/search?q=hrhlorfn [Pingback]
http://math.uwstout.edu/professional_description.asp?event_id=107 [Pingback]
http://gatecrafters.webdirectbrands.com/product.asp?intProdID=6793 [Pingback]
http://gatecrafters.webdirectbrands.com/product.asp?intProdID=6818 [Pingback]
http://math.uwstout.edu/professional_description.asp?event_id=123 [Pingback]
http://gatecrafters.webdirectbrands.com/product.asp?intProdID=6813 [Pingback]
http://www.imagistics.com/products/downloadsDetail.asp?prod=236 [Pingback]
http://www.chem.queensu.ca/CourseDetails.asp?classID=687 [Pingback]
http://www.northeastipm.org/ipm_news_popover.cfm?id=3587 [Pingback]
http://www.imagistics.com/products/downloadsDetail.asp?prod=248 [Pingback]
http://www.imagistics.com/products/downloadsDetail.asp?prod=243 [Pingback]
http://www.northeastipm.org/ipm_news_popover.cfm?id=3585 [Pingback]
http://www.ihl.state.ms.us/newsarchive/newsstory.asp?ID=676 [Pingback]
http://www.ihl.state.ms.us/newsarchive/newsstory.asp?ID=648 [Pingback]
http://www.ihl.state.ms.us/newsarchive/newsstory.asp?ID=667 [Pingback]
http://www.northeastipm.org/ipm_news_popover.cfm?id=3613 [Pingback]
http://www.ihl.state.ms.us/newsarchive/newsstory.asp?ID=687 [Pingback]
http://www.fgv.br/cur_corp/areas4.asp?aa_curso_id=126 [Pingback]
http://www.bearpowered.com/forum/topic.asp?TOPIC_ID=947 [Pingback]
Tuesday, January 09, 2007 8:37:16 AM (Central Standard Time, UTC-06:00)
Chris,
I've created a bunch of custom NAnt tasks and they are fun to do. I've also used the free MSBuild tasks from Tigris (http://msbuildtasks.tigris.org/)

In regards to the version number, I've used the following tool in the past to auto-update the version number my AssemblyVersion.

http://code.mattgriffith.net/UpdateVersion/
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):