forked from kaimallea/minicat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminicat.php
More file actions
342 lines (289 loc) · 10.2 KB
/
Copy pathminicat.php
File metadata and controls
342 lines (289 loc) · 10.2 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env php
<?php
/**
* @copyright 2012 Kai Mallea
* @author Kai Mallea <kai@mallea.net>
* @license MIT
*/
require_once(__DIR__ . '/lib/Yaml/Yaml.php');
require_once(__DIR__ . '/lib/Yaml/Parser.php');
require_once(__DIR__ . '/lib/Yaml/Exception/ExceptionInterface.php');
require_once(__DIR__ . '/lib/Yaml/Exception/ParseException.php');
require_once(__DIR__ . '/lib/Yaml/Inline.php');
require_once(__DIR__ . '/lib/Yaml/Unescaper.php');
use Symfony\Component\Yaml\Yaml;
/* Idk the real identity of the person who created this ascii
art, but it's awesome, so I'm using it. I shall call you...
Minicat
_
\`*-.
) _`-.
. : `. .
: _ ' \
; *` _. `*-._
`-.-' `-.
; ` `.
:. . \
. \ . : .-' .
' `+.; ; ' :
: ' | ; ;-.
; ' : :`-: _.`* ;
[bug] .*' / .*' ; .*`- +' `*'
`*-* `*-* `*-*'
Automate your JS/CSS minification and concatenation process
*/
class Minicat {
const DEFAULT_CONFIG_FILENAME = 'minicat.yaml';
private static $manifest;
private static $config;
private static $verbose;
private static $conditional_build;
private static $filenames_only;
/**
* Initialization ensures that:
* a. there is an explicitly defined config or we will look
* for the default (./minicat.yaml), and
* b. the config exists and can be parsed without issue
*
* @param array $args Command line arguments from getopt()
*
* @return void
*/
public static function init ($args) {
foreach ($args as $arg => $val) {
switch ($arg) {
case 'h': // fall through
case 'help':
self::print_help();
exit(1);
case 'm': // fall through
case 'manifest':
self::$manifest = $val;
break;
case 'v': // fall through
case 'verbose':
self::$verbose = true;
break;
case 'c': // fall through
case 'conditional':
self::$conditional_build = $val;
break;
case 'f': // fall through
case 'filename':
self::$filenames_only = true;
break;
default:
self::print_help();
exit(1);
}
}
if (!self::$manifest) {
self::log(sprintf('No manifest specified, trying %s', (self::$manifest = self::get_default_config_path())));
}
if (!file_exists(self::$manifest)) {
self::log(sprintf('Could not open manifest file %s', self::$manifest), true);
exit(1);
}
try {
self::$manifest = Yaml::parse(self::$manifest);
} catch (Exception $e) {
self::log($e->getMessage(), true);
exit(1);
}
if ( !(self::$manifest['config']) ) {
self::log('No config section found in manifest.');
exit(1);
}
self::separate_config();
if (self::$conditional_build) {
self::$conditional_build = explode(' ', self::$conditional_build);
self::log('Conditional build mode');
}
self::build();
}
/**
* The main build loop which peforms minification and
* concatenation on each asset
*
* @return void
*/
public static function build () {
self::log(sprintf('Identified %s target assets', count(self::$manifest)));
foreach (self::$manifest as $target_asset => $source_asset_collection) {
if (self::$conditional_build) {
if (!self::conditional_test($target_asset)) {
self::log(sprintf('Skipping %s (no matches)', $target_asset));
continue;
} else {
self::log(sprintf('Conditional match: %s', $target_asset));
}
}
self::log(sprintf('Building %s...', $target_asset));
self::log("Step 1: Minify...");
foreach ($source_asset_collection as $source_asset) {
self::log(sprintf(' |`-%s', $source_asset['file']));
if (isset($source_asset['minify']) &&
strtolower($source_asset['minify']) === 'no') {
self::log(' | `-Skipping minification');
} else {
$temp_files[] = self::minify($source_asset['file']);
}
}
self::log('Step 2: Concatenate...');
self::concat($temp_files, $target_asset);
self::log('Build successful' . "\n");
}
}
/**
* Check if any of the "conditional" files passed in on
* the command line are defined in the manifest for a
* particular target file.
*
* @param string $target Target file
*
* @return true|false
*/
public static function conditional_test ($target) {
foreach (self::$manifest[$target] as $source_asset) {
if (self::$filenames_only) {
foreach (self::$conditional_build as $conditional_file) {
if (strpos($conditional_file, basename($source_asset['file'])) !== false) {
return true;
}
}
} else {
if (in_array($source_asset['file'], self::$conditional_build)) {
return true;
}
}
}
return false;
}
/**
* Remove the "config" section from the config file and store it
* in a class variable
*
* @return void
*/
public static function separate_config () {
for ($i = 0; $i < count(self::$manifest['config']); ++$i) {
foreach(self::$manifest['config'][$i] as $setting => $val) {
switch (strtolower($setting)) {
case 'js_compiler_path':
self::$config['js_compiler_path'] = $val;
break;
case 'js_compiler_command':
self::$config['js_compiler_command'] = $val;
break;
case 'css_compiler_path':
self::$config['css_compiler_path'] = $val;
break;
case 'css_compiler_command':
self::$config['css_compiler_command'] = $val;
break;
default:
self::log(sprintf('Unknown config setting "%s" (skipped)', $setting));
}
}
}
unset(self::$manifest['config']);
}
/**
* Return a file's extension
*
* @param string $filename File name
*
* @return string File's extension
*/
public static function extension ($filename) {
return pathinfo($filename, PATHINFO_EXTENSION);
}
/**
* Concatenate an array of files using native OS commands
*
* @param array $source_files Files to concatenate
* @param string $target_file File to output
*
* @return void
*/
public static function concat ($source_files, $target_file) {
if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
$cmd = sprintf('copy /b /y %s %s', implode('+', $source_files), $target_file);
} else {
$cmd = sprintf('cat %s > %s', implode(' ', $source_files), $target_file);
}
exec($cmd, $output, $result);
if ($result) {
self::log(
sprintf('There was an error concatenating %s',
implode(',', $source_files),
true)
);
exit(1);
}
}
/**
* Minify a file and output a temporary file
*
* @param string $source_file File to minify
*
* @return string Path to minified temp file
*/
public static function minify ($source_file) {
$temp_dir = sys_get_temp_dir();
$temp_file = tempnam($temp_dir, $source_file);
if (!$temp_file) {
self::log('Could not create temp file in ' . $temp_dir, true);
exit(1);
}
$ext = self::extension($source_file);
$minify_cmd = str_replace(
array('{$compiler_path}', '{$input_file}', '{$output_file}'),
array(self::$config[$ext.'_compiler_path'], $source_file, $temp_file),
self::$config[$ext.'_compiler_command']
);
exec($minify_cmd, $output, $result);
if ($result) {
self::log('There was a problem minifying ' . $source_file, true);
exit(1);
}
return $temp_file;
}
/**
* Log a message to stdout in verbose mode
*
* @param string $msg Message to log
* @param boolean $force Force output even when not in verbose mode
*
* @return string File's extension
*/
public static function log ($msg, $force=false) {
if (self::$verbose || $force) {
echo '[MC] ' . $msg . PHP_EOL;
}
}
/**
* Return path to minicat.yaml in current working dir
*
* @return string Current working directory + '/minicat.yaml'
*/
public static function get_default_config_path () {
return getcwd() . DIRECTORY_SEPARATOR . self::DEFAULT_CONFIG_FILENAME;
}
/**
* Print program help
*
* @return void
*/
public static function print_help () {
echo 'Help coming soon!' . "\n";
}
}
if (PHP_SAPI === 'cli') {
Minicat::init(
getopt(
'm:c:hvf',
array('manifest:', 'conditional:', 'help', 'verbose', 'filename')
)
);
}