forked from pilif/sacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.php
215 lines (177 loc) · 6.59 KB
/
build.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env php
<?php
$args = getopt("z:o:cjh", array('with-phamlp::', 'with-lessphp::', 'with-coffeescript-php::'));
if ($args){
$pruneargv = array();
foreach ($args as $o => $v) {
foreach ($_SERVER['argv'] as $key => $chunk) {
$regex = '/^'. (isset($o[1]) ? '--' : '-') . $o . '/';
if ($chunk == $v && $_SERVER['argv'][$key-1][0] == '-' || preg_match($regex, $chunk)) {
array_push($pruneargv, $key);
}
}
}
while ($key = array_pop($pruneargv))
unset($_SERVER['argv'][$key]);
$_SERVER['argv'] = array_values($_SERVER['argv']);
}
if (!$args || $args['h']){
fwrite(STDERR, "usage: build.php [-o <output dir name>] [-c] [-j] [-z [g|b]] [--with-PACKAGE=<path>]
-c: Include CSSMin into bundle
-j: Include JSMin into bundle
-o: Write output to <output dir name> instead of build/
-z: Compress the contents of the bundle with bzip2 (b) or gzip (g)
Packages are optional additional packages to include support for. If you
don't provide them, sacy will try using existing classes at runtime or
leave corresponding tags alone
--with-coffeescript-php= path to unpacked source of coffeescript-php
--with-phamlp= path to unpacked source bundle of phamlp for
text/sass and text/scss support
--with-lessphp= path to unpacked source bundle of lessphp for
text/less support\n");
die(1);
}
$comp = Phar::NONE;
if (isset($args['z'])){
switch($args['z']){
case 'g':
$comp = Phar::GZ;
break;
case 'b':
$comp = Phar::BZ2;
break;
default:
fwrite(STDERR, "Invalid compression method\n");
}
}
$skipfiles = array();
if (!isset($args['c'])) $skipfiles['cssmin.php'] = true;
if (!isset($args['j'])) $skipfiles['jsmin.php'] = true;
$skipfiles['block.asset_compile.php'] = true; // this will be used as phar stub
$srcdir = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'src'));
$outfile = 'block.asset_compile.php';
$outdir = isset($args['o']) ? $args['o'] : implode(DIRECTORY_SEPARATOR, array(__DIR__, 'build'));
$target = implode(DIRECTORY_SEPARATOR, array($outdir, 'temp.phar'));
if (file_exists($target)) unlink($target);
$arch = new ContentHashedPhar($target, 0, 'sacy.phar');
$arch->startBuffering();
$arch->buildFromIterator(new SacySupportFilesFilter(
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcdir), RecursiveIteratorIterator::SELF_FIRST),
$skipfiles
), $srcdir);
if ($args['with-coffeescript-php']){
$dir = $args['with-coffeescript-php'];
if (!is_dir($dir)){
fwrite(STDERR, "--with-coffeescript-php specified, but coffeescript not found there.\n");
exit(1);
}
$d = preg_quote(implode(DIRECTORY_SEPARATOR, array($dir, 'coffeescript')), '#');
$i = new SacyRegexWhitelistFilter(new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::SELF_FIRST
), "#^$d#");
$arch->buildFromIterator($i, $dir);
}
if ($args['with-phamlp']){
$p = sprintf('patch -N -d %s -p1 -i %s',
escapeshellarg($args['with-phamlp']),
escapeshellarg(implode(DIRECTORY_SEPARATOR, array(__DIR__, 'sass-fix-import.patch')))
);
exec($p);
$arch->buildFromIterator(new RecursiveIteratorIterator(
new SacySkipSubdirsFilter(
new RecursiveDirectoryIterator($args['with-phamlp']),
array('haml')
),
RecursiveIteratorIterator::SELF_FIRST
), $args['with-phamlp']);
}
if ($args['with-lessphp']){
$arch['sacy/lessc.inc.php'] = file_get_contents(
implode(DIRECTORY_SEPARATOR, array($args['with-lessphp'], 'lessc.inc.php'))
);
}
$arch->stopBuffering();
$opcache_bug_workaround_name = sha1($arch->getContentHash().file_get_contents(__FILE__));
if ($comp != Phar::NONE)
$arch->compressFiles($comp);
$stub ="<?php Phar::interceptFileFuncs();
define('____SACY_BUNDLED', 1);
Phar::mapPhar('$opcache_bug_workaround_name.phar');
include('phar://$opcache_bug_workaround_name.phar/sacy/ext-translators.php');
include('phar://$opcache_bug_workaround_name.phar/sacy/fragment-cache.php');
include('phar://$opcache_bug_workaround_name.phar/sacy/phpsass.php');
include('phar://$opcache_bug_workaround_name.phar/sacy/sacy.php');
".
de_phptag(file_get_contents($srcdir.DIRECTORY_SEPARATOR.'block.asset_compile.php')).
"\n__HALT_COMPILER();";
$arch->setStub($stub);
$arch = null;
rename($target, implode(DIRECTORY_SEPARATOR, array($outdir, $outfile)));
die(0);
function de_phptag($str){
return preg_replace('#<\?(php)?|\?>#', '', $str);
}
class ContentHashedPhar extends Phar {
private $hash;
function __construct($fname, $flags){
parent::__construct($fname, $flags);
$this->hash = null;
}
function buildFromIterator(Iterator $it, $base_dir=null){
foreach($it as $f){
$this->addFileToHash($f);
}
parent::buildFromIterator($it, $base_dir);
}
function offsetSet($k, $v){
$this->addFileToHash($v);
parent::offsetSet($k, $v);
}
function getContentHash(){
return $this->hash;
}
function addFile($file, $localname=null){
$this->addFile($file);
parent::addFile($file, $localname);
}
private function addFileToHash($f){
$this->hash = sha1($this->hash . file_get_contents($f));
}
}
class SacySkipSubdirsFilter extends RecursiveFilterIterator{
private $skipdirs;
function __construct(RecursiveIterator $it, $skipdirs=null){
parent::__construct($it);
$this->skipdirs = $skipdirs ?: array();
}
public function accept() {
return !in_array($this->current()->getFilename(), $this->skipdirs);
}
}
class SacyRegexWhitelistFilter extends FilterIterator{
private $wl = '#.#';
function __construct(Iterator $it, $wl = null){
parent::__construct($it);
$this->wl = $wl ?: '#.#';
}
function accept(){
return preg_match($this->wl, $this->current()->getPathName());
}
}
class SacySupportFilesFilter extends FilterIterator{
private $skipfiles;
function __construct(Iterator $it, $skipfiles=null){
parent::__construct($it);
$this->skipfiles = $skipfiles ?: array();
}
public function accept(){
if ($this->current()->isDir())
return false;
if (!preg_match('#\.(php)$#', $this->current()->getFilename()))
return false;
if ($this->skipfiles[$this->current()->getFilename()])
return false;
return true;
}
}