1#!/usr/bin/env php
2<?php
3
4require_once dirname(__FILE__) . '/../lib/Less/Autoloader.php';
5Less_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
14array_shift($argv);
15if (!count($argv)) {
16	$argv[] = '-h';
17}
18
19// parse arguments
20foreach ($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
30Usage: 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 -sm=on|off            Turn on or off strict math, where in strict mode, math
39 --strict-math=on|off  requires brackets. This option may default to on and then
40                       be removed in the future.
41 -su=on|off            Allow mixed units, e.g. 1px+1em or 1px*1px which have units
42 --strict-units=on|off that cannot be represented.
43 -ru, --relative-urls  re-write relative urls to the base less file.
44 -rp, --rootpath=URL   Set rootpath for url rewriting in relative imports and urls.
45                       Works with or without the relative-urls option.
46 -w, --watch           Watch input files for changes.
47
48
49EOD;
50				exit;
51			case 's':
52			case 'silent':
53				$silent = true;
54				break;
55
56			case 'w':
57			case 'watch':
58				$watch = true;
59				break;
60
61			case 'v':
62			case 'version':
63				echo "lessc " . Less_Version::version . " (less.php)\n\n";
64				exit;
65
66			case 'rp':
67			case 'rootpath':
68				$rootpath = $value;
69				break;
70
71
72			//parser options
73			case 'compress':
74				$env['compress'] = true;
75				break;
76
77			case 'ru':
78			case 'relative-urls':
79				$env['relativeUrls'] = true;
80				break;
81
82			case 'su':
83			case 'strict-units':
84				$env['strictUnits'] = ($value === 'on');
85				break;
86
87			case 'sm':
88			case 'strict-math':
89				$env['strictMath'] = ($value === 'on');
90				break;
91
92			case 'x':
93			case 'include-path':
94				$env['import_dirs'] = preg_split('#;|\:#', $value);
95				break;
96
97		}
98	}
99}
100
101if (count($argv) > 1) {
102	$output = array_pop($argv);
103	$inputs = $argv;
104}
105else {
106	$inputs = $argv;
107	$output = false;
108}
109
110if (!count($inputs)) {
111	echo("lessc: no input files\n");
112	exit;
113}
114
115if ($watch) {
116	if (!$output) {
117		echo("lessc: you must specify the output file if --watch is given\n");
118		exit;
119	}
120
121	$lastAction = 0;
122
123	echo("lessc: watching input files\n");
124
125	while (1) {
126		clearstatcache();
127
128		$updated = false;
129		foreach ($inputs as $input) {
130			if ($input == '-') {
131				if (count($inputs) == 1) {
132					echo("lessc: during watching files is not possible to watch stdin\n");
133					exit;
134				}
135				else {
136					continue;
137				}
138			}
139
140			if (filemtime($input) > $lastAction) {
141				$updated = true;
142				break;
143			}
144		}
145
146		if ($updated) {
147			$lastAction = time();
148			$parser = new Less_Parser($env);
149			foreach ($inputs as $input) {
150				try {
151					$parser->parseFile($input, $rootpath);
152				}
153				catch (Exception $e) {
154					echo("lessc: " . $e->getMessage() . " \n");
155					continue; // Invalid processing
156				}
157			}
158
159			file_put_contents($output, $parser->getCss());
160			echo("lessc: output file recompiled\n");
161		}
162
163		sleep(1);
164	}
165}
166else {
167	$parser = new Less_Parser($env);
168	foreach ($inputs as $input) {
169		if ($input == '-') {
170			$content = file_get_contents('php://stdin');
171			$parser->parse($content);
172		}
173		else {
174			try {
175				$parser->parseFile($input);
176			}
177			catch (Exception $e) {
178				if (!$silent) {
179					echo("lessc: " . ((string)$e) . " \n");
180				}
181			}
182		}
183	}
184
185	if ($output) {
186		file_put_contents($output, $parser->getCss());
187	}
188	else {
189		echo $parser->getCss();
190	}
191}
192