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