-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuGetItListModel.pas
More file actions
299 lines (265 loc) · 9.7 KB
/
Copy pathuGetItListModel.pas
File metadata and controls
299 lines (265 loc) · 9.7 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
unit uGetItListModel;
interface
uses
System.Generics.Collections,
uGetItTypes;
type
TGetItListModel = record
public
class function IsDelphiPackage(const Pkg: TGetItPackageInfo): Boolean; static;
class function BuildPackageGroupKey(const Pkg: TGetItPackageInfo): string; static;
class function ComparePackageDate(const Left, Right: TGetItPackageInfo): Integer; static;
class function ShouldIncludeByInstallStateFilter(const Pkg: TGetItPackageInfo;
const InstalledOnly, NotInstalledOnly, UpdatableOnly: Boolean): Boolean; static;
class procedure CountVersionStatusForVisible(const VisiblePackages: TList<TGetItPackageInfo>;
out InstalledCount, OutdatedCount, ModifiedCount, UnavailableCount: Integer); static;
class procedure UpdateVersionStatus(
const AllPackages: TObjectDictionary<string, TGetItPackageInfo>;
const InstalledPackages: TObjectDictionary<string, TInstalledGetItPackageInfo>); static;
end;
implementation
uses
System.SysUtils, System.DateUtils, uGetItCore;
function HasComparableFileValue(const Value: string): Boolean;
var
Text: string;
begin
Text := Trim(Value);
Result := (Text <> '') and (not SameText(Text, 'NA'));
end;
function IsRemotePackageUrl(const Value: string): Boolean;
begin
Result := SameText(Copy(Trim(Value), 1, 7), 'http://') or
SameText(Copy(Trim(Value), 1, 8), 'https://');
end;
function TryNormalizePackageSize(const Value: string; out SizeValue: Int64): Boolean;
var
FloatValue: Double;
FormatSettings: TFormatSettings;
begin
Result := TryStrToInt64(Trim(Value), SizeValue);
if Result then
Exit;
FormatSettings := TFormatSettings.Invariant;
if TryStrToFloat(Trim(Value), FloatValue, FormatSettings) or TryStrToFloat(Trim(Value), FloatValue) then
begin
SizeValue := Round(FloatValue);
Exit(True);
end;
SizeValue := 0;
Result := False;
end;
function PackageFileChanged(const Pkg: TGetItPackageInfo; const Installed: TInstalledGetItPackageInfo): Boolean;
var
CatalogSize: Int64;
InstalledSize: Int64;
begin
Result := False;
// A local import changes the source path, not necessarily the archive contents.
if IsRemotePackageUrl(Pkg.LibUrl) and IsRemotePackageUrl(Installed.Url) and
(not SameText(Trim(Pkg.LibUrl), Trim(Installed.Url))) then
Exit(True);
if HasComparableFileValue(Pkg.LibSize) and HasComparableFileValue(Installed.LibSize) and
TryNormalizePackageSize(Pkg.LibSize, CatalogSize) and
TryNormalizePackageSize(Installed.LibSize, InstalledSize) and
(CatalogSize > 0) and (InstalledSize > 0) and
(CatalogSize <> InstalledSize) then
Exit(True);
if HasComparableFileValue(Pkg.LibMD5) and HasComparableFileValue(Installed.LibMD5) and
(not SameText(Trim(Pkg.LibMD5), Trim(Installed.LibMD5))) then
Exit(True);
end;
procedure ApplyInstalledMetadata(const Pkg: TGetItPackageInfo; const Installed: TInstalledGetItPackageInfo);
begin
Pkg.InstalledVersion := Installed.Version;
Pkg.InstalledUrl := Installed.Url;
Pkg.InstalledModified := Installed.Modified;
Pkg.InstalledVersionTimestamp := Installed.VersionTimestamp;
Pkg.InstalledLibSize := Installed.LibSize;
Pkg.InstalledLibMD5 := Installed.LibMD5;
Pkg.InstalledState := Installed.State;
Pkg.InstalledAllUsers := Installed.AllUsers;
Pkg.InstalledSubscription := Installed.Subscription;
end;
class function TGetItListModel.IsDelphiPackage(const Pkg: TGetItPackageInfo): Boolean;
var
CodeValue: Integer;
CodeName: string;
IdText: string;
NameText: string;
begin
if TryStrToInt(Trim(Pkg.LibCode), CodeValue) then
begin
if CodeValue = 2 then
Exit(False);
if (CodeValue = 1) or (CodeValue = 5) then
Exit(True);
end;
CodeName := Trim(LowerCase(Pkg.LibCodeName));
if CodeName <> '' then
begin
if Pos('cplusplusbuilder', CodeName) > 0 then
Exit(False);
if (Pos('delphi', CodeName) > 0) or (Pos('allpersonalities', CodeName) > 0) then
Exit(True);
end;
IdText := LowerCase(Pkg.Id + ' ' + Pkg.PackageId);
NameText := LowerCase(Pkg.Name);
if (Pos('-cb-', IdText) > 0) or IdText.EndsWith('-cb') or
(Pos('cb-', IdText) > 0) or (Pos('(c++)', NameText) > 0) then
Exit(False);
Result := True;
end;
class function TGetItListModel.BuildPackageGroupKey(const Pkg: TGetItPackageInfo): string;
var
BaseKey: string;
LibCodeKey: string;
begin
BaseKey := Trim(LowerCase(Pkg.Name));
if BaseKey = '' then
BaseKey := Trim(LowerCase(Pkg.Id));
LibCodeKey := Trim(LowerCase(Pkg.LibCode));
if LibCodeKey = '' then
LibCodeKey := Trim(LowerCase(Pkg.LibCodeName));
Result := BaseKey + '|' + Trim(LowerCase(Pkg.Vendor)) + '|' + LibCodeKey;
end;
class function TGetItListModel.ComparePackageDate(const Left, Right: TGetItPackageInfo): Integer;
var
LeftDate, RightDate: TDateTime;
LeftDateStr, RightDateStr: string;
begin
LeftDateStr := Trim(Left.VersionTimestamp);
if LeftDateStr = '' then
LeftDateStr := Trim(Left.Modified);
RightDateStr := Trim(Right.VersionTimestamp);
if RightDateStr = '' then
RightDateStr := Trim(Right.Modified);
if TryISO8601ToDate(LeftDateStr, LeftDate) and TryISO8601ToDate(RightDateStr, RightDate) then
begin
if LeftDate > RightDate then
Exit(-1);
if LeftDate < RightDate then
Exit(1);
Exit(0);
end;
if TryStrToDateTime(LeftDateStr, LeftDate) and TryStrToDateTime(RightDateStr, RightDate) then
begin
if LeftDate > RightDate then
Exit(-1);
if LeftDate < RightDate then
Exit(1);
Exit(0);
end;
Result := TGetItCore.CompareVersionText(Right.Version, Left.Version);
end;
class function TGetItListModel.ShouldIncludeByInstallStateFilter(const Pkg: TGetItPackageInfo;
const InstalledOnly, NotInstalledOnly, UpdatableOnly: Boolean): Boolean;
begin
if UpdatableOnly then
Exit(not Pkg.Incompatible and (Pkg.InstalledVersion <> '') and
(SameText(Pkg.VersionStatus, 'Modified') or
(SameText(Pkg.VersionStatus, 'Outdated') and
(TGetItCore.CompareVersionText(Pkg.Version, Pkg.InstalledVersion) > 0))));
if InstalledOnly then
Exit(Pkg.InstalledVersion <> '');
if NotInstalledOnly then
Exit(Pkg.InstalledVersion = '');
Result := True;
end;
class procedure TGetItListModel.CountVersionStatusForVisible(const VisiblePackages: TList<TGetItPackageInfo>;
out InstalledCount, OutdatedCount, ModifiedCount, UnavailableCount: Integer);
begin
InstalledCount := 0;
OutdatedCount := 0;
ModifiedCount := 0;
UnavailableCount := 0;
for var Pkg in VisiblePackages do
begin
if SameText(Pkg.VersionStatus, 'Outdated') then
begin
Inc(InstalledCount);
Inc(OutdatedCount);
end
else if SameText(Pkg.VersionStatus, 'Modified') then
begin
Inc(InstalledCount);
Inc(ModifiedCount);
end
else if SameText(Pkg.VersionStatus, 'Unavailable') then
begin
Inc(InstalledCount);
Inc(UnavailableCount);
end
else if SameText(Pkg.VersionStatus, 'Installed') then
Inc(InstalledCount);
end;
end;
class procedure TGetItListModel.UpdateVersionStatus(
const AllPackages: TObjectDictionary<string, TGetItPackageInfo>;
const InstalledPackages: TObjectDictionary<string, TInstalledGetItPackageInfo>);
var
LatestRepo: TDictionary<string, string>;
IdIndex: TDictionary<string, TGetItPackageInfo>;
InstalledByGroup: TDictionary<string, TInstalledGetItPackageInfo>;
Key, LatestVersion: string;
InstalledPkg, Pkg: TGetItPackageInfo;
InstalledInfo, CurrentInstalled: TInstalledGetItPackageInfo;
IsInstalledById: Boolean;
begin
LatestRepo := TDictionary<string, string>.Create;
IdIndex := TDictionary<string, TGetItPackageInfo>.Create;
InstalledByGroup := TDictionary<string, TInstalledGetItPackageInfo>.Create;
try
for Pkg in AllPackages.Values do
begin
IdIndex.AddOrSetValue(Trim(LowerCase(Pkg.Id)), Pkg);
if Pkg.CatalogUnavailable or (irBuild in Pkg.IncompatibilityReasons) then
Continue;
Key := BuildPackageGroupKey(Pkg);
if not LatestRepo.TryGetValue(Key, LatestVersion) or
(TGetItCore.CompareVersionText(LatestVersion, Pkg.Version) < 0) then
LatestRepo.AddOrSetValue(Key, Pkg.Version);
end;
for var InstalledPair in InstalledPackages do
if IdIndex.TryGetValue(InstalledPair.Key, InstalledPkg) then
begin
Key := BuildPackageGroupKey(InstalledPkg);
if not InstalledByGroup.TryGetValue(Key, CurrentInstalled) or
(TGetItCore.CompareVersionText(CurrentInstalled.Version, InstalledPair.Value.Version) < 0) then
InstalledByGroup.AddOrSetValue(Key, InstalledPair.Value);
end;
for Pkg in AllPackages.Values do
begin
Pkg.ClearInstalledMetadata;
Key := BuildPackageGroupKey(Pkg);
if LatestRepo.TryGetValue(Key, LatestVersion) then
Pkg.LatestVersion := LatestVersion
else
Pkg.LatestVersion := Pkg.Version;
IsInstalledById := InstalledPackages.TryGetValue(Trim(LowerCase(Pkg.Id)), InstalledInfo);
if not InstalledByGroup.TryGetValue(Key, CurrentInstalled) then
CurrentInstalled := InstalledInfo;
if CurrentInstalled = nil then
begin
Pkg.VersionStatus := 'NotInstalled';
Continue;
end;
ApplyInstalledMetadata(Pkg, CurrentInstalled);
if Pkg.CatalogUnavailable and IsInstalledById then
Pkg.VersionStatus := 'Unavailable'
else if TGetItCore.CompareVersionText(Pkg.InstalledVersion, Pkg.LatestVersion) < 0 then
Pkg.VersionStatus := 'Outdated'
else if IsInstalledById and
(TGetItCore.CompareVersionText(Pkg.Version, Pkg.InstalledVersion) = 0) and
PackageFileChanged(Pkg, CurrentInstalled) then
Pkg.VersionStatus := 'Modified'
else
Pkg.VersionStatus := 'Installed';
end;
finally
InstalledByGroup.Free;
IdIndex.Free;
LatestRepo.Free;
end;
end;
end.