Repository navigation
Expand file tree
/
Copy pathDN.Setup.pas
More file actions
272 lines (240 loc) · 8 KB
/
Copy pathDN.Setup.pas
File metadata and controls
272 lines (240 loc) · 8 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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.Setup;
interface
uses
DN.Types,
DN.Setup.Intf,
DN.Installer.Intf,
DN.Uninstaller.Intf,
DN.Package.Intf,
DN.Package.Version.Intf,
DN.PackageProvider.Intf;
type
TDNSetup = class(TInterfacedObject, IDNSetup)
private
FComponentDirectory: string;
FOnMessage: TMessageEvent;
FProvider: IDNPackageProvider;
FInstaller: IDNInstaller;
FUninstaller: IDNUninstaller;
function GetComponentDirectory: string;
procedure SetComponentDirectory(const Value: string);
function GetOnMessage: TMessageEvent;
procedure SetOnMessage(const Value: TMessageEvent);
protected
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;
function GetInstallDirectoryForDirectory(const ADirectory: string): string;
function ExtendInfoFile(const APackage: IDNPackage; const AVersion: IDNPackageVersion; const AInstallDirectory: string): Boolean;
function GetSetupTempDir: string;
procedure CleanupTemp();
function ConvertNameToValidDirectoryName(const AName: string): string;
public
constructor Create(const AInstaller: IDNInstaller; const AUninstaller: IDNUninstaller; const APackageProvider: IDNPackageProvider);
function Install(const APackage: IDNPackage; const AVersion: IDNPackageVersion): Boolean;
function Update(const APackage: IDNPackage; const AVersion: IDNPackageVersion): Boolean;
function Uninstall(const APackage: IDNPackage): Boolean;
function InstallDirectory(const ADirectory: string): Boolean;
function UninstallDirectory(const ADirectory: string): Boolean;
property ComponentDirectory: string read GetComponentDirectory write SetComponentDirectory;
property OnMessage: TMessageEvent read GetOnMessage write SetOnMessage;
end;
implementation
uses
SysUtils,
IOUtils,
DN.JSonFile.InstalledInfo;
{ TDNSetup }
procedure TDNSetup.CleanupTemp;
begin
ReportInfo('deleting tempfiles');
if TDirectory.Exists(GetSetupTempDir()) then
begin
TDirectory.Delete(GetSetupTempDir(), True);
end;
end;
function TDNSetup.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 TDNSetup.Create(const AInstaller: IDNInstaller;
const AUninstaller: IDNUninstaller;
const APackageProvider: IDNPackageProvider);
begin
inherited Create();
FInstaller := AInstaller;
FInstaller.OnMessage := DoMessage;
FUninstaller := AUninstaller;
FUninstaller.OnMessage := DoMessage;
FProvider := APackageProvider;
end;
procedure TDNSetup.DoMessage(AType: TMessageType; const AMessage: string);
begin
if Assigned(FOnMessage) then
FOnMessage(AType, AMessage);
end;
function TDNSetup.DownloadPackage(const APackage: IDNPackage;
const AVersion: IDNPackageVersion; out AContentDirectory: string): Boolean;
var
LTempDir: string;
LVersion: string;
begin
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;
end;
function TDNSetup.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.Name;
LInstalledInfo.ProjectUrl := APackage.ProjectUrl;
LInstalledInfo.HomepageUrl := APackage.HomepageUrl;
LInstalledInfo.ReportUrl := APackage.ReportUrl;
LInstalledInfo.SaveToFile(LInfoFile);
Result := True;
finally
LInstalledInfo.Free;
end;
end;
end;
function TDNSetup.GetComponentDirectory: string;
begin
Result := FComponentDirectory;
end;
function TDNSetup.GetInstallDirectoryForDirectory(
const ADirectory: string): string;
begin
Result := TPath.Combine(FComponentDirectory, ExtractFileName(ExcludeTrailingPathDelimiter(ADirectory)));
end;
function TDNSetup.GetInstallDirectoryForPackage(const APackage: IDNPackage): string;
begin
Result := TPath.Combine(FComponentDirectory, ConvertNameToValidDirectoryName(APackage.Name));
end;
function TDNSetup.GetOnMessage: TMessageEvent;
begin
Result := FOnMessage;
end;
function TDNSetup.GetSetupTempDir: string;
begin
Result := TPath.Combine(GetEnvironmentVariable('Temp'), 'Delphinus');
end;
function TDNSetup.Install(const APackage: IDNPackage;
const AVersion: IDNPackageVersion): Boolean;
var
LContentDirectory: string;
LInstallDirectory: string;
begin
try
Result := DownloadPackage(APackage, AVersion, LContentDirectory);
if Result then
begin
LInstallDirectory := GetInstallDirectoryForPackage(APackage);
Result := FInstaller.Install(LContentDirectory, LInstallDirectory);
if Result then
Result := ExtendInfoFile(APackage, AVersion, LInstallDirectory)
end;
finally
CleanupTemp();
end;
if Result then
ReportInfo('Installation finished')
else
ReportError('Installation failed');
end;
function TDNSetup.InstallDirectory(const ADirectory: string): Boolean;
var
LInstallDirectory: string;
begin
LInstallDirectory := GetInstallDirectoryForDirectory(ADirectory);
Result := FInstaller.Install(ADirectory, LInstallDirectory);
if Result then
ReportInfo('Installation finished')
else
ReportError('Installation failed');
end;
procedure TDNSetup.ReportError(const AError: string);
begin
DoMessage(mtError, AError);
end;
procedure TDNSetup.ReportInfo(const AInfo: string);
begin
DoMessage(mtNotification, AInfo);
end;
procedure TDNSetup.ReportWarning(const AWarning: string);
begin
DoMessage(mtWarning, AWarning);
end;
procedure TDNSetup.SetComponentDirectory(const Value: string);
begin
FComponentDirectory := Value;
end;
procedure TDNSetup.SetOnMessage(const Value: TMessageEvent);
begin
FOnMessage := Value;
end;
function TDNSetup.Uninstall(const APackage: IDNPackage): Boolean;
begin
Result := UninstallDirectory(GetInstallDirectoryForPackage(APackage));
end;
function TDNSetup.UninstallDirectory(const ADirectory: string): Boolean;
begin
ReportInfo('Uninstalling...');
Result := FUninstaller.Uninstall(ADirectory);
if Result then
ReportInfo('success')
else
ReportError('failed');
end;
function TDNSetup.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.