This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
InstalledPackageResolver.pas
137 lines (118 loc) · 4 KB
/
InstalledPackageResolver.pas
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
{**
DelphiPI (Delphi Package Installer)
Author : ibrahim dursun (ibrahimdursun gmail)
License : GNU General Public License 2.0
**}
unit InstalledPackageResolver;
interface
uses Classes, CompilationData;
type
IInstalledPackageResolver = interface
procedure AddDefaultPackageList;
function GetInstalledPackages: TStringList;
procedure AddIDEPackageList(const CompilationData: TCompilationData);
procedure Clear;
end;
TInstalledPackageResolver = class(TInterfacedObject, IInstalledPackageResolver)
private
fVersionSuffix: string;
fSearchFolders: TStringList;
fInstalledPackages: TStringList;
function RemoveVersionSuffix(const name, suffix: string): string;
function GetInstalledPackages: TStringList;
protected
public
constructor Create; overload; virtual;
constructor Create(const CompilationData: TCompilationData); overload; virtual;
destructor Destroy; override;
procedure AddDefaultPackageList; virtual;
procedure AddIDEPackageList(const CompilationData: TCompilationData); virtual;
procedure Clear;
property InstalledPackages: TStringList read GetInstalledPackages;
end;
implementation
uses SysUtils, JclFileUtils, JclIDEUtils;
constructor TInstalledPackageResolver.Create;
begin
inherited Create;
fSearchFolders := TStringList.Create;
fInstalledPackages := TStringList.Create;
end;
constructor TInstalledPackageResolver.Create(const CompilationData: TCompilationData);
var
systemPath, versionPattern: string;
filePattern: string;
begin
Assert(CompilationData <> nil, 'Compilation Data cannot be null');
Assert(CompilationData.Installation <> nil, 'Installation cannot be null');
Create;
fVersionSuffix := CompilationData.GetIdeVersionSuffix;
versionPattern := Copy(fVersionSuffix, 2, Length(fVersionSuffix) - 1) + '0';
filePattern := '*' + versionPattern + '.bpl';
systemPath := GetEnvironmentVariable('WINDIR') + '\System32\';
fSearchFolders.Add(PathAppend(systemPath, filePattern));
fSearchFolders.Add(PathAppend(CompilationData.Installation.LibFolderName[bpWin32], '*.bpl'));
fSearchFolders.Add(PathAppend(CompilationData.Installation.BinFolderName, '*.bpl'));
end;
procedure TInstalledPackageResolver.AddDefaultPackageList;
var
packageName: string;
internalList: TStringList;
path, entry: string;
begin
internalList := TStringList.Create;
try
for path in fSearchFolders do
BuildFileList(path, faAnyFile, internalList);
for entry in internalList do
begin
packageName := PathExtractFileNameNoExt(entry);
packageName := UpperCase(packageName);
packageName := RemoveVersionSuffix(packageName, fVersionSuffix);
fInstalledPackages.Add(packageName);
end;
finally
internalList.Free;
end;
fInstalledPackages.Add('DESIGNIDE');
end;
procedure TInstalledPackageResolver.AddIDEPackageList(const CompilationData: TCompilationData);
var
i: integer;
idePackageName: string;
list: TStringList;
begin
list := TStringList.Create;
try
CompilationData.GetIdePackages(list);
for i := 0 to CompilationData.Installation.IdePackages.Count - 1 do
list.Add(CompilationData.Installation.IdePackages.PackageFileNames[i]);
for i := 0 to list.Count - 1 do
begin
idePackageName := PathExtractFileNameNoExt(list[i]);
fInstalledPackages.Add(UpperCase(idePackageName));
end;
finally
list.Free;
end;
end;
procedure TInstalledPackageResolver.Clear;
begin
fInstalledPackages.Clear;
end;
destructor TInstalledPackageResolver.Destroy;
begin
FreeAndNil(fInstalledPackages);
FreeAndNil(fSearchFolders);
inherited;
end;
function TInstalledPackageResolver.GetInstalledPackages: TStringList;
begin
Result := fInstalledPackages;
end;
function TInstalledPackageResolver.RemoveVersionSuffix(const name, suffix: string): string;
begin
Result := name;
Delete(Result, Length(Result) - Length(suffix) + 1, Length(suffix))
end;
end.