【PE】利用SVN的SubWCRev.exe管理同步项目的版本号

### Date: 2017/3/18

###Author: SoaringLee

参考:http://blog.csdn.net/listener51/article/details/53199086

一、Windows平台

关于subwcrev.exe参见: 
http://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/tsvn-subwcrev.html 
http://tortoisesvn.net/docs/nightly/TortoiseSVN_zh_CN/tsvn-subwcrev-keywords.html

基本格式是: 
SubWCRev WorkingCopyPath [SrcVersionFile DstVersionFile] [-nmdfe]

所以需要在VS对应的工程中新建两个文件,一个是SrcVersionFile和DstVersionFile,其中: 

1.WorkingCopyPath工程所在的目录 
2.SrcVersionFile含有SubWCRev.exe程序可以替换的宏; 
3.DstVersionFile为更新svn之后把宏替换掉以后的目标文件。 
例如: 
SrcVersionFile为一个头文件,名字叫Template.Source.Version.h,其内容如下:

#pragma once

#define VER_UMS_HQ_MAJOR    2
#define VER_UMS_HQ_MINOR    2
#define VER_UMS_HQ_SVN  $WCREV$

#define VER_UMS_HQ_REVISION "2.2.$WCREV$.0"

DstVersionFile文件为另外一个头文件,名字叫Source.Version.h,其内容如下:

#pragma once

#define VER_UMS_HQ_MAJOR    2
#define VER_UMS_HQ_MINOR    2
#define VER_UMS_HQ_SVN  8465

#define VER_UMS_HQ_REVISION "2.2.8465.0"

然后在工程属性设置: 

Build Event选项->Pre-Build Event的Command Line中输入: 
subwcrev.exe “./” “./Template.Source.Version.h” “./Source.Version.h”

这样每次用svn更新代码时,svn的项目版本号也更新下来了,并最终替换掉 Template.Source.Version.h中宏$WCREV$,生成Source.Version.h中的内容。

TortoiseSVN下有一个SubWCRev程序,可以获取任意个路径对应的SVN版本库的版本号。并替换到文件对应位置。

VC2010的资源文件可以控制生成的二进制文件版本号。

可以利用VC2010的生成前命令,调用SubWCRev来获取最新的SVN版本号并写入生成的二进制文件中。

程序也可以判断调用的DLL版本号是否比本身大。以控制不会调用不对应的动态库。可以使依赖多了一层运行时保证。

实现该目的,需要

  • 每次编译前,从svn获取当前版本库的版本号,并写入资源文件
  • 编译完成后,打安装包时,读取版本信息

需要注意两点:

1,svn的工具可获取版本号并更新到项目文件

 如果提供了源文件和目的文件,SubWCRev 会复制源文件到目标文件,执行如下所属的关键字替换:


关键字 描述
$WCREV$ 工作副本中最高的提交版本来替换
$WCDATE$ 用最高提交版本的日期/时间替换。默认使用国际化格式:yyyy-mm-dd hh:mm:ss。作为选择,你可以指定strftime()使用自定义格式,例如:$WCDATE=%a %b %d %I:%M:%S %p$。格式字符的列表参见在线引用
$WCNOW$ Replaced with the current system date/time. This can be used to indicate the build time. Time formatting can be used as described for$WCDATE$.
$WCRANGE$ 在工作目录用更新版本范围替换。如果工作目录处于一致的状态,它是一个单一版本。如果工作目录包含混合版本,或者是过时,或者是故意更新到版本,那么这个范围会用象100:200这样的格式来显示。
$WCMIXED$ 当有混合版本时用TText替换$WCMIXED?TText:FText$,否则用FText替换。
$WCMODS$ 若本地存在修改,就用TText替换$WCMODS?TText:FText$,否则用FText替换。
$WCURL$ 用传递给SubWCRev的工作目录的版本库地址替换。
$WCINSVN$ $WCINSVN?TText:FText$is replaced withTTextif the entry is versioned, orFTextif not.
$WCNEEDSLOCK$ $WCNEEDSLOCK?TText:FText$is replaced withTTextif the entry has thesvn:needs-lockproperty set, orFTextif not.
$WCISLOCKED$ $WCISLOCKED?TText:FText$is replaced withTTextif the entry is locked, orFTextif not.
$WCLOCKDATE$ Replaced with the lock date. Time formatting can be used as described for$WCDATE$.
$WCLOCKOWNER$ Replaced with the name of the lock owner.
$WCLOCKCOMMENT$ Replaced with the comment of the lock.


2、SubWCRev.exe从命令行或脚本中运行,使用命令行参数控制。

SubWCRev WorkingCopyPath [SrcVersionFile DstVersionFile] [-nmdfe]

WorkingCopyPath是要检查的工作副本路径,你可以只对工作副本使用SubWCRev,而不是直接对版本库,这个路径可以是绝对路径,也可以是工作目录的相对路径。

如果你想让SubWCRev执行关键字替换,象版本库版本,地址等字段保存到文本文件,就需要提供一个模版文件SrcVersionFile,输出文件DstVersionFile就是模版替换之后的版本。

有几个开关影响 SubWCRev工作。如果使用多个,必须用单个组指定,例如要用-nm,不能用-n -m

表 5.2. 列出可用的命令行开关

切换 描述
-n If this switch is given, SubWCRev will exit withERRORLEVEL 7if the working copy contains local modifications. This may be used to prevent building with uncommitted changes present.
-m If this switch is given, SubWCRev will exit withERRORLEVEL 8if the working copy contains mixed revisions. This may be used to prevent building with a partially updated working copy.
-d If this switch is given, SubWCRev will exit withERRORLEVEL 9if the destination file already exists.
-f 如果给出这个开关,SubWCRev 就会包含文件夹的最后修改版本。默认行为是取得版本号时只考虑文件。
-e If this switch is given, SubWCRev will examine directories which are included withsvn:externals, but only if they are from the same repository. The default behaviour is to ignore externals.
-x If this switch is given, SubWCRev will output the revision numbers in HEX.
-X If this switch is given, SubWCRev will output the revision numbers in HEX, with '0X' prepended.

二、Linux平台

       Linux平台下可以通过Subversion或者svn info生成定义,命令如下:
$(shell subversion -n|grep [0-9]*[A-M])
$(shell svn info|grep Revision|awk 'print $2’)

猜你喜欢

转载自blog.csdn.net/soaringlee_fighting/article/details/63251728