一个去年写的小tips,一个利用CruiseControl.NET做baseline的技巧, in english.

Tips: .NET Code Makes Nant Build More Powerful

Introduction

Many projects have started using Cruisecontrol .NET and Nant to build the baselines. We can do a lot with the functions provided by them. But still, in some cases, it will be hard or too tedious to implement some specific feature that is required by us. Luckily, Nant has provided the support of embedded .NET code (in C# or VB.NET). I'd like to cover this topic and provide some examples in the article.

How-To

When setting up the baseline for my project, we faced the issue of modifying the assemblies’ version number, based on the build number we have. Normally, the version info locates at the AssemblyInfo.cs (at least for our project J). But unfortunately, we can not do the version modification manually because it is so frequent.

 

In the previous baseline which is not Cruisecontrol based, there is some complex  perl language for this purpose (The word “complex” is from my point of view, because I do not know much about it). It will be hard for me to do it this way and I tried to look for other solutions. Fortunately, I got one.

 

The solution comes from the fact that Nant can support some languages as script, compile them at runtime and call them as target tasks. The languages can be:  VB.NET, C#, etc. Of course, when we want to use the language, we should have the compiler or interpreter on the baseline machine.

 

With no doubt, I chose the C# because it’s what I’m familiar with. For .NET, we have to reference some assemblies and add some namespaces in our code. Luckily, Nant has included some default assemblies and references for us, which is enough in most cases, at least in my case. They are:

  • System
  • System.Collections
  • System.Collections.Specialized
  • System.IO
  • System.Text
  • System.Text.RegularExpressions
  • NAnt.Core

 

If you want to add some extra assemblies and references, you can do like this:

<script language="C#" >

       <references>

              <include name="System.Data.dll" />

        </references>

 

       <imports>

              <import namespace="System.Data.SqlClient" />

       </imports>

       <code>

       <![CDATA[

            //Add code here.

        ]]>

   </code>

</script>

 

The sample code above adds the reference “System.Data.Dll” and the namespace “System.Data.SqlClient”.

 

With the knowledge we have now, we can start to write code for some special tasks. No matter what kind of work we want our code to do, we should define a class and have an entry method called ScriptMethod, which takes one Project parameter. The Project is defined by Nant, for accessing the properties we defined in other Nant script.

 

Following is the code that I used to build the task called “SetVersion”. Forgive me because of the hidden issues

 

<target name="SetVersion">

     <script language="C#" mainclass="VersionSetter">

          <code>

               <![CDATA[

//The following code use c# script to modify the assembly version.

class VersionSetter

  {

     public static void ScriptMain(Project project)

    {

      string version2003 = "1.0.*";  //For version of VS2005;

      string version2005 = "1.0.0.0"; //For version of VS2003;

 

      //We can access some predefined properties through project

      string newVersion = project.Properties["WellScanVersion"]; 

      string srcDir = project.Properties["src.dir"];

 

      string[] aiFiles = Directory.GetFiles(srcDir, "*AssemblyInfo.cs",                 

                         SearchOption.AllDirectories);

      //Parse the AssemblyInfo files one by one

foreach (string aiFile in aiFiles)

      {

         FileInfo asmFile = new FileInfo(aiFile);

         string contents = "";

 

         using (StreamReader reader = asmFile.OpenText())

              contents = reader.ReadToEnd();

         //Set new version.

         contents = contents.Replace(version2003, newVersion);

         contents = contents.Replace(version2005, newVersion);

 

        if ((asmFile.Attributes & FileAttributes.ReadOnly)

             == FileAttributes.ReadOnly)

        asmFile.Attributes = asmFile.Attributes ^ leAttributes.ReadOnly;

 

        asmFile.Delete();

 

        //Write as the new AssemblyInfo file.

  using (FileStream stream = asmFile.Create())

        {

            StreamWriter writer = new StreamWriter(stream);

            writer.Write(contents);

            writer.Flush();

        }

      }

     }

   }

                ]]>

            </code>

        </script>

    <echo message="Set version to ${MYPROJECT}"/>

</target>

 

At the end of the article, I’d like to mention that because we can not debug the code which locates in the Nant script, we’d better to make our code work in the VS IDE first, which can help to reduce the time effort.

 

For more information, please read the doc at NantDir/doc/help/tasks/script.html.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值