Change dproj file and product version

Posted by on in Programming

If you have a project with automatic builds is posible that you want change the File/Product version of the file.

Using the code below you can create a console application for to modify these values easily.

program ChangeVersion;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  UnParseDproj in 'UnParseDproj.pas';

procedure ChangeDprojVersion;
var
  DprojParser : TDprojParser;
begin
  DprojParser := TDprojParser.Create;
  try
    DprojParser.DprojFile := ParamStr(1);
    DprojParser.VersionString := ParamStr(2);
    DprojParser.ChangeVersion;
  finally
    DprojParser.Free;
  end;
end;

begin
  try
    if ParamCount < 2 then
    begin
      WriteLn('Wrong syntax. Usage:');
      WriteLn('ChangeVersion "Delphi project file (dproj)" "Version string(x.x.x.x)"');
    end
  else
    begin
      ChangeDprojVersion;
    end;

  except
    on E: Exception do
      WriteLn(E.ClassName, ': ', E.Message);
  end;

end.
unit UnParseDproj;

interface

uses
  System.Sysutils,
  Winapi.Ole2,
  Xml.XMLIntf,
  Xml.XMLDoc;

type

  TDprojParser = class
    private
      FXMLDocument: IXMLDocument;
      FDprojFile: string;
      FVersionString: string;
      procedure SetDprojFile(const Value: string);
      procedure SetVersionString(const Value: string);
    public
      property DprojFile: string read FDprojFile write SetDprojFile;
      property VersionString: string read FVersionString write SetVersionString;
      procedure ChangeVersion;
      constructor Create;
      destructor Destroy; override;
  end;

implementation

{ TDprojParser }

procedure TDprojParser.ChangeVersion;
var
  Project, Node, VerInfo_Keys: IXMLNode;
  I, J, K: Integer;
  Keys_String: String;
  Keys : TArray<string>;
  Version: TArray<string>;
begin
  try
    FXMLDocument.LoadFromFile(DprojFile);
    Project := FXMLDocument.ChildNodes.First;
    J := Project.ChildNodes.Count - 1;
    for I := 0 to J do
    begin
      Node := Project.ChildNodes.Nodes[I];
      VerInfo_Keys := Node.ChildNodes.FindNode('VerInfo_Keys');
      if VerInfo_Keys <> nil then
        begin
        Keys_String := VerInfo_Keys.NodeValue;
        Keys := Keys_String.Split([';']);
        for K := 0 to Length(Keys) - 1  do
          begin
            Version := Keys[K].Split(['=']);
            if Version[0]= 'FileVersion' then
              Keys[K] := 'FileVersion='+FVersionString;
            if Version[0]= 'ProductVersion' then
              Keys[K] := 'ProductVersion='+FVersionString;
          end;
        Keys_String := '';
        for K := 0 to Length(Keys) - 1 do
          Keys_String := Keys_String + Keys[K] + ';';
        Keys_String := Keys_String.Substring(0,Keys_String.Length -1);
        VerInfo_Keys.NodeValue := Keys_String;
        end;
    end;
    FXMLDocument.SaveToFile(Dprojfile);
    WriteLn('dproj file changed');
  except
    on E: Exception do
      WriteLn(E.ClassName + ':' + E.Message)
  end;
end;

constructor TDprojParser.Create;
begin
  FXMLDocument := TXMLDocument.Create(nil);
  FXMLDocument.ParseOptions := FXMLDocument.ParseOptions+[poPreserveWhiteSpace];
end;

destructor TDprojParser.Destroy;
begin
end;

procedure TDprojParser.SetDprojFile(const Value: string);
begin
  FDprojFile := Value;
end;

procedure TDprojParser.SetVersionString(const Value: string);
begin
  FVersionString := Value;
end;

initialization
  CoInitialize(nil);

end.



Tags: Delphi XML


About
Gold User, Rank: 92, Points: 3

