NAnt扩展Task源代码: 读取及增加资源文件中的版本号

类别:.NET开发 点击:0 评论:0 推荐:

在VS.NET中新建一个动态库工程,编译即可。
利用此Task,可以在自动Build中实现每日构建时自动增加版本号
以及获取现在的版本号(以便出每日构建结果报告)。

适用于VC的资源文件(.rc).



using NAnt.Core;
using NAnt.Core.Attributes;

using System;
using System.IO;

using System.Text;
using System.Text.RegularExpressions;

namespace NAnt.ResourceFileVersionTask {

 public class ResourceFileManipulator
 {
  /* field can be FILEVERSION or PRODUCTVERSION */
  private static string ExtraceHeaderVersion(string source,string field)
  {
   Regex regex = new Regex(@"^\s*" + field + @"\s*(?<WholeVersion>(?<Major>[0123456789]*)\s*,\s*(?<Minor>[0123456789]*)\s*,\s*(?<Revision>[0123456789]*)\s*,\s*(?<Build>[0123456789]*))\s*(\n|$)",RegexOptions.Multiline);

   Match m = regex.Match(source);

   string strMajor = m.Groups["Major"].Value;
   string strMinor = m.Groups["Minor"].Value;
   string strRevision = m.Groups["Revision"].Value;
   string strBuild = m.Groups["Build"].Value;

   string strVersion = strMajor + "." + strMinor + "." + strRevision + "." + strBuild;

   return strVersion;
  }

  public static string ExtractHeaderFileVersion(string source)
  {
   return ExtraceHeaderVersion(source,"FILEVERSION");
  }

  public static string ExtraceHeaderProductVersion(string source)
  {
   return ExtraceHeaderVersion(source,"PRODUCTVERSION");
  }

  /* field can be FILEVERSION or PRODUCTVERSION */
  private static string IncrementHeaderVersion(string source,string field)
  {
   Regex regex = new Regex(@"^\s*" + field + @"\s*(?<WholeVersion>(?<Major>[0123456789]*)\s*,\s*(?<Minor>[0123456789]*)\s*,\s*(?<Revision>[0123456789]*)\s*,\s*(?<Build>[0123456789]*))\s*(\n|$)",RegexOptions.Multiline);

   Match m = regex.Match(source);

   string strMajor = m.Groups["Major"].Value;
   string strMinor = m.Groups["Minor"].Value;
   string strRevision = m.Groups["Revision"].Value;
   string strBuild = m.Groups["Build"].Value;

   string strNewVersion = strMajor + "," + strMinor + "," + strRevision + "," + (Int32.Parse(strBuild)+1).ToString();

   string strNewFile = source.Substring(0,m.Groups["WholeVersion"].Index);
   strNewFile += strNewVersion;
   strNewFile += source.Substring(m.Groups["WholeVersion"].Index + m.Groups["WholeVersion"].Length);

   return strNewFile;
  }

  public static string IncrementHeaderFileVersion(string source)
  {
   return IncrementHeaderVersion(source,"FILEVERSION");
  }

  public static string IncrementHeaderProductVersion(string source)
  {
   return IncrementHeaderVersion(source,"PRODUCTVERSION");
  }

