如何动态更新Assemblyinfo.cs中的值

7

我已经编写了一个从SVN仓库获取值的程序。现在我想使用这个值来更新AssemblyFileversion。

由于我不能在Assemblyinfo.cs中编写任何代码,所以我该如何更新AssemblyFileVersion的值?

我想要实现类似于下面这样的效果:

..........................
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

 SvnInfoEventArgs info;
                    Uri repoURI = new Uri("ServerAddress");
                    svnClient.GetInfo(repoURI, out info);


[assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion(String.Format("{0}.{1}.{2}. {3}",
                                          major,minor,build,info.Revision))]

svnversion(http://svnbook.red-bean.com/en/1.7/svn.ref.svnversion.re.html)是从工作目录获取版本号的推荐工具。 - Micha Wiedenmann
3个回答

4
我假设您正在尝试生成某个构建系统-因此,您需要使用svn编号修改AssemblyInfo.cs文件。
您可以为此创建MS Build任务:请参见http://florent.clairambault.fr/insert-svn-version-and-build-number-in-your-c-assemblyinfo-file
还有一个命令行工具(SubWCRev),可以用于基于关键字的替换($WCREV$为修订版)--请参见此处的示例:http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev-example.html
最后,您可能可以推出自己的自定义代码(进行正则表达式搜索/替换)来执行相同的操作,其中一些人(包括我在内)已经这样做了--请参见此类示例之一:http://www.codeproject.com/Articles/168528/Automatically-keep-Assembly-revisions-in-sync-with

我已经添加了MsBuildTask并更新了ToolPath ="$(ProgramFiles)\TortoiseSVN\bin",但我的assemblyinfo正在更新为1.0.-1!这是因为Tortoise SVN的原因吗? - Simsons
@Simsons,请检查您的bin文件夹中是否有svn.exe。如果我没记错的话,在安装Tortoise SVN时,您必须选择安装命令行客户端工具(重新运行安装程序以获取它们)。否则,您可以随时下载svn命令行客户端。 - VinayC
谢谢Vinay,问题是我在一个没有从SVN签出的文件夹上运行脚本。我进行了评论,以便任何来到这个页面的人不会被我的先前评论所困惑。 - Simsons
为了澄清Simsons的评论,我们遇到了同样的问题,因为我们的Teamcity只配置了检出代码(它实际上不在一个由SVN管理的文件夹中),因此这个解决方案不起作用。如果没有由SVN管理的文件夹,命令行svn可执行文件(我们使用的是Tortoise)将失败。我们必须更改配置,使其在构建代理机器上检出并保持受管理的SVN文件夹。从而解决1.0.-1问题。 - Simply G.

2

我正在使用基于 ant 的构建系统,并且对更新 AssemblyInfo.cs 有类似的问题。

我的构建系统已经生成了在构建时包含在其他项目中的 version.h 文件。对于 C#,我决定使用生成的 VersionInfo.cs 来注入版本信息到 AssemblyInfo.cs 中。

AssemblyInfo.cs (设置一次指定 VersionInfo 常量)

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany(VersionInfo.Company)]
[assembly: AssemblyProduct(VersionInfo.ProductName)]
[assembly: AssemblyCopyright(VersionInfo.Copyright)]

[assembly: Guid("12345678-1234-1234-1234-1234567890ab")]

[assembly: AssemblyVersion(VersionInfo.product.FileVersion)]
[assembly: AssemblyFileVersion(VersionInfo.sdk.FileVersion)]

VersionInfo.cs (由ant构建系统动态生成)

// Shared Product Version Header
// Automatically Generated: Sat, 2 May 2015 15:09:09 EDT
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
internal struct VersionInfo
{
  public const string Company = @"My Company Corp";
  public const string Copyright = @"Copyright (c) 2015 My Company Corp.  All rights reserved.";
  public const string ProductName = @"My Software Service";
  public const string SpecialBuild = @"";
  public const string ProductConfiguration = @"";
  public const string ProductCode = @"MSS";
  public const string ProductUrl = @"www.MyCompany.com";
  public const string ProductEmail = @"support@MyCompany.com";
  public const string ProductVendor = @"My Company Corp";
  internal struct product
  {
    public const string FileVersion = @"1.5.0.240";
  }
  internal struct sdk
  {
    public const string FileVersion = @"1.4.5.222";
  }
  internal struct service
  {
    public const string FileVersion = @"1.3.2.142";
  }
}

version.xml (ant import - snipped)

<echo file="${artifacts.dir}\VersionInfo.cs" append="false">// Shared Product Version Header${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// Automatically Generated: ${version.generated.time}${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable CheckNamespace${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable InconsistentNaming${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">internal struct VersionInfo${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">{${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">  public const string Company = @"${product.company}";${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">  public const string Copyright = @"${product.copyright}";${line.separator}</echo>
 <!-- other version info here -->
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">}${line.separator}</echo>

version.properties (ant属性文件,用于指定产品版本信息)

product.company=My Company Corp
product.copyright=Copyright (c) 2015 My Company Corp.  All rights reserved.
product.website=www.MyCompany.com
product.email=support@MyCompany.com
product.vendor=My Company Corp

product=1.5.0.240
service=1.3.2.142
sdk=1.4.5.222

C#预构建事件(将自动生成的VersionInfo.cs复制到项目属性目录)

@ECHO OFF
SETLOCAL
SET ARTDIR=$(ProjectDir)..\..\..\MySoftwareService\installer
SET VERCS=%ARTDIR%\VersionInfo.cs
ECHO Updating %VERCS%
xcopy /D /Y "%VERCS%" "$(ProjectDir)Properties"

MSBuild扩展 (无)

<!-- none, not required -->

顺便说一下,我的版本方案是:主版本.次版本.修订版本.构建版本,这不是AssemblyInfo.cs文件指定的。


1

您需要在构建过程中执行此操作。

既然您正在使用Visual Studio,那么您已经在使用MSBuild,这种情况下MSBuild Extension Pack有一个AssemblyInfo任务。

如果要从Subversion获取修订版本,则MSBuild Extension Pack也可以提供相应的任务

或者,如果您正在使用TeamCity进行持续集成(CI),它具有“AssemblyInfo patcher”生成功能。我想大多数其他CI系统也会有类似的功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接