您的位置:首页 > 其它

利用SVN实现自动版本号生成

2010-12-20 10:53 411 查看



以 vc6 为例, 文件的版本信息保存在 rc 文件. 编译成 res 文件, 然后和其他 obj 一起 link. 现在的思路就是. 编辑 rc 文件, 将版本号比如 2.2.4.0 改成 2.2.4.$WCREV$, 在每次 link 之前, 先用 subwcrev.exe 处理 rc 文件, 进行宏替换. 然后调用

rc.exe 编译替换后的新文件. 生成 res 之后一起 link.……

/*
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <fstream>
#include <sstream>

#pragma warning(disable:4996)

int main(int argc, char **argv)
{
std::string path;

if(argc >= 1 && argv[1] )
{
path = argv[1];
if(path.size() > 0 && (path[path.size()-1]!='/' || path[path.size()-1]!='//'))
path += '/';
}

FILE* EntriesFile = fopen((path+".svn/entries").c_str(), "r");
if(!EntriesFile)
EntriesFile = fopen((path+"_svn/entries").c_str(), "r");

std::ostringstream newData;

if(!EntriesFile)
{
newData << "#ifndef __SVN_REVISION_H__" << std::endl;
newData << "#define __SVN_REVISION_H__"  << std::endl;
newData << " #define SVN_REVISION /"Unknown/"" << std::endl;
newData << " #define SVN_DATE /"Unknown/"" << std::endl;
newData << " #define SVN_TIME /"Unknown/""<< std::endl;
newData << "#endif // __SVN_REVISION_H__" << std::endl;
}
else
{
char buf[200];
int revision;
char date_str[200];
char time_str[200];

fgets(buf,200,EntriesFile);
fgets(buf,200,EntriesFile);
fgets(buf,200,EntriesFile);
fscanf(EntriesFile,"%i",&revision);
fgets(buf,200,EntriesFile);
fgets(buf,200,EntriesFile);
fgets(buf,200,EntriesFile);
fgets(buf,200,EntriesFile);
fgets(buf,200,EntriesFile);
fscanf(EntriesFile,"%10sT%8s",date_str,time_str);

newData << "#ifndef __SVN_REVISION_H__" << std::endl;
newData << "#define __SVN_REVISION_H__"  << std::endl;
newData << " #define SVN_REVISION /"" << revision << "/"" << std::endl;
newData << " #define SVN_REVISIONSTR /"" << revision/1000 << "." << revision%1000/100 << "."
<< revision%100/10 << "." << revision%10
<< "/"" << std::endl;
newData << " #define SVN_FILESTR " << revision/1000 << "," << revision%1000/100 << ","
<< revision%100/10 << "," << revision%10
<< std::endl;
newData << " #define SVN_DATE /"" << date_str << "/"" << std::endl;
newData << " #define SVN_TIME /"" << time_str << "/""<< std::endl;
newData << "#endif // __SVN_REVISION_H__" << std::endl;

fclose(EntriesFile);
}

std::string oldData;

if(FILE* HeaderFile = fopen((path+"svn_revision.h").c_str(),"rb"))
{
while(!feof(HeaderFile))
{
int c = fgetc(HeaderFile);
if(c < 0)
break;
oldData += (char)c;
}

fclose(HeaderFile);
}

if(newData.str() != oldData)
{
if(FILE* OutputFile = fopen((path+"svn_revision.h").c_str(),"wb"))
{
fprintf(OutputFile,"%s",newData.str().c_str());
fclose(OutputFile);
}
}

return 0;
}


在vc的precompile步骤里面调用gensvnrevision,生成svn_revision.h, 如:

#ifndef __SVN_REVISION_H__
#define __SVN_REVISION_H__
#define SVN_REVISION "1707"
#define SVN_REVISIONSTR "1.7.0.7"
#define SVN_FILESTR 1,7,0,7
#define SVN_DATE "2010-02-02"
#define SVN_TIME "01:36:29"
#endif // __SVN_REVISION_H__


在 project.rc2里面添加include "svn_revision.h", 在版本里面用SVN_REVISIONSTR和SVN_FILESTR替换ProductVersion、FileVersion和 FILEVERSION 、PRODUCTVERSION 。

搞定!使用tsvn的童鞋们可以用第一种方法,要方便很 多!;-)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: