lessc 3.9 KB

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