-
Notifications
You must be signed in to change notification settings - Fork 123
/
uUpdate.pas
113 lines (99 loc) · 2.8 KB
/
uUpdate.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
unit uUpdate;
interface
uses REST.Client, uGitHub, REST.JSON, JSON,
IPPeerClient, SysUtils, System.Threading, Classes, Pkg.Json.Mapper;
const
ProgramVersion : double = 0.65;
UpdateUrl = 'https://api.github.com/repos/PKGeorgiev/Delphi-JsonToDelphiClass/releases';
ProgramUrl = 'https://github.com/PKGeorgiev/Delphi-JsonToDelphiClass';
function InternalCheckForUpdate: TObject;
procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>);
implementation
uses Math;
function InternalCheckForUpdate: TObject;
var
LRestClient: TRESTClient;
LRestRequest: TRESTRequest;
LRestResponse: TRESTResponse;
LRelease,
LResult: TObject;
LJsonArray: TJsonArray;
LJsonValue: TJsonValue;
LTag: double;
begin
LResult := nil;
try
LRestClient := TRESTClient.Create('');
try
LRestClient.BaseURL := UpdateUrl;
LRestResponse := TRESTResponse.Create(nil);
try
LRestRequest := TRESTRequest.Create(nil);
try
LRestRequest.Client := LRestClient;
LRestRequest.Response := LRestResponse;
LRestRequest.Timeout := 10000;
LRestRequest.Execute;
if LRestResponse.StatusCode = 200 then
begin
LJsonArray := TJSONObject.ParseJSONValue(LRestResponse.Content) as TJSONArray;
try
for LJsonValue in LJsonArray do
begin
LRelease := TReleaseClass.FromJsonString(LJsonValue.ToJSON);
LTag := StrToFloat((LRelease as TReleaseClass).tag_name, PointDsFormatSettings);
if Math.CompareValue(LTag, ProgramVersion) = 1 then
begin
LResult := LRelease;
break;
end
else
LRelease.Free;
end;
finally
LJsonArray.Free;
end;
end
else
LResult := TErrorClass.FromJsonString(LRestResponse.Content);
finally
LRestRequest.Free;
end;
finally
LRestResponse.Free;
end;
finally
LRestClient.Free;
end;
except
on e: Exception do
begin
LResult := TErrorClass.Create;
(LResult as TErrorClass).message := e.Message;
end;
end;
result := LResult;
end;
procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>);
begin
TTask.Run(
procedure
var
LResult: TObject;
begin
// Asynchronously check for update
LResult := InternalCheckForUpdate();
try
// Execute AOnFinish in the context of the Main Thread
TThread.Synchronize(nil,
procedure
begin
AOnFinish(LResult);
end
);
except
end;
end
);
end;
end.