/
compile.php
101 lines (77 loc) · 2.66 KB
/
compile.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace ngyuki\Compiler;
use Symfony\Component\Finder\Finder;
require __DIR__ . '/../vendor/autoload.php';
error_reporting(-1);
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});
chdir(dirname(__DIR__));
echo "composer into build/vendor\n";
exec('COMPOSER_VENDOR_DIR=build/vendor composer install --no-dev');
$version = trim(`git describe --tags --always`);
if (strlen($version) === 0) {
echo "Unable detect version.\n\n\t" . "git describe --tags --always\n";
exit(1);
}
$phar = 'db-migrate.phar';
$stub = 'bin/compile.stub';
$finders = array();
$finders[] = $finder = new Finder();
$finder->files()->in('src');
$finders[] = $finder = new Finder();
$finder->files()->in('build/vendor')
->notName('*.md')
->notName('composer.json')
->notName('phpunit.xml.dist')
->exclude('Tests')
;
if (file_exists($phar)) {
unlink($phar);
}
$stripWhitespace = function ($source) {
$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= str_repeat("\n", substr_count($token[1], "\n"));
} elseif (T_WHITESPACE === $token[0]) {
// reduce wide spaces
$whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
// normalize newlines to \n
$whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
// trim leading spaces
$whitespace = preg_replace('{\n +}', "\n", $whitespace);
$output .= $whitespace;
} else {
$output .= $token[1];
}
}
return $output;
};
$pharObj = new \Phar($phar, 0);
$pharObj->setSignatureAlgorithm(\Phar::SHA1);
$pharObj->addFromString('version', $version);
$pharObj->setMetadata(array('version' => $version));
$pharObj->startBuffering();
foreach ($finders as $finder) {
/** @var $file \SplFileInfo */
foreach ($finder as $file) {
if (pathinfo($file->getPathname(), PATHINFO_EXTENSION) === 'php') {
$source = file_get_contents($file->getPathname());
$source = $stripWhitespace($source);
$pharObj->addFromString($file->getPathname(), $source);
echo "* " . $file->getPathname() . "\n";
} else {
$pharObj->addFile($file->getPathname());
echo " " . $file->getPathname() . "\n";
}
}
}
$pharObj->setStub(file_get_contents($stub));
$pharObj->stopBuffering();
unset($pharObj);
chmod($phar, 0777);
$size = filesize($phar);
echo "\nCompiled $phar ... $size Byte\n";