forked from phoboslab/Impact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbake.php
157 lines (126 loc) · 3.55 KB
/
bake.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
<?php
if( count($argv) < 3 ) {
echo "Usage: bake.php <in...> <out>\n";
echo "e.g. bake.php lib/impact/impact.js lib/game/game.js mygame-baked.js\n";
die;
}
$inFiles = array_slice( $argv, 1, -1 );
$outFile = $argv[ count($argv)-1 ];
$baker = new Baker( Baker::MINIFIED );
$baker->bake( $inFiles, $outFile );
class Baker {
const PLAIN = 0;
const MINIFIED = 1;
const GZIPPED = 2;
protected $base = 'lib/';
protected $format = 0;
protected $loaded = array();
protected $currentInput = 'Command Line';
protected $fileCount = 0, $bytesIn = 0, $bytesOut = 0;
public function __construct( $format = 0 ) {
$this->format = $format;
if( $this->format & self::MINIFIED ) {
require_once( 'jsmin.php' );
}
}
public function bake( $inFiles, $outFile ) {
$this->fileCount = 0;
$this->bytesIn = 0;
$out = "/*! Built with IMPACT - impactjs.com */\n\n";
foreach( $inFiles as $f ) {
$out .= $this->load( $f );
}
$bytesOut = strlen($out);
$bytesOutZipped = 0;
echo "writing $outFile\n";
@file_put_contents( $outFile, $out ) or
die("ERROR: Couldn't write to $outFile\n");
if( $this->format & self::GZIPPED ) {
$gzFile = "$outFile.gz";
echo "writing $gzFile\n";
$fh = gzopen( $gzFile, 'w9' ) or
die("ERROR: Couldn't write to $gzFile\n");
gzwrite( $fh, $out );
gzclose( $fh );
$bytesOutZipped = filesize( $gzFile );
}
echo
"\nbaked {$this->fileCount} files: ".
round($this->bytesIn/1024,1)."kb -> ".round($bytesOut/1024,1)."kb" .
( $this->format & self::GZIPPED
? " (".round($bytesOutZipped/1024,1)."kb gzipped)\n"
: "\n"
);
}
protected function load( $path ) {
if( isset($this->loaded[$path]) ) {
return '';
}
if( !file_exists($path) ) {
die("ERROR: Couldn't load $path required from {$this->currentInput}\n");
}
echo "loading $path \n";
$this->loaded[$path] = true;
$this->currentInput = $path;
$code = file_get_contents( $path );
$this->bytesIn += strlen($code);
$this->fileCount++;
if( $this->format & self::MINIFIED ) {
$code = trim(JSMin::minify($code));
}
// Naively probe the file for 'ig.module().requires().defines()' code;
// the 'requries()' part will be handled by the regexp callback
$this->definesModule = false;
$code = preg_replace_callback(
'/ig\s*
\.\s*module\s*\((.*?)\)\s*
(\.\s*requires\s*\((.*?)\)\s*)?
\.\s*defines\s*\(
/smx',
array($this,'loadCallback'),
$code
);
// All files should define a module; maybe we just missed it? Print a
// friendly reminder :)
if( !$this->definesModule ) {
echo "WARNING: file $path seems to define no module!\n";
}
return $code;
}
protected function loadCallback( $matches ) {
$currentInput = $this->currentInput;
$this->definesModule = true;
$moduleName = $matches[1];
$requiredFiles = isset($matches[3]) ? $matches[3] : '';
$requiredCode = '';
if( $requiredFiles ) {
// Explode the module names and map them to file names. Ignore the
// dom.ready module if present
$moduleFiles = array_diff(
explode(
',',
preg_replace(
'/[\s\'"]|\/\/.*|\/\*.*\*\//', // strip quotes and spaces
'',
str_replace('.', '/', $requiredFiles ) // . to /
)
),
array('dom/ready')
);
foreach( $moduleFiles as $f ) {
$requiredCode .= $this->load( $this->base . $f.'.js' );
}
}
return
$requiredCode .
"\n\n// $currentInput\n" .
'ig.baked=true;' .
'ig.module('.$moduleName.')' .
( $requiredFiles
? '.requires('.$requiredFiles.')'
: ''
) .
'.defines(';
}
}
?>