forked from peteboere/css-crush
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
executable file
·303 lines (229 loc) · 7.7 KB
/
cli.php
File metadata and controls
executable file
·303 lines (229 loc) · 7.7 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
#!/usr/bin/env php
<?php
/**
*
* Command line utility.
*
*/
require_once 'CssCrush.php';
// Exit status constants.
define( 'STATUS_OK', 0 );
define( 'STATUS_ERROR', 1 );
##################################################################
## Helpers.
function stderr ( $lines, $closing_newline = true ) {
$out = implode( PHP_EOL, (array) $lines ) . ( $closing_newline ? PHP_EOL : '' );
fwrite( STDERR, $out );
}
function stdout ( $lines, $closing_newline = true ) {
$out = implode( PHP_EOL, (array) $lines ) . ( $closing_newline ? PHP_EOL : '' );
// On OSX terminal is sometimes truncating 'visual' output to terminal
// with fwrite to STDOUT.
echo $out;
}
function get_stdin_contents () {
$stdin = fopen( 'php://stdin', 'r' );
stream_set_blocking( $stdin, false );
$stdin_contents = stream_get_contents( $stdin );
fclose( $stdin );
return $stdin_contents;
}
##################################################################
## Version detection.
$version = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
$required_version = 5.3;
if ( $version < $required_version ) {
stderr( array(
"PHP version $required_version or higher is required to use this tool.",
"You are currently running PHP version $version" )
);
exit( STATUS_ERROR );
}
##################################################################
## Options
$short_opts = array(
"f:", // Input file. Defaults to sdtin.
"o:", // Output file. Defaults to stdout.
"p", // Pretty formatting.
'b', // Output boilerplate.
'h', // Display help.
);
$long_opts = array(
'file:', // Input file. Defaults to sdtin.
'output:', // Output file. Defaults to stdout.
'pretty', // Pretty formatting.
'boilerplate', // Output boilerplate.
'help', // Display help.
'version', // Display version.
'trace', // Output sass tracing stubs.
'formatter:', // Formatter name for formatted output.
'vendor-target:', // Vendor target.
'variables:', // Map of variable names in an http query string format.
'enable:', // List of plugins to enable.
'disable:', // List of plugins to disable.
'context:', // Context for resolving URLs.
'newlines:', // Newline style.
);
$opts = getopt( implode( $short_opts ), $long_opts );
$input_file = @( $opts['f'] ?: $opts['file'] );
$output_file = @( $opts['o'] ?: $opts['output'] );
$pretty = @( isset( $opts['p'] ) ?: isset( $opts['pretty'] ) );
$boilerplate = @( isset( $opts['b'] ) ?: isset( $opts['boilerplate'] ) );
$help_flag = @( isset( $opts['h'] ) ?: isset( $opts['help'] ) );
$version_flag = @isset( $opts['version'] );
$trace_flag = @isset( $opts['trace'] );
$formatter = @$opts['formatter'];
$vendor_target = @$opts['vendor-target'];
$variables = @$opts['variables'];
$newlines = @$opts['newlines'];
$enable_plugins = isset( $opts['enable'] ) ? (array) $opts['enable'] : null;
$disable_plugins = isset( $opts['disable'] ) ? (array) $opts['disable'] : null;
$context = isset( $opts['context'] ) ? (array) $opts['context'] : null;
##################################################################
## Help page.
$help = <<<TPL
Usage:
csscrush [-f|--file] [-o|--output-file] [-p|--pretty] [-b|--boilerplate]
[-h|--help] [--formatter] [--variables] [--vendor-target]
[--version] [--newlines]
Options:
-f, --file:
The input file, if omitted takes input from stdin.
-o, --output:
The output file, if omitted prints to stdout.
-p, --pretty:
Formatted, unminified output.
-b, --boilerplate:
Whether or not to output a boilerplate.
-h, --help:
Display this help mesasge.
--context:
Filepath context for resolving URLs.
--disable:
List of plugins to disable. Pass 'all' to disable all.
--enable:
List of plugins to enable. Overrides --disable.
--formatter:
Formatter to use for formatted (--pretty) output.
Available formatters:
'block' (default) -
Rules are block formatted.
'single-line' -
Rules are printed in single lines.
'padded' -
Rules are printed in single lines with right padded selectors.
--newlines:
Force newline style on output css. Defaults to the current platform
newline. Possible values: 'windows' (or 'win'), 'unix', 'use-platform'.
--trace:
Output debug-info stubs compatible with client-side sass debuggers.
--variables:
Map of variable names in an http query string format.
--vendor-target:
Set to 'all' for all vendor prefixes (default).
Set to 'none' for no vendor prefixes.
Set to a specific vendor prefix.
--version:
Print version number.
Examples:
csscrush -f styles.css --pretty --vendor-target webkit
# Piped input
cat 'styles.css' | csscrush --boilerplate
# Linting
csscrush -f screen.css -p --enable property-sorter -o screen-linted.css
TPL;
if ( $version_flag ) {
stdout( 'CSS Crush ' . CssCrush::$config->version );
exit( STATUS_OK );
}
if ( $help_flag ) {
stdout( $help );
exit( STATUS_OK );
}
##################################################################
## Input.
$input = null;
if ( $input_file ) {
if ( ! file_exists( $input_file ) ) {
stderr( 'Input file not found' . PHP_EOL );
exit( STATUS_ERROR );
}
$input = file_get_contents( $input_file );
}
elseif ( $stdin_contents = get_stdin_contents() ) {
$input = $stdin_contents;
}
else {
// No input, just output help screen.
stdout( $help );
exit( STATUS_OK );
}
##################################################################
## Processing.
$process_opts = array();
$process_opts[ 'boilerplate' ] = $boilerplate ? true : false;
$process_opts[ 'minify' ] = $pretty ? false : true;
$process_opts[ 'formatter' ] = $formatter ?: null;
$process_opts[ 'rewrite_import_urls' ] = true;
// Newlines.
if ( isset( $newlines ) ) {
$process_opts[ 'newlines' ] = $newlines;
}
// Enable plugin args.
if ( $enable_plugins ) {
foreach ( $enable_plugins as $arg ) {
foreach ( preg_split( '!\s*,\s*!', $arg ) as $plugin ) {
$process_opts[ 'enable' ][] = $plugin;
}
}
}
// Disable plugin args.
if ( $disable_plugins ) {
foreach ( $disable_plugins as $arg ) {
foreach ( preg_split( '!\s*,\s*!', $arg ) as $plugin ) {
$process_opts[ 'disable' ][] = $plugin;
}
}
}
// Tracing.
if ( $trace_flag ) {
$process_opts[ 'trace' ] = true;
}
// Vendor target args.
if ( $vendor_target ) {
$process_opts[ 'vendor_target' ] = $vendor_target;
}
// Variables args.
if ( $variables ) {
parse_str( $variables, $in_vars );
$process_opts[ 'vars' ] = $in_vars;
}
// Resolve a context for URLs.
if ( ! $context ) {
$context = $input_file ? dirname( realpath( $input_file ) ) : null;
}
// If there is an import context set document root also.
if ( $context ) {
$process_opts[ 'doc_root' ] = $context;
$process_opts[ 'context' ] = $context;
}
$output = CssCrush::string( $input, $process_opts );
##################################################################
## Output.
if ( $output_file ) {
if ( ! @file_put_contents( $output_file, $output ) ) {
$message[] = "Could not write to path '$output_file'";
if ( strpos( $output_file, '~' ) === 0 ) {
$message[] = 'Tilde expansion does not work here';
}
stderr( $message );
exit( STATUS_ERROR );
}
}
else {
if ( CssCrush::$process->errors ) {
stderr( CssCrush::$process->errors );
}
stdout( $output );
exit( STATUS_OK );
}