-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmake.pas
More file actions
198 lines (188 loc) · 4.79 KB
/
make.pas
File metadata and controls
198 lines (188 loc) · 4.79 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
program Make;
{$mode objfpc}{$H+}
uses
Classes,
SysUtils,
StrUtils,
FileUtil,
Zipper,
fphttpclient,
RegExpr,
openssl,
opensslsockets,
Process;
const
Target: string = '.';
Dependencies: array of string = ();
type
Output = record
Code: boolean;
Output: ansistring;
end;
function CheckModules: Output;
begin
if FileExists('.gitmodules') then
if RunCommand('git', ['submodule', 'update', '--init', '--recursive',
'--force', '--remote'], Result.Output) then
Writeln(stderr, #27'[33m', Result.Output, #27'[0m');
end;
function AddPackage(Path: string): Output;
begin
with TRegExpr.Create do
begin
Expression :=
{$IFDEF MSWINDOWS}
'(cocoa|x11|_template)'
{$ELSE}
'(cocoa|gdi|_template)'
{$ENDIF}
;
if not Exec(Path) and RunCommand('lazbuild', ['--add-package-link', Path],
Result.Output) then
Writeln(stderr, #27'[33m', 'added ', Path, #27'[0m');
Free;
end;
end;
function BuildProject(Path: string): Output;
var
Line: string;
begin
Write(stderr, #27'[33m', 'build from ', Path, #27'[0m');
try
Result.Code := RunCommand('lazbuild', ['--build-all', '--recursive',
'--no-write-project', Path], Result.Output);
if Result.Code then
for Line in SplitString(Result.Output, LineEnding) do
begin
if ContainsStr(Line, 'Linking') then
begin
Result.Output := SplitString(Line, ' ')[2];
Writeln(stderr, #27'[32m', ' to ', Result.Output, #27'[0m');
break;
end;
end
else
begin
ExitCode += 1;
for Line in SplitString(Result.Output, LineEnding) do
with TRegExpr.Create do
begin
Expression := '(Fatal|Error):';
if Exec(Line) then
begin
WriteLn(stderr);
Writeln(stderr, #27'[31m', Line, #27'[0m');
end;
Free;
end;
end;
except
on E: Exception do
WriteLn(stderr, 'Error: ' + E.ClassName + #13#10 + E.Message);
end;
end;
function RunTest(Path: string): Output;
var
Temp: string;
begin
Result := BuildProject(Path);
Temp:= Result.Output;
if Result.Code then
try
if not RunCommand(Temp, ['--all', '--format=plain', '--progress'], Result.Output) then
ExitCode += 1;
WriteLn(stderr, Result.Output);
except
on E: Exception do
WriteLn(stderr, 'Error: ' + E.ClassName + #13#10 + E.Message);
end;
end;
function AddOPM(Each: string): string;
var
TempFile, Url: string;
Zip: TStream;
begin
Result :=
{$IFDEF MSWINDOWS}
GetEnvironmentVariable('APPDATA') + '\.lazarus\onlinepackagemanager\packages\'
{$ELSE}
GetEnvironmentVariable('HOME') + '/.lazarus/onlinepackagemanager/packages/'
{$ENDIF}
+ Each;
TempFile := GetTempFileName;
Url := 'https://packages.lazarus-ide.org/' + Each + '.zip';
if not DirectoryExists(Result) then
begin
Zip := TFileStream.Create(TempFile, fmCreate or fmOpenWrite);
with TFPHttpClient.Create(nil) do
begin
try
AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
AllowRedirect := True;
Get(Url, Zip);
WriteLn(stderr, 'Download from ', Url, ' to ', TempFile);
finally
Free;
end;
end;
Zip.Free;
CreateDir(Result);
with TUnZipper.Create do
begin
try
FileName := TempFile;
OutputPath := Result;
Examine;
UnZipAllFiles;
WriteLn(stderr, 'Unzip from ', TempFile, ' to ', Result);
finally
Free;
end;
end;
DeleteFile(TempFile);
end;
end;
function Main: Output;
var
Each, Item: string;
List: TStringList;
begin
CheckModules;
InitSSLInterface;
for Each in Dependencies do
begin
List := FindAllFiles(AddOPM(Each), '*.lpk', True);
try
for Item in List do
AddPackage(Item);
finally
List.Free;
end;
end;
List := FindAllFiles(GetCurrentDir, '*.lpk', True);
try
for Each in List do
AddPackage(Each);
finally
List.Free;
end;
List := FindAllFiles(Target, '*.lpi', True);
try
for Each in List do
if ContainsStr(ReadFileToString(ReplaceStr(Each, '.lpi', '.lpr')),
'consoletestrunner') then
RunTest(Each)
else
BuildProject(Each);
finally
List.Free;
end;
WriteLn(stderr);
if ExitCode <> 0 then
WriteLn(stderr, #27'[31m', 'Errors: ', ExitCode, #27'[0m')
else
WriteLn(stderr, #27'[32m', 'Errors: ', ExitCode, #27'[0m');
end;
begin
Main;
end.