没有CC.NET的.NET程序集中的SVN修订版本

没有CC.NET的.NET程序集中的SVN修订版本

SVN Revision Version in .NET Assembly w/ out CC.NET

有没有办法在.NET程序集的版本字符串中包含SVN存储库修订号? 像Major.Minor.SVNRev这样的东西

我已经看到提到用CC.NET这样的东西(虽然实际上是在ASP.NET上),但有没有办法在没有任何额外软件的情况下做到这一点? 在使用构建批处理脚本之前,我在C / C ++中做了类似的事情,但是通过读取版本号来完成,然后让脚本每次都写出一个名为"ver.h"的文件,其效果如下:

1
2
3
#define MAJORVER 4
#define MINORVER 23
#define SOURCEVER 965

然后我们将使用这些定义来生成版本字符串。

这样的.NET可能吗?


这是C#示例,用于自动更新程序集中的修订信息。它基于Will Dean的答案,这不是很精细。

示例:

  • 将AssemblyInfo.cs复制到项目中的AssemblyInfoTemplate.cs
    文件夹属性。
  • 对于AssemblyInfoTemplate.cs,将Build Action更改为None。
  • 使用AssemblyFileVersion将行修改为:

    [assembly: AssemblyFileVersion("1.0.0.$WCREV$")]

  • 考虑添加:

    [assembly: AssemblyInformationalVersion("Build date: $WCNOW=%Y-%m-%d %H:%M:%S$; Revision date: $WCDATE=%Y-%m-%d %H:%M:%S$; Revision(s) in working copy: $WCRANGE$$WCMODS?; WARNING working copy had uncommitted modifications:$.")]

    这将提供有关程序集构建源的修订状态的详细信息。

  • 将以下预构建事件添加到项目文件属性:

    subwcrev"$(SolutionDir).""$(ProjectDir)Properties\AssemblyInfoTemplate.cs""$(ProjectDir)Properties\AssemblyInfo.cs" -f

  • 考虑将AssemblyInfo.cs添加到svn ignore列表中。替换的修订号和日期将修改该文件,这将导致无关紧要的更改和修订,$ WCMODS $将评估为true。当然,AssemblyInfo.cs必须包含在项目中。

  • 为了回应Wim Coenen的反对意见,我注意到,与Darryl的建议相反,AssemblyFileVersion也不支持2 ^ 16以上的数字。构建将完成,但实际程序集中的属性File Version将为AssemblyFileVersion模65536.因此,1.0.0.65536以及1.0.0.131072将产生1.0.0.0等。在此示例中,始终存在真正的修订号在AssemblyInformationalVersion属性中。如果您认为这是一个重要问题,您可以省略第3步。

    编辑:使用此解决方案一段时间后的一些其他信息。

  • 它现在使用AssemblyInfo.cst而不是AssemblyInfoTemplate.cs,因为它将自动具有Build Action选项None,并且它不会使你的错误列表混乱,但你会松开语法高亮。
  • 我在AssemblyInfo.cst文件中添加了两个测试:

    1
    2
    3
    4
    5
    6
    #if(!DEBUG)    
        $WCMODS?#error Working copy has uncommitted modifications, please commit all modifications before creating a release build.:$
    #endif
    #if(!DEBUG)      
        $WCMIXED?#error Working copy has multiple revisions, please update to the latest revision before creating a release build.:$
    #endif

    使用此功能,您通常必须在提交之后以及成功发布版本之前执行完整的SVN更新。否则,$ WCMIXED将成立。这似乎是由于提交后的文件在提交后重新修改,但其他文件没有。

  • 我有一些疑问是否第一个参数subwcrev,"$(SolutionDir)",它设置检查svn版本信息的范围,总是按预期工作。也许,它应该是$(ProjectDir),如果你满意,如果每个单独的程序集都在一致的修订版中。
  • 加成
    要回答@tommylux的评论。

    SubWcRev可用于项目中的任何文件。如果要在网页中显示修订信息,可以使用此VersionInfo模板:

    1
    2
    3
    4
    5
    6
    7
    8
    public class VersionInfo
    {      
        public const int RevisionNumber = $WCREV$;
        public const string BuildDate ="$WCNOW=%Y-%m-%d %H:%M:%S$";
        public const string RevisionDate ="$WCDATE=%Y-%m-%d %H:%M:%S$";
        public const string RevisionsInWorkingCopy ="$WCRANGE$";
        public const bool UncommitedModification = $WCMODS?true:false$;
    }

    添加一个与AssemblyInfo.cst类似的预构建事件,您可以轻松访问所有相关的SubVersion信息。


    这是可能的,但你不应该:汇编版本字符串的组件限制为16位数字(最大65535)。 Subversion版本号很容易变得比那些大,所以在某些时候编译器突然会抱怨。


    看看SubWCRev - http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html

    程序集版本号通常位于assemblyinfo.cs中


    阅读/浏览这些文档:

    使用DotSVN从.NET访问Subversion存储库

    如何:编写任务

    在C#AssemblyInfo文件中插入SVN版本和内部版本号

    使用Microsoft Build Engine的自定义任务编译应用程序

    第三个参考文献中提到的MSBuildCommunityTasks svnversion不会在Parallels托管Vista(即,跨操作系统)的Mac 10.5.6和VS2008 C#项目构建中使用svn执行。

    编写您自己的任务以使用DotSVN从存储库中检索修订版:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    using System;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;
    using DotSVN.Common;
    using DotSVN.Common.Entities;
    using DotSVN.Common.Util;
    using DotSVN.Server.RepositoryAccess;

    namespace GetSVNVersion
    {
        public class GetRevision : Task
        {
            [Required]
            public string Repository { get; set; }
            [Output]
            public string Revision { get; set; }

            public override bool Execute()
            {
                ISVNRepository repo;
                bool connected = true;
                try
                {
                    repo = SVNRepositoryFactory.Create(new SVNURL(Repository));
                    repo.OpenRepository();
                    Revision = repo.GetLatestRevision().ToString();
                    Log.LogCommandLine(Repository +" is revision" + Revision);
                    repo.CloseRepository();
                }
                catch(Exception e)
                {
                    Log.LogError("Error retrieving revision number for" + Repository +":" + e.Message);
                    connected = false;
                }
                return connected;
            }
        }
    }

    这种方式允许存储库路径为"file:/// Y:/ repo",其中Y:是映射到Vista的Mac目录。


    另一个答案提到SVN修订号可能不是一个好主意,因为数量的大小有限。

    以下链接不仅提供SNV修订号,还提供日期版本信息模板。

    将其添加到.NET项目很简单 - 只需要做很少的工作。

    这是一个解决这个问题的github项目
    https://github.com/AndrewFreemantle/When-The-Version/downloads

    以下网址可能会缓慢加载,但是如何使这项工作逐步解释(简单和简短的3或4个步骤)

    http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/


    如果要更新项目AssemblyInfo.cs中的版本号,您可能对本文感兴趣:

    CodeProject:在Visual Studio项目中使用Subversion版本号

    If you enable SVN Keywords then every time you check in the project Subversion scans your files for certain"keywords" and replaces the keywords with some information.

    For example, At the top of my source files I would create a header contain the following keywords:

    '$Author:$
    '$Id:$
    '$Rev:$

    When I check this file into Subversion these keywords are replaced with the following:

    '$Author: paulbetteridge $
    '$Id: myfile.vb 145 2008-07-16 15:24:29Z paulbetteridge $
    '$Rev: 145 $


    svn info,告诉你你所使用的版本,你可以在你的项目中在VS中进行"预构建"事件,通过运行svn info并使用自己开发的命令行应用程序解析其结果来生成assemblyinfo.cs。

    我之前已经这样做了,但很快就切换到让ccnet将它作为变量传递给nant。


    您可以使用可在所有项目中引用的共享程序集版本文件。

    UppercuT这样做 - http://ferventcoder.com/archive/2009/05/21/uppercut---automated-builds---versionbuilder.aspx

    这将让您了解如何在程序集中获取版本。


    推荐阅读