forked from leafo/lessphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plessc
executable file
·155 lines (132 loc) · 2.88 KB
/
plessc
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
#!/usr/bin/php -q
<?php
//
// command line utility to compile less to stdout
// leaf corcoran <leafo.net>
$VERSION = "v0.1.6";
error_reporting(E_ALL);
$path = realpath(dirname(__FILE__)).'/';
require $path."lessc.inc.php";
$fa = "Fatal Error: ";
function err($msg) {
fwrite(STDERR, $msg."\n");
}
if (php_sapi_name() != "cli") {
err($fa.$argv[0]." must be run in the command line.");
exit(1);
}
$exe = array_shift($argv); // remove filename
function process($data, $import = null) {
global $fa;
$l = new lessc();
if ($import) $l->importDir = $import;
try {
echo $l->parse($data);
exit(0);
} catch (exception $ex) {
err($fa."\n".str_repeat('=', 20)."\n".
$ex->getMessage());
exit(1);
}
}
// process args
$opts = array();
foreach ($argv as $loc => $a) {
if (preg_match("/^-([a-zA-Z]+)$/", $a, $m)) {
$m = $m[1];
for ($i = 0; $i < strlen($m); $i++)
$opts[$m{$i}] = $loc;
unset($argv[$loc]);
}
}
function has($o, &$loc = null)
{
global $opts;
if (!isset($opts[$o])) return false;
$loc = $opts[$o];
return true;
}
function hasValue($o, &$value = null)
{
global $argv;
if (!has($o,$loc)) return false;
if (!isset($argv[$loc+1])) return false;
$value = $argv[$loc+1];
return true;
}
if (has("v")) {
exit($VERSION."\n");
}
if (has("r", $loc)) {
if (!hasValue("r", $data)) {
while (!feof(STDIN)) {
$data .= fread(STDIN, 8192);
}
}
return process($data);
}
if (has("w")) {
// need two files
if (!is_file($in = array_shift($argv)) ||
null == $out = array_shift($argv))
{
err($fa.$exe." -w infile outfile");
exit(1);
}
echo "Watching ".$in.
(has("n") ? ' with notifications' : '').
", press Ctrl + c to exit.\n";
$fail_time = 0;
// $l = new lessc($in);
$cache = $in;
$last_build = 0;
while (1) {
// check if anything has changed since last fail
$updated = false;
if (is_array($cache)) {
foreach ($cache['files'] as $fname=>$_) {
if (filemtime($fname) > $fail_time) {
$updated = true;
break;
}
}
} else $updated = true;
// try to compile it
if ($updated) try {
$cache = lessc::cexecute($cache);
if ($cache['updated'] > $last_build) {
$last_build = $cache['updated'];
echo "Writing updated file: ".$out."\n";
if (!file_put_contents($out, $cache['compiled'])) {
err($fa."Could not write to file ".$out);
exit(1);
}
}
} catch (exception $ex) {
echo "\nFatal Error:\n".str_repeat('=', 20)."\n".$ex->getMessage()."\n\n";
$fail_time = time();
if (has("n")) {
`notify-send -u critical "compile failed" "{$ex->getMessage()}"`;
}
}
sleep(1);
}
exit(0);
}
if (!$fname = array_shift($argv)) {
echo "Usage: ".$exe." input-file [output-file]\n";
exit(1);
}
try {
$l = new lessc($fname);
$out = $l->parse();
if (!$fout = array_shift($argv)) {
echo $out;
} else {
file_put_contents($fout, $out);
}
} catch (exception $ex) {
err($fa.$ex->getMessage());
exit(1);
}
?>