]> BookStack Code Mirror - system-cli/blob - scripts/compile.php
Outlined a general working backup approach
[system-cli] / scripts / compile.php
1 <?php
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     // create phar
22     $phar = new Phar($pharFile);
23
24     // start buffering. Mandatory to modify stub to add shebang
25     $phar->startBuffering();
26
27     // Create the default stub from main.php entrypoint
28     $defaultStub = $phar->createDefaultStub('run');
29
30     // Add the rest of the apps files
31     $phar->addFile(__DIR__ . '/run', 'run');
32     $phar->buildFromDirectory(__DIR__, '/vendor(.*)/');
33     $phar->buildFromDirectory(__DIR__, '/Commands(.*)/');
34
35     // Customize the stub to add the shebang
36     $stub = "#!/usr/bin/env php \n" . $defaultStub;
37
38     // Add the stub
39     $phar->setStub($stub);
40
41     $phar->stopBuffering();
42
43     // plus - compressing it into gzip
44     $phar->compressFiles(Phar::GZ);
45
46     # Make the file executable
47     chmod(__DIR__ . "/{$pharFile}", 0770);
48
49     echo "$pharFile successfully created" . PHP_EOL;
50 } catch (Exception $e) {
51     echo $e->getMessage();
52 }