-
Notifications
You must be signed in to change notification settings - Fork 3
/
Settings.pas
317 lines (284 loc) · 8.46 KB
/
Settings.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit Settings;
// Also includes odds + sods functions etc
interface
uses
System.Classes,
System.Zip,
FMX.Dialogs,
FMX.Graphics;
type
TTemplateFile = Class
TplFileName: String; // Filename
TplTemplate: String; // Template Text with replacement tokens
constructor Create(AFileName: string; ATemplate: String);
End;
//
TReplacementToken = Class
tfile: String; // Filename
xlat: TArray<String>; // The tokens that need replacing in this template
tmime: Integer; // A semi-bitmapped Mime-esque numbering system
// 0 = One-Off file
// 1 = Pascal File (pas)
// 2 = Package File (dpk)
// 3 = Project File (dproj)
// Bit 2 : Set if part of component, unset if runtime
constructor Create(AFile: string; AToken: TArray<String>; ATMime: Integer);
end;
TReplacementTokens = TArray<TReplacementToken>;
TZipFileHelper = class helper for TZipFile
function ExtractToTemplate(Index: Integer): TTemplateFile; overload;
end;
TComponentSettings = Class(TObject)
DelphiPackageName: String;
PublicPackageName: String;
PythonPackageName: String;
PIPPackageName: String;
CondaPackageName: String;
HomeURL: String;
PyPiURL: String;
DocURL: String;
GithubURL: String;
PackageDesc: String;
PackageIcon: String;
constructor Create;
procedure CopyFrom(AObject: TComponentSettings);
end;
TProjectComponentSettings = TArray<TComponentSettings>;
TProjectSettings = Class(TObject)
ProjectTitle: String;
ProjectVersion: String;
ReadMe: String;
ProjectGroupName: String;
ProjectDesc: String;
ProjectHomepage: String;
PalettePage: String;
IncludePackageInfo: Boolean;
ComponentSettings: TProjectComponentSettings;
constructor Create;
destructor Destroy; override;
end;
var
AppHome: String;
ProjectSettings: TProjectSettings;
LogStrings: TStringList;
SkipWebsiteChecks: Boolean;
const
DefaultProjectFile = 'WorkingProject.p4d';
procedure SaveProjectSettings(const AProjectFile: String);
procedure LoadProjectSettings(const AProjectFile: String);
procedure DecodeBase64Image(var ABitmap: TBitmap; Base64Image: String);
procedure Log(const AMsg: String);
function TokenSortFunc(Item1, Item2: Pointer): Integer;
function TemplateSortFunc(Item1, Item2: Pointer): Integer;
function FullCopy(const AStr: String): String;
implementation
uses
System.IOUtils,
System.NetEncoding,
System.SysUtils,
System.Json.Serializers;
function FullCopy(const AStr: String): String;
begin
Result := Copy(AStr, 1, Length(AStr));
end;
procedure Log(const AMsg: String);
begin
LogStrings.Add(AMsg);
end;
function TokenSortFunc(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TReplacementToken(Item1).tfile, TReplacementToken(Item2).tfile);
end;
function TemplateSortFunc(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TTemplateFile(Item1).TplFileName, TTemplateFile(Item2).TplFileName);
end;
constructor TComponentSettings.Create;
begin
Inherited Create;
end;
procedure TComponentSettings.CopyFrom(AObject: TComponentSettings);
begin
DelphiPackageName := FullCopy(AObject.DelphiPackageName);
PublicPackageName := FullCopy(AObject.PublicPackageName);
PythonPackageName := FullCopy(AObject.PythonPackageName);
PIPPackageName := FullCopy(AObject.PIPPackageName);
CondaPackageName := FullCopy(AObject.CondaPackageName);
HomeURL := FullCopy(AObject.HomeURL);
PyPiURL := FullCopy(AObject.PyPiURL);
DocURL := FullCopy(AObject.DocURL);
GithubURL := FullCopy(AObject.GithubURL);
PackageDesc := FullCopy(AObject.PackageDesc);
PackageIcon := FullCopy(AObject.PackageIcon);
end;
constructor TProjectSettings.Create;
begin
Inherited Create;
ProjectVersion := '1.0.0';
end;
destructor TProjectSettings.Destroy;
var
I: Integer;
AComponent: TComponentSettings;
begin
if Length(ComponentSettings) > 0 then
begin
for I := 0 to Length(ComponentSettings) - 1 do
begin
AComponent := ComponentSettings[I];
FreeAndNil(AComponent);
end;
SetLength(ComponentSettings, 0);
end;
Inherited Destroy;
end;
procedure LoadProjectSettings(const AProjectFile: String);
var
lSerializer: TJsonSerializer;
JsonText: String;
begin
if Assigned(ProjectSettings) then
FreeAndNil(ProjectSettings);
if FileExists(AProjectFile) then
begin
lSerializer := TJsonSerializer.Create;
try
JsonTExt := TFile.ReadAllText(AProjectFile);
try
ProjectSettings := lSerializer.Deserialize<TProjectSettings>(JsonText);
except
on E : Exception do
Raise Exception.Create('LoadSystemSettings - Exception : Class = ' +
E.ClassName + ', Message = ' + E.Message);
end;
finally
FreeAndNil(lSerializer);
end;
end
else
ProjectSettings := TProjectSettings.Create;
end;
procedure SaveProjectSettings(const AProjectFile: String);
var
JSONText: String;
lSerializer: TJsonSerializer;
begin
if Assigned(ProjectSettings) then
begin
lSerializer := TJsonSerializer.Create;
try
try
JSONText := lSerializer.Serialize<TProjectSettings>(ProjectSettings);
try
TFile.WriteAllText(AProjectFile, JsonText);
except
on E : Exception do
Raise Exception.Create('SaveSystemSettings - Exception : Class = ' +
E.ClassName + ', Message = ' + E.Message);
end;
except
on E : Exception do
begin
Raise Exception.Create('SaveSystemSettings - Exception : Class = ' +
E.ClassName + ', Message = ' + E.Message);
end;
end;
finally
FreeAndNil(lSerializer);
end;
end;
end;
procedure DecodeBase64Image(var ABitmap: TBitmap; Base64Image: String);
var
MStream: TMemoryStream;
SStream: TStringStream;
begin
MStream := TMemoryStream.Create;
SStream := TStringStream.Create(Base64Image);
try
try
SStream.Position := 0;
TNetEncoding.Base64.Decode(SStream, MStream);
MStream.Position := 0;
ABitmap.LoadFromStream(MStream);
except
on E : Exception do
begin
Raise Exception.Create('SaveSystemSettings - Exception : Class = ' +
E.ClassName + ', Message = ' + E.Message);
end;
end;
finally
SStream.Free;
MStream.Free;
end;
end;
constructor TReplacementToken.Create(AFile: string; AToken: TArray<String>; ATMime: Integer);
begin
Inherited Create;
tfile := AFile;
xlat := AToken;
tmime := ATMime;
end;
constructor TTemplateFile.Create(AFileName: string; ATemplate: String);
begin
Inherited Create;
TplFileName := AFileName;
TplTemplate := ATemplate;
end;
Function TZipFileHelper.ExtractToTemplate(Index: Integer): TTemplateFile;
var
LInStream: TStream;
LHeader: TZipHeader;
LFileName: string;
Template: TTemplateFile;
TemplateText: String;
SS: TStringStream;
begin
SS := TStringStream.Create;
Template := Nil;
// Get decompression stream for file
Read(Index, LInStream, LHeader);
try
if not GetUTF8PathFromExtraField(LHeader, LFileName) then
LFileName := InternalGetFileName(Index);
{$IFDEF MSWINDOWS} // ZIP stores files with '/', so translate to a relative Windows path.
LFileName := StringReplace(LFileName, '/', '\', [rfReplaceAll]);
{$ENDIF}
// Open the File For output
if not (LFileName.Chars[LFileName.Length-1] = PathDelim) then
Begin
try // And Copy from the decompression stream.
// See Bit 3 at http://www.pkware.com/documents/casestudies/APPNOTE.TXT
if (LHeader.Flag and (1 shl 3)) = 0 then
begin
// Empty files should not be read
if LHeader.UncompressedSize > 0 then
begin
SS.CopyFrom(LInStream , 0);
TemplateText := SS.DataString;
Template := TTemplateFile.Create(LFileName, TemplateText);
end;
end
else
begin
SS.CopyFrom(LInStream , 0);
TemplateText := SS.DataString;
Template := TTemplateFile.Create(LFileName, TemplateText);
end;
except
on E: Exception do
begin
Log('Unhandled Exception in ExtractToTemplate');
Log('Class : ' + E.ClassName);
Log('Error : ' + E.Message);
end;
end;
end;
finally
SS.Free;
LInStream.Free;
Result := Template;
end;
end;
end.