NAnt扩展Task源代码: 读取及增加资源文件中的版本号
来源:互联网 发布:视客眼镜网怎么样 知乎 编辑:程序博客网 时间:2024/05/16 05:13
在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);
}
}
}
}
- NAnt扩展Task源代码: 读取及增加资源文件中的版本号
- 读取资源中的文件
- 怎样读取资源文件中的文件
- 怎么读取dll文件中的资源
- 读取DLL中的嵌入式资源文件
- 读取jar包中的资源文件
- 读取 其他Apk 中的资源文件
- ServletContext读取web应用中的资源文件
- ServletContext 读取Web应用中的资源文件
- javaweb读取 web 应用中的资源文件
- 对于读取扩展名为xml的资源文件
- 利用NAnt取得远程VSS服务器中的文件并且编译
- 自动复制指定文件到指定目录中的NANT脚本
- 如何读取文件的版本号
- 动态增加表格及读取动态表格中的值
- 动态增加表格及读取动态表格中的值
- AndroidManifest文件中的版本号与代码中得到的版本号不一致问题及解决办法
- NANT修改XML文件
- 一些C++连接,以及个人的C++学习总结。
- MDA核心之MOF原理和实现(中文)(附图)
- [水晶报表]创建不受数据库限制的报表 .Net 版本 -1
- [SATA] RHAS3.2下安装SATA硬盘的问题
- MDA核心之用MOF构建一个软件开发系统
- NAnt扩展Task源代码: 读取及增加资源文件中的版本号
- [水晶报表]创建不受数据库限制的报表 .Net 版本 -2
- 第一帖
- Groovy在EOS问题上的痛苦权衡
- 10-16东南大学微软招聘笔试经过
- System.Collections.ArrayList代码分析
- DotNetNuke2.1.2汉化版提供下载
- 睡前一帖 好习惯
- 忘掉将来