2 namespace Civi\Compile
;
7 * Compile some SCSS file(s).
9 * NOTE: This function runs during 'composer install', which is a pre-boot
10 * environment. The composer autoloader has been configured, but no other
11 * Civi services are online.
15 * - scss-includes: string[], list of paths with SCSS helper files
16 * - scss-files: array, key-value mapping with input-files and output-files
19 * @link https://github.com/civicrm/composer-compile-plugin/blob/master/doc/tasks.md
21 public static function build(array $task) {
22 $scssCompiler = new \ScssPhp\ScssPhp\
Compiler();
23 $includes = $task['scss-includes'] ??
[];
24 foreach ($includes as $include) {
25 $scssCompiler->addImportPath($include);
28 if (empty($task['scss-files'])) {
29 throw new \
InvalidArgumentException("Invalid task: required argument 'scss-files' is missing");
31 foreach ($task['scss-files'] as $inputFile => $outputFile) {
32 if (!file_exists($inputFile)) {
33 throw new \
InvalidArgumentException("File does not exist: " . $inputFile);
35 $inputScss = file_get_contents($inputFile);
36 $css = $scssCompiler->compile($inputScss);
37 $autoprefixer = new \Padaliyajay\PHPAutoprefixer\
Autoprefixer($css);
39 if (!file_exists(dirname($outputFile))) {
40 mkdir(dirname($outputFile), 0777, TRUE);
42 $outputCss = $autoprefixer->compile();
43 if (!file_put_contents($outputFile, $outputCss)) {
44 throw new \
RuntimeException("Failed to write file: $outputFile");