-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUC.Delphi.Versions.pas
More file actions
261 lines (210 loc) · 5.7 KB
/
UC.Delphi.Versions.pas
File metadata and controls
261 lines (210 loc) · 5.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
Unit UC.Delphi.Versions;
Interface
Uses
System.Classes,
System.Win.Registry;
Type
TDelphiVersion = (Seattle, Berlin, Tokyo, Rio, Sydney, Alexandria);
TDelphiVersionSet = Set of TDelphiVersion;
TDelphiVersions = Class
Private
FVersionList: TStrings;
FCurrentPaths: TStrings;
FRegistry: TRegistry;
Procedure LoadAvailableVersions;
Function LookUpVersionName(Const Value: String): String;
Function GetVersion(Const AName: String): Integer;
Function GetAvailableVersionName(Const Index: Integer): String;
Public
Constructor Create;
Destructor Destroy; Override;
Function AvailableCount: Integer;
function OpenLibrary(AVersion, APlatform, ABackUpFolder: String): boolean;
Procedure SaveAndCloseLibrary;
Procedure CloseLibrary;
Function AddToLibrary(APath: String): boolean;
Procedure SupportedPlatformsToStrings(const AVersion: String;
Strings: TStrings);
Procedure AppendPaths(Strings: TStrings);
Function LibraryKey(AVersion: String): String;
Property Version[Const AName: String]: Integer Read GetVersion;
Property AvailableVersionName[Const Index: Integer]: String
Read GetAvailableVersionName;
End;
const
DELPHI_BDS_VERSION: Array [TDelphiVersion] of Integer = (17 // Seattle = 17
, 18 // Berlin = 18
, 19 // Tokyo = 19
, 20 // Rio = 20,
, 21 // Sydney = 21
, 22 // Alexandria
);
BASE_VERSION_NUM = 17;
function DelphiVersions: TDelphiVersions;
Implementation
Uses
WinAPI.Windows,
System.Rtti,
System.IOUtils,
System.SysUtils;
ResourceString
SRootPath = '\Software\Embarcadero\BDS\';
SSearchPath = 'Search Path';
var
FDelphiVersions: TDelphiVersions;
function DelphiVersions: TDelphiVersions;
begin
if not Assigned(FDelphiVersions) then
FDelphiVersions := TDelphiVersions.Create;
Result := FDelphiVersions;
end;
{ TDelphiVersions }
Function TDelphiVersions.AddToLibrary(APath: String): boolean;
Begin
Result := FCurrentPaths.IndexOf(APath) < 0;
If Result Then
FCurrentPaths.Add(APath);
End;
Function TDelphiVersions.AvailableCount: Integer;
Begin
Result := FVersionList.Count;
End;
Procedure TDelphiVersions.CloseLibrary;
Begin
FCurrentPaths.Free;
FCurrentPaths := Nil;
FRegistry.Free;
FRegistry := Nil;
End;
procedure TDelphiVersions.AppendPaths(Strings: TStrings);
begin
Strings.AddStrings(FCurrentPaths);
end;
Constructor TDelphiVersions.Create;
Begin
Inherited Create;
FVersionList := TStringList.Create;
LoadAvailableVersions;
End;
Destructor TDelphiVersions.Destroy;
Begin
FVersionList.Free;
If FCurrentPaths <> Nil Then
FCurrentPaths.Free;
If FRegistry <> Nil Then
FRegistry.Free;
Inherited;
End;
Function TDelphiVersions.GetVersion(Const AName: String): Integer;
Begin
Result := DELPHI_BDS_VERSION
[TRttiEnumerationType.GetValue<TDelphiVersion>(AName)];
End;
Function TDelphiVersions.GetAvailableVersionName(Const Index: Integer): String;
Begin
Result := FVersionList.Names[Index];
End;
Function TDelphiVersions.LibraryKey(AVersion: String): String;
Var
lVersion: Integer;
Begin
lVersion := Version[AVersion];
Result := SRootPath + lVersion.ToString + '.0\Library'
End;
Procedure TDelphiVersions.LoadAvailableVersions;
Var
Registry: TRegistry;
I: Integer;
lName: String;
Begin
Registry := TRegistry.Create;
Try
Registry.RootKey := HKEY_CURRENT_USER;
If Registry.OpenKey(SRootPath, False) Then
Begin
Registry.GetKeyNames(FVersionList);
End;
Finally
Registry.Free;
End;
TStringList(FVersionList).Sort;
For I := 0 To FVersionList.Count - 1 Do
Begin
lName := LookUpVersionName(FVersionList[I]);
If lName = '' Then
Continue;
FVersionList[I] := lName + '=' + FVersionList[I];
End;
End;
Function TDelphiVersions.LookUpVersionName(Const Value: String): String;
Var
lValue: Integer;
lHigh, lLow: SmallInt;
lVersion: TDelphiVersion;
lVal: String;
Begin
Result := '';
lVal := Value.Substring(0, Value.IndexOf('.'));
lValue := StrToIntDef(lVal, 0) - BASE_VERSION_NUM;
lHigh := Ord(High(TDelphiVersion));
lLow := Ord(Low(TDelphiVersion));
If ((lValue > lHigh) Or (lValue < lLow)) Then
Exit;
lVersion := TDelphiVersion(lValue);
Result := TRttiEnumerationType.GetName<TDelphiVersion>(lVersion);
End;
Function TDelphiVersions.OpenLibrary(AVersion, APlatform,
ABackUpFolder: String): boolean;
var
lSearchPath: String;
Begin
If FRegistry <> Nil Then
FRegistry.Free;
If FCurrentPaths <> Nil Then
Begin
FCurrentPaths.Free;
FCurrentPaths := Nil;
End;
FRegistry := TRegistry.Create;
FRegistry.RootKey := HKEY_CURRENT_USER;
Result := FRegistry.OpenKey(LibraryKey(AVersion) + '\' + APlatform, False);
If Not Result Then
Exit;
lSearchPath := FRegistry.ReadString(SSearchPath);
if ABackUpFolder <> '' then
TFile.WriteAllText(TPath.Combine(ABackUpFolder, AVersion + '_' + APlatform +
'_SearchPath.bak'), lSearchPath);
FCurrentPaths := TStringList.Create;
FCurrentPaths.Delimiter := ';';
FCurrentPaths.StrictDelimiter := True;
FCurrentPaths.DelimitedText := lSearchPath;
End;
Procedure TDelphiVersions.SaveAndCloseLibrary;
Begin
FRegistry.WriteString(SSearchPath, FCurrentPaths.DelimitedText);
FCurrentPaths.Free;
FCurrentPaths := Nil;
FRegistry.Free;
FRegistry := Nil;
End;
procedure TDelphiVersions.SupportedPlatformsToStrings(const AVersion: String;
Strings: TStrings);
Var
Registry: TRegistry;
Begin
Registry := TRegistry.Create;
Try
Registry.RootKey := HKEY_CURRENT_USER;
If Registry.OpenKey(LibraryKey(AVersion), False) Then
Begin
Registry.GetKeyNames(Strings);
End;
Finally
Registry.Free;
End;
end;
initialization
finalization
if Assigned(FDelphiVersions) then
FDelphiVersions.Free;
End.