-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathDN.Setup.Core.pas
294 lines (259 loc) · 8.78 KB
/
DN.Setup.Core.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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.Setup.Core;
interface
uses
DN.Types,
DN.Setup.Intf,
DN.Installer.Intf,
DN.Uninstaller.Intf,
DN.Package.Intf,
DN.Package.Version.Intf,
DN.PackageProvider.Intf,
DN.Progress.Intf;
type
TDNSetupCore = class(TInterfacedObject, IDNSetup)
private
FComponentDirectory: string;
FOnMessage: TMessageEvent;
FOnProgress: TDNProgressEvent;
FProvider: IDNPackageProvider;
function GetComponentDirectory: string;
procedure SetComponentDirectory(const Value: string);
function GetOnMessage: TMessageEvent;
procedure SetOnMessage(const Value: TMessageEvent);
function GetOnProgress: TDNProgressEvent;
procedure SetOnProgress(const Value: TDNProgressEvent);
procedure HandleProgress(const ATask, AItem: string; AProgress, AMax: Int64);
procedure HandleProviderProgress(const ATask, AItem: string; AProgress, AMax: Int64);
procedure DoProgress(const ATask, AItem: string; AProgress, AMax: Int64);
protected
FProgress: IDNProgress;
procedure DoMessage(AType: TMessageType; const AMessage: string);
procedure ReportInfo(const AInfo: string);
procedure ReportWarning(const AWarning: string);
procedure ReportError(const AError: string);
function DownloadPackage(const APackage: IDNPackage; const AVersion: IDNPackageVersion; out AContentDirectory: string): Boolean;
function GetInstallDirectoryForPackage(const APackage: IDNPackage): string; virtual;
function GetInstallDirectoryForDirectory(const ADirectory: string): string; virtual;
function GetHasPendingChanges: Boolean; virtual;
function ExtendInfoFile(const APackage: IDNPackage; const AVersion: IDNPackageVersion; const AInstallDirectory: string): Boolean;
function GetSetupTempDir: string;
procedure CleanupTemp();
function ConvertNameToValidDirectoryName(const AName: string): string;
procedure RegisterProgressHandler(const AInterface: IInterface; AHandler: TDNProgressEvent = nil);
procedure UnregisterProgressHandler(const AInterface: IInterface);
public
constructor Create(const APackageProvider: IDNPackageProvider);
destructor Destroy; override;
function Install(const APackage: IDNPackage; const AVersion: IDNPackageVersion): Boolean; virtual; abstract;
function Update(const APackage: IDNPackage; const AVersion: IDNPackageVersion): Boolean; virtual;
function Uninstall(const APackage: IDNPackage): Boolean; virtual; abstract;
function InstallDirectory(const ADirectory: string): Boolean; virtual; abstract;
function UninstallDirectory(const ADirectory: string): Boolean; virtual; abstract;
property ComponentDirectory: string read GetComponentDirectory write SetComponentDirectory;
property OnMessage: TMessageEvent read GetOnMessage write SetOnMessage;
property OnProgress: TDNProgressEvent read GetOnProgress write SetOnProgress;
property HasPendingChanges: Boolean read GetHasPendingChanges;
end;
implementation
uses
SysUtils,
IOUtils,
StrUtils,
DN.JSonFile.InstalledInfo,
DN.Progress,
DN.Environment;
{ TDNSetupCore }
procedure TDNSetupCore.CleanupTemp;
begin
ReportInfo('deleting tempfiles');
if TDirectory.Exists(GetSetupTempDir()) then
begin
TDirectory.Delete(GetSetupTempDir(), True);
end;
end;
function TDNSetupCore.ConvertNameToValidDirectoryName(const AName: string): string;
var
i: Integer;
{$i DN.LowHighString.inc}
begin
SetLength(Result, Length(AName));
for i := Low(Result) to High(Result) do
begin
if TPath.IsValidFileNameChar(AName[i]) then
Result[i] := AName[i]
else
Result[i] := ' ';
end;
Result := Trim(Result);
end;
constructor TDNSetupCore.Create(const APackageProvider: IDNPackageProvider);
begin
inherited Create();
FProvider := APackageProvider;
FProgress := TDNProgress.Create();
FProgress.OnProgress := DoProgress;
end;
destructor TDNSetupCore.Destroy;
begin
inherited;
end;
procedure TDNSetupCore.DoMessage(AType: TMessageType; const AMessage: string);
begin
if Assigned(FOnMessage) then
FOnMessage(AType, AMessage);
end;
procedure TDNSetupCore.DoProgress(const ATask, AItem: string; AProgress, AMax: Int64);
begin
if Assigned(FOnProgress) then
FOnProgress(ATask, AItem, AProgress, AMax);
end;
function TDNSetupCore.DownloadPackage(const APackage: IDNPackage;
const AVersion: IDNPackageVersion; out AContentDirectory: string): Boolean;
var
LTempDir: string;
LVersion: string;
begin
RegisterProgressHandler(FProvider, HandleProviderProgress);
try
ReportInfo('Downloading ' + APackage.Name);
LTempDir := GetSetupTempDir();
ForceDirectories(LTempDir);
if Assigned(AVersion) then
LVersion := AVersion.Name
else
LVersion := '';
ReportInfo('Version: ' + LVersion);
Result := FProvider.Download(APackage, LVersion, LTempDir, AContentDirectory);
if not Result then
begin
ReportError('failed to download');
end;
finally
UnregisterProgressHandler(FProvider);
end;
end;
function TDNSetupCore.ExtendInfoFile(const APackage: IDNPackage; const AVersion: IDNPackageVersion;
const AInstallDirectory: string): Boolean;
var
LInfoFile: string;
LInstalledInfo: TInstalledInfoFile;
begin
Result := False;
LInfoFile := TPath.Combine(AInstallDirectory, CInfoFile);
if TFile.Exists(LInfoFile) then
begin
LInstalledInfo := TInstalledInfoFile.Create();
try
LInstalledInfo.LoadFromFile(LInfoFile);
LInstalledInfo.Author := APackage.Author;
LInstalledInfo.Description := APackage.Description;
if Assigned(AVersion) then
LInstalledInfo.Version := AVersion.Value;
LInstalledInfo.ProjectUrl := APackage.ProjectUrl;
LInstalledInfo.HomepageUrl := APackage.HomepageUrl;
LInstalledInfo.ReportUrl := APackage.ReportUrl;
LInstalledInfo.SaveToFile(LInfoFile);
Result := True;
finally
LInstalledInfo.Free;
end;
end;
end;
function TDNSetupCore.GetComponentDirectory: string;
begin
Result := FComponentDirectory;
end;
function TDNSetupCore.GetHasPendingChanges: Boolean;
begin
Result := False;
end;
function TDNSetupCore.GetInstallDirectoryForDirectory(
const ADirectory: string): string;
begin
Result := TPath.Combine(FComponentDirectory, ExtractFileName(ExcludeTrailingPathDelimiter(ADirectory)));
end;
function TDNSetupCore.GetInstallDirectoryForPackage(const APackage: IDNPackage): string;
begin
Result := TPath.Combine(FComponentDirectory, ConvertNameToValidDirectoryName(APackage.Name));
end;
function TDNSetupCore.GetOnMessage: TMessageEvent;
begin
Result := FOnMessage;
end;
function TDNSetupCore.GetOnProgress: TDNProgressEvent;
begin
Result := FOnProgress;
end;
function TDNSetupCore.GetSetupTempDir: string;
begin
Result := TPath.Combine(GetDelphinusTempFolder(), 'Setup');
end;
procedure TDNSetupCore.HandleProviderProgress(const ATask, AItem: string;
AProgress, AMax: Int64);
begin
FProgress.SetTaskProgress(IfThen(AItem <> '', AItem, 'Archive'), AProgress, AMax);
end;
procedure TDNSetupCore.HandleProgress(const ATask, AItem: string; AProgress,
AMax: Int64);
begin
FProgress.SetTaskProgress(IfThen(AItem <> '', ATask + ': ' + AItem, ATask), AProgress, AMax);
end;
procedure TDNSetupCore.RegisterProgressHandler(const AInterface: IInterface; AHandler: TDNProgressEvent);
var
LProgress: IDNProgress;
begin
if Assigned(AInterface) and Supports(AInterface, IDNProgress, LProgress) then
begin
if Assigned(AHandler) then
LProgress.OnProgress := AHandler
else
LProgress.OnProgress := HandleProgress;
end;
end;
procedure TDNSetupCore.ReportError(const AError: string);
begin
DoMessage(mtError, AError);
end;
procedure TDNSetupCore.ReportInfo(const AInfo: string);
begin
DoMessage(mtNotification, AInfo);
end;
procedure TDNSetupCore.ReportWarning(const AWarning: string);
begin
DoMessage(mtWarning, AWarning);
end;
procedure TDNSetupCore.SetComponentDirectory(const Value: string);
begin
FComponentDirectory := Value;
end;
procedure TDNSetupCore.SetOnMessage(const Value: TMessageEvent);
begin
FOnMessage := Value;
end;
procedure TDNSetupCore.SetOnProgress(const Value: TDNProgressEvent);
begin
FOnProgress := Value;
end;
procedure TDNSetupCore.UnregisterProgressHandler(const AInterface: IInterface);
var
LProgress: IDNProgress;
begin
if Assigned(AInterface) and Supports(AInterface, IDNProgress, LProgress) then
LProgress.OnProgress := nil;
end;
function TDNSetupCore.Update(const APackage: IDNPackage;
const AVersion: IDNPackageVersion): Boolean;
begin
ReportInfo('Updating ' + APackage.Name);
Result := Uninstall(APackage);
if Result then
Result := Install(APackage, AVersion);
end;
end.