-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWP.GitHub.Helper.pas
302 lines (267 loc) · 9.3 KB
/
WP.GitHub.Helper.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
295
296
297
298
299
300
301
302
{ ***************************************************}
{ Auhtor: Ali Dehbansiahkarbon([email protected]) }
{ GitHub: https://github.com/AliDehbansiahkarbon }
{ ***************************************************}
unit WP.GitHub.Helper;
interface
uses
System.SysUtils, System.Classes, System.JSON, System.Net.HttpClient,
System.Net.URLClient, System.Net.HttpClientComponent, System.StrUtils,
System.DateUtils, System.TimeSpan, Vcl.ExtCtrls, Vcl.Graphics, Winapi.ShellAPI,
Vcl.Imaging.pngimage, System.Types, Vcl.Dialogs, Winapi.Windows,
WP.GitHub.Constants, ToolsAPI, System.IOUtils, System.UITypes,
Vcl.Controls, WP.GitHub.Setting, WP.GitHub.CustomMessage;
type
TGitHubHelper = class
private
class function DateTimeToISO8601(const ADateTime: TDateTime): string;
class function GetPeriodRange(const APeriod: string): string;
class function FindFirstDelphiProject(const Directory: string): string; static;
public
class procedure CloneGitHubRepo(const ARepoURL: string; const ARepoName: string); static;
class procedure CloneGitHubRepoAndOpen(const ARepoURL: string; const ARepoName: string); static;
class function GetTrendingPascalRepositories(const APeriod: string; const ALanguage: string): string; static;
class function CheckInternetAvailabilityAsync(const URL: string; var AException: string): Boolean; static;
class procedure OpenProjectInIDE(const ProjectDirectoryPath: string); static;
end;
TImageHelper = class helper for TImage
public
procedure LoadImageFromURL(const AImageURL: string);
end;
implementation
{ TGitHubHelper }
class function TGitHubHelper.DateTimeToISO8601(const ADateTime: TDateTime): string;
var
TZ: TTimeZone;
Offset: TTimeSpan;
Hours, Mins: string;
begin
Result := FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', ADateTime);
TZ := TTimeZone.Local;
Offset := TZ.GetUtcOffset(ADateTime);
if Offset = TTimeSpan.Zero then
Result := Result + 'Z' // UTC time
else
begin
Hours := Format('%.*d', [2, Abs(Offset.Hours)]); // Hours
Mins := Format('%.*d', [2, Abs(Offset.Minutes)]); // Minutes
if Offset.TotalHours >= 0 then
Result := Result + '%2B' + Hours + ':' + Mins
else
Result := Result + '-' + Hours + ':' + Mins;
end;
end;
class function TGitHubHelper.GetPeriodRange(const APeriod: string): string;
var
FromDate, ToDate: TDateTime;
begin
ToDate := Now;
case IndexStr(APeriod, ['daily', 'weekly', 'monthly', 'yearly']) of
0: FromDate := IncHour(ToDate, -24); // 24 hours ago
1: FromDate := IncDay(ToDate, -7); // 7 days ago
2: FromDate := IncDay(ToDate, -30); // 30 days ago
3: FromDate := IncDay(ToDate, -365); // 365 days ago
else
FromDate := IncHour(ToDate, -24); // 24 hours ago
end;
Result := DateTimeToISO8601(FromDate) + '..' + DateTimeToISO8601(ToDate);
end;
class function TGitHubHelper.GetTrendingPascalRepositories(const APeriod: string; const ALanguage: string): string;
var
HTTPClient: TNetHTTPClient;
Response: IHTTPResponse;
URL: string;
begin
if IndexStr(APeriod.ToLower, ['daily', 'weekly', 'monthly', 'yearly']) = -1 then
raise Exception.Create('Invalid PeriodType. Must be "daily", "weekly", "monthly", or "yearly".');
URL := Format(cURL, [ALanguage, GetPeriodRange(APeriod.ToLower)]);
HTTPClient := TNetHTTPClient.Create(nil);
try
Response := HTTPClient.Get(URL);
if Response.StatusCode = 200 then
Result := Response.ContentAsString
else
raise Exception.CreateFmt('Error: %d - %s', [Response.StatusCode, Response.StatusText]);
finally
HTTPClient.Free;
end;
end;
class function TGitHubHelper.CheckInternetAvailabilityAsync(const URL: string; var AException: string): Boolean;
var
HttpClient: THttpClient;
begin
Result := False;
HttpClient := THttpClient.Create;
HttpClient.ConnectionTimeout := 2000;
HttpClient.SendTimeout := 2000;
HttpClient.ResponseTimeout := 2000;
try
try
HttpClient.Head(URL);
Result := True;
except on E: Exception do
begin
Result := False;
AException := 'No internet connection.' + sLineBreak + sLineBreak + 'Exception: ' + sLineBreak + E.Message;
end;
end;
finally
HttpClient.Free;
end;
end;
class procedure TGitHubHelper.CloneGitHubRepo(const ARepoURL: string; const ARepoName: string);
var
LvOpenDialog: TFileOpenDialog;
LvTargetPath: string;
LvCommand: string;
begin
LvOpenDialog := TFileOpenDialog.Create(nil);
try
LvOpenDialog.Options := LvOpenDialog.Options + [fdoPickFolders]; // Enable folder selection
LvOpenDialog.Title := 'Select Target Folder for Cloning Repository';
if LvOpenDialog.Execute then
begin
LvTargetPath := LvOpenDialog.FileName;
if not DirectoryExists(LvTargetPath + '\' + ARepoName) then
begin
if ForceDirectories(LvTargetPath + '\' + ARepoName) then
LvTargetPath := LvTargetPath + '\' + ARepoName;
end;
LvCommand := Format('git clone %s "%s"', [ARepoURL, LvTargetPath]);
ShellExecute(0, 'open', 'cmd.exe', PChar('/C ' + LvCommand), nil, SW_SHOWNORMAL);
end;
finally
LvOpenDialog.Free;
end;
end;
class procedure TGitHubHelper.CloneGitHubRepoAndOpen(const ARepoURL: string; const ARepoName: string);
var
LvOpenDialog: TFileOpenDialog;
LvTargetPath: string;
LvCommand: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
CmdLine: string;
begin
LvOpenDialog := TFileOpenDialog.Create(nil);
try
LvOpenDialog.Options := LvOpenDialog.Options + [fdoPickFolders]; // Enable folder selection
LvOpenDialog.Title := 'Select Target Folder for Cloning Repository';
if LvOpenDialog.Execute then
begin
LvTargetPath := LvOpenDialog.FileName;
if not DirectoryExists(LvTargetPath + '\' + ARepoName) then
begin
if ForceDirectories(LvTargetPath + '\' + ARepoName) then
LvTargetPath := LvTargetPath + '\' + ARepoName;
end;
LvCommand := Format('git clone %s "%s"', [ARepoURL, LvTargetPath]);
CmdLine := Format('cmd.exe /C %s', [LvCommand]);
ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOW;
if CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo) then
try
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
finally
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
OpenProjectInIDE(LvTargetPath);
end;
finally
LvOpenDialog.Free;
end;
end;
function ShowStyledMessage(const MsgText, MsgCaption: string): Integer;
var
MsgForm: TFrm_CustomMSG;
begin
MsgForm := TFrm_CustomMSG.CreateMessage(MsgText, MsgCaption);
try
TSingletonSettings.RegisterFormClassForTheming(TFrm_CustomMSG, MsgForm);
Result := MsgForm.ShowModal;
finally
MsgForm.Free;
end;
end;
class procedure TGitHubHelper.OpenProjectInIDE(const ProjectDirectoryPath: string);
var
ModuleServices: IOTAModuleServices;
begin
var LvFullPath := FindFirstDelphiProject(ProjectDirectoryPath);
if LvFullPath.Trim.IsEmpty then
Exit
else
begin
var LvMsg := 'A project file with the name "' + ExtractFileName(LvFullPath) + '" has been found,'
+ sLineBreak + 'Would you like to open it in the IDE now?';
if ShowStyledMessage(LvMsg, 'Open Project') = mrYes then
begin
ModuleServices := BorlandIDEServices as IOTAModuleServices;
if Assigned(ModuleServices) then
begin
try
ModuleServices.OpenModule(LvFullPath);
except on E: Exception do
ShowMessage('Error opening project: ' + E.Message);
end;
end
else
ShowMessage('Module services are not available in the IDE.');
end;
end;
end;
class function TGitHubHelper.FindFirstDelphiProject(const Directory: string): string;
var
Files: TArray<string>;
begin
Result := '';
Files := TDirectory.GetFiles(Directory, '*.dproj', TSearchOption.soAllDirectories);
if Length(Files) = 0 then
Files := TDirectory.GetFiles(Directory, '*.dpr', TSearchOption.soAllDirectories);
if Length(Files) = 0 then
Files := TDirectory.GetFiles(Directory, '*.cppproj', TSearchOption.soAllDirectories);
if Length(Files) > 0 then
Result := Files[0];
end;
{ TImaheHelper }
procedure TImageHelper.LoadImageFromURL(const AImageURL: string);
var
HTTPClient: THTTPClient;
begin
HTTPClient := THTTPClient.Create;
HTTPClient.BeginGet(
procedure(const AsyncResult: IAsyncResult)
var
Response: IHTTPResponse;
MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
Response := HTTPClient.EndAsyncHTTP(AsyncResult);
if Response.StatusCode = 200 then
begin
MemoryStream.CopyFrom(Response.ContentStream, Response.ContentStream.Size);
MemoryStream.Position := 0;
TThread.Synchronize(nil, procedure begin Self.Picture.LoadFromStream(MemoryStream); end);
end
else
begin
TThread.Synchronize(nil,
procedure
begin
Self.Picture := nil;
Self.Hint := Format('Failed to load image. Status code: %d', [Response.StatusCode]);
end);
end;
finally
MemoryStream.Free;
HTTPClient.Free;
end;
end,
AImageURL
);
end;
end.