-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildPhar.php
182 lines (154 loc) · 5.2 KB
/
buildPhar.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Created by PhpStorm.
* User: Don
* Date: 4/20/2017
* Time: 12:44 AM
*/
class PharBuilder
{
var $pharfile; // File name of the PHAR.
var $startfile; // File to start the program
var $basedir; // Base directory of the source tree.
var $phar; // Phar object
var $excludePrefixes = array(); // File prefixes to exclude from PHAR
var $excludeSuffixes = array(); // File suffixes to exclude from PHAR
var $squash = 'stripWhitespace';
function __construct($filename, $startup, $basedir)
{
$this->pharfile = $filename;
$this->startfile = $startup;
$this->basedir = $basedir;
}
public function addExcludePrefix($prefix)
{
array_push($this->excludePrefixes, $prefix);
}
public function addExcludeSuffix($suffix)
{
array_push($this->excludeSuffixes, $suffix);
}
public function compile()
{
@unlink($this->pharfile);
$this->phar = new \Phar($this->pharfile);
$fileIter = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->basedir, \FileSystemIterator::SKIP_DOTS));
$files = iterator_to_array($fileIter);
foreach ($this->excludePrefixes as $prefix) {
$files = $this->prunePrefix($files, $this->basedir . $prefix);
}
foreach ($this->excludeSuffixes as $suffix) {
$files = $this->pruneSuffix($files, $suffix);
}
$this->phar->startBuffering();
foreach ($files as $file) {
$name = substr($file, strlen($this->basedir) + 1);
print_r("Adding $name ...");
if (substr($file, -3) === 'php') {
$this->phar[$name] = $this->{$this->squash}(file_get_contents($file));
} else {
$this->phar[$name] = file_get_contents($file);
}
}
$this->addGITinfo();
$this->addStub();
$this->phar->stopBuffering();
$this->phar->compressFiles(Phar::GZ);
$this->setExecutableBit();
}
public function prunePrefix($files, $prefix)
{
$newlist = array();
$prefix_len = strlen($prefix);
foreach ($files as $file => $data) {
if (substr($file, 0, $prefix_len) !== $prefix) {
$newlist[$file] = $data;
}
}
return $newlist;
}
// This is from the composer source. Handy!
// Se mantiene el código fuente de los números de línea.
public function pruneSuffix($files, $suffix)
{
$newlist = array();
$suffix_len = strlen($suffix);
foreach ($files as $file => $data) {
if (substr($file, -$suffix_len) !== $suffix) {
$newlist[$file] = $data;
}
}
return $newlist;
}
private function addGITinfo()
{
$this->phar['compiled.php'] = "<?php return array( 'date' => '"
. date('Y-m-d H:i')
. "', 'git' => '"
. exec("git -C {$this->basedir} describe --tags 2>&1") . "',);";
}
private function addStub()
{
$defaultStub = $this->phar->createDefaultStub($this->startfile);
$stub = "#!/usr/bin/php -q\n" . $defaultStub;
$this->phar->setStub($stub);
}
private function setExecutableBit()
{
system("chmod +x {$this->pharfile}");
}
private function stripWhitespace($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))) {
// Replace comments with the just the newlines, to
// keep the source line numbers the same.
$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;
}
private function stripWhitespaceAgressive($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))) {
// No comments at all
} elseif (T_WHITESPACE === $token[0]) {
$output .= ' ';
} else {
$output .= $token[1];
}
}
return $output;
}
private function nostrip($source)
{
return $source;
}
}
;
$pb = new PharBuilder('vsk.phar', 'sendtovm.php', './src');
$pb->addExcludePrefix('/.idea');
$pb->addExcludePrefix('/.git');
$pb->addExcludePrefix('/composer');
$pb->addExcludePrefix('/build.php');
$pb->addExcludeSuffix('~');
$pb->compile();
rename('vsk.phar', './bin/vsk');