summaryrefslogtreecommitdiffstats
path: root/bin/build
blob: 67b7d7028e2117921957dc5d47e2800599a7c7c3 (plain)
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
#!/usr/bin/env php
<?php

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

require_once(__DIR__ . '/../vendor/autoload.php');

$rootDir = __DIR__ . '/..';

$filename = 'php-gameboy.phar';
$filePath = $rootDir . '/bin/' . $filename;
$stub = <<<STUB
#!/usr/bin/env php
<?php

Phar::mapPhar("php-gameboy.phar");

require_once("phar://php-gameboy.phar/php-terminal-gameboy-emulator/boot.php");

__HALT_COMPILER();

STUB;

if (file_exists($filePath)) {
    unlink($filePath);
}

$finderSort = function ($a, $b) {
    return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/'));
};

function addFile($phar, $file)
{
    $path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/');

    $content = file_get_contents($file);

    $phar->addFromString($path, $content);
}

$phar = new Phar(
    $filePath,
    FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME,
    $filename
);
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->startBuffering();

addFile($phar, new SplFileInfo('boot.php', $rootDir . '/boot.php', 'boot.php'));
addFile($phar, new SplFileInfo('composer.json', $rootDir . '/composer.json', 'composer.json'));
addFile($phar, new SplFileInfo('vendor/autoload.php', $rootDir . '/vendor/autoload.php', 'vendor/autoload.php'));

$finder = new Finder();
$finder->files()
    ->ignoreVCS(true)
    ->name('*.php')
    ->in([
        $rootDir . '/src/',
        $rootDir . '/vendor/composer',
        $rootDir . '/vendor/whatthejeff',
    ])
    ->sort($finderSort)
;

foreach ($finder as $file) {
    addFile($phar, $file);
}

$phar->setStub($stub);

$phar->stopBuffering();

chmod($filePath, 0775);