| 1 |
#!/usr/bin/env php |
| 2 |
<?php |
| 3 |
|
| 4 |
require_once __DIR__ . '/../lib/Less/Autoloader.php'; |
| 5 |
Less_Autoloader::register(); |
| 6 |
|
| 7 |
// Create our environment |
| 8 |
$env = array('compress' => false, 'relativeUrls' => false); |
| 9 |
$silent = false; |
| 10 |
$watch = false; |
| 11 |
$rootpath = ''; |
| 12 |
|
| 13 |
// Check for arguments |
| 14 |
array_shift($argv); |
| 15 |
if (!count($argv)) { |
| 16 |
$argv[] = '-h'; |
| 17 |
} |
| 18 |
|
| 19 |
// parse arguments |
| 20 |
foreach ($argv as $key => $arg) { |
| 21 |
if (preg_match('/^--?([a-z][0-9a-z-]*)(?:=([^\s]+))?$/i', $arg, $matches)) { |
| 22 |
$option = $matches[1]; |
| 23 |
$value = isset($matches[2]) ? $matches[2] : false; |
| 24 |
unset($argv[$key]); |
| 25 |
|
| 26 |
switch ($option) { |
| 27 |
case 'h': |
| 28 |
case 'help': |
| 29 |
echo <<<EOD |
| 30 |
Usage: lessc [options] sources [destination] |
| 31 |
|
| 32 |
-h, --help Print help (this message) and exit. |
| 33 |
-s, --silent Suppress output of error messages. |
| 34 |
-v, --version Print version number and exit. |
| 35 |
-x, --compress Compress output by removing some whitespaces. |
| 36 |
--include-path=PATHS Set include paths. Separated by `:'. Use `;' on Windows. |
| 37 |
--strict-imports Force evaluation of imports. |
| 38 |
-m=, --math= |
| 39 |
always Less will eagerly perform math operations always. |
| 40 |
parens-division Math performed except for division (/) operator |
| 41 |
parens | strict Math only performed inside parentheses'); |
| 42 |
-su=on|off Allow mixed units, e.g. 1px+1em or 1px*1px which have units |
| 43 |
--strict-units=on|off that cannot be represented. |
| 44 |
-ru, --relative-urls re-write relative urls to the base less file. |
| 45 |
-rp, --rootpath=URL Set rootpath for url rewriting in relative imports and urls. |
| 46 |
Works with or without the relative-urls option. |
| 47 |
-w, --watch Watch input files for changes. |
| 48 |
-------------------------- Deprecated ---------------- |
| 49 |
-sm=on|off Legacy parens-only math. Use --math instead. |
| 50 |
--strict-math=on|off |
| 51 |
|
| 52 |
EOD; |
| 53 |
exit; |
| 54 |
case 's': |
| 55 |
case 'silent': |
| 56 |
$silent = true; |
| 57 |
break; |
| 58 |
|
| 59 |
case 'w': |
| 60 |
case 'watch': |
| 61 |
$watch = true; |
| 62 |
break; |
| 63 |
|
| 64 |
case 'v': |
| 65 |
case 'version': |
| 66 |
echo "lessc " . Less_Version::version . " (less.php)\n\n"; |
| 67 |
exit; |
| 68 |
|
| 69 |
case 'rp': |
| 70 |
case 'rootpath': |
| 71 |
$rootpath = $value; |
| 72 |
break; |
| 73 |
|
| 74 |
|
| 75 |
//parser options |
| 76 |
case 'compress': |
| 77 |
$env['compress'] = true; |
| 78 |
break; |
| 79 |
|
| 80 |
case 'ru': |
| 81 |
case 'relative-urls': |
| 82 |
$env['relativeUrls'] = true; |
| 83 |
break; |
| 84 |
|
| 85 |
case 'su': |
| 86 |
case 'strict-units': |
| 87 |
$env['strictUnits'] = ($value === 'on'); |
| 88 |
break; |
| 89 |
|
| 90 |
case 'sm': |
| 91 |
case 'strict-math': |
| 92 |
fwrite( STDERR, "lessc: The --strict-math option has been deprecated. Use --math=parens\n" ); |
| 93 |
$env['strictMath'] = ( $value === "on" ); |
| 94 |
break; |
| 95 |
|
| 96 |
case 'm': |
| 97 |
case 'math': |
| 98 |
$env['math'] = $value; |
| 99 |
break; |
| 100 |
|
| 101 |
case 'x': |
| 102 |
case 'include-path': |
| 103 |
$env['import_dirs'] = preg_split('#;|\:#', $value); |
| 104 |
break; |
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (count($argv) > 1) { |
| 111 |
$output = array_pop($argv); |
| 112 |
$inputs = $argv; |
| 113 |
} |
| 114 |
else { |
| 115 |
$inputs = $argv; |
| 116 |
$output = false; |
| 117 |
} |
| 118 |
|
| 119 |
if (!count($inputs)) { |
| 120 |
echo("lessc: no input files\n"); |
| 121 |
exit; |
| 122 |
} |
| 123 |
|
| 124 |
if ($watch) { |
| 125 |
if (!$output) { |
| 126 |
echo("lessc: you must specify the output file if --watch is given\n"); |
| 127 |
exit; |
| 128 |
} |
| 129 |
|
| 130 |
$lastAction = 0; |
| 131 |
|
| 132 |
echo("lessc: watching input files\n"); |
| 133 |
|
| 134 |
while (1) { |
| 135 |
clearstatcache(); |
| 136 |
|
| 137 |
$updated = false; |
| 138 |
foreach ($inputs as $input) { |
| 139 |
if ($input == '-') { |
| 140 |
if (count($inputs) == 1) { |
| 141 |
echo("lessc: during watching files is not possible to watch stdin\n"); |
| 142 |
exit; |
| 143 |
} |
| 144 |
else { |
| 145 |
continue; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if (filemtime($input) > $lastAction) { |
| 150 |
$updated = true; |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if ($updated) { |
| 156 |
$lastAction = time(); |
| 157 |
$parser = new Less_Parser($env); |
| 158 |
foreach ($inputs as $input) { |
| 159 |
try { |
| 160 |
$parser->parseFile($input, $rootpath); |
| 161 |
} |
| 162 |
catch (Exception $e) { |
| 163 |
echo("lessc: " . $e->getMessage() . " \n"); |
| 164 |
continue; // Invalid processing |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
file_put_contents($output, $parser->getCss()); |
| 169 |
echo("lessc: output file recompiled\n"); |
| 170 |
} |
| 171 |
|
| 172 |
sleep(1); |
| 173 |
} |
| 174 |
} |
| 175 |
else { |
| 176 |
$parser = new Less_Parser($env); |
| 177 |
foreach ($inputs as $input) { |
| 178 |
if ($input == '-') { |
| 179 |
$content = file_get_contents('php://stdin'); |
| 180 |
$parser->parse($content); |
| 181 |
} |
| 182 |
else { |
| 183 |
try { |
| 184 |
$parser->parseFile($input); |
| 185 |
} |
| 186 |
catch (Exception $e) { |
| 187 |
if (!$silent) { |
| 188 |
echo("lessc: " . ((string)$e) . " \n"); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if ($output) { |
| 195 |
file_put_contents($output, $parser->getCss()); |
| 196 |
} |
| 197 |
else { |
| 198 |
echo $parser->getCss(); |
| 199 |
} |
| 200 |
} |