-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCryptoLibProcessor.pas
More file actions
250 lines (227 loc) · 6.9 KB
/
CryptoLibProcessor.pas
File metadata and controls
250 lines (227 loc) · 6.9 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
unit CryptoLibProcessor;
interface
uses
System.Generics.Collections,
System.SysUtils,
System.Types,
System.Classes,
UC.Delphi.Versions;
type
TLogProcess = procedure(const LogMessage: string) of object;
TCryptoLibProcessor = class
private
class procedure ProcessThisFolder(const AFolder, ACombinedPath: string;
const ALevel: Integer; ALogProcess: TLogProcess);
class procedure AddPathsToStrings(const APath: string; AList: TStrings);
class procedure InstallPathsToRegistry(const AVersion, APlatform: String;
PathList: TStrings);
public
class procedure AddToCombinedLib(const SourcePath, CombinedPath: string;
ALogProcess: TLogProcess);
class procedure CopyStandardFiles(const SourcePath, CombinedPath: string);
class procedure InstallPath(const APath: string;
const AVersions, APlatforms: TStringDynArray);
class procedure InstallPaths(const SourcePath: string;
const AVersions, APlatforms: TStringDynArray);
class procedure BackupPaths(const AVersions, APlatforms: TStringDynArray);
end;
implementation
uses
System.StrUtils,
System.Win.Registry,
WinAPI.Windows,
System.IOUtils;
const
DELPHI_SEARCH_PATH = 'Search Path';
BACKUP_SEARCH_PATH = 'Search Path Backup UC';
{ TCryptoLibProcessor }
class procedure TCryptoLibProcessor.AddPathsToStrings(const APath: string;
AList: TStrings);
var
lFolders: TStringDynArray;
I: Integer;
begin
lFolders := TDirectory.GetDirectories(APath);
for I := 0 to Length(lFolders) - 1 do
begin
AList.Add(lFolders[I]);
AddPathsToStrings(lFolders[I], AList);
end;
end;
class procedure TCryptoLibProcessor.AddToCombinedLib(const SourcePath,
CombinedPath: string; ALogProcess: TLogProcess);
var
lFolders: TStringDynArray;
I: Integer;
begin
lFolders := TDirectory.GetDirectories(SourcePath);
for I := 0 to Length(lFolders) - 1 do
ProcessThisFolder(lFolders[I], CombinedPath, 1, ALogProcess);
end;
class procedure TCryptoLibProcessor.BackupPaths(const AVersions,
APlatforms: TStringDynArray);
var
VI, PI: Integer;
Registry: TRegistry;
S: string;
begin
for VI := 0 to Length(AVersions) - 1 do
begin
for PI := 0 to Length(APlatforms) do
begin
Registry := TRegistry.Create(KEY_READ);
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(DelphiVersions.LibraryKey(AVersions[VI]) + '\' +
APlatforms[PI], False) and Registry.KeyExists(DELPHI_SEARCH_PATH) then
begin
S := Registry.ReadString(DELPHI_SEARCH_PATH);
Registry.Access := KEY_WRITE;
Registry.WriteString(BACKUP_SEARCH_PATH, S);
end;
finally
Registry.Free;
end;
end;
end;
end;
class procedure TCryptoLibProcessor.CopyStandardFiles(const SourcePath,
CombinedPath: string);
var
lFiles: TStringDynArray;
lPath: String;
I: Integer;
begin
lFiles := TDirectory.GetFiles(SourcePath);
for I := 0 to Length(lFiles) - 1 do
begin
if SameText(TPath.GetFileName(lFiles[I]), 'README.md') then
TFile.Copy(lFiles[I], TPath.Combine(CombinedPath, 'README.md'))
else if SameText(TPath.GetFileName(lFiles[I]), 'LICENSE') then
begin
lPath := TPath.Combine(CombinedPath, 'LICENSE') + '.txt';
TFile.Copy(lFiles[I], lPath);
end;
end;
end;
class procedure TCryptoLibProcessor.InstallPath(const APath: string;
const AVersions, APlatforms: TStringDynArray);
var
VI, PI: Integer;
Registry: TRegistry;
S: string;
begin
for VI := 0 to Length(AVersions) - 1 do
begin
for PI := 0 to Length(APlatforms) do
begin
Registry := TRegistry.Create(KEY_READ);
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(DelphiVersions.LibraryKey(AVersions[VI]) + '\' +
APlatforms[PI], False) and Registry.KeyExists(DELPHI_SEARCH_PATH) then
begin
S := Registry.ReadString(DELPHI_SEARCH_PATH);
if S.Contains(APath) then
Exit;
S := S + IfThen(S.EndsWith(';'), '', ';') + APath;
Registry.Access := KEY_WRITE;
Registry.WriteString(DELPHI_SEARCH_PATH, S);
end;
finally
Registry.Free;
end;
end;
end;
end;
class procedure TCryptoLibProcessor.InstallPaths(const SourcePath: string;
const AVersions, APlatforms: TStringDynArray);
var
TS: TStringList;
VI, PI: Integer;
begin
TS := TStringList.Create;
try
TS.Delimiter := ';';
TS.StrictDelimiter := True;
AddPathsToStrings(SourcePath, TS);
for VI := 0 to Length(AVersions) - 1 do
for PI := 0 to Length(APlatforms) do
InstallPathsToRegistry(AVersions[VI], APlatforms[PI], TS);
finally
TS.Free;
end;
end;
class procedure TCryptoLibProcessor.InstallPathsToRegistry(const AVersion,
APlatform: String; PathList: TStrings);
var
Registry: TRegistry;
RegistryList: TStringList;
originalSearchPath, newSearchPath: string;
I, A: Integer;
begin
Registry := TRegistry.Create(KEY_READ);
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(DelphiVersions.LibraryKey(AVersion) + '\' + APlatform,
False) and Registry.KeyExists(DELPHI_SEARCH_PATH) then
begin
RegistryList := TStringList.Create;
try
RegistryList.Delimiter := ';';
RegistryList.StrictDelimiter := True;
originalSearchPath := Registry.ReadString(DELPHI_SEARCH_PATH);
RegistryList.DelimitedText := originalSearchPath;
A := 0;
for I := 0 to PathList.Count - 1 do
begin
if RegistryList.IndexOf(PathList[I]) = -1 then
begin
RegistryList.Add(PathList[I]);
Inc(A);
end;
end;
if A > 0 then
begin
newSearchPath := RegistryList.DelimitedText;
Registry.Access := KEY_WRITE;
Registry.WriteString(DELPHI_SEARCH_PATH, newSearchPath);
end;
finally
RegistryList.Free;
end;
end;
finally
Registry.Free;
end;
end;
class procedure TCryptoLibProcessor.ProcessThisFolder(const AFolder,
ACombinedPath: string; const ALevel: Integer; ALogProcess: TLogProcess);
var
lFiles, lFolders: TStringDynArray;
S, lName, lTargetName, fileContents, lInc: string;
begin
if Assigned(ALogProcess) then
ALogProcess(' ' + StringOfChar('-', ALevel * 2) + ' Processing ' + AFolder);
lFolders := TDirectory.GetDirectories(AFolder);
lFiles := TDirectory.GetFiles(AFolder);
for S in lFiles do
begin
lName := TPath.GetFileName(S);
lTargetName := TPath.Combine(ACombinedPath, lName);
if TPath.GetExtension(S).ToLower = '.pas' then
begin
fileContents := TFile.ReadAllText(S);
lInc := '{$I ' + DupeString('..\', ALevel) + 'Include\';
fileContents := fileContents.Replace(lInc, '{$I ', [rfReplaceAll]);
TFile.WriteAllText(lTargetName, fileContents);
end
else
begin
TFile.Copy(S, lTargetName);
end;
end;
for S in lFolders do
ProcessThisFolder(S, ACombinedPath, ALevel + 1, ALogProcess);
end;
end.