-
Notifications
You must be signed in to change notification settings - Fork 4
/
build
executable file
·383 lines (347 loc) · 12 KB
/
build
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
#!/usr/bin/env php
<?php
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
require __DIR__ . '/vendor/autoload.php';
/**
* @param int $size
* @param int $precision
* @return string
*/
function formatBytes($size, $precision = 4)
{
$base = log($size, 1024);
$suffixes = ['', 'K', 'M', 'G', 'T'];
return round(1024 ** ($base - floor($base)), $precision) . ' ' . $suffixes[(int)floor($base)];
}
/**
* @param string $in infile
* @param string $out outfile
* @param int|null $mod chmod settings
* @return bool
*/
function cp(ConsoleOutput $output, $in, $out, $mod = null)
{
$output->writeln(sprintf(' Copying %s to %s', $in, $out));
$result = copy($in, $out);
if (!$result) {
$output->getErrorOutput()->writeln(sprintf(' <error>Something bad happened...</error>'));
}
if ($result && $mod) {
chmod($out, $mod);
}
return $result;
}
function init(ConsoleOutput $output)
{
$configFile = __DIR__ . DIRECTORY_SEPARATOR . 'build.json';
if (!is_file($configFile)) {
$output->getErrorOutput()->writeln(sprintf(' <error>Cannot find configuration file build.json</error>'));
exit(255);
}
$config = json_decode(file_get_contents($configFile), true);
if (empty($config['repository'])) {
$output->getErrorOutput()->writeln(sprintf(' <error>Please define "repository" in your build.json</error>'));
exit(255);
}
if (empty($config['pharName'])) {
$output->getErrorOutput()->writeln(sprintf(' <error>Please define "pharName" in your build.json</error>'));
exit(255);
}
define('BUILD_START_TIME', microtime(true));
define('REPO_URL', $config['repository']);
define('BUILD_COMMIT', isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'master');
define('PHAR_NAME', $config['pharName']);
}
/**
* @param ConsoleOutput $output
* @param array $tools
*/
function checkToolsInstalled(ConsoleOutput $output)
{
$tools = [
'git' => [
'version' => '2.5',
'url' => 'https://git-scm.com/downloads',
],
'box' => [
'version' => '4.1',
'url' => 'https://github.com/humbug/box/blob/master/doc/installation.md#installation',
'alter_path' => '/home/runner/work/jira/jira/box',
'legend' => 'box is a tool used for building PHAR files',
],
'composer' => [
'version' => '1.5',
'url' => 'https://getcomposer.org/download/',
],
];
foreach ($tools as $tool => $hints) {
if (isset($hints['alter_path']) && is_file($hints['alter_path']) && !is_executable($hints['alter_path'])) {
passthru($hints['alter_path'] . ' --version');
$output->getErrorOutput()->writeln(
"<error>Please install $tool and make executable in {$hints['alter_path']} to continue</error>"
);
if (isset($hints['legend'])) {
$output->writeln(sprintf('<info>hint:</info> %s', $hints['legend']));
}
if (isset($hints['url'])) {
$output->writeln(sprintf('Please visit %s for more info', $hints['url']));
}
exit(1);
} else {
if (!isset($hints['alter_path']) && !trim(`which $tool`)) {
$output->getErrorOutput()->writeln(
"<error>Please install $tool and make available in \$PATH to continue</error>"
);
if (isset($hints['legend'])) {
$output->writeln(sprintf('<info>hint:</info> %s', $hints['legend']));
}
if (isset($hints['url'])) {
$output->writeln(sprintf('Please visit %s for more info', $hints['url']));
}
exit(1);
}
}
$command = isset($hints['alter_path']) && is_executable($hints['alter_path']) ? $hints['alter_path'] : $tool;
list(, , $version) = explode(' ', `$command --version`);
if (!version_compare($version, $hints['version'], '>=')) {
$output->getErrorOutput()->writeln(
sprintf(
"<error>Please make sure %s is at least version %s (current version is %s)</error>",
$tool,
$hints['version'],
$version
)
);
if (isset($hints['legend'])) {
$output->writeln(sprintf('<info>hint:</info> %s', $hints['legend']));
}
if (isset($hints['url'])) {
$output->writeln(sprintf('Please visit %s for more info', $hints['url']));
}
exit(1);
}
}
}
function box($commands, &$returnVar = null)
{
static $box = null;
if (is_null($box)) {
$box = is_executable('/home/runner/work/jira/jira/box') ? '/home/runner/work/jira/jira/box' : 'box';
}
passthru("$box $commands", $returnVar);
}
/**
* @param ConsoleOutput $output
* @return string
*/
function createBuildDirectory(ConsoleOutput $output)
{
$buildDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . PHAR_NAME . '-' . BUILD_COMMIT . '-' . uniqid('jrbld', true);
$output->writeln('<info>Creating build directory</info>');
$output->writeln($buildDir);
if (!mkdir($buildDir, 0733, true) && !is_dir($buildDir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $buildDir));
}
if (!is_dir($buildDir)) {
$output->getErrorOutput()->writeln('<error>Cannot create build directory!</error>');
exit(2);
}
chdir($buildDir);
return $buildDir;
}
/**
* @param ConsoleOutput $output
* @param $buildDir
*/
function cloneSingleCommitIntoBuildDir(ConsoleOutput $output, $buildDir)
{
$output->writeln('<info>Initialising git repo for build</info>');
chdir($buildDir);
passthru(sprintf('git init && git remote add origin %s && git fetch origin', REPO_URL), $result);
if ($result !== 0) {
$output->getErrorOutput()->writeln(
'<error>Cannot perform git init in build directory</error>'
);
exit(3);
}
passthru(
sprintf('git checkout %s', BUILD_COMMIT),
$result
);
if (!is_dir($buildDir . DIRECTORY_SEPARATOR . '.git') || $result !== 0) {
$output->getErrorOutput()->writeln('<error>Git init failed!</error>');
exit(3);
}
}
function composerInstall(ConsoleOutput $output, $buildDir)
{
chdir($buildDir);
$output->writeln('<info>Running composer install...</info>');
exec('composer install --no-dev -o');
}
/**
* @param ConsoleOutput $output
* @param string $buildDir
*/
function generateBoxJson(ConsoleOutput $output, $buildDir)
{
$output->writeln('<info>Generating box config</info>');
$configFile = __DIR__ . DIRECTORY_SEPARATOR . 'build.json';
$config = json_decode(file_get_contents($configFile), true);
$boxFile = $buildDir . DIRECTORY_SEPARATOR . 'box.json';
$boxConfig = [
'directories' => ['src'],
'files' => [
'vendor/symfony/dependency-injection/Loader/schema/dic/services/services-1.0.xsd',
],
'output' => PHAR_NAME . '.phar',
'finder' => [
[
'name' => '*.*',
'exclude' => ['tests', 'spec', 'features', 'example', 'doc', 'docs', '*.md', '*.rst', '*.txt'],
'in' => 'vendor',
],
],
'compactors' => [
'KevinGH\Box\Compactor\Php',
],
'git-version' => 'git-version',
];
$generatedConfig = array_merge_recursive($boxConfig, isset($config['box']) ? $config['box'] : []);
$output->writeln(json_encode($generatedConfig));
file_put_contents($boxFile, json_encode($generatedConfig));
}
/**
* @param ConsoleOutput $output
* @param string $buildDir
* @return string
*/
function buildPhar(ConsoleOutput $output, $buildDir)
{
$buildfile = $buildDir . DIRECTORY_SEPARATOR . PHAR_NAME . '.phar';
$output->writeln('<info>Building phar...</info>');
box('validate');
box('compile', $compileResult);
if (!is_file($buildfile) || $compileResult !== 0) {
$output->getErrorOutput()->writeln('<error>Error while building phar!</error>');
exit(4);
} else {
$output->writeln('<info>Done!</info>');
}
return $buildfile;
}
/**
* @param ConsoleOutput $output
* @param string $buildFile
* @return string
*/
function copyPharToDestinations(ConsoleOutput $output, $buildFile)
{
$output->writeln('<info>Copying files</info>');
$releaseFile = __DIR__
. DIRECTORY_SEPARATOR . 'builds'
. DIRECTORY_SEPARATOR . PHAR_NAME . '-' . BUILD_COMMIT . '-' . md5_file($buildFile) . '.phar';
$localBuildFile = __DIR__
. DIRECTORY_SEPARATOR . PHAR_NAME . '.phar';
if (!mkdir(dirname($releaseFile), 0733, true) && !is_dir(dirname($releaseFile))) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', dirname($releaseFile)));
}
if (cp($output, $buildFile, $releaseFile, 0755) !== true || cp(
$output,
$buildFile,
$localBuildFile,
0755
) !== true) {
$output->getErrorOutput()->writeln(
"<error>Unable to copy files into destination</error>"
);
exit(5);
}
return $releaseFile;
}
/**
* @param ConsoleOutput $output
* @param string $pharFile
*/
function testPhar(ConsoleOutput $output, $pharFile)
{
$output->writeln('<info>Testing phar...</info>');
chdir(dirname($pharFile));
box(sprintf('verify %s', $pharFile), $verifyResult);
if ($verifyResult !== 0) {
$output->getErrorOutput()->writeln('<error>Box verify failed!!</error>');
exit(6);
}
$output->writeln('<info>Checking if phar version works</info>');
passthru(sprintf('./%s --version', pathinfo($pharFile, PATHINFO_BASENAME)), $result);
if ($result !== 0) {
$output->getErrorOutput()->writeln('<error>Error in PHAR file, please check the output above</error>');
exit(6);
}
}
/**
* @return void
*/
function main()
{
$input = new ArgvInput(
null,
new InputDefinition(
[
new InputArgument('version', InputArgument::OPTIONAL, 'git tag to build'),
new InputOption('workflow', 'w', InputOption::VALUE_NONE, 'run in github workflow mode'),
new InputOption('help', 'h', InputOption::VALUE_NONE, 'show help'),
]
)
);
$output = new ConsoleOutput;
if ($input->getOption('help')) {
$output->writeln(
[
'Usage:',
__FILE__ . ' [options]',
'',
'Options:',
'--workflow|-w: run in github workflow mode',
'--help|-h: show this help'
]
);
exit(0);
}
init($output);
checkToolsInstalled($output);
if ($input->getOption('workflow')) {
$buildDir = getcwd();
generateBoxJson($output, $buildDir);
$buildFile = buildPhar($output, $buildDir);
testPhar($output, $buildFile);
$releaseFile = $buildFile;
} else {
$buildDir = createBuildDirectory($output);
cloneSingleCommitIntoBuildDir($output, $buildDir);
composerInstall($output, $buildDir);
generateBoxJson($output, $buildDir);
$buildFile = buildPhar($output, $buildDir);
testPhar($output, $buildFile);
$releaseFile = copyPharToDestinations($output, $buildFile);
}
$output->writeln(
[
"<info>🍺 Build done</info>",
'',
sprintf(
'Built %s (size %s) in %.4f seconds, consumed %s memory',
$releaseFile,
formatBytes(filesize($releaseFile), 2),
microtime(true) - BUILD_START_TIME,
formatBytes(memory_get_peak_usage(true), 2)
),
]
);
exit(0);
}
main();