1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| program incversion;
{Obj. : Inclure le N° de version dans le code compilé d'un projet
Use : 1. Compiler ce programme une fois puis le placer dans le rep du projet en cours...
Projet->Option du compilateur->Executer avant->Commande : incversion $ProjFile()
... ou dans un autre rep [ex. your_dir]
Commande : your_dir\incversion $ProjFile() ou your_dir/incversion $ProjFile()
2. Ensuite dans le code du projet en cours, ajouter :
var
Form1: TForm1;
gsVersionAsString: string;
[...]
procedure TForm1.FormCreate(Sender: TObject);
begin
gsVersionAsString:= LazarusResources.Find('gsVersion').Value;
showmessage(gsVersionAsString);
end;
Prob : try except et finally traditionnel bloque le programme - Solution :
celle adoptée mais ne fonctionne qu'avec except
Test : Lazarus 09.28.2 [XP-Ubuntu 9.10] - Lazarus 0.9.26.2 [Debian Lenny]
Date : 20100317}
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes,sysutils
{ you can add units after this };
type ExceptClass = Class(Exception);
var
sl : TStringList;
rcPath , rcFile : string;
sResult : string;
F : text;
i : integer;
{$IFDEF WINDOWS}{$R incversion.rc}{$ENDIF}
begin
rcPath := '';
rcFile := '';
sResult := '';
if (Paramcount <> 0) then
if trim(ParamStr(1))<>'' then begin
//1. Load ParamStr
try
rcPath := trim(IncludeTrailingPathDelimiter(ExtractFilePath(trim(ParamStr(1)))));
if rcPath = PathDelim then rcPath := '';
rcFile := ExtractFileName(trim(ParamStr(1)));
rcFile := trim(ChangeFileExt(rcFile,'.rc'));
except
on e : SysUtils.Exception do begin
rcPath := '';
rcFile := '';
end;
end;
//2. File.rc exists ?
if rcFile <> '' then
if not FileExists(rcPath+rcFile) then rcFile := '';
//3. Recover FILEVERSION
if rcFile <> '' then begin
sl := TStringList.Create;
try
sl.LoadFromFile(rcFile);
for i := 0 to sl.Count-1 do
if pos('FILEVERSION',sl.Strings[i])>0 then begin
sResult := trim(
StringReplace(StringReplace(sl.Strings[i],'FILEVERSION','',[rfReplaceAll,rfIgnoreCase]),
',','.',[rfReplaceAll,rfIgnoreCase]));
break;
end;
except
on e : SysUtils.Exception do begin
// on e : Required to continue otherwise the program aborts on error
end;
end;
sl.Free;
end;
end; {End of if trim(ParamStr(1))<>''}
//In all cases
try
AssignFile(F,rcPath+'version.lrs');
Rewrite(F);
Writeln (F,'LazarusResources.Add(''gsVersion'',''TXT'',[');
Writeln (F,''''+sResult+'''');
Writeln(F,']);');
CloseFile(F);
except
on e : SysUtils.Exception do begin
// on e : Required to continue otherwise the program aborts on error
end;
end;
end. |
Partager