  /* field can be FileVersion or ProductVersion */
  private static string UpdateBlockVersions(string source,string field,string strVersion)
  {
   Regex regex = new Regex(@"^\s*VALUE\s*""" + field + @"""\s*,\s*""(?<WholeVersion>[0123456789\s,\.]*)""\s*(\n|$)",RegexOptions.Multiline);

   Match m = regex.Match(source);

   if (m.Groups.Count == 0)
    return source;
   if (m.Groups["WholeVersion"] == null)
    return source;
   if (m.Groups["WholeVersion"].Value == "")
    return source;

   string strNewFile = source.Substring(0,m.Groups["WholeVersion"].Index);
   strNewFile += strVersion;
   strNewFile += UpdateBlockVersions(source.Substring(m.Groups["WholeVersion"].Index + m.Groups["WholeVersion"].Length),field,strVersion);

   return strNewFile;
  }

  public static string UpdateBlockFileVersions(string source,string strVersion)
  {
   return UpdateBlockVersions(source,"FileVersion",strVersion);
  }

  public static string UpdateBlockProductVersions(string source,string strVersion)
  {
   return UpdateBlockVersions(source,"ProductVersion",strVersion);
  }

  // versino must be "a.b.c.d"
  public static string IncrementVersion(string strVersion)
  {
   Regex regex = new Regex(@"(?<Major>[0123456789]*)\.(?<Minor>[0123456789]*)\.(?<Revision>[0123456789]*)\.(?<Build>[0123456789]*)",RegexOptions.Multiline);

   Match m = regex.Match(strVersion);

   string strMajor = m.Groups["Major"].Value;
   string strMinor = m.Groups["Minor"].Value;
   string strRevision = m.Groups["Revision"].Value;
   string strBuild = m.Groups["Build"].Value;

   string strNewVersion = strMajor + "." + strMinor + "." + strRevision + "." + (Int32.Parse(strBuild)+1).ToString();

   return strNewVersion;
  }
 }

    [TaskName("IncrementVersion")]
    public class IncrementVersionTask : Task {

        string _ResourceFile = null;

        [TaskAttribute("ResourceFile", Required=true)]
        public string ResourceFile {
            get { return _ResourceFile; }
            set { _ResourceFile = value; }
        }

        protected override void ExecuteTask()
  {
   try
   {
    TextReader reader = new StreamReader(_ResourceFile,Encoding.GetEncoding("GB2312"));
    string strFile = reader.ReadToEnd();
    reader.Close();

    string strOldVersion = ResourceFileManipulator.ExtractHeaderFileVersion(strFile);
    string strNewVersion = ResourceFileManipulator.IncrementVersion(strOldVersion);
    
    strFile = ResourceFileManipulator.IncrementHeaderFileVersion(strFile);
    strFile = ResourceFileManipulator.IncrementHeaderProductVersion(strFile);

    strFile = ResourceFileManipulator.UpdateBlockFileVersions(strFile,strNewVersion);
    strFile = ResourceFileManipulator.UpdateBlockProductVersions(strFile,strNewVersion);

    string strActualNewVersion = ResourceFileManipulator.ExtractHeaderFileVersion(strFile);

    TextWriter writer = new StreamWriter(_ResourceFile,false,Encoding.GetEncoding("GB2312"));
    writer.Write(strFile);
    writer.Close();

    //Log(Level.Info, LogPrefix + "ResourceFile = [" + _ResourceFile + "] OldVersion = [" + strOldVersion + "] ---> NewVersoin = [" + strActualNewVersion + "]");
   }
   catch(System.Exception e)
   {
    throw new NAnt.Core.BuildException(e.Message,e);
   }
   finally
   {
   }
        }
    }

 [TaskName("ExtractVersion")]
 public class ExtractVersionTask : Task
 {
  private string _Property = null;

  [TaskAttribute("Property", Required=false)]
  public string Property
  {
   get { return _Property; }
   set { _Property = value; }
  }

  string _ResourceFile = null;

  [TaskAttribute("ResourceFile", Required=true)]
  public string ResourceFile
  {
   get { return _ResourceFile; }
   set { _ResourceFile = value; }
  }

  protected override void ExecuteTask()
  {
   try
   {
    TextReader reader = new StreamReader(_ResourceFile,Encoding.GetEncoding("GB2312"));
    string strFile = reader.ReadToEnd();
    reader.Close();

    string strVersion = ResourceFileManipulator.ExtractHeaderFileVersion(strFile);
    Properties[_Property] = strVersion;

    //Log(Level.Info, LogPrefix + "ResourceFile = [" + _ResourceFile + "] Property = [" + _Property + "]");
   }
   catch(System.Exception e)
   {
    throw new NAnt.Core.BuildException(e.Message,e);
   }
  }
 }
}

本文地址:http://com.8s8s.com/it/it43508.htm