Skip to content

Commit 7cfb05e

Browse files
author
NiallSmyth
committed
Added options to pass through to sassc.
Detecting errors in compilation and outputting them, setting failure exit code if necessary.
1 parent e3e60ba commit 7cfb05e

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

src/CompileScssCommand.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Rhubarb\Crown\String\StringTools;
2222
use Rhubarb\Custard\Command\CustardCommand;
2323
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Input\InputOption;
2425
use Symfony\Component\Console\Output\OutputInterface;
2526

2627
class CompileScssCommand extends CustardCommand
@@ -29,6 +30,13 @@ public function __construct()
2930
{
3031
parent::__construct('compile:scss');
3132

33+
$this->addOption('style', 's', InputOption::VALUE_REQUIRED, 'Output style. Can be: nested, compressed.', 'compressed');
34+
$this->addOption('line-numbers', 'l', InputOption::VALUE_NONE, 'Emit comments showing original line numbers.');
35+
$this->addOption('import-path', 'i', InputOption::VALUE_REQUIRED, 'Set Sass import path.');
36+
$this->addOption('sourcemap', 'm', InputOption::VALUE_NONE, 'Emit source map.');
37+
$this->addOption('omit-map-comment', 'M', InputOption::VALUE_NONE, 'Omits the source map url comment.');
38+
$this->addOption('precision', 'p', InputOption::VALUE_REQUIRED, 'Set the precision for numbers.');
39+
3240
$this->addArgument('input', null, 'File or directory to compile');
3341
$this->addArgument('output', null, 'File or directory to output to');
3442
}
@@ -46,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4654

4755
if (!file_exists($inputPath)) {
4856
$this->writeNormal('The input path specified does not exist.', true);
49-
return;
57+
return 1;
5058
}
5159

5260
$multiple = false;
@@ -58,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5866

5967
if (!count($scssFiles)) {
6068
$this->writeNormal('The input directory does not contain any .css files.', true);
61-
return;
69+
return 1;
6270
}
6371
} else {
6472
$scssFiles = [$inputPath];
@@ -73,15 +81,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
7381

7482
if (!file_exists($outputPath)) {
7583
$this->writeNormal('The output path specified does not exist.', true);
76-
return;
84+
return 1;
7785
}
7886

7987
if ($multiple && !is_dir($outputPath)) {
8088
$this->writeNormal('The input path was a directory so output path must also be a directory.', true);
81-
return;
89+
return 1;
90+
}
91+
92+
return $this->compileScssFiles($scssFiles, $multiple, $outputPath);
93+
}
94+
95+
protected function getOptionsForSassC()
96+
{
97+
$allowedOptions = array_flip(['style', 'line-numbers', 'import-path', 'sourcemap', 'omit-map-comment', 'precision']);
98+
$specifiedOptions = array_intersect_key($this->input->getOptions(), $allowedOptions);
99+
100+
$options = [];
101+
foreach ($specifiedOptions as $name => $value) {
102+
if ($value === true) {
103+
$options[] = "--$name";
104+
} elseif (is_string($value)) {
105+
$options[] = "--$name $value";
106+
}
82107
}
83108

84-
$this->compileScssFiles($scssFiles, $multiple, $outputPath);
109+
return implode(' ', $options);
85110
}
86111

87112
/**
@@ -105,14 +130,17 @@ protected function scanDirectoryForScssFiles($directoryPath)
105130
* @param string[] $scssFiles
106131
* @param bool $inputIsDirectory
107132
* @param string $outputPath
133+
* @return int Status code
108134
*/
109135
protected function compileScssFiles($scssFiles, $inputIsDirectory, $outputPath)
110136
{
111-
$cssFilePath = '';
137+
$cssFilePath = $outputPath;
112138
if (!$inputIsDirectory && is_dir($outputPath)) {
113139
$cssFilePath = $outputPath . '/' . pathinfo($scssFiles[0], PATHINFO_FILENAME) . '.css';
114140
}
115141

142+
$status = 0;
143+
116144
foreach ($scssFiles as $scssFilePath) {
117145
if ($inputIsDirectory) {
118146
$cssFilePath = $outputPath . '/' . pathinfo($scssFilePath, PATHINFO_FILENAME) . '.css';
@@ -126,10 +154,26 @@ protected function compileScssFiles($scssFiles, $inputIsDirectory, $outputPath)
126154
$exe = 'sassc';
127155
}
128156

129-
exec(VENDOR_DIR . '/eslider/sasscb/dist/' . $exe . ' ' . $scssFilePath . ' ' . $cssFilePath, $cliOutput);
157+
exec(VENDOR_DIR . "/eslider/sasscb/dist/$exe {$this->getOptionsForSassC()} $scssFilePath $cssFilePath 2>&1", $cliOutput, $returnStatus);
158+
159+
$cliOutput = trim(implode("\n", $cliOutput));
160+
if ($returnStatus) {
161+
$this->writeNormal("<error>Compiling $scssFilePath failed</error>", true);
162+
if ($cliOutput) {
163+
$this->writeNormal("<comment>$cliOutput</comment>", true);
164+
}
165+
} else {
166+
if ($cliOutput) {
167+
$this->writeVerbose($cliOutput, true);
168+
}
169+
$this->writeNormal("<info>$scssFilePath compiled to $cssFilePath</info>", true);
170+
}
130171

131-
$this->writeVerbose(implode("\n", $cliOutput), true);
132-
$this->writeNormal($scssFilePath . ' compiled to ' . $cssFilePath, true);
172+
if ($returnStatus) {
173+
$status = $returnStatus;
174+
}
133175
}
176+
177+
return $status;
134178
}
135179
}

0 commit comments

Comments
 (0)