Menu

[327ada]: / Source / Tools / Packman / InstallFiles.pas  Maximize  Restore  History

Download this file

423 lines (369 with data), 11.3 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
unit InstallFiles;
interface
uses
{$IFDEF WIN32}
Windows, Classes, IniFiles, SysUtils, Dialogs, PackmanUtils;
{$ENDIF}
{$IFDEF LINUX}
Classes, IniFiles, SysUtils, QDialogs, PackmanUtils;
{$ENDIF}
type
TIconFile = Class
public
FileName: AnsiString;
Target: AnsiString;
Icon: AnsiString;
end;
TInstallFile = Class
public
Source: AnsiString;
Dest: AnsiString;
end;
TInstallFiles = class
private
FAppDir: AnsiString;
FCount: Integer;
FIconCount: Integer;
List, IconList: TList;
FInfoFile: AnsiString;
FMenuName: AnsiString;
function GetFiles(IIndex: Integer): TInstallFile;
function GetIcons(IIndex: Integer): TIconFile;
procedure SetAppDir(Dir: AnsiString);
protected
procedure Clear;
procedure ApplyConstants(var Str: AnsiString);
function ParseLine(Line: AnsiString; var Src, Dest, Flags: AnsiString): Boolean;
public
constructor Create(InfoFile: AnsiString; AppDir: AnsiString; MenuName: AnsiString);
destructor Destroy; override;
procedure Parse;
property InfoFile: AnsiString read FInfoFile;
property AppDir: AnsiString read FAppDir write SetAppDir;
property Count: Integer read FCount;
property IconCount: Integer read FIconCount;
property Files[IIndex: Integer]: TInstallFile read GetFiles;
property Icons[IIndex: Integer]: TIconFile read GetIcons;
end;
TInstallInfo = Class
private
FFileName: AnsiString;
FAppName: AnsiString;
FAppVerName: AnsiString;
FAppVersion: AnsiString;
FDependencies: TStringList;
FDescription: AnsiString;
FLicense: AnsiString;
FLicenseFile: AnsiString;
FMenuName: AnsiString;
FReadme: AnsiString;
FReadmeFile: AnsiString;
FReboot: Boolean;
FFiles: TInstallFiles;
FURL: AnsiString;
FVersion: Integer;
public
constructor Create(AFileName: AnsiString);
destructor Destroy; override;
function Parse: Boolean;
function Files(AppDir: AnsiString): TInstallFiles;
property FileName: AnsiString read FFileName;
property AppName: AnsiString read FAppName;
property AppVerName: AnsiString read FAppVerName;
property AppVersion: AnsiString read FAppVersion;
property Dependencies: TStringList read FDependencies;
property Description: AnsiString read FDescription;
property License: AnsiString read FLicense;
property LicenseFile: AnsiString read FLicenseFile;
property MenuName: AnsiString read FMenuName;
property Readme: AnsiString read FReadme;
property ReadmeFile: AnsiString read FReadmeFile;
property Reboot: Boolean read FReboot;
property URL: AnsiString read FURL;
property Version: Integer read FVersion;
end;
const
SupportedVersion = 2;
implementation
constructor TInstallInfo.Create(AFileName: AnsiString);
begin
inherited Create;
FFileName := AFileName;
FFiles := nil;
if not Parse then
raise Exception.Create('Not a DevPackage file.');
end;
destructor TInstallInfo.Destroy;
begin
if Assigned(FFiles) then
FFiles.Free;
FDependencies.Free;
inherited Destroy;
end;
function TInstallInfo.Parse: Boolean;
var
Ini: TIniFile;
StrList: TStringList;
BaseName: AnsiString;
begin
Result := False;
Ini := TIniFile.Create(FFileName);
FAppName := Ini.ReadString('Setup', 'AppName', 'ThIsNaMeDoEsNoTeXiStS-6.1GoLd');
// this may not be a ini file...
if CompareStr(FAppName, 'ThIsNaMeDoEsNoTeXiStS-6.1GoLd') = 0 then
begin
FAppName := Ini.ReadString('Setup', 'AppName', 'ThIsIsNoTaNiNi');
if CompareStr(FAppName, 'ThIsIsNoTaNiNi') = 0 then
Exit;
end;
FAppVerName := Ini.ReadString('Setup', 'AppVerName', 'MyPackage version 1.0');
FAppVersion := Ini.ReadString('Setup', 'AppVersion', '1.0');
FDescription := Ini.ReadString('Setup', 'Description', '');
FMenuName := Ini.ReadString('Setup', 'MenuName', '');
FReboot := Ini.ReadBool('Setup', 'Reboot', False);
FURL := Ini.ReadString('Setup', 'Url', '');
FVersion := Ini.ReadInteger('Setup', 'Version', 1);
FDependencies := TStringList.Create;
FDependencies.Delimiter := ';';
FDependencies.DelimitedText := Ini.ReadString('Setup', 'Dependencies', '');
BaseName := ExtractFilePath(FFileName);
StrList := TStringList.Create;
FLicenseFile := Ini.ReadString('Setup', 'License', '');
if Length(FLicenseFile) > 0 then
begin
FLicenseFile := ExpandFileName(BaseName + FLicenseFile);
StrList.LoadFromFile(FLicenseFile);
FLicense := StrList.Text;
end else
FLicense := '';
FReadmeFile := Ini.ReadString('Setup', 'Readme', '');
if Length(FReadmeFile) > 0 then
begin
FReadmeFile := ExpandFileName(BaseName + FReadmeFile);
StrList.LoadFromFile(FReadmeFile);
FReadme := StrList.Text;
end else
FReadme := '';
StrList.Free;
Ini.Free;
Result := True;
end;
function TInstallInfo.Files(AppDir: AnsiString): TInstallFiles;
begin
if not Assigned(FFiles) then
FFiles := TInstallFiles.Create(FFileName, AppDir, MenuName)
else if CompareText(FFiles.AppDir, AppDir) <> 0 then
FFiles.AppDir := AppDir;
Result := FFiles;
end;
constructor TInstallFiles.Create(InfoFile: AnsiString; AppDir: AnsiString; MenuName: AnsiString);
begin
inherited Create;
List := TList.Create;
IconList := TList.Create;
FInfoFile := InfoFile;
FMenuName := MenuName;
SetAppDir(AppDir);
end;
destructor TInstallFiles.Destroy;
begin
Clear;
List.Free;
IconList.Free;
inherited Destroy;
end;
function TInstallFiles.GetFiles(IIndex: Integer): TInstallFile;
var
TheFile: TInstallFile;
begin
TheFile := List.Items[IIndex];
Result := TheFile;
end;
function TInstallFiles.GetIcons(IIndex: Integer): TIconFile;
var
TheIcon: TIconFile;
begin
TheIcon := IconList.Items[IIndex];
Result := TheIcon;
end;
procedure TInstallFiles.SetAppDir(Dir: AnsiString);
begin
FAppDir := Dir;
Parse;
end;
procedure TInstallFiles.Clear;
var
i: Integer;
TheFile: TInstallFile;
TheIcon: TIconFile;
begin
for i := 0 to List.Count - 1 do
begin
TheFile := TInstallFile(List.Items[i]);
TheFile.Free;
end;
for i := 0 to IconList.Count - 1 do
begin
TheIcon := TIconFile(IconList.Items[i]);
TheIcon.Free;
end;
FCount := 0;
FIconCount := 0;
List.Clear;
IconList.Clear;
end;
function TInstallFiles.ParseLine(Line: AnsiString; var Src, Dest,
Flags: AnsiString): Boolean;
var
ALine: AnsiString;
Sep, Sep2: Integer;
begin
ALine := Line;
{ Seperate the text before and after the '=' sign }
Sep := Pos('=', ALine);
{ An error occured }
if Sep = 0 then
begin
Result := False;
Exit;
end;
Src := ExtractRelativePath(ExtractFilePath(FInfoFile),
Copy(ALine, 0, Sep - 1));
ALine := Copy(Line, Sep + 1, Length(Line) - Sep);
{ Find out wether there are flags }
Sep2 := Pos(';', ALine);
if Sep2 <> 0 then
begin
Dest := Copy(ALine, 0, Sep2);
Flags := Copy(ALine, Sep2 + 1, Length(ALine) - Sep2);
end else
{ If not, just copy everything after '=' }
Dest := ALine;
Result := True;
end;
procedure ReplaceAll(var Str: AnsiString; SFrom, STo: AnsiString);
var
i: Integer;
begin
{ Pos is case sensitive; workaround }
i := Pos(LowerCase(SFrom), LowerCase(Str));
while i <> 0 do
begin
Delete(Str, i, Length(SFrom));
Insert(STo, Str, i);
i := Pos(LowerCase(SFrom), LowerCase(Str));
end;
end;
procedure TInstallFiles.ApplyConstants(var Str: AnsiString);
var
WinDir, SysDir: array[0..1024] of Char;
begin
GetWindowsDirectory(WinDir, SizeOf(WinDir));
GetSystemDirectory(SysDir, SizeOf(WinDir));
ReplaceAll(Str, '<app>', FAppDir);
ReplaceAll(Str, '<src>', ExtractFileDir(FInfoFile));
ReplaceAll(Str, '<win>', WinDir);
ReplaceAll(Str, '<sys>', SysDir);
end;
procedure TInstallFiles.Parse;
var
FileSection, IconSection: TStringList;
Icon: TIconFile;
i, j: Integer;
OrgSrc, Src, Dest, Flags: AnsiString;
TheFile: TInstallFile;
Files: TStringList;
label
LoadFiles;
begin
Clear;
{ Load all keys from the [Icons] section }
if Length(FMenuName) = 0 then
goto LoadFiles;
IconSection := TStringList.Create;
IconSection.LoadFromFile(FInfoFile);
for i := 0 to IconSection.Count - 1 do
begin
if CompareText(IconSection.Strings[0], '[Icons]') = 0 then
begin
IconSection.Delete(0);
Break;
end else
IconSection.Delete(0);
end;
{ Parse the icon filenames }
for i := 0 to IconSection.Count - 1 do
begin
if (Length(IconSection.Strings[i]) = 0) or
(IconSection.Strings[i][1] = '[') then
Break;
Icon := TIconFile.Create;
Icon.FileName := GetDevcppMenu + '\' + FMenuName + '\' +
IconSection.Names[i];
Icon.Target := IconSection.Values[IconSection.Names[i]];
j := Pos(',', Icon.Target);
if j > 0 then
begin
Icon.Icon := Copy(Icon.Target, j + 1, Length(Icon.Target) - j);
Delete(Icon.Target, j, Length(Icon.Target) - j + 1);
end;
if CompareText(Copy(Icon.Target, 1, 7), 'http://') = 0 then
Icon.FileName := Icon.FileName + '.url'
else
Icon.FileName := Icon.FileName + '.lnk';
ApplyConstants(Icon.Target);
ApplyConstants(Icon.Icon);
IconList.Add(Icon);
end;
LoadFiles:
{ Load all keys from the [Files] section }
FileSection := TStringList.Create;
FileSection.LoadFromFile(FInfoFile);
for i := 0 to FileSection.Count - 1 do
begin
if CompareText(FileSection.Strings[0], '[Files]') = 0 then
begin
FileSection.Delete(0);
Break;
end else
FileSection.Delete(0);
end;
{ Parse the filenames }
for i := 0 to FileSection.Count - 1 do
begin
if (Length(FileSection.Strings[i]) = 0) or
(FileSection.Strings[i][1] = '[') then
Break;
if not ParseLine(FileSection.Strings[i], Src, Dest, Flags) then
Continue;
ApplyConstants(Dest);
OrgSrc := Src;
Src := ExtractFilePath(FInfoFile) + Src;
if DirectoryExists(Src) then
begin
Files := TStringList.Create;
FilesFromWildcard(Src, '*', Files, True, False);
for j := 0 to Files.Count - 1 do
begin
TheFile := TInstallFile.Create;
TheFile.Source := Files.Strings[j];
TheFile.Dest := Dest + Copy(Files.Strings[j], Length(Src) + 2,
Length(Files.Strings[j]) - Length(Src));
List.Add(TheFile);
end;
Files.Free;
end else
begin
TheFile := TInstallFile.Create;
TheFile.Source := Src;
if Dest[Length(Dest)] = '\' then
Dest := Dest + ExtractFileName(Src);
TheFile.Dest := Dest;
List.Add(TheFile);
end;
end;
FCount := List.Count;
FIconCount := IconList.Count;
FileSection.Free;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.