forked from digao-dalpiaz/CompInstall
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUGitHub.pas
More file actions
296 lines (246 loc) · 6.41 KB
/
UGitHub.pas
File metadata and controls
296 lines (246 loc) · 6.41 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
unit UGitHub;
interface
procedure CheckGitHubUpdate(const Repository, CurrentVersion: string);
implementation
uses System.Classes, System.SysUtils, System.Generics.Collections,
Vcl.Forms, Vcl.Graphics, Vcl.Dialogs,
System.UITypes, System.IOUtils, System.StrUtils, System.Math,
System.Net.HttpClient, System.JSON, Vcl.ExtActns, System.Zip,
UFrm, UFrmOldFiles, UCommon;
const URL_GITHUB = 'https://api.github.com/repos/%s/releases/latest';
type
TOldFile = class
private
Path: string;
Folder: Boolean;
end;
TOldFiles = class(TObjectList<TOldFile>)
private
procedure Add(const Path: string; Folder: Boolean);
end;
TThCheck = class(TThread)
public
constructor Create(Suspended: Boolean);
destructor Destroy; override;
protected
procedure Execute; override;
private
Repository: string;
CurrentVersion: string;
OldFiles: TOldFiles;
procedure Check;
function ConfirmOldFiles: Boolean;
procedure Download(const URL, aNewVersion: string);
procedure Log(const A: string; bBold: Boolean = True; Color: TColor = clBlack);
procedure GetOldFiles;
procedure DeleteOldFiles;
end;
constructor TThCheck.Create(Suspended: Boolean);
begin
inherited;
OldFiles := TOldFiles.Create;
end;
destructor TThCheck.Destroy;
begin
OldFiles.Free;
inherited;
end;
procedure TThCheck.Execute;
begin
FreeOnTerminate := True;
try
Check;
except
on E: Exception do
Log('ERROR: '+E.Message, True, clRed);
end;
Synchronize(
procedure
begin
Frm.SetButtons(True);
end);
end;
procedure TThCheck.Log(const A: string; bBold: Boolean = True; Color: TColor = clBlack);
begin
Synchronize(
procedure
begin
Frm.Log(A, bBold, Color);
end);
end;
procedure TThCheck.Check;
var
H: THTTPClient;
Res, tag_url, tag_version, tag_zip: string;
data: TJSONObject;
Confirm: Boolean;
begin
Log('Checking for component update...');
H := THTTPClient.Create;
try
Res := H.Get(Format(URL_GITHUB, [Repository])).ContentAsString;
finally
H.Free;
end;
data := TJSONObject.ParseJSONValue(Res) as TJSONObject;
try
if data.GetValue('id')=nil then
raise Exception.Create('No releases found on GitHub');
tag_version := data.GetValue('tag_name').Value;
tag_url := data.GetValue('html_url').Value;
tag_zip := data.GetValue('zipball_url').Value;
finally
data.Free;
end;
if tag_version.StartsWith('v', True) then Delete(tag_version, 1, 1);
if CurrentVersion<>tag_version then
begin
Log(Format('New version "%s" available.', [tag_version]), True, clPurple);
Synchronize(
procedure
begin
Confirm := MessageDlg(Format(
'There is a new version "%s" of the component available at GitHub.'+
' Do you want to update it automatically?',
[tag_version]), mtInformation, mbYesNo, 0) = mrYes;
end);
if Confirm and ConfirmOldFiles then
begin
Download(tag_zip,tag_version);
end;
end else
Log('Your version is already updated.', True, clGreen);
end;
function TThCheck.ConfirmOldFiles: Boolean;
var
Confirm: Boolean;
begin
GetOldFiles;
if OldFiles.Count=0 then Exit(True); //nothing to delete, auto confirm
Synchronize(
procedure
var
OldFile: TOldFile;
begin
FrmOldFiles := TFrmOldFiles.Create(Application);
for OldFile in OldFiles do
with FrmOldFiles.LFiles.Items.Add do
begin
Caption := ExtractFileName(OldFile.Path);
ImageIndex := IfThen(OldFile.Folder, 1, 0);
end;
Confirm := FrmOldFiles.ShowModal = mrOk;
FrmOldFiles.Free;
end);
if Confirm then
begin
DeleteOldFiles;
Result := True;
end else
Result := False;
end;
procedure TThCheck.GetOldFiles;
var
Path, FileName: string;
begin
//directories
for Path in TDirectory.GetDirectories(AppDir) do
begin
{$WARN SYMBOL_PLATFORM OFF}
if TFileAttribute.faHidden in TDirectory.GetAttributes(Path) then Continue; //ignore hidden folders (like .git)
{$WARN SYMBOL_PLATFORM ON}
OldFiles.Add(Path, True);
end;
//files
for Path in TDirectory.GetFiles(AppDir) do
begin
FileName := ExtractFileName(Path);
//skip self EXE and CompInstall.ini
if SameText(FileName, ExtractFileName(ParamStr(0))) or
SameText(FileName, INI_FILE_NAME) then Continue;
OldFiles.Add(Path, False);
end;
end;
procedure TThCheck.DeleteOldFiles;
var
OldFile: TOldFile;
begin
Log('Cleaning component folder...');
for OldFile in OldFiles do
begin
try
if OldFile.Folder then
TDirectory.Delete(OldFile.Path, True)
else
TFile.Delete(OldFile.Path);
except
on E: Exception do
raise Exception.CreateFmt('Could not delete %s %s: %s',
[IfThen(OldFile.Folder, 'folder', 'file'), OldFile.Path, E.Message]);
end;
end;
end;
procedure TThCheck.Download(const URL,aNewVersion: string);
var
Dw: TDownLoadURL;
TmpFile: string;
Z: TZipFile;
ZPath, ZFile, ZFileNormalized: string;
begin
Log('Downloading new version...');
TmpFile := TPath.GetTempFileName;
Dw := TDownLoadURL.Create(nil);
try
Dw.URL := URL;
Dw.Filename := TmpFile;
Dw.ExecuteTarget(nil);
finally
Dw.Free;
end;
Log('Extracting component updates...');
Z := TZipFile.Create;
try
Z.Open(TmpFile, zmRead);
for ZFile in Z.FileNames do
begin
try
ZFileNormalized := NormalizeAndRemoveFirstDir(ZFile);
ZPath := TPath.Combine(AppDir, ExtractFilePath(ZFileNormalized));
if not DirectoryExists(ZPath) then ForceDirectories(ZPath);
Z.Extract(ZFile, ZPath, False);
except
on E: Exception do
raise Exception.CreateFmt('Error extracting "%s": %s', [ZFile, E.Message]);
end;
end;
finally
Z.Free;
end;
G_NewVersion := aNewVersion;
Log('Reloading component info...');
Synchronize(Frm.LoadDefinitions); //reaload definitions
Log('Update complete!', True, clGreen);
end;
//
procedure CheckGitHubUpdate(const Repository, CurrentVersion: string);
var
C: TThCheck;
begin
if Repository.IsEmpty then Exit;
Frm.SetButtons(False);
C := TThCheck.Create(True);
C.Repository := Repository;
C.CurrentVersion := CurrentVersion;
C.Start;
end;
{ TOldFiles }
procedure TOldFiles.Add(const Path: string; Folder: Boolean);
var
OldFile: TOldFile;
begin
OldFile := TOldFile.Create;
OldFile.Path := Path;
OldFile.Folder := Folder;
inherited Add(OldFile);
end;
end.