|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TaskRunner\Scss\TaskRunner\Commands; |
| 6 | + |
| 7 | +use Consolidation\AnnotatedCommand\CommandData; |
| 8 | +use OpenEuropa\TaskRunner\Commands\AbstractCommands; |
| 9 | +use Robo\Collection\CollectionBuilder; |
| 10 | +use Robo\Task\Assets\loadTasks; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | + |
| 13 | +/** |
| 14 | + * Command wrapper for the "taskScss" task that is included in Robo. |
| 15 | + * |
| 16 | + * @see \Robo\Task\Assets\loadTasks |
| 17 | + */ |
| 18 | +class ScssCommands extends AbstractCommands |
| 19 | +{ |
| 20 | + use loadTasks; |
| 21 | + |
| 22 | + /** |
| 23 | + * List of formatters that is offered by the ScssPhp compiler. |
| 24 | + * |
| 25 | + * @see \ScssPhp\ScssPhp\Formatter |
| 26 | + */ |
| 27 | + protected const SCSS_FORMATTERS = ['compact', 'compressed', 'crunched', 'expanded', 'nested']; |
| 28 | + |
| 29 | + /** |
| 30 | + * Compile SCSS to CSS. |
| 31 | + * |
| 32 | + * @command assets:compile-scss |
| 33 | + * |
| 34 | + * @param string $input_file The path to the SCSS file to process |
| 35 | + * @param string $output_file The path where to store the compiled CSS file |
| 36 | + * @option style Set the output format (compact, compressed, crunched, expanded, or nested) |
| 37 | + * @option import-dir Set an import path |
| 38 | + * |
| 39 | + * @param array $options |
| 40 | + * |
| 41 | + * @return \Robo\Collection\CollectionBuilder |
| 42 | + */ |
| 43 | + public function compileScss(string $input_file, string $output_file, array $options = [ |
| 44 | + 'style' => InputOption::VALUE_REQUIRED, |
| 45 | + 'import-dir' => [], |
| 46 | + ]): CollectionBuilder |
| 47 | + { |
| 48 | + $scss = $this->taskScss([$input_file => $output_file]); |
| 49 | + |
| 50 | + if ($options['style']) { |
| 51 | + $scss->setFormatter('ScssPhp\\ScssPhp\\Formatter\\' . ucfirst($options['style'])); |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($options['import-dir'] as $import_dir) { |
| 55 | + $scss->addImportPath($import_dir); |
| 56 | + } |
| 57 | + |
| 58 | + return $this->collectionBuilder()->addTask($scss); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @hook pre-validate assets:compile-scss |
| 63 | + */ |
| 64 | + public function preValidateCompileScss(CommandData $commandData): void |
| 65 | + { |
| 66 | + $input = $commandData->input(); |
| 67 | + $style = $input->getOption('style'); |
| 68 | + if ($style) { |
| 69 | + // Ensure case insensitive matching for the style option. |
| 70 | + $input->setOption('style', strtolower($style)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @hook validate assets:compile-scss |
| 76 | + */ |
| 77 | + public function validateCompileScss(CommandData $commandData): void |
| 78 | + { |
| 79 | + $input = $commandData->input(); |
| 80 | + $input_file = $input->getArgument('input_file'); |
| 81 | + if (!is_file($input_file) || !is_readable($input_file)) { |
| 82 | + throw new \Exception(sprintf('Input file "%s" does not exist or is not readable', $input_file)); |
| 83 | + } |
| 84 | + |
| 85 | + $style = $input->getOption('style'); |
| 86 | + if ($style && !in_array($style, self::SCSS_FORMATTERS)) { |
| 87 | + throw new \Exception(sprintf('Unknown style "%s"', $style)); |
| 88 | + } |
| 89 | + |
| 90 | + $import_dirs = $input->getOption('import-dir'); |
| 91 | + foreach ($import_dirs as $import_dir) { |
| 92 | + if (!is_dir($import_dir) || !is_readable($import_dir)) { |
| 93 | + throw new \Exception(sprintf('Import dir "%s" does not exist or is not readable', $import_dir)); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments