]> BookStack Code Mirror - system-cli/blob - compile.php
Bumped version
[system-cli] / compile.php
1 <?php declare(strict_types=1);
2
3 /**
4  * This file builds a phar archive to contain our CLI code.
5  * Attribution to https://blog.programster.org/creating-phar-files
6  * for the code in this file.
7  */
8
9 try {
10     $pharFile = 'app.phar';
11
12     // clean up
13     if (file_exists($pharFile)) {
14         unlink($pharFile);
15     }
16
17     if (file_exists($pharFile . '.gz')) {
18         unlink($pharFile . '.gz');
19     }
20
21     if (is_dir(__DIR__ . '/vendor/phpunit')) {
22         throw new Exception("You should only compile when dev dependencies are NOT installed");
23     }
24
25     // create phar
26     $phar = new Phar($pharFile);
27
28     // start buffering. Mandatory to modify stub to add shebang
29     $phar->startBuffering();
30
31     // Create the default stub from main.php entrypoint
32     $defaultStub = $phar->createDefaultStub('run.php');
33
34     // Add the rest of the apps files
35     $escapedDir = preg_quote(__DIR__, '/');
36     $phar->addFile(__DIR__ . '/run.php', 'run.php');
37     $phar->buildFromDirectory(__DIR__, "/{$escapedDir}\\/src(.*)\.php$/");
38     $phar->buildFromDirectory(__DIR__, "/{$escapedDir}\\/vendor(.*)\.php$/");
39
40     // Customize the stub to add the shebang
41     $stub = "#!/usr/bin/env php \n" . $defaultStub;
42
43     // Add the stub
44     $phar->setStub($stub);
45
46     $phar->stopBuffering();
47
48     // plus - compressing it into gzip
49     $phar->compressFiles(Phar::GZ);
50
51     # Make the file executable
52     chmod(__DIR__ . "/{$pharFile}", 0775);
53     if (filesize(__DIR__ . "/{$pharFile}") > 500000) {
54         throw new Exception("Phar size unusually large. Check extra files are not included by mistake.");
55     }
56
57     echo "$pharFile successfully created" . PHP_EOL;
58 } catch (Exception $e) {
59     echo "ERROR: " . $e->getMessage() . "\n";
60 }