HugoConfigCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Fgm\Drupal\Composer;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputDefinition;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Yaml\Exception\ParseException;
  9. use Symfony\Component\Yaml\Yaml;
  10. use Twig\Environment;
  11. use Twig\Extension\DebugExtension;
  12. use Twig\Loader\FilesystemLoader;
  13. use Twig\TemplateWrapper;
  14. class HugoConfigCommand extends BaseBuilderCommand
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $eventName;
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function configure()
  24. {
  25. parent::configure();
  26. $this->eventName = $this->getName();
  27. $this
  28. ->setName('build:config_doc')
  29. ->setDescription('Step 2 (Optional): configure the documentation site for the current environment.')
  30. ->setDefinition(
  31. new InputDefinition([
  32. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
  33. ])
  34. )
  35. ->setHelp(
  36. <<<EOT
  37. The build:config_doc modifies the doc/config.toml file and rebuilds the documentation
  38. site to account for the changes.
  39. EOT
  40. );
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. [$templateName, $err] = $this->validateTemplateName($input, $output);
  48. if ($err) {
  49. return 1;
  50. };
  51. $settingsPath = $this->getSettingsPath();
  52. $templatePath = "${settingsPath}/${templateName}";
  53. $realTemplatePath = realpath($templatePath);
  54. if (empty($realTemplatePath)) {
  55. $output->writeln(sprintf("Could not load template %s: no such file", $templateName));
  56. return 2;
  57. }
  58. $paramsPath = "${settingsPath}/merged.params.local.yml";
  59. $realParamsPath = realpath($paramsPath);
  60. if (empty($realParamsPath)) {
  61. $output->writeln(sprintf("Could not load parameters %s: no such file", $paramsPath));
  62. return 3;
  63. }
  64. $yaml = new Yaml();
  65. try {
  66. $params = $yaml->parseFile($realParamsPath);
  67. } catch (ParseException $e) {
  68. $output->writeln(sprintf("Could not parse %s: %s", $realParamsPath, $e->getMessage()));
  69. return 4;
  70. }
  71. $loader = new FilesystemLoader($settingsPath, $settingsPath);
  72. $twig = new Environment($loader, [
  73. 'auto_reload' => true,
  74. 'cache' => false,
  75. 'debug' => true,
  76. 'strict_variables' => true,
  77. ]);
  78. $twig->addExtension(new DebugExtension());
  79. $wrapper = $twig->load($templateName);
  80. $context = [
  81. 'base_url' => $params['instance']['doc']['base_url'],
  82. ];
  83. $error = $this->render($wrapper, $context, $output);
  84. if ($error) {
  85. $output->writeln(sprintf("Failed rendering doc configuration"));
  86. return 5;
  87. }
  88. $cwd = getcwd();
  89. chdir('doc');
  90. $err = system("hugo -D", $exit);
  91. if ($exit != 0) {
  92. $output->writeln(sprintf("Failed running hugo to rebuild documentation site: %s\n", $err));
  93. return 6;
  94. }
  95. chdir($cwd);
  96. }
  97. protected function render(TemplateWrapper $wrapper, array $context, OutputInterface $output): int
  98. {
  99. $configPath = "doc/config.toml";
  100. if (file_exists($configPath)) {
  101. $ok = unlink($configPath);
  102. if (!$ok) {
  103. $output->writeln(sprintf(
  104. "Could not remove old %s file",
  105. $configPath
  106. ));
  107. return 1;
  108. }
  109. }
  110. $rendered = $wrapper->render($context);
  111. $ok = file_put_contents($configPath, $rendered, LOCK_EX);
  112. if (!$ok) {
  113. $output->writeln(sprintf('Could not write new %s file', $configPath));
  114. return 2;
  115. }
  116. return 0;
  117. }
  118. }