-
Notifications
You must be signed in to change notification settings - Fork 47
/
build.php
executable file
·406 lines (277 loc) · 12.5 KB
/
build.php
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env php7.4
<?php
use Github\Client;
require(__DIR__ . '/vendor/autoload.php');
$config = require(__DIR__ . '/config.php');
error_reporting(E_ALL);
function exception_error_handler($severity, $message, $file, $line)
{
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
function run(string $cmd): array
{
echo "Running " . $cmd . "\n";
exec($cmd, $output, $return);
foreach ($output as $line) {
echo $line . "\n";
}
if ($return > 0) {
throw new Exception("Command failed with status " . $return);
}
return $output;
}
function cd(string $dir) : void
{
$dir = realpath($dir);
echo "\ncd $dir\n\n";
if (!chdir($dir)) {
throw new Exception("Could not change dir to '" . $dir . "'");
}
}
class Builder
{
public $test = false;
private $majorVersion = "6.8";
private $gitBranch = 'master';
/**
*
* @var string sixsix, sixseven etc or testing
*/
public $distro = "sixeight";
public $repreproDir = __DIR__ . "/deploy/reprepro";
/**
*
* @var int
*/
private $minorVersion;
/**
*
* @var string groupoffice-6.3.8-php-70
*/
private $packageName;
private $encoder = '/usr/local/share/sourceguardian/bin/sourceguardian --phpversion 8.1+';
private $encoderOptions = null;
private $proRepos = "[email protected]:groupoffice/promodules.git";
private $sourceDir = __DIR__ . "/deploy/source";
private $encodedDir = __DIR__ . "/deploy/encoded";
private $buildDir = __DIR__ . "/deploy/build";
private $proModules = ["gota", "documenttemplates", "savemailas", "professional", "tickets", "scanbox", "leavedays", "projects2", "timeregistration2", "hoursapproval2", "pr2analyzer", "workflow", "filesearch", "assistant", "billing"];
private $github = ['PERSONAL_ACCESS_TOKEN' => "secret", 'USERNAME' => 'intermesh', 'REPOSITORY' => 'groupoffice'];
private $githubRelease;
public function __construct($config)
{
$this->github = $config['github'];
}
public function build()
{
//dummy sign just to enter password at start
run("gpg --clear-sign -o- /dev/null");
$this->pullSource();
$this->minorVersion = explode(".", require(dirname(__DIR__) . "/www/version.php"))[2];
$this->packageName = "groupoffice-" . $this->majorVersion . "." . $this->minorVersion;
$this->buildDir = __DIR__ . "/deploy/build/" . $this->majorVersion . "/" . $this->distro;
run("rm -rf " . $this->buildDir);
run("mkdir -p " . $this->buildDir);
$this->encodedDir = __DIR__ . "/deploy/encoded/" . $this->majorVersion . "/" . $this->distro;
run("rm -rf " . $this->encodedDir);
run("mkdir -p " . $this->encodedDir);
$this->buildFromSource();
$this->buildDebianPackage();
if (!$this->test) {
$this->createGithubRelease();
$this->addToDebianRepository();
$this->sendTarToSF();
}
}
private function pullSource()
{
run("mkdir -p " . $this->sourceDir);
cd(dirname(__DIR__));
run("git fetch");
run("git pull");
run("git submodule update --init");
cd($this->sourceDir);
if (!is_dir($this->sourceDir . "/promodules")) {
run("git clone " . $this->proRepos . " -b " . $this->gitBranch);
}
cd($this->sourceDir . "/promodules");
run("git fetch");
run("git checkout " . $this->gitBranch);
run("git pull");
cd($this->sourceDir);
if (!is_dir($this->sourceDir . "/business")) {
run("git clone [email protected]:groupoffice/business.git -b " . $this->gitBranch);
}
cd($this->sourceDir . "/business");
run("git fetch");
run("git checkout " . $this->gitBranch);
run("git pull");
}
private function buildFromSource()
{
run("cp -r " . dirname(__DIR__) . "/www/ " . $this->buildDir . "/" . $this->packageName);
cd($this->buildDir . "/" . $this->packageName);
$this->encode();
$this->buildNodeCore();
$this->buildNodeModules();
putenv("COMPOSER_ALLOW_SUPERUSER=1");
run("composer install --no-dev --optimize-autoloader --ignore-platform-reqs");
$sassFiles = run("find views/Extjs3 go/modules modules \( -name style.scss -o -name style-mobile.scss -o -name htmleditor.scss \) -not -path '*/goui/*'");
foreach ($sassFiles as $sassFile) {
run("sass --no-source-map $sassFile " . dirname(dirname($sassFile)) . '/' . str_replace('scss', 'css', basename($sassFile)));
}
// remove sensitive files OWASP WSTG - WSTG-INFO-05
run("rm composer.json composer.lock vendor/composer/installed.json");
cd($this->buildDir);
run("tar czf " . $this->packageName . ".tar.gz " . $this->packageName);
echo "Created " . $this->buildDir . '/' . $this->packageName . ".tar.gz\n";
}
private function buildNodeCore()
{
cd($this->buildDir . "/" . $this->packageName);
cd("views/goui/goui");
run("npm ci");
cd("../groupoffice-core");
run("npm ci");
cd("../");
run("npm ci");
run("npm run build");
run("npm prune --omit=dev");
cd("groupoffice-core");
run("npm prune --omit=dev");
cd("../goui");
run("npm prune --omit=dev");
}
private function buildNodeModules()
{
cd($this->buildDir . "/" . $this->packageName);
//$packageFiles = array_reverse(run("find views/goui -name package.json -not -path '*/node_modules/*'"));
$packageFiles = run("find go/modules -name package.json -not -path '*/node_modules/*'");
foreach ($packageFiles as $packageFile) {
$nodeDir = dirname($packageFile);
cd($nodeDir);
run("npm ci");
run("pwd");
run("npm run build");
run("npm prune --omit=dev");
cd($this->buildDir . "/" . $this->packageName);
}
}
private function runEncoder(string $sourcePath, string $targetPath, array $excludes = []) : void
{
cd($this->sourceDir. dirname($sourcePath));
$cmd = $this->encoder . ' -r ';
if(!empty($excludes)) {
$cmd .= "--exclude " . implode("--exclude ", array_map("escapeshellarg", $excludes));
}
$cmd .= ' -f "*.php" -o ' . $this->buildDir . "/" . $this->packageName . $targetPath . " ".basename($sourcePath)."/*";
echo "Running: " . $cmd . "\n";
run($cmd);
}
private function encode()
{
foreach ($this->proModules as $module) {
$this->runEncoder('/promodules/' . $module, '/modules', ["vendor/*"]);
}
$this->runEncoder('/promodules/tickets/model', '/modules/tickets/');
$this->runEncoder('/promodules/tickets/customfields/model', '/modules/tickets/customfields/');
$this->runEncoder('/promodules/tickets/customfields/model', '/modules/tickets/customfields/');
foreach ($this->proModules as $module) {
// keep lang and module install stuff open source
if (is_dir($this->sourceDir . '/promodules/' . $module . '/language')) {
run('cp ' . $this->sourceDir . '/promodules/' . $module . '/language/* ' . $this->buildDir . "/" . $this->packageName . '/modules/' . $module . '/language/');
}
if (is_dir($this->sourceDir . '/promodules/' . $module . '/install')) {
run('cp -r ' . $this->sourceDir . '/promodules/' . $module . '/install/* ' . $this->buildDir . "/" . $this->packageName . '/modules/' . $module . '/install/');
}
if (file_exists($this->sourceDir . '/promodules/' . $module . '/' . ucfirst($module) . 'Module.php')) {
run('cp ' . $this->sourceDir . '/promodules/' . $module . '/' . ucfirst($module) . 'Module.php ' . $this->buildDir . "/" . $this->packageName . '/modules/' . $module . '/');
}
}
run('cp ' . $this->sourceDir . '/promodules/professional/Module.php ' . $this->buildDir . "/" . $this->packageName . '/modules/professional');
run('cp ' . $this->sourceDir . '/promodules/projects2/report/* ' . $this->buildDir . "/" . $this->packageName . '/modules/projects2/report/');
run('cp ' . $this->sourceDir . '/promodules/billing/Pdf.php ' . $this->buildDir . "/" . $this->packageName . '/modules/billing/Pdf.php');
//business package
$this->runEncoder('/business', '/go/modules', ["*/vendor/*"]);
$businessDir = new DirectoryIterator($this->sourceDir . '/business');
foreach ($businessDir as $fileinfo) {
if ($fileinfo->isDot() || !$fileinfo->isDir() || $fileinfo->getBasename() == '.git') {
continue;
}
$moduleName = $fileinfo->getBasename();
echo "Building business/" . $moduleName . "\n";
$moduleFile = $this->sourceDir . '/business/' . $moduleName . '/Module.php';
if (file_exists($moduleFile) && strpos(file_get_contents($moduleFile), "requiredLicense") === false) {
throw new Exception($moduleFile . " must contain a 'requiredLicense()' function override.");
}
if (is_dir($this->sourceDir . '/business/' . $moduleName . '/language')) {
run('cp ' . $this->sourceDir . '/business/' . $moduleName . '/language/* ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/' . $moduleName . '/language/');
}
if (is_dir($this->sourceDir . '/business/' . $moduleName . '/install')) {
run('cp -r ' . $this->sourceDir . '/business/' . $moduleName . '/install/* ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/' . $moduleName . '/install/');
}
if (file_exists($moduleFile)) {
run('cp ' . $moduleFile . ' ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/' . $moduleName . '/');
}
}
run('rm -rf ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/.git*');
run('rm -rf ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/kanban');
run('rm -rf ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/projects3');
//needs to be open source as it's used by Module files
run('cp ' . $this->sourceDir . '/business/finance/model/PaymentProviderInterface.php ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/finance/model/PaymentProviderInterface.php');
run('cp ' . $this->sourceDir . '/business/finance/model/RecurringPaymentProviderInterface.php ' . $this->buildDir . "/" . $this->packageName . '/go/modules/business/finance/model/RecurringPaymentProviderInterface.php');
}
private function sendTarToSF()
{
echo "Creating SourceForge.net release....\n\n";
cd($this->buildDir);
run("scp " . $this->packageName . ".tar.gz [email protected]:/home/frs/project/group-office/$this->majorVersion/");
}
private function createGithubRelease()
{
echo "Creating GitHub release....\n\n";
cd($this->buildDir);
$client = new Client();
$client->authenticate($this->github['PERSONAL_ACCESS_TOKEN'], null, Client::AUTH_ACCESS_TOKEN);
$tagName = 'v' . $this->majorVersion . "." . $this->minorVersion;
$r = $client->api('repo')->releases();
if (!isset($this->githubRelease)) {
$this->githubRelease = $r->create($this->github['USERNAME'], $this->github['REPOSITORY'], array('tag_name' => $tagName, 'name' => $tagName, 'prerelease' => $this->distro == "testing", 'target_commitish' => $this->gitBranch, 'body' => 'Use the ' . $this->packageName . '.tar.gz file for installations. It contains all the code, libraries and compiled code. For installation instructions read: https://groupoffice.readthedocs.io/en/latest/install/install.html'));
}
$asset = $r->assets()->create($this->github['USERNAME'], $this->github['REPOSITORY'], $this->githubRelease['id'], $this->packageName . '.tar.gz', 'application/tar+gzip', file_get_contents($this->packageName . '.tar.gz'));
}
private function buildDebianPackage()
{
$tpl = '{package} ({version}-' . $this->distro . ') ' . $this->distro . '; urgency=medium
* Changes can be found in /usr/share/groupoffice/changelog.md
-- Intermesh BV (Developer key) <[email protected]> {date}';
//Mon, 26 May 2010 12:30:00 +0200
$date = date('D, j M Y H:i:s O');
$debTarget = $this->buildDir . "/debian";
run("rm -rf " . $debTarget);
run("mkdir " . $debTarget);
run("cp -r " . __DIR__ . "/debian/* " . $debTarget);
file_put_contents($debTarget . '/debian/changelog', str_replace(array('{package}', '{version}', '{date}'), array("groupoffice", $this->majorVersion . '.' . $this->minorVersion, $date), $tpl));
run("cp -r " . $this->buildDir . "/" . $this->packageName . "/* " . $debTarget . "/usr/share/groupoffice");
cd($debTarget);
run("debuild --no-lintian -b");
}
public function addToDebianRepository()
{
if (!is_dir($this->repreproDir)) {
run("mkdir -p " . $this->repreproDir . "/conf");
run("cp " . $this->sourceDir . "/debian-groupoffice/reprepro/distributions " . $this->repreproDir . "/conf");
}
run("reprepro -b " . $this->repreproDir . " include " . $this->distro . " " . $this->buildDir . "/groupoffice_" . $this->majorVersion . "." . $this->minorVersion . "-" . $this->distro . "_amd64.changes");
}
}
$builder = new Builder($config);
if (isset($argv[1]) && $argv[1] == "test") {
$builder->test = true;
}
$builder->build();