如何在一个地方为多个应用程序定义应用程序版本?

时间:2012-08-02 17:00:33

标签: delphi msbuild compilation version delphi-xe2

我们有一个由众多应用程序组成的系统。所有应用程序的版本都同时更改。目前,当我们发布新版本时,我们必须手动打开每个应用程序的项目选项并逐个更改版本。有没有办法在同一版本上编译所有应用程序,例如,将其保存在全局文件中并在编译时,读取此文件并将该版本分配给项目?我只是想消除太多步骤,因为我们计划更频繁地更改版本号。我想在一个地方改变它。可以这样做吗?怎么样?

3 个答案:

答案 0 :(得分:24)

您可以在纯文本文件中创建VERSIONINFO资源(例如,Versioninfo.rc

1 VERSIONINFO
FILEVERSION 2,0,0,0
PRODUCTVERSION 2,0,0,0
FILEOS 0x4
FILETYPE 0x1
{
BLOCK "StringFileInfo"
{
    BLOCK "040904E4"
    {
        VALUE "CompanyName", "Your Company Name Here\0"
        VALUE "FileDescription", "Your File Description Here\0"
        VALUE "FileVersion", "2.0.0.0\0"
        VALUE "InternalName", "Your Internal Name\0"
        VALUE "LegalCopyright", "© Your Copyright Notice\0"
        VALUE "LegalTrademarks", "Your Trademark Notice\0"
        VALUE "OriginalFilename", "YourExeName\0"
        VALUE "ProductName", "Your Product Name\0"
        VALUE "ProductVersion", "2.0.0.0\0"
        VALUE "Comments", "No Comments\0"
    }
}

BLOCK "VarFileInfo"
{
    VALUE "Translation", 0x0409 0x04E4
}
}

注意:每个项目末尾都需要C样式的空终止符(\0),如图所示,以便资源编译器正确终止字符串。否则,当您使用资源管理器显示可执行文件的版本信息时,您可能会出现乱码或部分串联的值。

在项目源文件中添加一行:

{$R VersionInfo.res VersionInfo.rc}

我建议将常用版本信息资源放入版本控制系统的外部参考中,然后您可以将其检入每个项目的文件夹并轻松更新。

执行项目 - >构建,并将您的版本信息嵌入到.exe中。您可以使用Windows资源管理器进行验证并查看应用程序的属性。

在CodeNewsFast档案馆的Embarcadero Delphi论坛上有几个帖子(一个由我和一个在Jim Fleming的回复中)。我是[这里],我在这里逐步描述如何在项目中使用预构建事件来更新我在上面发布的资源脚本中的版本号。

Jim发布了一些回复,但是大约有十几个帖子可以从可用于他的预构建事件中调用可执行文件。 (有些事情我会做不同的事情,例如让IDE在命令行上传递项目名称和位置;如何执行此操作在逐步的文章中进行了描述。我还将处理版本解析和增量不同,但基本的应用程序是一个很好的起始位置。)

Embarcadero的团队目前已经失败了,但我也能够从CodeNewsFast检索Jim的代码,并且可以在这里重现:

  

     

感谢你,我让它发挥作用。

     

以防万一其他人想要实施此解决方案,下面您将找到必要的步骤和辅助程序。

     吉姆弗莱明

A)使用以下

在项目目录或任何地方创建版本信息资源文件

内容和文件扩展名.rc:

// Note the \000 !!!! Here and elsewhere below !!!! 
// C string terminator !!!
#define CONST_VERSION "4.1.1.1003\000" 

1 VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEOS 0x4
FILETYPE 0x1
{
BLOCK "StringFileInfo"
{

BLOCK "040904E4" // Will need changing if your language is not English and char-set not

1252 (multilingual).
{
VALUE "CompanyName", "Whatever\000"
VALUE "FileDescription", "Whatever\000"
VALUE "FileVersion", CONST_VERSION
VALUE "InternalName", "My Internal Name\000"
VALUE "LegalCopyright", "Copyright © whoever\000"
VALUE "LegalTrademarks", "\000"
VALUE "OriginalFileName", "If you wish\000"
VALUE "ProductName", "What pleases you\000"
VALUE "ProductVersion", CONST_VERSION
VALUE "Comments", "Anything you wish to add\000"
}
}
BLOCK "VarFileInfo"
{
VALUE "Translation", 0x0409 0x04E4
}
}

B)在某个文件夹中创建一个新项目,只有模块的代码应该类似于:

unit FormIncrementBuildNumber;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, System.IOUtils, System.StrUtils;


