HugoConfigCommand.php 2.1 KB

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