Recent

Author Topic: Project version within IDE package?  (Read 3639 times)

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Project version within IDE package?
« on: August 30, 2021, 05:36:16 pm »
Background: I've got these package that checks if an installer script (InnoSetup, NSIS, WiX) is part of the project, and if so, compiles the setup upon a successful compile.

Question: how do I access the version resource information of the current project (LazarusIDE.ActiveProject)?

 I checked that it has a property Resources, which is TObject, but probably TAbstractProjectResources. But to get the version number this way, I guess I would need TProjectVersionInfo.

TLazProject has a ProjectInfoFile property, but manually reading it there wouldn't be the best approach if there was something more direct available.

How would I best access the project version number from the version resource from within an IDE package?

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Project version within IDE package?
« Reply #1 on: August 30, 2021, 05:52:28 pm »
Thanks, but that's not what I was asking for :)

ParamStr(0) within the Lazarus IDE would be lazarus.exe. I don't want the file version of the IDE, I want the file version (or better project version) the IDE has for the currently open project.

 This might be another workaround - retrieving the version from the finished compiled file. Since I have that for codesigning it anyway, I could use it to read the file, using IDEMacros.SubstituteMacros('$(TargetFile)'). This was buggy though for DLLs if I remember correctly. Or  LazarusIDE.ActiveProject.LazBuildModes.BuildModes[LazarusIDE.ActiveProject.LazBuildModes.IndexOf(LazarusIDE.ActiveProject.ActiveBuildModeID)].LazCompilerOptions.TargetFileName :)

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Project version within IDE package?
« Reply #2 on: August 30, 2021, 06:05:34 pm »
I haven't attached any resource to my package - I'm speaking about the IDE project version properties!

Take a look at any projects .lpi file - they're stored in an XML file there, for example, which is fundamentally different from PE's version resource either as .rc or .res. And they're surely stored in memory somewhere, where the resource compiler takes them from while linking the file.

Taking the version from the compilers output works in this case, but think of IDE packages that want to access the version number without compiling first.

balazsszekely

  • Guest
Re: Project version within IDE package?
« Reply #3 on: August 30, 2021, 06:25:11 pm »
Hi CCRDude,

The version info is stored in the *.lpi. Just loop through the project files, then parse the lpi to get the version:
Code: Pascal  [Select][+][-]
  1.   for I := 0 to LazarusIDE.ActiveProject.FileCount - 1 do
  2.   begin
  3.     if LowerCase(ExtractFileExt(LazarusIDE.ActiveProject.Files[I].Filename)) = '.lpi' then
  4.     begin
  5.        ParseLPI(LazarusIDE.ActiveProject.Files[I].Filename);
  6.        Break;
  7.     end;
  8.   end;
  9.  

PS: ParseLPI is not implemented in the above example, but it shouldn't be to difficult to get the values from the xml file.

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Project version within IDE package?
« Reply #4 on: August 30, 2021, 06:44:43 pm »
 I probably posted in the wrong subforum :)

What it’s about: the IDE can be enhanced by custom packages. I’ve created a few. This question does is about accessing information from within the IDE, about the current project.

The “ParseLPI” approach is nice, but might not reflect changes made but not yet saved, and needs a lot of error handling already implemented in the code the IDE uses to parse it.

Might implement both, one as a fallback, but direct access through the LazarusIDE.ActiveProject.Resources would of course be better :)

balazsszekely

  • Guest
Re: Project version within IDE package?
« Reply #5 on: August 30, 2021, 07:14:00 pm »
@CCRDude

Quote
direct access through the LazarusIDE.ActiveProject.Resources would of course be better
I don't think ProjectIntf is ripe enough for that, it can be extended though.

Quote
The “ParseLPI” approach is nice, but might not reflect changes made but not yet saved, and needs a lot of error handling already implemented in the code the IDE uses to parse it.
Before you run the project, the lpi is saved. Until then the temporary modification of the version info is meaningless. You can try something like this(not tested):

Code: Pascal  [Select][+][-]
  1. uses Laz2_XMLCfg;
  2.  
  3. function ParseLPI(const APath: String): String;
  4.  
  5.   function VersionBound(const AVersion: Integer): Integer;
  6.   begin
  7.     if AVersion > 9999 then
  8.       Result := 9999
  9.     else if AVersion < 0 then
  10.       Result := 0
  11.     else
  12.       Result := AVersion;
  13.   end;
  14.  
  15. var
  16.   XML: TXMLConfig;
  17.   UseVersionInfo: Boolean;
  18.   Major, Minor, Release, Build: Integer;
  19.   BasePath: String;
  20. begin
  21.   Result := '';
  22.  
  23.   XML := TXMLConfig.Create(APath);
  24.   try
  25.     BasePath := 'ProjectOptions/VersionInfo/';
  26.     UseVersionInfo := XML.GetValue(BasePath + 'UseVersionInfo/Value', False);
  27.     if UseVersionInfo then
  28.     begin
  29.       Major := VersionBound(XML.GetValue(BasePath + 'MajorVersionNr/Value', 0));
  30.       Minor := VersionBound(XML.GetValue(BasePath + 'MinorVersionNr/Value', 0));
  31.       Release := VersionBound(XML.GetValue(BasePath + 'RevisionNr/Value', 0));
  32.       Build := VersionBound(XML.GetValue(BasePath + 'BuildNr/Value', 0));
  33.       Result := IntToStr(Major) + '.' + IntToStr(Minor) + '.' + IntToStr(Release) + '.' + IntToStr(Build);
  34.     end;
  35.   finally
  36.     XML.Free;
  37.   end;
  38. end;

balazsszekely

  • Guest
Re: Project version within IDE package?
« Reply #6 on: August 30, 2021, 10:36:27 pm »
@cdesim
Quote
The nodes you are referring to from the .lpi file are constain the information that is designated in the resource - the very same information that is returned by TFileVersionInfo class.
OK, but you must realize that you're inside a package and the only way to communicate with the IDE is the package/project editing interface(limited communication).

Quote
And you don't want to read it from a file becuase it may change while the project is actively being developed.
It may change until you save or run the project. Until then is just a hanging information. More over, if necessary, you can catch the changed version info on project close:
Code: Pascal  [Select][+][-]
  1. LazarusIDE.AddHandlerOnProjectClose(@OnProjectClose);
« Last Edit: August 30, 2021, 10:38:34 pm by GetMem »

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Project version within IDE package?
« Reply #7 on: August 31, 2021, 11:46:37 am »
Hi CCRDude,

The version info is stored in the *.lpi. Just loop through the project files, then parse the lpi to get the version:

Since the LPI kind of is the project, it is not listed as a file. But it's available as LazarusIDE.ActiveProject.ProjectInfoFile :)

I'm now using GetMems .lpi method, with a fallback to cdesims binary method, and it works like a charm, thanks a lot!
https://gitlab.com/ccrdude/lazinstallerhelper/-/commit/0669878fffa95d59827ca5f56ed113229dfa7b00

Next question will go into packages subforum I guess :)

 

TinyPortal © 2005-2018