65.9K
CodeProject 正在变化。 阅读更多。
Home

如何自动更新程序集版本号

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.46/5 (35投票s)

2008年11月25日

CPOL

2分钟阅读

viewsIcon

264798

downloadIcon

5133

一个小工具,允许修改在 AssemblyInfo.cs 文件中指定的 AssemblyVersion 属性。

问题

我想我不是唯一一个不喜欢 Visual Studio 在你指定如下内容在 AssemblyInfo.cs 文件中时自动生成的版本号的人。

[assembly: AssemblyVersion("2.7.*.*")] 

另一方面,我希望在构建过程中自动指定这些数字。而且,能够更改整个版本号或仅增加最新部分(构建号)会很好。 

当然,你可能会说这样的操作可以手动完成。对于一个或两个程序集来说,这是真的。但是我们的常规项目包含大约 10-15 个不同的程序集,我们希望在所有程序集中同步版本号。

解决方案

因此,我们需要一个能够自动执行所有描述的任务的工具,并且我们可以从构建脚本中调用它。这个简单的程序允许更改 AssemblyInfo 文件中的整个版本号,或者只增加构建号。

要使用此程序,只需调用 AssemblyInfoUtil.exe 并提供两个参数。第一个参数是你要修改的 AssemblyInfo.cs 文件的路径。第二个参数告诉程序如何修改版本号属性。它可以是以下选项之一:

-set:<New Version Number>

将版本号设置为指定的值。

-inc:<Parameter Index>

简单地将版本字符串中的一个参数增加 1。这里“参数索引”可以是 1 到 4 之间的任何数字。对于 1 - 将增加主版本号,对于 2 - 将增加次版本号,依此类推。

示例

AssemblyInfoUtil.exe -set:3.1.7.53 "C:\Program Files\MyProject1\AssemblyInfo.cs"

将版本字符串设置为“3.1.7.53”。

AssemblyInfoUtil.exe -inc:4 AssemblyInfo.cs

增加最后一个(修订)号。因此,在我们的例子中,它将变为 54

代码

所以这是这个工具的代码。我只发布了 Main.cs 文件的内容。其他项目文件(如 *.csprj)可以在附带的存档中找到。

using System;
using System.IO;
using System.Text;

namespace AssemblyInfoUtil
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class AssemblyInfoUtil
    {
	private static int incParamNum = 0;

	private static string fileName = "";
	
	private static string versionStr = null;

	private static bool isVB = false;

	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main(string[] args)
	{
	    for (int i = 0; i < args.Length; i++) {
	        if (args[i].StartsWith("-inc:")) {
		   string s = args[i].Substring("-inc:".Length);
		   incParamNum = int.Parse(s);
	        }
	        else if (args[i].StartsWith("-set:")) {
		   versionStr = args[i].Substring("-set:".Length);
	        }
	        else
		   fileName = args[i];
	    }

	    if (Path.GetExtension(fileName).ToLower() == ".vb")
		isVB = true;

	    if (fileName == "") {
		System.Console.WriteLine("Usage: AssemblyInfoUtil 
		    <path to AssemblyInfo.cs or AssemblyInfo.vb file> [options]");
		System.Console.WriteLine("Options: ");
		System.Console.WriteLine("  -set:<new version number> - 
				set new version number (in NN.NN.NN.NN format)");
		System.Console.WriteLine("  -inc:<parameter index>  - 
		   increases the parameter with specified index (can be from 1 to 4)");
		return;
	    }

	    if (!File.Exists(fileName)) {
		System.Console.WriteLine
			("Error: Can not find file \"" + fileName + "\"");
		return;
	    }

	    System.Console.Write("Processing \"" + fileName + "\"...");
	    StreamReader reader = new StreamReader(fileName);
             StreamWriter writer = new StreamWriter(fileName + ".out");
	    String line;

	    while ((line = reader.ReadLine()) != null) {
		line = ProcessLine(line);
		writer.WriteLine(line);
	    }
	    reader.Close();
	    writer.Close();

	    File.Delete(fileName);
	    File.Move(fileName + ".out", fileName);
	    System.Console.WriteLine("Done!");
	}

	private static string ProcessLine(string line) {
	    if (isVB) {
		line = ProcessLinePart(line, "<Assembly: AssemblyVersion(\"");
		line = ProcessLinePart(line, "<Assembly: AssemblyFileVersion(\"");
	    } 
	    else {
		line = ProcessLinePart(line, "[assembly: AssemblyVersion(\"");
		line = ProcessLinePart(line, "[assembly: AssemblyFileVersion(\"");
	    }
	    return line;
	}

	private static string ProcessLinePart(string line, string part) {
	    int spos = line.IndexOf(part);
	    if (spos >= 0) {
		spos += part.Length;
		int epos = line.IndexOf('"', spos);
		string oldVersion = line.Substring(spos, epos - spos);
		string newVersion = "";
		bool performChange = false;

		if (incParamNum > 0) {
	  	    string[] nums = oldVersion.Split('.');
		    if (nums.Length >= incParamNum && nums[incParamNum - 1] != "*") {
			Int64 val = Int64.Parse(nums[incParamNum - 1]);
			val++;
			nums[incParamNum - 1] = val.ToString();
			newVersion = nums[0]; 
			for (int i = 1; i < nums.Length; i++) {
			    newVersion += "." + nums[i];
			}
			performChange = true;
		    }
		}
		
		else if (versionStr != null) {
		    newVersion = versionStr;
		    performChange = true;
		}

		if (performChange) {
		    StringBuilder str = new StringBuilder(line);
		    str.Remove(spos, epos - spos);
		    str.Insert(spos, newVersion);
		    line = str.ToString();
		}
	    } 
	    return line;
	}
     }
}

历史

  • 2008 年 11 月 25 日:初始发布
  • 2009 年 1 月 27 日:文章更新
© . All rights reserved.