Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / api / scssphp / scssphp / bin / pscss
1 #!/usr/bin/env php
2 <?php
3
4 /**
5 * SCSSPHP
6 *
7 * @copyright 2012-2020 Leaf Corcoran
8 *
9 * @license http://opensource.org/licenses/MIT MIT
10 *
11 * @link http://scssphp.github.io/scssphp
12 */
13
14 error_reporting(E_ALL);
15
16 if (version_compare(PHP_VERSION, '5.6') < 0) {
17 die('Requires PHP 5.6 or above');
18 }
19
20 include __DIR__ . '/../scss.inc.php';
21
22 use ScssPhp\ScssPhp\Compiler;
23 use ScssPhp\ScssPhp\Parser;
24 use ScssPhp\ScssPhp\Version;
25
26 $style = null;
27 $loadPaths = null;
28 $dumpTree = false;
29 $inputFile = null;
30 $changeDir = false;
31 $encoding = false;
32 $sourceMap = false;
33
34 /**
35 * Parse argument
36 *
37 * @param integer $i
38 * @param array $options
39 *
40 * @return string|null
41 */
42 function parseArgument(&$i, $options) {
43 global $argc;
44 global $argv;
45
46 if (! preg_match('/^(?:' . implode('|', (array) $options) . ')=?(.*)/', $argv[$i], $matches)) {
47 return;
48 }
49
50 if (strlen($matches[1])) {
51 return $matches[1];
52 }
53
54 if ($i + 1 < $argc) {
55 $i++;
56
57 return $argv[$i];
58 }
59 }
60
61 for ($i = 1; $i < $argc; $i++) {
62 if ($argv[$i] === '-?' || $argv[$i] === '-h' || $argv[$i] === '--help') {
63 $exe = $argv[0];
64
65 $HELP = <<<EOT
66 Usage: $exe [options] [input-file]
67
68 Options include:
69
70 --help Show this message [-h, -?]
71 --continue-on-error [deprecated] Ignored
72 --debug-info [deprecated] Ignored [-g]
73 --dump-tree Dump formatted parse tree [-T]
74 --iso8859-1 Use iso8859-1 encoding instead of default utf-8
75 --line-numbers [deprecated] Ignored [--line-comments]
76 --load-path=PATH Set import path [-I]
77 --precision=N [deprecated] Ignored. (default 10) [-p]
78 --sourcemap Create source map file
79 --style=FORMAT Set the output format (compact, compressed, crunched, expanded, or nested) [-s, -t]
80 --version Print the version [-v]
81
82 EOT;
83 exit($HELP);
84 }
85
86 if ($argv[$i] === '-v' || $argv[$i] === '--version') {
87 exit(Version::VERSION . "\n");
88 }
89
90 // Keep parsing --continue-on-error to avoid BC breaks for scripts using it
91 if ($argv[$i] === '--continue-on-error') {
92 // TODO report it as a warning ?
93 continue;
94 }
95
96 // Keep parsing it to avoid BC breaks for scripts using it
97 if ($argv[$i] === '-g' || $argv[$i] === '--debug-info') {
98 // TODO report it as a warning ?
99 continue;
100 }
101
102 if ($argv[$i] === '--iso8859-1') {
103 $encoding = 'iso8859-1';
104 continue;
105 }
106
107 // Keep parsing it to avoid BC breaks for scripts using it
108 if ($argv[$i] === '--line-numbers' || $argv[$i] === '--line-comments') {
109 // TODO report it as a warning ?
110 continue;
111 }
112
113 if ($argv[$i] === '--sourcemap') {
114 $sourceMap = true;
115 continue;
116 }
117
118 if ($argv[$i] === '-T' || $argv[$i] === '--dump-tree') {
119 $dumpTree = true;
120 continue;
121 }
122
123 $value = parseArgument($i, array('-t', '-s', '--style'));
124
125 if (isset($value)) {
126 $style = $value;
127 continue;
128 }
129
130 $value = parseArgument($i, array('-I', '--load-path'));
131
132 if (isset($value)) {
133 $loadPaths = $value;
134 continue;
135 }
136
137 // Keep parsing --precision to avoid BC breaks for scripts using it
138 $value = parseArgument($i, array('-p', '--precision'));
139
140 if (isset($value)) {
141 // TODO report it as a warning ?
142 continue;
143 }
144
145 if (file_exists($argv[$i])) {
146 $inputFile = $argv[$i];
147 continue;
148 }
149 }
150
151
152 if ($inputFile) {
153 $data = file_get_contents($inputFile);
154
155 $newWorkingDir = dirname(realpath($inputFile));
156 $oldWorkingDir = getcwd();
157
158 if ($oldWorkingDir !== $newWorkingDir) {
159 $changeDir = chdir($newWorkingDir);
160 $inputFile = basename($inputFile);
161 }
162 } else {
163 $data = '';
164
165 while (! feof(STDIN)) {
166 $data .= fread(STDIN, 8192);
167 }
168 }
169
170 if ($dumpTree) {
171 $parser = new Parser($inputFile);
172
173 print_r(json_decode(json_encode($parser->parse($data)), true));
174
175 exit();
176 }
177
178 $scss = new Compiler();
179
180 if ($loadPaths) {
181 $scss->setImportPaths(explode(PATH_SEPARATOR, $loadPaths));
182 }
183
184 if ($style) {
185 $scss->setFormatter('ScssPhp\\ScssPhp\\Formatter\\' . ucfirst($style));
186 }
187
188 if ($sourceMap) {
189 $scss->setSourceMap(Compiler::SOURCE_MAP_INLINE);
190 }
191
192 if ($encoding) {
193 $scss->setEncoding($encoding);
194 }
195
196 echo $scss->compile($data, $inputFile);
197
198 if ($changeDir) {
199 chdir($oldWorkingDir);
200 }