【问题标题】:Linking Tortoise SVN revision number to assembly version将 Tortoise SVN 修订号链接到程序集版本
【发布时间】:2013-07-16 19:35:24
【问题描述】:

我正在 Visual Studio 中用 C# .net 开发一个程序,并使用 tortoise SVN 对其进行版本控制。

目前我正在根据内部版本号创建程序集版本。

有没有办法可以将项目程序集版本的最后部分链接到乌龟 SVN 中的修订号,例如:

伪代码:

[assembly: AssemblyVersion("1.0.0."+SvnRevisionNumber.ToString())]

这将确保我的程序集以提交到存储库的最后一个修订号而不是它们的内部版本号命名。

【问题讨论】:

  • 这是一个看起来经过深思熟虑的解决方案,但我还没有机会测试它:diogonunes.com/blog/…

标签: c# .net visual-studio-2010 svn tortoisesvn


【解决方案1】:

我使用的是在构建后事件中调用一个 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

【讨论】:

  • 不错的脚本,我使用了它,但在从这样的构建事件中调用它时发现了一个问题:UpdateSvnVerToAssembly.bat "$(ProjectDir)" $(ProjectDir) contains a trailing slash, but SubWCRev has a在包含空格和斜杠的路径上失败的错误。解决方法是插入一个“。”在尾部斜杠以及所有路径上的引号之后,所以它在 bat 文件中看起来像这样:%SubWCRev% "%ProjectDir%." "%ProjectDir%Properties\AssemblyInfo.subwcrev-template.cs" "%ProjectDir%Properties\AssemblyInfo.cs"
  • 一个问题:“我们想首先将最后编译的修订版与当前修订版进行比较,并且只有在它们发生更改时才更新” - 你的脚本在哪里做这个检查?
  • @Groo 你是对的。当我写这个答案时,我已经编辑了我的脚本以省略私人部分。这有些怎么没通过。我已经编辑了答案。
  • 我最近在一家使用 SVN 编号作为修订值的公司开始工作 - [assembly: AssemblyFileVersion("1.2.3.$WCREV$")] - 一旦我们到达 65535,什么那么我们可以这样做,因为最大值是 ushort。
【解决方案2】:

您可以使用类似的方法来获取 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 偷看以获取修订号。

您也许可以将其用作预构建事件,然后使用新版本号更新您的程序集信息。

还可以查看此线程以获取更多信息 SVN Revision Version in .NET Assembly w/ out CC.NET

【讨论】:

    【解决方案3】:

    我正在研究 MSbuild 扩展包。

    有一个 Subversion 扩展程序会自动为您检索 SVN 内容。链接在这里:MSBuild Subversion Extension Help

    其次,您可以将其与其他扩展一起使用,以编程方式设置目录的版本。根据您在 SCM 系统中的布局,您可能可以像这样对其自己的存储库中的每个目录进行版本控制。

    【讨论】:

    • 您的 2 个链接指向同一个 URL(MSBuild Subversion 扩展帮助)
    • 3 年来第一个拿起它的人......老实说,我什至不知道我试图引用的链接......
    【解决方案4】:

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

    <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>
    

    【讨论】:

    • 这是一个很好的答案。彻底解决了我的问题。要了解如何修改 AssemblyInfo.Base.cs 文件,请参阅 tortoisesvn.net/docs/release/TortoiseSVN_en/…。还要确保从 SVN 中删除您的 AssemblyInfo.cs 文件,否则每次运行时它都会进行更改,您需要将其保存到 SVN 并且它会循环。