Comments

  • Sebastiao Jose de S9230
    Sebastiao Jose de S9230 Monday, 7 December 2015

    Hello Ruben, okay?

        You could provide me the source for Delphi 2010? Rodqr tried, but did the following error message:

    Keys: = Keys_String.Split ([';']);
    [DCC Error] UnParseDproj.pas (56): E2018 Record, object or class required

        I thank you attention!

  • Rubén Pozo
    Rubén Pozo Wednesday, 9 December 2015

    This works for me in 2007:

    unit UnParseDproj;

    interface

    uses
    Sysutils,
    Strutils,
    Ole2,
    XMLIntf,
    XMLDoc;

    type

    TDprojParser = class
    private
    FXMLDocument: IXMLDocument;
    FDprojFile: string;
    FVersionString: string;
    procedure SetDprojFile(const Value: string);
    procedure SetVersionString(const Value: string);
    public
    property DprojFile: string read FDprojFile write SetDprojFile;
    property VersionString: string read FVersionString write SetVersionString;
    procedure ChangeVersion;
    constructor Create;
    destructor Destroy; override;
    end;

    implementation

    { TDprojParser }

    uses
    Variants;

    procedure TDprojParser.ChangeVersion;
    var
    Project, Node, BorlandProject, BorlandProjectchild, DelphiPersonality
    : IXMLNode;
    VersionInfo, VersionValue: IXMLNode;
    I, J, K, L: Integer;
    Keys_String: String;
    begin
    try
    FXMLDocument.LoadFromFile(DprojFile);
    Project := FXMLDocument.ChildNodes.First;
    J := Project.ChildNodes.Count - 1;
    for I := 0 to J do
    begin
    Node := Project.ChildNodes.Nodes[I];
    if Node.NodeName = 'ProjectExtensions' then
    begin
    BorlandProject := Node.ChildNodes.FindNode('BorlandProject');
    if BorlandProject nil then
    begin
    for K := 0 to BorlandProject.ChildNodes.Count - 1 do
    begin
    if BorlandProject.ChildNodes.Nodes[K].NodeName = 'BorlandProject'
    then
    begin
    BorlandProjectchild := BorlandProject.ChildNodes.Nodes[K];
    DelphiPersonality := BorlandProjectchild.ChildNodes.FindNode
    ('Delphi.Personality');
    if DelphiPersonality nil then
    begin
    VersionInfo := DelphiPersonality.ChildNodes.FindNode
    ('VersionInfoKeys');
    for L := 0 to VersionInfo.ChildNodes.Count - 1 do
    begin
    if VarToStr(VersionInfo.ChildNodes.Nodes[L].Attributes['Name']
    ) = 'FileVersion' then
    begin
    VersionValue := VersionInfo.ChildNodes.Nodes[L];
    VersionValue.NodeValue := FVersionString;
    end;
    if VarToStr(VersionInfo.ChildNodes.Nodes[L].Attributes['Name']
    ) = 'ProductVersion' then
    begin
    VersionValue := VersionInfo.ChildNodes.Nodes[L];
    VersionValue.NodeValue := FVersionString;
    end;
    end;
    end;
    end;
    end;
    end;
    end;
    end;
    FXMLDocument.SaveToFile(DprojFile);
    WriteLn('dproj file changed');
    except
    on E: Exception do
    WriteLn(E.ClassName + ':' + E.Message)
    end;
    end;

    constructor TDprojParser.Create;
    begin
    FXMLDocument := TXMLDocument.Create(nil);
    FXMLDocument.ParseOptions := FXMLDocument.ParseOptions +
    [poPreserveWhiteSpace];
    end;

    destructor TDprojParser.Destroy;
    begin
    end;

    procedure TDprojParser.SetDprojFile(const Value: string);
    begin
    FDprojFile := Value;
    end;

    procedure TDprojParser.SetVersionString(const Value: string);
    begin
    FVersionString := Value;
    end;

    initialization

    CoInitialize(nil);

    end.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: