greenwich - Define compilation task
authorTim Otten <totten@civicrm.org>
Mon, 14 Sep 2020 07:17:55 +0000 (00:17 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 17 Sep 2020 02:54:06 +0000 (19:54 -0700)
.gitignore
Civi/Compile/Scss.php [new file with mode: 0644]
composer.json
ext/greenwich/scss/main.scss [new file with mode: 0644]

index 09471dd838b9c111f37076fe9d7f4321e5391943..4e197028908e5c863a3109384fdd3255c8fda977 100644 (file)
@@ -7,6 +7,7 @@
 !/ext/eventcart
 !/ext/ewaysingle
 !/ext/greenwich
+/ext/greenwich/dist
 /ext/greenwich/extern
 !/ext/search
 !/ext/financialacls
diff --git a/Civi/Compile/Scss.php b/Civi/Compile/Scss.php
new file mode 100644 (file)
index 0000000..b695a89
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+namespace Civi\Compile;
+
+class Scss {
+
+  /**
+   * Compile some SCSS file(s).
+   *
+   * NOTE: This function runs during 'composer install', which is a pre-boot
+   * environment. The composer autoloader has been configured, but no other
+   * Civi services are online.
+   *
+   * @param array $task
+   *   With keys:
+   *   - scss-includes: string[], list of paths with SCSS helper files
+   *   - scss-files: array, key-value mapping with input-files and output-files
+   *
+   * @see composer.json
+   * @link https://github.com/civicrm/composer-compile-plugin/blob/master/doc/tasks.md
+   */
+  public static function build(array $task) {
+    $scssCompiler = new \ScssPhp\ScssPhp\Compiler();
+    $includes = $task['scss-includes'] ?? [];
+    foreach ($includes as $include) {
+      $scssCompiler->addImportPath($include);
+    }
+
+    if (empty($task['scss-files'])) {
+      throw new \InvalidArgumentException("Invalid task: required argument 'scss-files' is missing");
+    }
+    foreach ($task['scss-files'] as $inputFile => $outputFile) {
+      if (!file_exists($inputFile)) {
+        throw new \InvalidArgumentException("File does not exist: " . $inputFile);
+      }
+      $inputScss = file_get_contents($inputFile);
+      $css = $scssCompiler->compile($inputScss);
+      $autoprefixer = new \Padaliyajay\PHPAutoprefixer\Autoprefixer($css);
+
+      if (!file_exists(dirname($outputFile))) {
+        mkdir(dirname($outputFile), 0777, TRUE);
+      }
+      $outputCss = $autoprefixer->compile();
+      if (!file_put_contents($outputFile, $outputCss)) {
+        throw new \RuntimeException("Failed to write file: $outputFile");
+      }
+    }
+  }
+
+}
index 39299630d47985f81812a10ec2957d721b677e6f..a8b3d45c112101501a925a8a3ca145a98ac3687d 100644 (file)
       "zetacomponents/mail": {
         "CiviCRM Custom Patches for ZetaCompoents mail": "https://raw.githubusercontent.com/civicrm/civicrm-core/9d93748a36c7c5d44422911db1c98fb2f7067b34/tools/scripts/composer/patches/civicrm-custom-patches-zetacompoents-mail.patch"
       }
-    }
+    },
+    "compile": [
+      {
+        "title": "Greenwich SCSS (<comment>ext/greenwich/dist/bootstrap3.css</comment>)",
+        "php-method": "\\Civi\\Compile\\Scss::build",
+        "watch-files": ["ext/greenwich/scss"],
+        "scss-files": {"ext/greenwich/scss/main.scss": "ext/greenwich/dist/bootstrap3.css"},
+        "scss-includes": ["ext/greenwich/scss", "ext/greenwich/extern/bootstrap3/assets/stylesheets"]
+      }
+    ]
   }
 }
diff --git a/ext/greenwich/scss/main.scss b/ext/greenwich/scss/main.scss
new file mode 100644 (file)
index 0000000..6ce355c
--- /dev/null
@@ -0,0 +1,3 @@
+#bootstrap-theme {
+  @import "bootstrap";
+}
\ No newline at end of file