|
| 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 = 'QRCodeGenLib.Demo'; |
| 18 | + Dependencies: array of string = (); |
| 19 | + |
| 20 | +type |
| 21 | + TLog = (audit, info, error); |
| 22 | + |
| 23 | + Output = record |
| 24 | + Success: boolean; |
| 25 | + Output: string; |
| 26 | + end; |
| 27 | + |
| 28 | + procedure OutLog(const Knd: TLog; const 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: string; |
| 38 | + begin |
| 39 | + if FileExists('.gitmodules') then |
| 40 | + if RunCommand('git', ['submodule', 'update', '--init', '--recursive', |
| 41 | + '--force', '--remote'], Result) then |
| 42 | + OutLog(info, Result) |
| 43 | + else |
| 44 | + OutLog(error, Result); |
| 45 | + end; |
| 46 | + |
| 47 | + function AddPackage(const Path: string): string; |
| 48 | + begin |
| 49 | + if RunCommand('lazbuild', ['--add-package-link', Path], Result) then |
| 50 | + OutLog(audit, 'Add package:'#9 + Path); |
| 51 | + end; |
| 52 | + |
| 53 | + function SelectString(const Input, Reg: string): string; |
| 54 | + var |
| 55 | + Line: string; |
| 56 | + begin |
| 57 | + Result := ' '; |
| 58 | + for Line in Input.Split(LineEnding) do |
| 59 | + with TRegExpr.Create do |
| 60 | + begin |
| 61 | + Expression := Reg; |
| 62 | + if Exec(Line) then |
| 63 | + Result += Line + LineEnding; |
| 64 | + Free; |
| 65 | + end; |
| 66 | + end; |
| 67 | + |
| 68 | + function RunTest(const Path: String): string; |
| 69 | + begin |
| 70 | + OutLog(audit, #9'run:'#9 + Path); |
| 71 | + if RunCommand(Path, ['--all', '--format=plain'], Result) then |
| 72 | + OutLog(info, #9'success!') |
| 73 | + else |
| 74 | + ExitCode += 1; |
| 75 | + OutLog(audit, Result); |
| 76 | + end; |
| 77 | + |
| 78 | + function BuildProject(const Path: string): Output; |
| 79 | + begin |
| 80 | + OutLog(audit, 'Build from:'#9 + Path); |
| 81 | + Result.Success := RunCommand('lazbuild', |
| 82 | + ['--build-all', '--recursive', '--no-write-project', Path], Result.Output); |
| 83 | + Result.Output := SelectString(Result.Output, '(Fatal:|Error:|Linking)'); |
| 84 | + if Result.Success then |
| 85 | + begin |
| 86 | + Result.Output := Result.Output.Split(' ')[3].Replace(LineEnding, ''); |
| 87 | + OutLog(info, #9'to:'#9 + Result.Output); |
| 88 | + if ContainsStr(ReadFileToString(Path.Replace('.lpi', '.lpr')), 'consoletestrunner') then |
| 89 | + RunTest(Result.Output.Replace(#10, '')); |
| 90 | + end |
| 91 | + else |
| 92 | + begin |
| 93 | + ExitCode += 1; |
| 94 | + OutLog(error, Result.Output); |
| 95 | + end; |
| 96 | + end; |
| 97 | + |
| 98 | + function DownloadFile(const Uri: string): string; |
| 99 | + var |
| 100 | + OutFile: TStream; |
| 101 | + begin |
| 102 | + InitSSLInterface; |
| 103 | + Result := GetTempFileName; |
| 104 | + OutFile := TFileStream.Create(Result, fmCreate or fmOpenWrite); |
| 105 | + with TFPHttpClient.Create(nil) do |
| 106 | + begin |
| 107 | + try |
| 108 | + AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); |
| 109 | + AllowRedirect := True; |
| 110 | + Get(Uri, OutFile); |
| 111 | + OutLog(audit, 'Download from ' + Uri + ' to ' + Result); |
| 112 | + finally |
| 113 | + Free; |
| 114 | + OutFile.Free; |
| 115 | + end; |
| 116 | + end; |
| 117 | + end; |
| 118 | + |
| 119 | + procedure UnZip(const ZipFile, ZipPath: string); |
| 120 | + begin |
| 121 | + with TUnZipper.Create do |
| 122 | + begin |
| 123 | + try |
| 124 | + FileName := ZipFile; |
| 125 | + OutputPath := ZipPath; |
| 126 | + Examine; |
| 127 | + UnZipAllFiles; |
| 128 | + OutLog(audit, 'Unzip from'#9 + ZipFile + #9'to'#9 + ZipPath); |
| 129 | + DeleteFile(ZipFile); |
| 130 | + finally |
| 131 | + Free; |
| 132 | + end; |
| 133 | + end; |
| 134 | + end; |
| 135 | + |
| 136 | + function InstallOPM(const Path: string): string; |
| 137 | + begin |
| 138 | + Result := |
| 139 | + {$IFDEF MSWINDOWS} |
| 140 | + GetEnvironmentVariable('APPDATA') + '\.lazarus\onlinepackagemanager\packages\' |
| 141 | + {$ELSE} |
| 142 | + GetEnvironmentVariable('HOME') + '/.lazarus/onlinepackagemanager/packages/' |
| 143 | + {$ENDIF} |
| 144 | + + Path; |
| 145 | + if not DirectoryExists(Result) then |
| 146 | + begin |
| 147 | + CreateDir(Result); |
| 148 | + UnZip(DownloadFile('https://packages.lazarus-ide.org/' + Path + '.zip'), Result); |
| 149 | + end; |
| 150 | + end; |
| 151 | + |
| 152 | + function BuildAll: string; |
| 153 | + var |
| 154 | + List: TStringList; |
| 155 | + begin |
| 156 | + CheckModules; |
| 157 | + List := FindAllFiles(GetCurrentDir, '*.lpk', True); |
| 158 | + try |
| 159 | + for Result in Dependencies do |
| 160 | + List.AddStrings(FindAllFiles(InstallOPM(Result), '*.lpk', True)); |
| 161 | + for Result in List do |
| 162 | + AddPackage(Result); |
| 163 | + List := FindAllFiles(Target, '*.lpi', True); |
| 164 | + for Result in List do |
| 165 | + BuildProject(Result); |
| 166 | + finally |
| 167 | + List.Free; |
| 168 | + end; |
| 169 | + case ExitCode of |
| 170 | + 0: OutLog(info, 'Errors:'#9 + IntToStr(ExitCode)); |
| 171 | + else |
| 172 | + OutLog(error, 'Errors:'#9 + IntToStr(ExitCode)); |
| 173 | + end; |
| 174 | + end; |
| 175 | + |
| 176 | +begin |
| 177 | + try |
| 178 | + BuildAll |
| 179 | + except |
| 180 | + on E: Exception do |
| 181 | + Writeln(E.ClassName, #9, E.Message); |
| 182 | + end; |
| 183 | +end. |
0 commit comments