-
Notifications
You must be signed in to change notification settings - Fork 0
/
PABCNETDownloader.pas
329 lines (280 loc) · 10.3 KB
/
PABCNETDownloader.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
program downloader;
const
// PABCFile = 'https://pascalabc.net/downloads/PascalABCNETSetup.exe';
// PABCFile = 'http://it.mmcs.sfedu.ru/downloads/PABC/PascalABCNETSetup.exe';
PABCFile = 'https://github.com/SunSerega/pascalabcnet/releases/download/custom-build-tag/PascalABCNETSetup.exe';
var
TempFolder := System.IO.Path.GetTempPath + 'PABCInstallerTemp';
ExecHideFile := TempFolder + '\ExecHide.exe';
PreCompileFile := 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe';
AddToGACFile := TempFolder + '\gacutil.exe';
TempFile := System.IO.Path.GetTempFileName;
var
y := 0;
wc := new System.Net.WebClient;
file_pst: real;
pst_done: real;
pst_err: real;
comp := false;
wc_err := default(Exception);
procs_alive := new List<System.Diagnostics.Process>;
procedure AddOtp(s: string);
begin
System.Console.SetCursorPosition(0, y);
write(s, ' ' * (System.Console.WindowWidth-s.Length) );
y += 1;
end;
procedure DeleteFolder(path: string);
begin
if not System.IO.Directory.Exists(path) then exit;
foreach var dir in System.IO.Directory.EnumerateDirectories(path) do DeleteFolder(dir);
foreach var f in System.IO.Directory.EnumerateFiles(path) do System.IO.File.Delete(f);
System.IO.Directory.Delete(path);
end;
type
InstallableElement = abstract class
from, &to: string;
last_err:Exception;
function GetFileCount:integer; abstract;
function Install: List<InstallableElement>; abstract;
end;
InstallableFile = class(InstallableElement)
function Install: List<InstallableElement>; override;
begin
try
System.IO.File.Delete(&to);
System.IO.File.Copy(from, &to);
if System.IO.Path.GetExtension(&to) = '.exe' then procs_alive += System.Diagnostics.Process.Start($'{ExecHideFile}',$'{PreCompileFile} install {&to}');//.WaitForExit;
if System.IO.Path.GetExtension(&to) = '.dll' then procs_alive += System.Diagnostics.Process.Start($'{ExecHideFile}',$'{AddToGACFile} /i {&to}');//.WaitForExit;
Result := new List<InstallableElement>(0);
except
on e: Exception do
begin
self.last_err := e;
Result := new List<InstallableElement>(1);
Result.Add(self);
pst_err += file_pst;
end;
end;
pst_done += file_pst;
end;
function GetFileCount:integer; override :=
1;
constructor(from, &to: string);
begin
self.from := from;
self.to := &to;
end;
public function ToString: string; override;
begin
var fi1 := new System.IO.FileInfo(from);
var fi2 := new System.IO.FileInfo(&to);
Result := $'Файл {fi1.FullName}=>{fi2.FullName}{#10*2}{last_err}{#10}';
end;
end;
InstallableFolder = class(InstallableElement)
Elements := new List<InstallableElement>;
function Install: List<InstallableElement>; override;
begin
try
Result := InstallBody;
except
on e: Exception do
begin
self.last_err := e;
Result := new List<InstallableElement>(1);
Result.Add(self);
var pst := file_pst * self.GetFileCount;
pst_done += pst;
pst_err += pst;
end;
end;
end;
function InstallBody: List<InstallableElement>;
begin
Result := new List<InstallableElement>;
System.IO.Directory.CreateDirectory(&to);
foreach var el in Elements do
Result.AddRange(el.Install);
end;
function GetFileCount:integer; override :=
Elements.Select(el->el.GetFileCount).Sum;
function GetElements: sequence of InstallableElement :=
System.IO.Directory.EnumerateDirectories(from).Select(dir -> InstallableElement(
new InstallableFolder(
self.from + '\' + System.IO.Path.GetFileName(dir),
self.&to + '\' + System.IO.Path.GetFileName(dir)
)
)) +
System.IO.Directory.EnumerateFiles(from).Select(fl -> InstallableElement(
new InstallableFile(
self.from + '\' + System.IO.Path.GetFileName(fl),
self.&to + '\' + System.IO.Path.GetFileName(fl)
)
));
constructor(from, &to: string);
begin
self.from := from;
self.to := &to;
Elements := GetElements.ToList;
end;
public function ToString: string; override;
begin
var fi1 := new System.IO.FileInfo(from);
var fi2 := new System.IO.FileInfo(&to);
Result := $'Папка {fi1.FullName}=>{fi2.FullName}{#10*2}{last_err}{#10}';
end;
end;
var
FailedToInstall: List<InstallableElement>;
procedure Подготовка;
begin
System.Console.CursorVisible := false;
AddOtp('подготовка');
System.IO.File.Delete(TempFile);
System.Net.ServicePointManager.SecurityProtocol := System.Net.SecurityProtocolType.Tls12;
wc.DownloadProgressChanged += procedure(o, e)-> pst_done := e.BytesReceived / e.TotalBytesToReceive;
wc.DownloadFileCompleted += procedure(o, e)->
begin
comp := true;
wc_err := e.Error;
end;
end;
procedure Скачивание;
begin
AddOtp('скачиваю');
wc.DownloadFileAsync(new System.Uri(PABCFile), TempFile);
while not comp do
begin
AddOtp($'{pst_done*100:N2}%');
y -= 1;
Sleep(10);
end;
if wc_err<>nil then
raise wc_err;
System.IO.File.Copy(TempFile, System.Environment.GetEnvironmentVariable('USERPROFILE')+'\Desktop\PascalABCNETSetup.exe', true);
end;
procedure DestroyWindow(ptr:System.IntPtr);
external 'User32.dll';
procedure Распаковка;
begin
AddOtp('распаковываю');
DeleteFolder(TempFolder);
(**
var p := new System.Diagnostics.Process;
p.StartInfo.FileName := $'C:\Program Files\7-Zip\7zG.exe';
//p.StartInfo.Arguments := $'x "{TempFile}" -o"{TempFolder}"';
AddOtp($'{System.IO.Path.GetTempPath}\temp_donwload.exe');
p.StartInfo.Arguments := $'x "{System.IO.Path.GetTempPath}\temp_donwload.exe" -o"{TempFolder}"';
p.StartInfo.UseShellExecute := false;
p.StartInfo.RedirectStandardError := true;
p.StartInfo.CreateNoWindow := true;
p.StartInfo.RedirectStandardOutput := true;
p.Start;
p.StandardError.ReadToEnd;
p.StandardOutput.ReadToEnd;
p.WaitForExit;
(**)
//System.Diagnostics.Process.Start($'C:\Program Files\7-Zip\7zG.exe',$'x "{TempFile*0+''temp_donwload.exe''}" -o"{TempFolder}" -bso0 -bsp0').WaitForExit;
System.Diagnostics.Process.Start($'C:\Program Files\7-Zip\7zG.exe',$'x "{TempFile}" -o"{TempFolder}"').WaitForExit;
System.IO.File.Delete(TempFile);
end;
procedure Установка(installing:List<InstallableElement>);
begin
AddOtp($'Устанавливаю');
FailedToInstall := new List<InstallableElement>;
file_pst := 1/installing.Select(el->el.GetFileCount).Sum;
pst_done := 0;
pst_err := 0;
comp := false;
var thr := new System.Threading.Thread(()->
begin
foreach var el in installing do
FailedToInstall.AddRange(el.Install);
comp := true;
end);
thr.Start;
while not comp do
begin
AddOtp($'{pst_done*100:N2}%');
y -= 1;
Sleep(10);
end;
end;
procedure ОжиданиеЗакрытияПаскаля;
begin
var pas_proc := System.Diagnostics.Process.GetProcessesByName('PascalABCNET');
if pas_proc.Length <> 0 then
begin
AddOtp($'не удалось установить {pst_err:P}');
AddOtp($'закройте паскаль, чтоб установить остальное');
while pas_proc.Length <> 0 do
begin
pas_proc := pas_proc.Where(proc -> not proc.HasExited).ToArray;
Sleep(100);
end;
Sleep(1000);
end;
end;
begin
try
Console.OutputEncoding := Encoding.UTF8;
Подготовка;
Скачивание;
Распаковка;
var try2 := System.Diagnostics.Process.GetProcessesByName('PascalABCNET').Length <> 0;
Установка((
System.IO.Directory.EnumerateDirectories(TempFolder)
.Select(fname->System.IO.Path.GetFileName(fname))
.Where(dir->not dir.StartsWith('$'))
.Select(dir->InstallableElement(new InstallableFolder($'{TempFolder}\{dir}',$'C:\Program Files (x86)\PascalABC.NET\{dir}')))
+
System.IO.Directory.EnumerateFiles(TempFolder)
.Select(fname->System.IO.Path.GetFileName(fname))
//.Where(fname->fname<>'pabcworknet.ini')
//.Where(fname->fname<>'gacutlrc.dll')
//.Where(fname->fname<>'gacutil.exe.config')
.Where(fname->System.IO.Path.GetExtension(fname) = '.exe')
.Select(fname->InstallableElement(new InstallableFile($'{TempFolder}\{fname}',$'C:\Program Files (x86)\PascalABC.NET\{fname}')))
+
System.IO.Directory.EnumerateFiles(TempFolder)
.Select(fname->System.IO.Path.GetFileName(fname))
.Where(fname->fname<>'pabcworknet.ini')
// .Where(fname->fname<>'gacutlrc.dll')
// .Where(fname->fname<>'gacutil.exe.config')
.Where(fname->System.IO.Path.GetExtension(fname) <> '.exe')
.Select(fname->InstallableElement(new InstallableFile($'{TempFolder}\{fname}',$'C:\Program Files (x86)\PascalABC.NET\{fname}')))
+
(
new InstallableFolder($'{TempFolder}\$_1_\Samples','C:\PABCWork.NET\Samples')
as InstallableElement
)
).ToList);
if try2 and (FailedToInstall.Count <> 0) then
begin
ОжиданиеЗакрытияПаскаля;
Установка(FailedToInstall);
end;
if FailedToInstall.Count <> 0 then
begin
AddOtp($'{FailedToInstall.Select(el->el.GetFileCount).Sum} файлов не было установлено:');
FailedToInstall.PrintLines;
readln;
end;
AddOtp('Жду окончания установочных процедур');
while procs_alive.Any do
begin
procs_alive.RemoveAll(p->p.HasExited);
Sleep(10);
end;
AddOtp('Удаляю папку в которую распоковывал');
DeleteFolder(TempFolder);
except
on e: System.Exception do
begin
AddOtp('при выполнении возникла ошибка:');
AddOtp(e.ToString);
readln;
end;
end;
end.