Skip to content

Commit 591a07b

Browse files
committed
Created TEvaluator for excluding and selecting suitable Java installations
1 parent 726d8c0 commit 591a07b

2 files changed

Lines changed: 248 additions & 0 deletions

File tree

Core.pas

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ interface
2424
Classes, Windows, SysUtils, Utils, RegExpr, fgl;
2525

2626
type
27+
TCompareType = (less, lessOrEqual, equal, moreOrEqual, more);
28+
TVersionCondition = record
29+
Valid : Boolean;
30+
CompareType : TCompareType;
31+
Version : Integer;
32+
Build : Integer;
33+
end;
2734

2835
{ TSettings }
2936

@@ -32,6 +39,9 @@ interface
3239
function GetEnvironmentVariables() : TVStringList;
3340
function GetFilePaths() : TVStringList;
3441
function GetFilteredPaths() : TVStringList;
42+
function GetMinVersion() : VString;
43+
function GetMaxVersion() : VString;
44+
function GetOptimalVersion() : VString;
3545
end;
3646

3747
{ TJavaInstallation }
@@ -86,6 +96,26 @@ TParser = class(TObject)
8696
property Installations : TFPGObjectList<TJavaInstallation> read GetInstallations;
8797
end;
8898

99+
{ TEvaluator }
100+
101+
TEvaluator = class(TObject)
102+
protected
103+
FLogger : TLogger;
104+
FSettings : TSettings;
105+
FInstallations : TFPGObjectList<TJavaInstallation>;
106+
FVersionExpRegEx : TRegExpr;
107+
function ParseVersionExpression(VersionExpr : VString) : TVersionCondition;
108+
procedure Exclude();
109+
public
110+
procedure Process();
111+
constructor Create(
112+
const Installations : TFPGObjectList<TJavaInstallation>;
113+
const Settings : TSettings;
114+
const Logger : TLogger
115+
);
116+
destructor Destroy(); override;
117+
end;
118+
89119
function GetJavawInfo(Path : VString) : TJavaInstallation;
90120
{
91121
TFGObjectList "helper" methods
@@ -662,6 +692,175 @@ destructor TParser.Destroy();
662692
inherited Destroy;
663693
end;
664694

695+
{ TEvaluator }
696+
697+
function TEvaluator.ParseVersionExpression(VersionExpr : VString) : TVersionCondition;
698+
699+
var
700+
s : VString;
701+
702+
begin
703+
Result.Valid := False;
704+
Result.CompareType := TCompareType.equal;
705+
Result.Version := -1;
706+
Result.Build := -1;
707+
if VersionExpr = '' then Exit;
708+
if FVersionExpRegEx.Exec(VersionExpr) then begin
709+
s := FVersionExpRegEx.Match[1];
710+
if (s = '') or (s = '=') then Result.CompareType := TCompareType.equal
711+
else if s = '<' then Result.CompareType := TCompareType.less
712+
else if s = '<=' then Result.CompareType := TCompareType.lessOrEqual
713+
else if s = '>=' then Result.CompareType := TCompareType.moreOrEqual
714+
else if s = '>' then Result.CompareType := TCompareType.more;
715+
Result.Version := VStrToIntDef(FVersionExpRegEx.Match[2], -1);
716+
if Result.Version >= 0 then Result.Valid := True;
717+
Result.Build := VStrToIntDef(FVersionExpRegEx.Match[3], -1);
718+
end;
719+
end;
720+
721+
procedure TEvaluator.Exclude;
722+
723+
var
724+
min, max : TVersionCondition;
725+
i : Integer;
726+
727+
begin
728+
min := ParseVersionExpression(FSettings.GetMinVersion());
729+
max := ParseVersionExpression(FSettings.GetMaxVersion());
730+
if FLogger <> Nil then begin
731+
if (min.Valid = False) and (FSettings.GetMinVersion() <> '') then
732+
FLogger.Log('Ignoring invalid minimum version expression: ' + FSettings.GetMinVersion(), TLogLevel.WARN);
733+
if (max.Valid = False) and (FSettings.GetMaxVersion() <> '') then
734+
FLogger.Log('Ignoring invalid maximum version expression: ' + FSettings.GetMaxVersion(), TLogLevel.WARN);
735+
end;
736+
if min.Valid and ((min.CompareType = TCompareType.less) or (min.CompareType = TCompareType.lessOrEqual)) then begin
737+
if FLogger <> Nil then
738+
FLogger.Log('Ignoring invalid minimum version expression: ' + FSettings.GetMinVersion(), TLogLevel.WARN);
739+
min.Valid := False;
740+
end;
741+
if max.Valid and ((max.CompareType = TCompareType.more) or (max.CompareType = TCompareType.moreOrEqual)) then begin
742+
if FLogger <> Nil then
743+
FLogger.Log('Ignoring invalid maximum version expression: ' + FSettings.GetMaxVersion(), TLogLevel.WARN);
744+
max.Valid := False;
745+
end;
746+
747+
if min.Valid or max.Valid then begin
748+
i := 0;
749+
while i < FInstallations.Count do begin
750+
if FInstallations[i].Version < 1 then begin
751+
if FLogger <> Nil then FLogger.Log(
752+
'Excluding installation because the version is unknown: ' + FInstallations[i].Path,
753+
TLogLevel.INFO
754+
);
755+
FInstallations.Delete(i);
756+
Continue;
757+
end;
758+
if min.Valid then begin
759+
if (
760+
(min.CompareType = TCompareType.more) and (FInstallations[i].Version <= min.Version) and (min.Build < 0)
761+
) or (
762+
FInstallations[i].Version < min.Version
763+
) then begin
764+
if FLogger <> Nil then FLogger.Log(
765+
'Excluding installation because the version (' + IntToVStr(FInstallations[i].Version) +
766+
') is too low: ' + FInstallations[i].Path,
767+
TLogLevel.INFO
768+
);
769+
FInstallations.Delete(i);
770+
Continue;
771+
end;
772+
if (min.Version = FInstallations[i].Version) and (min.Build >= 0) then begin
773+
if FInstallations[i].Build < 0 then begin
774+
if FLogger <> Nil then FLogger.Log(
775+
'Excluding installation because the build is unknown: ' + FInstallations[i].Path,
776+
TLogLevel.INFO
777+
);
778+
FInstallations.Delete(i);
779+
Continue;
780+
end;
781+
if (
782+
(min.CompareType = TCompareType.more) and (FInstallations[i].Build <= min.Build)
783+
) or (
784+
FInstallations[i].Build < min.Build
785+
) then begin
786+
if FLogger <> Nil then FLogger.Log(
787+
'Excluding installation because the build (' + IntToVStr(FInstallations[i].Build) +
788+
') is too low: ' + FInstallations[i].Path,
789+
TLogLevel.INFO
790+
);
791+
FInstallations.Delete(i);
792+
Continue;
793+
end;
794+
end
795+
end;
796+
797+
if max.Valid then begin
798+
if (
799+
(max.CompareType = TCompareType.less) and (FInstallations[i].Version >= max.Version) and (max.Build < 0)
800+
) or (
801+
FInstallations[i].Version > max.Version
802+
) then begin
803+
if FLogger <> Nil then FLogger.Log(
804+
'Excluding installation because the version (' + IntToVStr(FInstallations[i].Version) +
805+
') is too high: ' + FInstallations[i].Path,
806+
TLogLevel.INFO
807+
);
808+
FInstallations.Delete(i);
809+
Continue;
810+
end;
811+
if (max.Version = FInstallations[i].Version) and (max.Build >= 0) then begin
812+
if FInstallations[i].Build < 0 then begin
813+
if FLogger <> Nil then FLogger.Log(
814+
'Excluding installation because the build is unknown: ' + FInstallations[i].Path,
815+
TLogLevel.INFO
816+
);
817+
FInstallations.Delete(i);
818+
Continue;
819+
end;
820+
if (
821+
(max.CompareType = TCompareType.less) and (FInstallations[i].Build >= max.Build)
822+
) or (
823+
FInstallations[i].Build > max.Build
824+
) then begin
825+
if FLogger <> Nil then FLogger.Log(
826+
'Excluding installation because the build (' + IntToVStr(FInstallations[i].Build) +
827+
') is too high: ' + FInstallations[i].Path,
828+
TLogLevel.INFO
829+
);
830+
FInstallations.Delete(i);
831+
Continue;
832+
end;
833+
end
834+
end;
835+
Inc(i);
836+
end;
837+
end;
838+
end;
839+
840+
procedure TEvaluator.Process();
841+
842+
begin
843+
Exclude();
844+
end;
845+
846+
constructor TEvaluator.Create(
847+
const Installations : TFPGObjectList<TJavaInstallation>;
848+
const Settings : TSettings;
849+
const Logger : TLogger
850+
);
851+
begin
852+
FInstallations := Installations;
853+
FSettings := Settings;
854+
FLogger := Logger;
855+
FVersionExpRegEx := TRegExpr.Create('^\s*(<|<=|=|>=|>)?\s*(\d+)(?:\.(\d+))?\s*$');
856+
end;
857+
858+
destructor TEvaluator.Destroy;
859+
begin
860+
FVersionExpRegEx.Free;
861+
inherited Destroy;
862+
end;
863+
665864
{
666865
Creates a TJavaInstallation instance if a valid result is found.
667866
IE, Result must be Free'd if it's non-nil upon return.

NsJavaLocator.lpr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ TParameters = class(TObject, TSettings)
4242
FEnvironmentVariables : TVStringList;
4343
FFilePaths : TVStringList;
4444
FFilteredPaths : TVStringList;
45+
FMinVersion : VString;
46+
FMaxVersion : VString;
47+
FOptimalVersion : VString;
4548
FLogLevel : TLogLevel;
4649
FIsLogging : Boolean;
4750
FIsDialogDebug : Boolean;
@@ -54,6 +57,9 @@ TParameters = class(TObject, TSettings)
5457
function GetEnvironmentVariables() : TVStringList;
5558
function GetFilePaths() : TVStringList;
5659
function GetFilteredPaths() : TVStringList;
60+
function GetMinVersion() : VString;
61+
function GetMaxVersion() : VString;
62+
function GetOptimalVersion() : VString;
5763
procedure ParseParams();
5864
constructor Create();
5965
destructor Destroy(); override;
@@ -62,6 +68,9 @@ TParameters = class(TObject, TSettings)
6268
property EnvironmentVariables : TVStringList read GetEnvironmentVariables;
6369
property FilePaths : TVStringList read GetFilePaths;
6470
property FilteredPaths : TVStringList read GetFilteredPaths;
71+
property MaxVersion : VString read GetMaxVersion;
72+
property MinVersion : VString read GetMinVersion;
73+
property OptimalVersion : VString read GetOptimalVersion;
6574
property LogLevel : TLogLevel read GetLogLevel;
6675
property IsLogging : Boolean read GetLogging;
6776
property IsDialogDebug : Boolean read GetDialogDebug;
@@ -128,13 +137,20 @@ procedure TRunner.Run();
128137

129138
var
130139
Parser : TParser;
140+
Evaluator : TEvaluator;
131141

132142
begin
133143
FParams.ParseParams();
134144
FLogLevel := FParams.LogLevel;
135145
Parser := TParser.Create(FParams, Self);
136146
try
137147
Parser.Process();
148+
Evaluator := TEvaluator.Create(Parser.Installations, FParams, Self);
149+
try
150+
Evaluator.Process();
151+
finally
152+
Evaluator.Free();
153+
end;
138154
finally
139155
Parser.Free();
140156
end;
@@ -172,6 +188,21 @@ function TParameters.GetFilteredPaths() : TVStringList;
172188
Result := FFilteredPaths;
173189
end;
174190

191+
function TParameters.GetMinVersion() : VString;
192+
begin
193+
Result := FMinVersion;
194+
end;
195+
196+
function TParameters.GetMaxVersion() : VString;
197+
begin
198+
Result := FMaxVersion;
199+
end;
200+
201+
function TParameters.GetOptimalVersion() : VString;
202+
begin
203+
Result := FOptimalVersion;
204+
end;
205+
175206
function TParameters.ReadParams() : TVStringList;
176207

177208
var
@@ -210,6 +241,9 @@ procedure TParameters.ParseParams();
210241
poEnvDel = '/DELENVSTR';
211242
poFilterAdd = '/ADDFILTER';
212243
poFilterDel = '/DELFILTER';
244+
poMinVer = '/MINVER';
245+
poMaxVer = '/MAXVER';
246+
poOptVer = '/OPTVER';
213247
poLog = '/LOG';
214248
poDialogDebug = '/DIALOGDEBUG';
215249
poLogLevel = '/LOGLEVEL';
@@ -263,6 +297,18 @@ procedure TParameters.ParseParams();
263297
if (s <> '') and (s <> parameterList[i + 1]) then FFilteredPaths.RemoveMatching(s, False);
264298
Inc(i);
265299
end
300+
else if EqualStr(poMinVer, parameterList[i], False) then begin
301+
FMinVersion := parameterList[i + 1];
302+
Inc(i);
303+
end
304+
else if EqualStr(poMaxVer, parameterList[i], False) then begin
305+
FMaxVersion := parameterList[i + 1];
306+
Inc(i);
307+
end
308+
else if EqualStr(poOptVer, parameterList[i], False) then begin
309+
FOptimalVersion := parameterList[i + 1];
310+
Inc(i);
311+
end
266312
else if EqualStr(poLogLevel, parameterList[i], False) then begin
267313
tmpLogLevel := VStrToLogLevel(parameterList[i + 1]);
268314
if tmpLogLevel = TLogLevel.INVALID then
@@ -424,6 +470,9 @@ constructor TParameters.Create();
424470
// Only add filters that expand - otherwise they don't exist on the system
425471
if s <> StandardFilteredPaths[i] then FFilteredPaths.AddUnique(s, False);
426472
end;
473+
FMinVersion := '';
474+
FMaxVersion := '';
475+
FOptimalVersion := '';
427476
FLogLevel := TLogLevel.INFO;
428477
FIsLogging := False;
429478
FIsDialogDebug := False;

0 commit comments

Comments
 (0)