]> BookStack Code Mirror - system-cli/blob - compile.php
Removed old imports
[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     $phar->addFile(__DIR__ . '/run.php', 'run.php');
36     $phar->buildFromDirectory(__DIR__, '/src(.*)/');
37     $phar->buildFromDirectory(__DIR__, '/vendor(.*)\.php$/');
38
39     // Customize the stub to add the shebang
40     $stub = "#!/usr/bin/env php \n" . $defaultStub;
41
42     // Add the stub
43     $phar->setStub($stub);
44
45     $phar->stopBuffering();
46
47     // plus - compressing it into gzip
48     $phar->compressFiles(Phar::GZ);
49
50     # Make the file executable
51     chmod(__DIR__ . "/{$pharFile}", 0775);
52
53     echo "$pharFile successfully created" . PHP_EOL;
54 } catch (Exception $e) {
55     echo "ERROR: " . $e->getMessage() . "\n";
56 }