-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-phar.php
64 lines (49 loc) · 1.78 KB
/
create-phar.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
<?php
declare(strict_types=1);
// remove development dependencies
shell_exec('composer install --no-dev --optimize-autoloader');
// the php.ini setting phar.readonly must be set to 0ff
$pharFile = 'bumpversion.phar';
// clean up
if (file_exists($pharFile)) {
unlink($pharFile);
}
// create phar
$phar = new Phar($pharFile);
// start buffering. Mandatory to modify stub to add shebang
$phar->startBuffering();
// create the default stub from main.php entrypoint
$defaultStub = $phar::createDefaultStub('/bin/bumpversion.php');
// creating our library using whole directory
$gitignore = array_filter(array_map(static function (string $line): ?string {
return trim(preg_replace('/(#.*)$/', '', $line));
}, file('.gitignore')));
$gitignore[] = basename(__FILE__);
$gitignore[] = '.idea';
$gitignore[] = 'tests';
$gitignore[] = 'phpunit.xml.dist';
$gitignore[] = '!composer.json';
$phar->buildFromDirectory('.', sprintf(
'/^\.\/((?!%s)|%s).*$/',
implode('|', array_map(static function (string $file): string {
return str_replace('\\*', '.*', preg_quote($file, '/'));
}, array_filter($gitignore, static function (string $file): bool {
return 0 !== strpos($file, '!');
}))),
implode('|', array_map(static function (string $file): string {
return str_replace('\\*', '.*', preg_quote(substr($file, 1), '/'));
}, array_filter($gitignore, static function (string $file): bool {
return 0 === strpos($file, '!');
})))
));
// customize the stub to add the shebang
$stub = "#!/usr/bin/php\n$defaultStub";
// add the stub
$phar->setStub($stub);
// stop buffering
$phar->stopBuffering();
// plus - compressing it into gzip
$phar->compressFiles(Phar::GZ);
// make it executable
shell_exec("chmod +x $pharFile");
echo "$pharFile successfully created" . PHP_EOL;