|
8 | 8 | FileUtil,
|
9 | 9 | Zipper,
|
10 | 10 | fphttpclient,
|
| 11 | + RegExpr, |
11 | 12 | openssl,
|
12 | 13 | opensslsockets,
|
13 | 14 | Process;
|
14 | 15 |
|
15 | 16 | const
|
16 |
| - Src: string = 'demos'; |
| 17 | + Src: string = 'Examples'; |
17 | 18 | Use: string = 'Package';
|
18 | 19 | Tst: string = 'testconsole.lpi';
|
19 | 20 | Pkg: array of string = ();
|
20 | 21 |
|
| 22 | +type |
| 23 | + Output = record |
| 24 | + Code: integer; |
| 25 | + Output: ansistring; |
| 26 | + end; |
| 27 | + |
21 | 28 | var
|
22 |
| - Output, Line: ansistring; |
23 |
| - List: TStringList; |
24 | 29 | Each, Item, PackagePath, TempFile, Url: string;
|
| 30 | + Line: ansistring; |
| 31 | + Answer: Output; |
| 32 | + List: TStringList; |
25 | 33 | Zip: TStream;
|
26 | 34 |
|
27 |
| -begin |
28 |
| - InitSSLInterface; |
29 |
| - if FileExists('.gitmodules') then |
30 |
| - if RunCommand('git', ['submodule', 'update', '--init', '--recursive', |
31 |
| - '--force', '--remote'], Output) then |
32 |
| - Writeln(#27'[33m', Output, #27'[0m') |
33 |
| - else |
34 |
| - begin |
35 |
| - ExitCode += 1; |
36 |
| - Writeln(#27'[31m', Output, #27'[0m'); |
37 |
| - end; |
38 |
| - List := FindAllFiles(Use, '*.lpk', True); |
39 |
| - try |
40 |
| - for Each in List do |
41 |
| - if RunCommand('lazbuild', ['--add-package-link', Each], Output) then |
42 |
| - Writeln(#27'[33m', 'added ', Each, #27'[0m') |
| 35 | + procedure CheckModules; |
| 36 | + begin |
| 37 | + if FileExists('.gitmodules') then |
| 38 | + if RunCommand('git', ['submodule', 'update', '--init', '--recursive', |
| 39 | + '--force', '--remote'], Answer.Output) then |
| 40 | + Writeln(stderr, #27'[33m', Answer.Output, #27'[0m') |
43 | 41 | else
|
44 | 42 | begin
|
45 | 43 | ExitCode += 1;
|
46 |
| - Writeln(#27'[31m', 'added ', Each, #27'[0m'); |
| 44 | + Writeln(stderr, #27'[31m', Answer.Output, #27'[0m'); |
47 | 45 | end;
|
48 |
| - finally |
49 |
| - List.Free; |
50 | 46 | end;
|
51 |
| - for Each in Pkg do |
| 47 | + |
| 48 | + procedure AddPackage(Path: string); |
52 | 49 | begin
|
53 |
| - PackagePath := GetEnvironmentVariable('HOME') + |
54 |
| - '/.lazarus/onlinepackagemanager/packages/' + Each; |
55 |
| - TempFile := GetTempFileName; |
56 |
| - Url := 'https://packages.lazarus-ide.org/' + Each + '.zip'; |
57 |
| - if not DirectoryExists(PackagePath) then |
58 |
| - begin |
59 |
| - Zip := TFileStream.Create(TempFile, fmCreate or fmOpenWrite); |
60 |
| - with TFPHttpClient.Create(nil) do |
61 |
| - begin |
62 |
| - try |
63 |
| - AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); |
64 |
| - AllowRedirect := True; |
65 |
| - Get(Url, Zip); |
66 |
| - WriteLn('Download from ', Url, ' to ', TempFile); |
67 |
| - finally |
68 |
| - Free; |
| 50 | + List := FindAllFiles(Use, '*.lpk', True); |
| 51 | + try |
| 52 | + for Each in List do |
| 53 | + if RunCommand('lazbuild', ['--add-package-link', Each], Answer.Output) then |
| 54 | + Writeln(stderr, #27'[33m', 'added ', Each, #27'[0m') |
| 55 | + else |
| 56 | + begin |
| 57 | + ExitCode += 1; |
| 58 | + Writeln(stderr, #27'[31m', 'added ', Each, #27'[0m'); |
69 | 59 | end;
|
70 |
| - end; |
71 |
| - Zip.Free; |
72 |
| - CreateDir(PackagePath); |
73 |
| - with TUnZipper.Create do |
| 60 | + finally |
| 61 | + List.Free; |
| 62 | + end; |
| 63 | + end; |
| 64 | + |
| 65 | + procedure AddOPM; |
| 66 | + begin |
| 67 | + InitSSLInterface; |
| 68 | + for Each in Pkg do |
| 69 | + begin |
| 70 | + PackagePath := |
| 71 | + {$IFDEF MSWINDOWS} |
| 72 | + GetEnvironmentVariable('APPDATA') + '\.lazarus\onlinepackagemanager\packages\' |
| 73 | + {$ELSE} |
| 74 | + GetEnvironmentVariable('HOME') + '/.lazarus/onlinepackagemanager/packages/' |
| 75 | + {$ENDIF} |
| 76 | + + Each; |
| 77 | + TempFile := GetTempFileName; |
| 78 | + Url := 'https://packages.lazarus-ide.org/' + Each + '.zip'; |
| 79 | + if not DirectoryExists(PackagePath) then |
74 | 80 | begin
|
75 |
| - try |
76 |
| - FileName := TempFile; |
77 |
| - OutputPath := PackagePath; |
78 |
| - Examine; |
79 |
| - UnZipAllFiles; |
80 |
| - WriteLn('Unzip from ', TempFile, ' to ', PackagePath); |
81 |
| - finally |
82 |
| - Free; |
| 81 | + Zip := TFileStream.Create(TempFile, fmCreate or fmOpenWrite); |
| 82 | + with TFPHttpClient.Create(nil) do |
| 83 | + begin |
| 84 | + try |
| 85 | + AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); |
| 86 | + AllowRedirect := True; |
| 87 | + Get(Url, Zip); |
| 88 | + WriteLn(stderr, 'Download from ', Url, ' to ', TempFile); |
| 89 | + finally |
| 90 | + Free; |
| 91 | + end; |
83 | 92 | end;
|
84 |
| - end; |
85 |
| - DeleteFile(TempFile); |
86 |
| - List := FindAllFiles(PackagePath, '*.lpk', True); |
87 |
| - try |
88 |
| - for Item in List do |
89 |
| - if RunCommand('lazbuild', ['--add-package-link', Item], Output) then |
90 |
| - Writeln(#27'[33m', 'added ', Item, #27'[0m') |
91 |
| - else |
92 |
| - begin |
93 |
| - ExitCode += 1; |
94 |
| - Writeln(#27'[31m', 'added ', Item, #27'[0m'); |
| 93 | + Zip.Free; |
| 94 | + CreateDir(PackagePath); |
| 95 | + with TUnZipper.Create do |
| 96 | + begin |
| 97 | + try |
| 98 | + FileName := TempFile; |
| 99 | + OutputPath := PackagePath; |
| 100 | + Examine; |
| 101 | + UnZipAllFiles; |
| 102 | + WriteLn(stderr, 'Unzip from ', TempFile, ' to ', PackagePath); |
| 103 | + finally |
| 104 | + Free; |
95 | 105 | end;
|
96 |
| - finally |
97 |
| - List.Free; |
| 106 | + end; |
| 107 | + DeleteFile(TempFile); |
| 108 | + AddPackage(PackagePath); |
98 | 109 | end;
|
99 | 110 | end;
|
100 | 111 | end;
|
101 |
| - List := FindAllFiles('.', Tst, True); |
102 |
| - try |
103 |
| - for Each in List do |
104 |
| - begin |
105 |
| - Writeln(#27'[33m', 'build ', Each, #27'[0m'); |
| 112 | + |
| 113 | + procedure BuildProject(Path: string); |
| 114 | + begin |
| 115 | + Write(stderr, #27'[33m', 'build from ', Each, #27'[0m'); |
| 116 | + try |
106 | 117 | if RunCommand('lazbuild', ['--build-all', '--recursive',
|
107 |
| - '--no-write-project', Each], Output) then |
108 |
| - for Line in SplitString(Output, LineEnding) do |
| 118 | + '--no-write-project', Each], Answer.Output) then |
| 119 | + Answer.Code := 0 |
| 120 | + else |
| 121 | + begin |
| 122 | + Answer.Code := 1; |
| 123 | + ExitCode += Answer.Code; |
| 124 | + end; |
| 125 | + except |
| 126 | + on E: Exception do |
| 127 | + WriteLn(stderr, 'Error: ' + E.ClassName + #13#10 + E.Message); |
| 128 | + end; |
| 129 | + end; |
| 130 | + |
| 131 | + procedure RunTest; |
| 132 | + begin |
| 133 | + List := FindAllFiles('.', Tst, True); |
| 134 | + try |
| 135 | + for Each in List do |
| 136 | + begin |
| 137 | + BuildProject(Each); |
| 138 | + if Answer.Code <> 0 then |
109 | 139 | begin
|
110 |
| - if Pos('Linking', Line) <> 0 then |
111 |
| - begin |
112 |
| - if not RunCommand('command', [SplitString(Line, ' ')[2], |
113 |
| - '--all', '--format=plain', '--progress'], Output) then |
114 |
| - ExitCode += 1; |
115 |
| - WriteLn(Output); |
116 |
| - end; |
| 140 | + for Line in SplitString(Answer.Output, LineEnding) do |
| 141 | + with TRegExpr.Create do |
| 142 | + begin |
| 143 | + Expression := '(Fatal|Error):'; |
| 144 | + if Exec(Line) then |
| 145 | + begin |
| 146 | + WriteLn(stderr); |
| 147 | + Writeln(stderr, #27'[31m', Line, #27'[0m'); |
| 148 | + end; |
| 149 | + Free; |
| 150 | + end; |
117 | 151 | end
|
118 |
| - else |
119 |
| - for Line in SplitString(Output, LineEnding) do |
120 |
| - if Pos('Fatal', Line) <> 0 or Pos('Error', Line) then |
121 |
| - Writeln(#27'[31m', Line, #27'[0m'); |
| 152 | + else |
| 153 | + for Line in SplitString(Answer.Output, LineEnding) do |
| 154 | + if Pos('Linking', Line) <> 0 then |
| 155 | + try |
| 156 | + begin |
| 157 | + Writeln(stderr, #27'[32m', ' to ', SplitString(Line, ' ')[2], #27'[0m'); |
| 158 | + if not RunCommand(ReplaceStr(SplitString(Line, ' ')[2], |
| 159 | + SplitString(Tst, '.')[0], './' + SplitString(Tst, '.')[0]), |
| 160 | + ['--all', '--format=plain', '--progress'], Answer.Output) then |
| 161 | + ExitCode += 1; |
| 162 | + WriteLn(stderr, Answer.Output); |
| 163 | + break; |
| 164 | + end; |
| 165 | + except |
| 166 | + on E: Exception do |
| 167 | + WriteLn(stderr, 'Error: ' + E.ClassName + #13#10 + E.Message); |
| 168 | + end; |
| 169 | + end; |
| 170 | + finally |
| 171 | + List.Free; |
122 | 172 | end;
|
123 |
| - finally |
124 |
| - List.Free; |
125 | 173 | end;
|
| 174 | + |
| 175 | +begin |
| 176 | + CheckModules; |
| 177 | + AddPackage(Use); |
| 178 | + AddOPM; |
| 179 | + {$IFDEF LINUX} |
| 180 | + RunTest; |
| 181 | + {$ENDIF} |
126 | 182 | List := FindAllFiles(Src, '*.lpi', True);
|
127 | 183 | try
|
128 | 184 | for Each in List do
|
129 |
| - begin |
130 |
| - Write(#27'[33m', 'build from ', Each, #27'[0m'); |
131 |
| - if RunCommand('lazbuild', ['--build-all', '--recursive', |
132 |
| - '--no-write-project', Each], Output) then |
133 |
| - for Line in SplitString(Output, LineEnding) do |
134 |
| - begin |
135 |
| - if Pos('Linking', Line) <> 0 then |
136 |
| - Writeln(#27'[32m', ' to ', SplitString(Line, ' ')[2], #27'[0m'); |
137 |
| - end |
138 |
| - else |
| 185 | + if Pos(Tst, Each) = 0 then |
139 | 186 | begin
|
140 |
| - ExitCode += 1; |
141 |
| - for Line in SplitString(Output, LineEnding) do |
142 |
| - if Pos('Fatal:', Line) <> 0 or Pos('Error:', Line) then |
| 187 | + BuildProject(Each); |
| 188 | + if Answer.Code <> 0 then |
| 189 | + begin |
| 190 | + for Line in SplitString(Answer.Output, LineEnding) do |
| 191 | + with TRegExpr.Create do |
143 | 192 | begin
|
144 |
| - WriteLn(); |
145 |
| - Writeln(#27'[31m', Line, #27'[0m'); |
| 193 | + Expression := '(Fatal|Error):'; |
| 194 | + if Exec(Line) then |
| 195 | + begin |
| 196 | + WriteLn(stderr); |
| 197 | + Writeln(stderr, #27'[31m', Line, #27'[0m'); |
| 198 | + end; |
| 199 | + Free; |
146 | 200 | end;
|
| 201 | + end |
| 202 | + else |
| 203 | + for Line in SplitString(Answer.Output, LineEnding) do |
| 204 | + if Pos('Linking', Line) <> 0 then |
| 205 | + Writeln(stderr, #27'[32m', ' to ', SplitString(Line, ' ')[2], #27'[0m'); |
147 | 206 | end;
|
148 |
| - end; |
149 | 207 | finally
|
150 | 208 | List.Free;
|
151 | 209 | end;
|
| 210 | + WriteLn(stderr); |
| 211 | + if ExitCode <> 0 then |
| 212 | + WriteLn(stderr, #27'[31m', 'Errors: ', ExitCode, #27'[0m') |
| 213 | + else |
| 214 | + WriteLn(stderr, #27'[32m', 'Errors: ', ExitCode, #27'[0m'); |
152 | 215 | end.
|
0 commit comments