Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 644 lines (501 sloc) 17.198 kb
0c51ab0 @peteboere Added command line application
authored
1 <?php
2 /**
3 *
3fd598c @peteboere Implemented autoloader for loading all classes (moving towards PSR-0 …
authored
4 * Command line utility.
0c51ab0 @peteboere Added command line application
authored
5 *
6 */
7 require_once 'CssCrush.php';
8
ad963ed @peteboere Added file watching option to cli.
authored
9 define('STATUS_OK', 0);
10 define('STATUS_ERROR', 1);
9e2e510 @peteboere Added a workaround for issue #41 (stream_set_blocking not working wit…
authored
11
0c51ab0 @peteboere Added command line application
authored
12 $version = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
13 $requiredVersion = 5.3;
a8eb34f @peteboere Added version flag to command line application
authored
14
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
15 if ($version < $requiredVersion) {
e640f06 @peteboere Updating cli utility.
authored
16
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
17 stderr(array("PHP version $requiredVersion or higher is required to use this tool.",
18 "You are currently running PHP $version"));
ad963ed @peteboere Added file watching option to cli.
authored
19
20 exit(STATUS_ERROR);
0c51ab0 @peteboere Added command line application
authored
21 }
22
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
23 try {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
24 $args = parse_args();
7074138 @peteboere Some refactoring for command line util.
authored
25 }
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
26 catch (Exception $ex) {
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
27
28 stderr(message($ex->getMessage(), array('type'=>'error')));
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
29
30 exit($ex->getCode());
7074138 @peteboere Some refactoring for command line util.
authored
31 }
0c51ab0 @peteboere Added command line application
authored
32
b13156e @peteboere Added --enable and --disable command line options
authored
33
ad963ed @peteboere Added file watching option to cli.
authored
34 ##################################################################
789fd78 @peteboere Added option to cli for listing available plugins.
authored
35 ## Information options.
36
37 if ($args->version) {
38
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
39 stdout((string) csscrush_version(true));
789fd78 @peteboere Added option to cli for listing available plugins.
authored
40
41 exit(STATUS_OK);
42 }
43 elseif ($args->help) {
44
45 stdout(manpage());
46
47 exit(STATUS_OK);
48 }
49 elseif ($args->list) {
50
1526649 @peteboere Main changes to take advantage of php 5.3
authored
51 foreach (CssCrush\Plugin::info() as $name => $docs) {
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
52 $headline = isset($docs[0]) ? $docs[0] : '';
53 stdout(message(array($name => $headline), array('color'=>'g')));
789fd78 @peteboere Added option to cli for listing available plugins.
authored
54 }
55
56 exit(STATUS_OK);
57 }
58
59
60 ##################################################################
35147d4 @peteboere Fixed issue with nested rules not inheriting.
authored
61 ## Resolve input.
0c51ab0 @peteboere Added command line application
authored
62
63 $input = null;
64
ad963ed @peteboere Added file watching option to cli.
authored
65 if ($args->input_file) {
a8eb34f @peteboere Added version flag to command line application
authored
66
ad963ed @peteboere Added file watching option to cli.
authored
67 $input = file_get_contents($args->input_file);
0c51ab0 @peteboere Added command line application
authored
68 }
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
69 elseif ($stdin = get_stdin_contents()) {
ad963ed @peteboere Added file watching option to cli.
authored
70
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
71 $input = $stdin;
0c51ab0 @peteboere Added command line application
authored
72 }
73 else {
ad963ed @peteboere Added file watching option to cli.
authored
74 stdout(manpage());
75
76 exit(STATUS_OK);
77 }
78
79
80 if ($args->watch && ! $args->input_file) {
81
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
82 stderr(message('Watch mode requires an input file.', array('type'=>'error')));
ad963ed @peteboere Added file watching option to cli.
authored
83
84 exit(STATUS_ERROR);
0c51ab0 @peteboere Added command line application
authored
85 }
86
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
87
0c51ab0 @peteboere Added command line application
authored
88 ##################################################################
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
89 ## Resolve process options.
0c51ab0 @peteboere Added command line application
authored
90
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
91 $configFile = 'crushfile.php';
92 if (file_exists($configFile)) {
93 $options = CssCrush\Util::readConfigFile($configFile);
94 }
95 else {
96 $options = array();
97 }
ad963ed @peteboere Added file watching option to cli.
authored
98
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
99 if ($args->pretty) {
100 $options['minify'] = false;
ad963ed @peteboere Added file watching option to cli.
authored
101 }
102
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
103 foreach (array('boilerplate', 'formatter', 'newlines', 'stat_dump', 'source_map') as $option) {
104 if ($args->$option) {
105 $options[$option] = $args->$option;
106 }
3fd598c @peteboere Implemented autoloader for loading all classes (moving towards PSR-0 …
authored
107 }
108
ad963ed @peteboere Added file watching option to cli.
authored
109 if ($args->enable_plugins) {
03179dd @peteboere Removed `disable` option. Renamed `enable` option to `plugins`, old n…
authored
110 $options['plugins'] = parse_list($args->enable_plugins);
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
111 }
112
113 if ($args->vendor_target) {
114 $options['vendor_target'] = parse_list($args->vendor_target);
b13156e @peteboere Added --enable and --disable command line options
authored
115 }
116
ad963ed @peteboere Added file watching option to cli.
authored
117 if ($args->vars) {
118 parse_str($args->vars, $in_vars);
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
119 $options['vars'] = $in_vars;
0c51ab0 @peteboere Added command line application
authored
120 }
121
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
122 if ($args->output_file) {
123 $options['output_dir'] = dirname($args->output_file);
124 $options['output_file'] = basename($args->output_file);
157b188 @peteboere Added trace option to output SASS compatible debug-info stubs for use…
authored
125 }
0c51ab0 @peteboere Added command line application
authored
126
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
127 $options += array(
128 'doc_root' => getcwd(),
129 'context' => $args->context,
130 );
3fd598c @peteboere Implemented autoloader for loading all classes (moving towards PSR-0 …
authored
131
a2c46f8 @peteboere Some updates to the command line application, a little cleaning up
authored
132
0c51ab0 @peteboere Added command line application
authored
133 ##################################################################
3fd598c @peteboere Implemented autoloader for loading all classes (moving towards PSR-0 …
authored
134 ## Output.
0c51ab0 @peteboere Added command line application
authored
135
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
136 error_reporting(0);
137
ad963ed @peteboere Added file watching option to cli.
authored
138 if ($args->watch) {
a8eb34f @peteboere Added version flag to command line application
authored
139
1526649 @peteboere Main changes to take advantage of php 5.3
authored
140 csscrush_set('config', array('io' => 'CssCrush\IO\Watch'));
a8eb34f @peteboere Added version flag to command line application
authored
141
ad963ed @peteboere Added file watching option to cli.
authored
142 stdout('CONTROL-C to quit.');
a8eb34f @peteboere Added version flag to command line application
authored
143
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
144 $outstandingErrors = false;
46f4686 @peteboere Improving feedback for command line watched files.
authored
145
ad963ed @peteboere Added file watching option to cli.
authored
146 while (true) {
147
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
148 csscrush_file($args->input_file, $options);
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
149 $stats = csscrush_stat();
ad963ed @peteboere Added file watching option to cli.
authored
150
46f4686 @peteboere Improving feedback for command line watched files.
authored
151 $changed = $stats['compile_time'] && ! $stats['errors'];
152 $errors = $stats['errors'];
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
153 $warnings = $stats['warnings'];
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
154 $showErrors = $errors && (! $outstandingErrors || ($outstandingErrors != $errors));
ad963ed @peteboere Added file watching option to cli.
authored
155
46f4686 @peteboere Improving feedback for command line watched files.
authored
156 if ($errors) {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
157 if ($showErrors) {
158 $outstandingErrors = $errors;
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
159 stderr(message($errors, array('type'=>'error')));
46f4686 @peteboere Improving feedback for command line watched files.
authored
160 }
161 }
162 elseif ($changed) {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
163 $outstandingErrors = false;
e640f06 @peteboere Updating cli utility.
authored
164 stderr(message(fmt_fileinfo($stats, 'output'), array('type'=>'write')));
4831993 @peteboere Untabbifying project.
authored
165 }
a8eb34f @peteboere Added version flag to command line application
authored
166
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
167 if (($showErrors || $changed) && $warnings) {
168 stderr(message($warnings, array('type'=>'warning')));
169 }
170
e640f06 @peteboere Updating cli utility.
authored
171 if ($changed && $args->stats) {
172 stderr(message($stats, array('type'=>'stats')));
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
173 }
174
ad963ed @peteboere Added file watching option to cli.
authored
175 sleep(1);
4831993 @peteboere Untabbifying project.
authored
176 }
0c51ab0 @peteboere Added command line application
authored
177 }
178 else {
179
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
180 $stdOutput = null;
ad963ed @peteboere Added file watching option to cli.
authored
181
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
182 if ($args->input_file && isset($options['output_dir'])) {
183 $options['cache'] = false;
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
184 csscrush_file($args->input_file, $options);
185 }
9a48f6e @peteboere Removed error notice when creating directories via command line.
authored
186 else {
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
187 $stdOutput = csscrush_string($input, $options);
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
188 }
189
190 $stats = csscrush_stat();
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
191 $errors = $stats['errors'];
192 $warnings = $stats['warnings'];
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
193
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
194 if ($errors) {
195 stderr(message($errors, array('type'=>'error')));
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
196
197 exit(STATUS_ERROR);
9a48f6e @peteboere Removed error notice when creating directories via command line.
authored
198 }
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
199 elseif ($args->input_file && ! empty($stats['output_filename'])) {
e640f06 @peteboere Updating cli utility.
authored
200 stderr(message(fmt_fileinfo($stats, 'output'), array('type'=>'write')));
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
201 }
202
203 if ($warnings) {
204 stderr(message($warnings, array('type'=>'warning')));
205 }
ad963ed @peteboere Added file watching option to cli.
authored
206
e640f06 @peteboere Updating cli utility.
authored
207 if ($args->stats) {
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
208 stderr(message($stats, array('type'=>'stats')));
209 }
210
211 if ($stdOutput) {
212 stdout($stdOutput);
4831993 @peteboere Untabbifying project.
authored
213 }
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
214
215 exit(STATUS_OK);
ad963ed @peteboere Added file watching option to cli.
authored
216 }
217
218
219 ##################################################################
220 ## Helpers.
221
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
222 function stderr($lines, $closing_newline = true) {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
223
ad963ed @peteboere Added file watching option to cli.
authored
224 $out = implode(PHP_EOL, (array) $lines) . ($closing_newline ? PHP_EOL : '');
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
225 fwrite(defined('TESTMODE') && TESTMODE ? STDOUT : STDERR, $out);
ad963ed @peteboere Added file watching option to cli.
authored
226 }
227
8114309 @peteboere Minor code style refresh.
authored
228 function stdout($lines, $closing_newline = true) {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
229
ad963ed @peteboere Added file watching option to cli.
authored
230 $out = implode(PHP_EOL, (array) $lines) . ($closing_newline ? PHP_EOL : '');
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
231 fwrite(STDOUT, $out);
ad963ed @peteboere Added file watching option to cli.
authored
232 }
233
8114309 @peteboere Minor code style refresh.
authored
234 function get_stdin_contents() {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
235
e640f06 @peteboere Updating cli utility.
authored
236 stream_set_blocking(STDIN, 0);
237 $contents = stream_get_contents(STDIN);
238 stream_set_blocking(STDIN, 1);
ad963ed @peteboere Added file watching option to cli.
authored
239
e640f06 @peteboere Updating cli utility.
authored
240 return $contents;
ad963ed @peteboere Added file watching option to cli.
authored
241 }
242
8114309 @peteboere Minor code style refresh.
authored
243 function parse_list(array $option) {
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
244
ad963ed @peteboere Added file watching option to cli.
authored
245 $out = array();
246 foreach ($option as $arg) {
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
247 if (is_string($arg)) {
248 foreach (preg_split('~\s*,\s*~', $arg) as $item) {
249 $out[] = $item;
250 }
251 }
252 else {
253 $out[] = $arg;
ad963ed @peteboere Added file watching option to cli.
authored
254 }
255 }
256 return $out;
257 }
258
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
259 function message($messages, $options = array()) {
260
261 $defaults = array(
262 'color' => 'b',
263 'label' => null,
264 'indent' => false,
265 'format_label' => false,
266 );
267 $preset = ! empty($options['type']) ? $options['type'] : null;
268 switch ($preset) {
269 case 'error':
270 $defaults['color'] = 'r';
271 $defaults['label'] = 'ERROR';
272 break;
273 case 'warning':
274 $defaults['color'] = 'y';
275 $defaults['label'] = 'WARNING';
276 break;
277 case 'write':
278 $defaults['color'] = 'g';
279 $defaults['label'] = 'WRITE';
280 break;
281 case 'stats':
e640f06 @peteboere Updating cli utility.
authored
282 // Making stats concise and readable.
283 $messages['input_file'] = $messages['input_path'];
284 $messages['compile_time'] = round($messages['compile_time'], 5) . ' seconds';
285 foreach (array('input_filename', 'input_path', 'output_filename',
286 'output_path', 'vars', 'errors', 'warnings') as $key) {
287 unset($messages[$key]);
288 }
289 ksort($messages);
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
290 $defaults['indent'] = true;
291 $defaults['format_label'] = true;
292 break;
293 }
294 extract($options + $defaults);
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
295
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
296 $out = array();
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
297 foreach ((array) $messages as $_label => $value) {
298 $_label = $label ?: $_label;
299 if ($format_label) {
300 $_label = ucfirst(str_replace('_', ' ', $_label));
301 }
302 $prefix = $indent ? '└── ' : '';
303 $colorUp = strtoupper($color);
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
304 if (is_scalar($value)) {
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
305 $out[] = colorize("<$color>$prefix<$colorUp>$_label:<$color> $value</>");
0c87864 @peteboere Command utility now has proper support for the trace option.
authored
306 }
307 }
308 return implode(PHP_EOL, $out);
309 }
310
e640f06 @peteboere Updating cli utility.
authored
311 function fmt_fileinfo($stats, $type) {
312 return $stats[$type . '_filename'] . ' ' . '(' . $stats[$type . '_path'] . ')';
313 }
314
8114309 @peteboere Minor code style refresh.
authored
315 function pick(array &$arr) {
ad963ed @peteboere Added file watching option to cli.
authored
316
317 $args = func_get_args();
318 array_shift($args);
319
320 foreach ($args as $key) {
321 if (isset($arr[$key])) {
322 // Optional values return false but we want true is argument is present.
323 return is_bool($arr[$key]) ? true : $arr[$key];
324 }
325 }
326 return null;
327 }
328
8114309 @peteboere Minor code style refresh.
authored
329 function colorize($str) {
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
330
b9fd282 @peteboere Suppressing colour output for terminals that don't support it
authored
331 static $color_support;
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
332 static $tags = array(
333 '<b>' => "\033[0;30m",
334 '<r>' => "\033[0;31m",
335 '<g>' => "\033[0;32m",
336 '<y>' => "\033[0;33m",
337 '<b>' => "\033[0;34m",
338 '<v>' => "\033[0;35m",
339 '<c>' => "\033[0;36m",
340 '<w>' => "\033[0;37m",
341
342 '<B>' => "\033[1;30m",
343 '<R>' => "\033[1;31m",
344 '<G>' => "\033[1;32m",
345 '<Y>' => "\033[1;33m",
346 '<B>' => "\033[1;34m",
347 '<V>' => "\033[1;35m",
348 '<C>' => "\033[1;36m",
349 '<W>' => "\033[1;37m",
350
351 '</>' => "\033[m",
352 );
353
b9fd282 @peteboere Suppressing colour output for terminals that don't support it
authored
354 if (! isset($color_support)) {
61855cc @peteboere Adding cli utility test.
authored
355 $color_support = defined('TESTMODE') && TESTMODE ? false : true;
b9fd282 @peteboere Suppressing colour output for terminals that don't support it
authored
356 if (DIRECTORY_SEPARATOR == '\\') {
357 $color_support = false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
358 }
359 }
360
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
361 $find = array_keys($tags);
b9fd282 @peteboere Suppressing colour output for terminals that don't support it
authored
362 $replace = $color_support ? array_values($tags) : '';
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
363
364 return str_replace($find, $replace, $str);
365 }
366
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
367 function get_trailing_io_args($required_value_opts) {
7074138 @peteboere Some refactoring for command line util.
authored
368
369 $trailing_input_file = null;
370 $trailing_output_file = null;
371
372 // Get raw script args, shift off calling scriptname and reduce to last three.
373 $trailing_args = $GLOBALS['argv'];
374 array_shift($trailing_args);
375 $trailing_args = array_slice($trailing_args, -3);
376
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
377 // Create patterns for detecting options.
378 $required_values = implode('|', $required_value_opts);
7074138 @peteboere Some refactoring for command line util.
authored
379 $value_opt_patt = "~^-{1,2}($required_values)$~";
380 $other_opt_patt = "~^-{1,2}([a-z0-9\-]+)?(=|$)~ix";
381
382 // Step through the args.
383 $filtered = array();
384 for ($i = 0; $i < count($trailing_args); $i++) {
385
386 $current = $trailing_args[$i];
387
388 // If tests as a required value option, reset and skip next.
389 if (preg_match($value_opt_patt, $current)) {
390 $filtered = array();
391 $i++;
392 }
393 // If it looks like any other kind of flag, or optional value option, reset.
394 elseif (preg_match($other_opt_patt, $current)) {
395 $filtered = array();
396 }
397 else {
398 $filtered[] = $current;
399 }
400 }
401
402 // We're only interested in the last two values.
403 $filtered = array_slice($filtered, -2);
404
405 switch (count($filtered)) {
406 case 1:
407 $trailing_input_file = $filtered[0];
408 break;
409 case 2:
410 $trailing_input_file = $filtered[0];
411 $trailing_output_file = $filtered[1];
412 break;
413 }
414
415 return array($trailing_input_file, $trailing_output_file);
416 }
417
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
418 function parse_args() {
419
420 $required_value_opts = array(
421 'i|input|f|file', // Input file. Defaults to STDIN.
422 'o|output', // Output file. Defaults to STDOUT.
03179dd @peteboere Removed `disable` option. Renamed `enable` option to `plugins`, old n…
authored
423 'E|enable|plugins',
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
424 'D|disable',
425 'vars|variables',
426 'formatter',
427 'vendor-target',
428 'context',
429 'newlines',
430 );
431
432 $optional_value_opts = array(
433 'b|boilerplate',
434 'stat-dump',
435 );
436
437 $flag_opts = array(
438 'p|pretty',
439 'w|watch',
440 'list',
441 'help',
442 'version',
443 'source-map',
e640f06 @peteboere Updating cli utility.
authored
444 'stats',
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
445 'test',
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
446 );
447
448 // Create option strings for getopt().
449 $short_opts = array();
450 $long_opts = array();
451 $join_opts = function ($opts_list, $modifier) use (&$short_opts, &$long_opts) {
452 foreach ($opts_list as $opt) {
453 foreach (explode('|', $opt) as $arg) {
454 if (strlen($arg) === 1) {
455 $short_opts[] = "$arg$modifier";
456 }
457 else {
458 $long_opts[] = "$arg$modifier";
459 }
460 }
461 }
462 };
463 $join_opts($required_value_opts, ':');
464 $join_opts($optional_value_opts, '::');
465 $join_opts($flag_opts, '');
466
467 $opts = getopt(implode($short_opts), $long_opts);
468
469 $args = new stdClass();
470
471 // Information options.
472 $args->help = isset($opts['h']) ?: isset($opts['help']);
473 $args->version = isset($opts['version']);
474 $args->list = isset($opts['l']) ?: isset($opts['list']);
475
476 // File arguments.
477 $args->input_file = pick($opts, 'i', 'input', 'f', 'file');
478 $args->output_file = pick($opts, 'o', 'output');
479 $args->context = pick($opts, 'context');
480
481 // Flags.
482 $args->pretty = isset($opts['p']) ?: isset($opts['pretty']);
483 $args->watch = isset($opts['w']) ?: isset($opts['watch']);
484 $args->source_map = isset($opts['source-map']);
e640f06 @peteboere Updating cli utility.
authored
485 $args->stats = pick($opts, 'stats');
bc3c871 @peteboere Some improvements to logging and error reporting.
authored
486 define('TESTMODE', isset($opts['test']));
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
487
488 // Arguments that optionally accept a single value.
489 $args->boilerplate = pick($opts, 'b', 'boilerplate');
490 $args->stat_dump = pick($opts, 'stat-dump');
491
492 // Arguments that require a single value.
493 $args->formatter = pick($opts, 'formatter');
494 $args->vars = pick($opts, 'vars', 'variables');
495 $args->newlines = pick($opts, 'newlines');
496
497 // Arguments that require a value but accept multiple values.
03179dd @peteboere Removed `disable` option. Renamed `enable` option to `plugins`, old n…
authored
498 $args->enable_plugins = pick($opts, 'E', 'enable', 'plugins');
e640f06 @peteboere Updating cli utility.
authored
499 $args->vendor_target = pick($opts, 'vendor-target');
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
500
501 // Run multiple value arguments through array cast.
03179dd @peteboere Removed `disable` option. Renamed `enable` option to `plugins`, old n…
authored
502 foreach (array('enable_plugins', 'vendor_target') as $arg) {
e640f06 @peteboere Updating cli utility.
authored
503 if ($args->$arg) {
504 $args->$arg = (array) $args->$arg;
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
505 }
506 }
507
508 // Detect trailing IO files from raw script arguments.
509 list($trailing_input_file, $trailing_output_file) = get_trailing_io_args($required_value_opts);
510
511 // If detected apply, not overriding explicit IO file options.
512 if (! $args->input_file && $trailing_input_file) {
513 $args->input_file = $trailing_input_file;
514 }
515 if (! $args->output_file && $trailing_output_file) {
516 $args->output_file = $trailing_output_file;
517 }
518
519 if ($args->input_file) {
3401875 @peteboere Possible hhvm issue with `mkdir()`?
authored
520 $inputFile = $args->input_file;
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
521 if (! ($args->input_file = realpath($args->input_file))) {
3401875 @peteboere Possible hhvm issue with `mkdir()`?
authored
522 throw new Exception("Input file '$inputFile' does not exist.", STATUS_ERROR);
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
523 }
524 }
525
526 if ($args->output_file) {
3401875 @peteboere Possible hhvm issue with `mkdir()`?
authored
527 $outDir = dirname($args->output_file);
ec83d4f @peteboere Updating dev dependencies.
authored
528 if (! realpath($outDir) && ! @mkdir($outDir, 0755, true)) {
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
529 throw new Exception('Output directory does not exist and could not be created.', STATUS_ERROR);
530 }
3401875 @peteboere Possible hhvm issue with `mkdir()`?
authored
531 $args->output_file = realpath($outDir) . '/' . basename($args->output_file);
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
532 }
533
534 if ($args->context) {
535 if (! ($args->context = realpath($args->context))) {
536 throw new Exception('Context path does not exist.', STATUS_ERROR);
537 }
538 }
539 else {
540 $args->context = $args->input_file ? dirname($args->input_file) : getcwd();
541 }
542 if (is_string($args->boilerplate)) {
543 if (! ($args->boilerplate = realpath($args->boilerplate))) {
544 throw new Exception('Boilerplate file does not exist.', STATUS_ERROR);
545 }
546 }
547
548 return $args;
549 }
550
8114309 @peteboere Minor code style refresh.
authored
551 function manpage() {
ad963ed @peteboere Added file watching option to cli.
authored
552
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
553 $manpage = <<<TPL
ad963ed @peteboere Added file watching option to cli.
authored
554
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
555 <B>USAGE:</>
7074138 @peteboere Some refactoring for command line util.
authored
556 <B>csscrush <G>[OPTIONS] <g>[input-file] [output-file]
ad963ed @peteboere Added file watching option to cli.
authored
557
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
558 <B>OPTIONS:</>
e640f06 @peteboere Updating cli utility.
authored
559 <G>-i<g>, --input</>
7074138 @peteboere Some refactoring for command line util.
authored
560 Input file. If omitted takes input from STDIN.
ad963ed @peteboere Added file watching option to cli.
authored
561
e640f06 @peteboere Updating cli utility.
authored
562 <G>-o<g>, --output</>
7074138 @peteboere Some refactoring for command line util.
authored
563 Output file. If omitted prints to STDOUT.
ad963ed @peteboere Added file watching option to cli.
authored
564
e640f06 @peteboere Updating cli utility.
authored
565 <G>-p<g>, --pretty</>
ad963ed @peteboere Added file watching option to cli.
authored
566 Formatted, un-minified output.
567
e640f06 @peteboere Updating cli utility.
authored
568 <G>-w<g>, --watch</>
ad963ed @peteboere Added file watching option to cli.
authored
569 Watch input file for changes.
570 Writes to file specified with -o option or to the input file
571 directory with a '.crush.css' file extension.
0c51ab0 @peteboere Added command line application
authored
572
03179dd @peteboere Removed `disable` option. Renamed `enable` option to `plugins`, old n…
authored
573 <G>-E<g>, --plugins</>
574 List of plugins (comma separated) to enable.
ad963ed @peteboere Added file watching option to cli.
authored
575
e640f06 @peteboere Updating cli utility.
authored
576 <g>--boilerplate</>
7074138 @peteboere Some refactoring for command line util.
authored
577 Whether or not to output a boilerplate. Optionally accepts filepath
578 to a custom boilerplate template.
579
e640f06 @peteboere Updating cli utility.
authored
580 <g>--context</>
7074138 @peteboere Some refactoring for command line util.
authored
581 Filepath context for resolving relative URLs. Only meaningful when
582 taking raw input from STDIN.
583
e640f06 @peteboere Updating cli utility.
authored
584 <g>--formatter</>
585 Possible values:
586 'block' (default)
587 Rules are block formatted.
588 'single-line'
589 Rules are printed in single lines.
590 'padded'
591 Rules are printed in single lines with right padded selectors.
ad963ed @peteboere Added file watching option to cli.
authored
592
e640f06 @peteboere Updating cli utility.
authored
593 <g>--help</>
5d58e69 @peteboere Updated plugin summaries. Fixed some typos.
authored
594 Display this help message.
7074138 @peteboere Some refactoring for command line util.
authored
595
1588fa2 @peteboere Moved command line app alias to a more intuitive location.
authored
596 <g>--list</>
789fd78 @peteboere Added option to cli for listing available plugins.
authored
597 Show plugins.
598
e640f06 @peteboere Updating cli utility.
authored
599 <g>--newlines</>
ad963ed @peteboere Added file watching option to cli.
authored
600 Force newline style on output css. Defaults to the current platform
601 newline. Possible values: 'windows' (or 'win'), 'unix', 'use-platform'.
602
e640f06 @peteboere Updating cli utility.
authored
603 <g>--source-map</>
604 Create a source map file (compliant with the Source Map v3 proposal).
53a54ee @peteboere Adding source map docs to command line utility.
authored
605
e640f06 @peteboere Updating cli utility.
authored
606 <g>--stats</>
607 Display post-compile stats.
ad963ed @peteboere Added file watching option to cli.
authored
608
e640f06 @peteboere Updating cli utility.
authored
609 <g>--vars</>
ad963ed @peteboere Added file watching option to cli.
authored
610 Map of variable names in an http query string format.
611
e640f06 @peteboere Updating cli utility.
authored
612 <g>--vendor-target</>
613 Possible values:
614 'all'
615 For all vendor prefixes (default).
616 'none'
617 For no vendor prefixing.
618 'moz', 'webkit', 'ms' etc.
619 Limit to a specific vendor prefix (or comma separated list).
620
621 <g>--version</>
622 Display version number.
ad963ed @peteboere Added file watching option to cli.
authored
623
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
624 <B>EXAMPLES:</>
ad963ed @peteboere Added file watching option to cli.
authored
625 # Restrict vendor prefixing.
626 csscrush --pretty --vendor-target webkit -i styles.css
627
628 # Piped input.
629 cat styles.css | csscrush --vars 'foo=black&bar=white' > alt-styles.css
630
631 # Linting.
d46b2d1 @peteboere Added support for a command line config file (`crushfile.php`).
authored
632 csscrush --pretty -E property-sorter -i styles.css -o linted.css
ad963ed @peteboere Added file watching option to cli.
authored
633
634 # Watch mode.
635 csscrush --watch -i styles.css -o compiled/styles.css
636
637 # Using custom boilerplate template.
7074138 @peteboere Some refactoring for command line util.
authored
638 csscrush --boilerplate=css/boilerplate.txt css/styles.css
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
639
ad963ed @peteboere Added file watching option to cli.
authored
640 TPL;
ffe5405 @peteboere Added ease plugin - Expanded keywords for transitions, ported from re…
authored
641
642 return colorize($manpage);
a8eb34f @peteboere Added version flag to command line application
authored
643 }
Something went wrong with that request. Please try again.