将Tortoise SVN版本号与程序集版本绑定

19

我正在使用Visual Studio和Tortoise SVN对C#.net程序进行开发和版本控制。

目前,我正在创建基于构建编号的程序集版本。

有没有一种方法可以将项目程序集版本的最后一部分与Tortoise SVN的修订号码相链接呢?例如:

伪代码:

[assembly: AssemblyVersion("1.0.0."+SvnRevisionNumber.ToString())]
这将确保我的程序集的命名不是基于构建号,而是基于提交到存储库的最后修订号。

1
这里有一个看起来经过深思熟虑的解决方案,但我还没有测试的机会:http://www.diogonunes.com/blog/embed-svns-revision-into-assemblyinfos-version-number/ - kmote
4个回答

10

我使用的是在后期构建事件中调用cmd文件:

@echo off

if %1x==x goto ERROR

SET ProjectDir=%1
SET SubWCRev="C:\Program Files\TortoiseSVN\bin\SubWCRev.exe"
if exist %SubWCRev% goto USESUBWCREV

REM Default to copying a default version
copy %ProjectDir%\Properties\AssemblyInfo.default.cs %ProjectDir%\Properties\AssemblyInfo.cs
echo default
goto END

REM We don't want to modify AssemblyInfo.cs every time, only when a revision
REM changes. Thus, we want to first compare the last compiled revision with
REM the current revision, and only update if they've changed.
:USESUBWCREV
%SubWCRev% %ProjectDir% %ProjectDir%\Properties\rev.subwcrev-template %ProjectDir%\Properties\rev.current.tmp
if exist %ProjectDir%\Properties\rev.last-build.tmp goto CHECKREV
goto NEWREV

REM Fetch the current revision and compare to last-build revision
:CHECKREV
fc %ProjectDir%\Properties\rev.last-build.tmp %ProjectDir%\Properties\rev.current.tmp > NUL
REM Only update if it's a new revision
if errorlevel 1 goto NEWREV
goto END

REM Current revision doesn't match last-build revision. Update!
:NEWREV
echo newRev
if exist %ProjectDir%\Properties\rev.last-build.tmp del %ProjectDir%\Properties\rev.last-build.tmp
copy %ProjectDir%\Properties\rev.current.tmp rev.last-build.tmp
echo use template
%SubWCRev% %ProjectDir% %ProjectDir%\Properties\AssemblyInfo.subwcrev-template.cs %ProjectDir%\Properties\AssemblyInfo.cs
echo done
goto END

:ERROR
echo Usage: %0 project_dir
echo.
echo For example:
echo    %0 C:\projects\MyProjectDir
echo.
goto END

:END

2
很棒的脚本,我使用它时发现了一个问题,当它像这样从构建事件中调用时: UpdateSvnVerToAssembly.bat "$(ProjectDir)" $(ProjectDir) 包含尾部斜杠,但 SubWCRev 存在一个错误,无法处理包含空格和尾部斜杠的路径。解决方法是在尾部斜杠之后插入一个 ".",并且在所有路径上加引号,因此在批处理文件中看起来像这样: %SubWCRev% "%ProjectDir%." "%ProjectDir%Properties\AssemblyInfo.subwcrev-template.cs" "%ProjectDir%Properties\AssemblyInfo.cs" - Grant
一个问题:“我们想先将最后编译的版本与当前版本进行比较,只有在它们不同的情况下才更新” - 您的脚本在哪里执行此检查? - vgru
@Groo 你说得对。当我写这个答案时,我编辑了我的脚本以省略私人部分。但不知何故,这部分没有被省略。我已经编辑了答案。 - Avi Turner
我最近加入了一家使用 SVN 编号作为修订版本值的公司 - [assembly: AssemblyFileVersion("1.2.3.$WCREV$")] - 一旦达到 65535,由于最大值是 ushort,我们该怎么办? - Harag

2
我将会为您翻译相关的IT技术内容,建议使用MSbuild Extension Pack。此外,还有一个Subversion扩展可以自动检索SVN内容。链接在此:MSBuild Subversion Extension Help。其次,您可以与其他扩展一起使用它来以编程方式设置目录版本。根据您在SCM系统中的布局,您可以像这样将每个目录版本化到自己的存储库中。

你的两个链接引用了同一个URL(MSBuild Subversion扩展帮助)。 - krlzlx
1
三年来第一个注意到这个问题的人... 说实话,我甚至都不知道我想要引用的链接是什么... - Spence

2

检查C#项目文件中的空格和引号。

<PreBuildEvent>
        if exist "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(ProjectDir)." "$(ProjectDir)Properties\AssemblyInfo.Base.cs" "$(ProjectDir)Properties\AssemblyInfo.cs"
</PreBuildEvent>

1
这是一个很好的答案。完全解决了我的问题。要查看如何修改AssemblyInfo.Base.cs文件,请参见https://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev-keywords.html。还要确保从SVN中删除您的AssemblyInfo.cs文件,否则每次运行此操作都会进行更改,您需要保存到SVN并且它会形成一个循环。 - BoredBsee

1
您可以使用类似以下的方式获取svn版本号。
<echo message="Retrieving Subversion command line: ${rvsnCommandLine} into ${deployment.SourceDir}"/>
     <exec program="svn.exe" workingdir="${deployment.SourceDir}" commandline='update ${rvsnCommandLine}' failonerror="false"/>

     <echo message="Retrieving Subversion revision number ${svn.revision}"/>
     <exec
       program="svn.exe"
       commandline='log "${deployment.SourceDir}" ${rvsnCommandLine} --xml --limit 1'
       output="${deployment.SourceDir}\_revision.xml"
       failonerror="false"/>
     <xmlpeek
       file="${deployment.SourceDir}\_revision.xml"
       xpath="/log/logentry/@revision"
       property="svn.revision"
       failonerror="false"/>
     <echo message="Using Subversion revision number: ${svn.revision}"/>

它基本上会将svn修订版本输出到一个xml文件中,然后使用xml peeks来获取修订号。你可以将其用作预构建事件,然后使用新的版本号更新你的assemblyinfo。另外,请查看此线程以获取更多信息SVN Revision Version in .NET Assembly w/out CC.NET

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