@@ -24,6 +24,13 @@ interface
2424 Classes, Windows, SysUtils, Utils, RegExpr, fgl;
2525
2626type
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+
89119function GetJavawInfo (Path : VString) : TJavaInstallation;
90120{
91121 TFGObjectList "helper" methods
@@ -662,6 +692,175 @@ destructor TParser.Destroy();
662692 inherited Destroy;
663693end ;
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.
0 commit comments