HugoConfigCommand.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /**
  9. * Configure the Hugo-based documentation site for the project.
  10. *
  11. * @package Fgm\Drupal\Composer
  12. */
  13. class HugoConfigCommand extends BaseBuilderCommand {
  14. /**
  15. * The event triggering this command.
  16. *
  17. * @var string
  18. */
  19. protected $eventName;
  20. /**
  21. * Configures the current command.
  22. */
  23. public function configure() {
  24. parent::configure();
  25. $this->eventName = $this->getName();
  26. $this
  27. ->setName('build:config_doc')
  28. ->setDescription('Step 2 (Optional): configure the documentation site for the current environment.')
  29. ->setDefinition(
  30. new InputDefinition([
  31. new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL),
  32. ])
  33. )
  34. ->setHelp(
  35. <<<EOT
  36. The build:config_doc modifies the doc/config.toml file and rebuilds the documentation
  37. site to account for the changes.
  38. EOT
  39. );
  40. }
  41. /**
  42. * Executes the current command.
  43. *
  44. * {@inheritDoc}
  45. */
  46. public function execute(InputInterface $input, OutputInterface $output): int {
  47. [$wrapper, $params, $msg, $err] = $this->prepare($input, $output);
  48. if ($err != 0) {
  49. $output->writeln($msg);
  50. return $err;
  51. }
  52. $context = [
  53. 'base_url' => $params['instance']['doc']['base_url'],
  54. ];
  55. $destination = "doc/config.toml";
  56. [$msg, $error] = $this->render($wrapper, $context, $destination);
  57. if ($error) {
  58. $output->writeln(sprintf("Failed rendering doc configuration: %s", $msg));
  59. return 5;
  60. }
  61. $cwd = getcwd();
  62. chdir('doc');
  63. $err = system("hugo -D", $exit);
  64. if ($exit != 0) {
  65. $output->writeln(sprintf("Failed running hugo to rebuild documentation site: %s\n", $err));
  66. return 6;
  67. }
  68. chdir($cwd);
  69. return 0;
  70. }
  71. }