Parcourir la source

New build:config_doc config_doc command to build doc site.

Frederic G. MARAND il y a 5 ans
Parent
commit
8e2d68d42d

+ 29 - 0
src/BaseBuilderCommand.php

@@ -4,10 +4,17 @@ declare(strict_types = 1);
 namespace Fgm\Drupal\Composer;
 
 use Composer\Command\BaseCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
 
 abstract class BaseBuilderCommand extends BaseCommand
 {
 
+    /**
+     * The name of the argument defining the file to generate.
+     */
+    const ARG_FILE = 'file';
+
     protected function getBuilderConfig(): array
     {
         $conf = $this->getComposer()->getPackage()->getExtra()[Builder::NAME] ?? [];
@@ -19,4 +26,26 @@ abstract class BaseBuilderCommand extends BaseCommand
         $settingsPath = getcwd() . "/settings";
         return $settingsPath;
     }
+
+  /**
+   * @param \Symfony\Component\Console\Input\InputInterface $input
+   * @param \Symfony\Component\Console\Output\OutputInterface $output
+   *
+   * @return array
+   *   - string Template name
+   *   - int Error
+   */
+  public function validateTemplateName(InputInterface $input, OutputInterface $output): array {
+    $file = $input->getArgument(static::ARG_FILE);
+    $conf = $this->getBuilderConfig();
+    $templateName = $conf['templates'][$file] ?? '';
+    if (empty($templateName)) {
+        $output->writeln(sprintf(
+            'Could not build file %s: no such template in composer.json extra section',
+          $file
+        ));
+      return ["", 1];
+    }
+    return [$templateName, 0];
+  }
 }

+ 1 - 27
src/BuildSettingsCommand.php

@@ -18,11 +18,6 @@ use Twig\TemplateWrapper;
 class BuildSettingsCommand extends BaseBuilderCommand
 {
 
-    /**
-     * The name of the argument defining the file to generate.
-     */
-    const ARG_FILE = 'file';
-
     /**
      * @var string
      */
@@ -72,7 +67,7 @@ EOT
 
         $paramsPath = "${settingsPath}/merged.params.local.yml";
         $realParamsPath = realpath($paramsPath);
-        if (empty($realTemplatePath)) {
+        if (empty($realParamsPath)) {
             $output->writeln(sprintf("Could not load parameters %s: no such file", $paramsPath));
             return 3;
         }
@@ -142,25 +137,4 @@ EOT
         return 0;
     }
 
-  /**
-   * @param \Symfony\Component\Console\Input\InputInterface $input
-   * @param \Symfony\Component\Console\Output\OutputInterface $output
-   *
-   * @return array
-   *   - string Template name
-   *   - int Error
-   */
-  protected function validateTemplateName(InputInterface $input, OutputInterface $output): array {
-    $file = $input->getArgument(static::ARG_FILE);
-    $conf = $this->getBuilderConfig();
-    $templateName = $conf['templates'][$file] ?? '';
-    if (empty($templateName)) {
-      $output->writeln(sprintf(
-        'Could not build file %s: no such template in composer.json extra section',
-        $file
-      ));
-      return ["", 1];
-    }
-    return [$templateName, 0];
-  }
 }

+ 1 - 0
src/BuilderCommandProvider.php

@@ -45,6 +45,7 @@ class BuilderCommandProvider implements CommandProvider
     {
         return [
             new BuildSettingsCommand(),
+            new HugoConfigCommand(),
             new MergeParamsCommand(),
         ];
     }

+ 135 - 0
src/HugoConfigCommand.php

@@ -0,0 +1,135 @@
+<?php
+declare(strict_types = 1);
+
+namespace Fgm\Drupal\Composer;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Yaml\Exception\ParseException;
+use Symfony\Component\Yaml\Yaml;
+use Twig\Environment;
+use Twig\Extension\DebugExtension;
+use Twig\Loader\FilesystemLoader;
+use Twig\TemplateWrapper;
+
+class HugoConfigCommand extends BaseBuilderCommand
+{
+
+    /**
+     * @var string
+     */
+    protected $eventName;
+
+    /**
+     * {@inheritDoc}
+     */
+    public function configure()
+    {
+        parent::configure();
+        $this->eventName = $this->getName();
+        $this
+            ->setName('build:config_doc')
+            ->setDescription('Configure the documentation site for the current environment.')
+            ->setDefinition(
+                new InputDefinition([
+                new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
+                ])
+            )
+            ->setHelp(
+                <<<EOT
+The build:config_doc modifies the doc/config.toml file and rebuilds the documentation
+site to account for the changes.
+
+EOT
+            );
+    }
+
+    /**
+   * {@inheritDoc}
+   */
+    public function execute(InputInterface $input, OutputInterface $output)
+    {
+        [$templateName, $err] = $this->validateTemplateName($input, $output);
+        if ($err) {
+            return 1;
+        };
+
+        $settingsPath = $this->getSettingsPath();
+        $templatePath = "${settingsPath}/${templateName}";
+        $realTemplatePath = realpath($templatePath);
+        if (empty($realTemplatePath)) {
+          $output->writeln(sprintf("Could not load template %s: no such file", $templateName));
+          return 2;
+        }
+
+        $paramsPath = "${settingsPath}/merged.params.local.yml";
+        $realParamsPath = realpath($paramsPath);
+        if (empty($realParamsPath)) {
+            $output->writeln(sprintf("Could not load parameters %s: no such file", $paramsPath));
+            return 3;
+        }
+        $yaml = new Yaml();
+
+        try {
+            $params = $yaml->parseFile($realParamsPath);
+        } catch (ParseException $e) {
+            $output->writeln(sprintf("Could not parse %s: %s", $realParamsPath, $e->getMessage()));
+            return 4;
+        }
+
+        $loader = new FilesystemLoader($settingsPath, $settingsPath);
+        $twig = new Environment($loader, [
+            'auto_reload' => true,
+            'cache' => false,
+            'debug' => true,
+            'strict_variables' => true,
+        ]);
+        $twig->addExtension(new DebugExtension());
+
+        $wrapper = $twig->load($templateName);
+        $context = [
+          'base_url' => $params['instance']['doc']['base_url'],
+        ];
+        $error = $this->render($wrapper, $context, $output);
+        if ($error) {
+          $output->writeln(sprintf("Failed rendering doc configuration"));
+          return 5;
+        }
+
+        $cwd = getcwd();
+        chdir('doc');
+        $err = system("hugo -D", $exit);
+        if ($exit != 0) {
+          $output->writeln(sprintf("Failed running hugo to rebuild documentation site: %s\n", $err));
+          return 6;
+        }
+        chdir($cwd);
+    }
+
+  protected function render(TemplateWrapper $wrapper, array $context, OutputInterface $output): int
+    {
+        $configPath = "doc/config.toml";
+        if (file_exists($configPath)) {
+          $ok = unlink($configPath);
+          if (!$ok) {
+            $output->writeln(sprintf(
+              "Could not remove old %s file",
+              $configPath
+            ));
+            return 1;
+          }
+        }
+
+        $rendered = $wrapper->render($context);
+        $ok = file_put_contents($configPath, $rendered, LOCK_EX);
+        if (!$ok) {
+          $output->writeln(sprintf('Could not write new %s file', $configPath));
+          return 2;
+        }
+
+        return 0;
+    }
+
+}