type
  TIncrementBuildNumber = class(TForm)
    IncrementingBuildNumberLabel: TLabel;
    procedure FormShow (Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  IncrementBuildNumber: TIncrementBuildNumber;

implementation

{$R *.dfm}

procedure TIncrementBuildNumber.FormShow (Sender: TObject);
var
  Resource_File_Contents:       TStringList;
  Full_File_Name_And_Path:      string;
  First_Line_Of_File:           string;
  Position_First_Dot:           Integer;
  Position_Second_Dot:          Integer;
  Position_Third_Dot:           Integer;
  Position_Trailing_Backslash:  Integer;
  Start_of_Build_Number:        Integer;
  Length_of_Build_Number:       Integer;
  Build_Number_In_ASCII:        string;
  Build_Number_Numeric:         Integer;
  Old_Resource_File_Name:       string;
  Success:      Boolean;
begin
  if (System.ParamCount <> 1) then
  begin
    ShowMessage ('Resource File name not in first command-line parameter.');
    Exit;
  end;

  Full_File_Name_And_Path := System.ParamStr(1);
  if (not TFile.Exists(Full_File_Name_And_Path, False)) then
  begin
    ShowMessage ('Resource file ' + Full_File_Name_And_Path + 
                 ' not found.');
    Exit;
  end;

  Resource_File_Contents := TStringList.Create;
  try
    Resource_File_Contents.LoadFromFile(Full_File_Name_And_Path);
    First_Line_Of_File := Resource_File_Contents.Strings[0];

    if (Copy(First_Line_Of_File, 1, 21) <> '#define CONST_VERSION') then
    begin
      ShowMessage ('First line of Version Info must start with "#define CONST_VERSION".' + 
                   #13 + 'Version not incremented.');
      Exit;
    end;

    Position_First_Dot := Pos('.', First_Line_Of_File);
    if (Position_First_Dot = 0) then
    begin
      ShowMessage ('Version must have format "a.b.c.d".' + #13 + 
                   'Build Number not incremented.');
      Exit;
    end;

    Position_Second_Dot := PosEx('.', First_Line_Of_File, 
                                 Position_First_Dot+1);
    if (Position_Second_Dot = 0) then
    begin
      ShowMessage ('Version must have format "a.b.c.d".' + #13 + 
                   'Build Number not incremented.');
      Exit;
    end;

    Position_Third_Dot := PosEx('.', First_Line_Of_File, 
                                Position_Second_Dot+1);

    if (Position_Third_Dot = 0) then
    begin
      ShowMessage ('Version must have format "a.b.c.d".' + #13 + 
                   'Build Number not incremented.');

      Exit;
    end;

    Position_Trailing_Backslash := PosEx('\', First_Line_Of_File, 
                                         Position_Third_Dot+1);

    if (Position_Trailing_Backslash = 0) then
    begin
      ShowMessage ('Version must have format "a.b.c.d\000".' + #13 + 
                   'Build Number not incremented.');
      Exit;
    end;

    Start_of_Build_Number  := Position_Third_Dot + 1;
    Length_of_Build_Number := Position_Trailing_Backslash - 
                              Start_of_Build_Number;

    if (Length_of_Build_Number < 1) then
    begin
      ShowMessage ('Build Number must be present.' + #13 + 
                   'Build Number not incremented.');
      Exit;
    end;

    Build_Number_In_ASCII := Copy (First_Line_Of_File, 
                                   Start_of_Build_Number, 
                                   Length_of_Build_Number);
    Success := TryStrToInt (Build_Number_In_ASCII, Build_Number_Numeric);
    if (not Success) then
    begin
      ShowMessage ('Build Number must be numeric integer.' + #13 + 
                   'Build Number not incremented.');
      Exit;
    end;

    Build_Number_Numeric := Build_Number_Numeric + 1;
    Build_Number_In_ASCII := IntToStr(Build_Number_Numeric);
    Resource_File_Contents.Strings[0] := Copy(First_Line_Of_File, 1, 
                                              Position_Third_Dot) +
                                              Build_Number_In_ASCII + 
                                              '\000"';
    Old_Resource_File_Name := Full_File_Name_And_Path;
    Old_Resource_File_Name := TPath.ChangeExtension(Old_Resource_File_Name, '~rc');

    if TFile.Exists(Old_Resource_File_Name, False) then
      TFile.Delete(Old_Resource_File_Name);

    Success := RenameFile(Full_File_Name_And_Path, Old_Resource_File_Name);
    if (not Success) then
    begin
      ShowMessage ('Error renaming old resource file to have extension "~rc".' + #13 + 
                  'Build Number not incremented.');
      Exit;
    end;

    Resource_File_Contents.SaveToFile(Full_File_Name_And_Path);
  finally
    Resource_File_Contents.Free;
  end;
end;

procedure TIncrementBuildNumber.FormActivate (Sender: TObject);
begin
  Close;
end;

end.

C)在项目的项目选项中,其内部编号应增加:

  • 删除勾选“包含版本信息”。

  • 使用以下文本添加预构建事件(包括两对双引号),替换&lt;中的部分。 &GT;:

“&lt;自动增量程序exe的完整文件名和路径&gt;” “&lt; .rc资源文件的完整文件名和路径&gt;”

D)在“program”关键字下面添加到项目源:

{$R '<whatever you called it>.res' '<whatever you called it>.rc'} // I think both names must

在这里是一样的:IIRC,当他们不同时会出错。

E)尽管Embarcadero已经移除了设施,但编译,运行并享受自动增量版本号的返回。

  

Jim的内容结束

您可以使用预构建事件来更新ProductNameFileDescription值,或任何其他必须与基本脚本不同的值。

答案 1 :(得分:15)

更新:它不是RADStudio本身的一部分,而是来自 Andreas Hausladen's DDevExtensions (我已经习惯了......!)。

如果安装了Andreas Hausladen的优秀 DDevExtensions ,您可以使用ProjectGroup从IDE

  • 让项目组包含所有项目
  • 确保您的每个项目都在Options|Version Info page中选中了“在项目中包含版本信息”。
  • 使用菜单Project|Set Versioninfo...打开Set Project Versioninfo对话框(仅一次,当前项目无关紧要)。
  • 在那里,您可以指定所有版本信息并选择“适用于所有”或仅选择项目,如果选中“应用于选定项目”。

例如,看看我如何一次为两个项目设置版本: Example where I set version to both projects at once

然后ProjectGroup上的Build All产生了两个版本设置为1.1.1.9的exes以及所有其他细节......

答案 2 :(得分:2)

这是dzPrepBuild的一个用例: http://www.dummzeuch.de/delphi/dzprepbuild/englisch.html

(注意:该项目已移至sourceforge,因为去年berlios将被关闭。http://sourceforge.net/projects/dzprepbuild/