|
| 1 | +//https://castle-engine.io/modern_pascal |
| 2 | + |
| 3 | +program Make; |
| 4 | +{$mode objfpc}{$H+} |
| 5 | + |
| 6 | +uses |
| 7 | + Classes, |
| 8 | + SysUtils, |
| 9 | + StrUtils, |
| 10 | + FileUtil, |
| 11 | + Zipper, |
| 12 | + fphttpclient, |
| 13 | + RegExpr, |
| 14 | + openssl, |
| 15 | + LazUTF8, |
| 16 | + opensslsockets, |
| 17 | + eventlog, |
| 18 | + Process; |
| 19 | + |
| 20 | + function OutLog(const Knd: TEventType; const Msg: string): string; |
| 21 | + begin |
| 22 | + case Knd of |
| 23 | + etError: Result := #27'[31m%s'#27'[0m'; |
| 24 | + etInfo: Result := #27'[32m%s'#27'[0m'; |
| 25 | + etDebug: Result := #27'[33m%s'#27'[0m'; |
| 26 | + end; |
| 27 | + Writeln(stderr, UTF8ToConsole(Result.Format([Msg]))); |
| 28 | + end; |
| 29 | + |
| 30 | + function AddPackage(const Path: string): string; |
| 31 | + begin |
| 32 | + if RunCommand('lazbuild', ['--add-package-link', Path], Result, [poStderrToOutPut]) then |
| 33 | + OutLog(etDebug, 'Add package:'#9 + Path) |
| 34 | + else |
| 35 | + begin |
| 36 | + ExitCode += 1; |
| 37 | + OutLog(etError, Result); |
| 38 | + end; |
| 39 | + end; |
| 40 | + |
| 41 | + function SelectString(const Input, Reg: string): string; |
| 42 | + var |
| 43 | + Line: string; |
| 44 | + begin |
| 45 | + Result := EmptyStr; |
| 46 | + with TRegExpr.Create do |
| 47 | + begin |
| 48 | + Expression := Reg; |
| 49 | + for Line in Input.Split(LineEnding) do |
| 50 | + if Exec(Line) then |
| 51 | + Result += Line + LineEnding; |
| 52 | + Free; |
| 53 | + end; |
| 54 | + end; |
| 55 | + |
| 56 | + function RunTest(const Path: String): string; |
| 57 | + begin |
| 58 | + OutLog(etDebug, #9'run:'#9 + Path); |
| 59 | + if RunCommand(Path, ['--all', '--format=plain'], Result, [poStderrToOutPut]) then |
| 60 | + OutLog(etInfo, #9'success!') |
| 61 | + else |
| 62 | + begin |
| 63 | + ExitCode += 1; |
| 64 | + OutLog(etError, Result); |
| 65 | + end; |
| 66 | + end; |
| 67 | + |
| 68 | + function AddDDL(const Path: String): string; |
| 69 | + begin |
| 70 | + OutLog(etDebug, #9'add:'#9 + Path); |
| 71 | + if RunCommand('sudo', ['bash', '-c', 'cp %s /usr/lib/; ldconfig --verbose'.Format([Path])], Result, [poStderrToOutPut]) then |
| 72 | + OutLog(etInfo, #9'success!') |
| 73 | + else |
| 74 | + begin |
| 75 | + ExitCode += 1; |
| 76 | + OutLog(etError, Result); |
| 77 | + end; |
| 78 | + end; |
| 79 | + |
| 80 | + function BuildProject(const Path: string): string; |
| 81 | + begin |
| 82 | + OutLog(etDebug, 'Build from:'#9 + Path); |
| 83 | + if RunCommand('lazbuild', |
| 84 | + ['--build-all', '--recursive', '--no-write-project', Path], Result, [poStderrToOutPut]) then |
| 85 | + begin |
| 86 | + Result := SelectString(Result, 'Linking').Split(' ')[2].Replace(LineEnding, EmptyStr); |
| 87 | + OutLog(etInfo, #9'to:'#9 + Result); |
| 88 | + if ReadFileToString(Path.Replace('.lpi', '.lpr')).Contains('consoletestrunner') then |
| 89 | + RunTest(Result) |
| 90 | + else if ReadFileToString(Path.Replace('.lpi', '.lpr')).Contains('exports') then |
| 91 | + AddDDL(Result) |
| 92 | + end |
| 93 | + else |
| 94 | + begin |
| 95 | + ExitCode += 1; |
| 96 | + OutLog(etError, SelectString(Result, '(Fatal|Error):')); |
| 97 | + end; |
| 98 | + end; |
| 99 | + |
| 100 | + function DownloadFile(const Uri: string): string; |
| 101 | + var |
| 102 | + OutFile: TStream; |
| 103 | + begin |
| 104 | + InitSSLInterface; |
| 105 | + Result := GetTempFileName; |
| 106 | + OutFile := TFileStream.Create(Result, fmCreate or fmOpenWrite); |
| 107 | + with TFPHttpClient.Create(nil) do |
| 108 | + begin |
| 109 | + try |
| 110 | + AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); |
| 111 | + AllowRedirect := True; |
| 112 | + Get(Uri, OutFile); |
| 113 | + OutLog(etDebug, 'Download from %s to %s'.Format([Uri, Result])); |
| 114 | + finally |
| 115 | + Free; |
| 116 | + OutFile.Free; |
| 117 | + end; |
| 118 | + end; |
| 119 | + end; |
| 120 | + |
| 121 | + procedure UnZip(const ZipFile, ZipPath: string); |
| 122 | + begin |
| 123 | + with TUnZipper.Create do |
| 124 | + begin |
| 125 | + try |
| 126 | + FileName := ZipFile; |
| 127 | + OutputPath := ZipPath; |
| 128 | + Examine; |
| 129 | + UnZipAllFiles; |
| 130 | + OutLog(etDebug, 'Unzip from'#9 + ZipFile + #9'to'#9 + ZipPath); |
| 131 | + DeleteFile(ZipFile); |
| 132 | + finally |
| 133 | + Free; |
| 134 | + end; |
| 135 | + end; |
| 136 | + end; |
| 137 | + |
| 138 | + function InstallOPM(const Path: string): string; |
| 139 | + begin |
| 140 | + Result := |
| 141 | + {$IFDEF MSWINDOWS} |
| 142 | + GetEnvironmentVariable('APPDATA') + '\.lazarus\onlinepackagemanager\packages\' |
| 143 | + {$ELSE} |
| 144 | + GetEnvironmentVariable('HOME') + '/.lazarus/onlinepackagemanager/packages/' |
| 145 | + {$ENDIF} |
| 146 | + + Path; |
| 147 | + if not DirectoryExists(Result) then |
| 148 | + begin |
| 149 | + if ForceDirectories(Result) then |
| 150 | + UnZip(DownloadFile('https://packages.lazarus-ide.org/%s.zip'.Format([Path])), Result); |
| 151 | + end; |
| 152 | + end; |
| 153 | + |
| 154 | + function BuildAll(const Target: string; const Dependencies: array of string): string; |
| 155 | + var |
| 156 | + List: TStringList; |
| 157 | + DT: TDateTime; |
| 158 | + begin |
| 159 | + DT := Time; |
| 160 | + if FileExists('.gitmodules') then |
| 161 | + if RunCommand('git', ['submodule', 'update', '--init', '--recursive', |
| 162 | + '--force', '--remote'], Result, [poStderrToOutPut]) then |
| 163 | + OutLog(etInfo, Result) |
| 164 | + else |
| 165 | + begin |
| 166 | + ExitCode += 1; |
| 167 | + OutLog(etError, Result); |
| 168 | + end; |
| 169 | + List := FindAllFiles(GetCurrentDir, '*.lpk'); |
| 170 | + try |
| 171 | + for Result in Dependencies do |
| 172 | + List.AddStrings(FindAllFiles(InstallOPM(Result), '*.lpk')); |
| 173 | + for Result in List do |
| 174 | + AddPackage(Result); |
| 175 | + List := FindAllFiles(Target, '*.lpi'); |
| 176 | + List.Sort; |
| 177 | + for Result in List do |
| 178 | + if not Result.Contains('backup') then |
| 179 | + BuildProject(Result); |
| 180 | + finally |
| 181 | + List.Free; |
| 182 | + end; |
| 183 | + if not RunCommand('delp', ['-r', GetCurrentDir], Result, [poStderrToOutPut]) then |
| 184 | + OutLog(etError, Result); |
| 185 | + OutLog(etDebug, 'Duration:'#9 + FormatDateTime('hh:nn:ss', DT - Time)); |
| 186 | + end; |
| 187 | + |
| 188 | +begin |
| 189 | + try |
| 190 | + BuildAll('demos', []); |
| 191 | + case ExitCode of |
| 192 | + 0: OutLog(etInfo, 'Errors:'#9 + ExitCode.ToString); |
| 193 | + else |
| 194 | + OutLog(etError, 'Errors:'#9 + ExitCode.ToString); |
| 195 | + end; |
| 196 | + except |
| 197 | + on E: Exception do |
| 198 | + Writeln(E.ClassName, #9, E.Message); |
| 199 | + end; |
| 200 | +end. |
0 commit comments