NewThemeCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Grav\Plugin\Console;
  3. use Symfony\Component\Console\Input\InputOption;
  4. use Symfony\Component\Console\Question\ChoiceQuestion;
  5. use Symfony\Component\Console\Question\Question;
  6. require_once(__DIR__ . '/../classes/DevToolsCommand.php');
  7. /**
  8. * Class NewThemeCommand
  9. * @package Grav\Console\Cli\DevTools
  10. */
  11. class NewThemeCommand extends DevToolsCommand
  12. {
  13. /**
  14. * @return void
  15. */
  16. protected function configure(): void
  17. {
  18. $this
  19. ->setName('new-theme')
  20. ->setAliases(['newtheme'])
  21. ->addOption(
  22. 'name',
  23. null,
  24. InputOption::VALUE_OPTIONAL,
  25. 'The name of your new Grav theme'
  26. )
  27. ->addOption(
  28. 'desc',
  29. null,
  30. InputOption::VALUE_OPTIONAL,
  31. 'A description of your new Grav theme'
  32. )
  33. ->addOption(
  34. 'dev',
  35. null,
  36. InputOption::VALUE_OPTIONAL,
  37. 'The name/username of the developer'
  38. )
  39. ->addOption(
  40. 'github',
  41. null,
  42. InputOption::VALUE_OPTIONAL,
  43. 'The developer\'s GitHub ID'
  44. )
  45. ->addOption(
  46. 'email',
  47. null,
  48. InputOption::VALUE_OPTIONAL,
  49. 'The developer\'s email'
  50. )
  51. ->addOption(
  52. 'offline',
  53. 'o',
  54. InputOption::VALUE_NONE,
  55. 'Skip online name collision check'
  56. )
  57. ->setDescription('Creates a new Grav theme with the basic required files')
  58. ->setHelp('The <info>new-theme</info> command creates a new Grav instance and performs the creation of a theme.');
  59. }
  60. /**
  61. * @return int
  62. */
  63. protected function serve(): int
  64. {
  65. $this->init();
  66. $input = $this->getInput();
  67. $io = $this->getIO();
  68. $this->component['type'] = 'theme';
  69. $this->component['template'] = 'blank';
  70. $this->component['version'] = '0.1.0';
  71. $this->options = [
  72. 'name' => $input->getOption('name'),
  73. 'description' => $input->getOption('desc'),
  74. 'author' => [
  75. 'name' => $input->getOption('dev'),
  76. 'email' => $input->getOption('email'),
  77. 'githubid' => $input->getOption('github'),
  78. ],
  79. 'offline' => $input->getOption('offline'),
  80. ];
  81. $this->validateOptions();
  82. $this->component = array_replace($this->component, $this->options);
  83. if (!$this->options['name']) {
  84. $question = new Question('Enter <yellow>Theme Name</yellow>');
  85. $question->setValidator(function ($value) {
  86. return $this->validate('name', $value);
  87. });
  88. $this->component['name'] = $io->askQuestion($question);
  89. }
  90. if (!$this->options['description']) {
  91. $question = new Question('Enter <yellow>Theme Description</yellow>');
  92. $question->setValidator(function ($value) {
  93. return $this->validate('description', $value);
  94. });
  95. $this->component['description'] = $io->askQuestion($question);
  96. }
  97. if (!$this->options['author']['name']) {
  98. $question = new Question('Enter <yellow>Developer Name</yellow>');
  99. $question->setValidator(function ($value) {
  100. return $this->validate('developer', $value);
  101. });
  102. $this->component['author']['name'] = $io->askQuestion($question);
  103. }
  104. if (!$this->options['author']['githubid']) {
  105. $question = new Question('Enter <yellow>GitHub ID</yellow> (can be blank)');
  106. $question->setValidator(function ($value) {
  107. return $this->validate('githubid', $value);
  108. });
  109. $this->component['author']['githubid'] = $io->askQuestion($question);
  110. }
  111. if (!$this->options['author']['email']) {
  112. $question = new Question('Enter <yellow>Developer Email</yellow>');
  113. $question->setValidator(function ($value) {
  114. return $this->validate('email', $value);
  115. });
  116. $this->component['author']['email'] = $io->askQuestion($question);
  117. }
  118. $question = new ChoiceQuestion(
  119. 'Please choose an option',
  120. ['pure-blank' => 'Basic Theme using Pure.css', 'tailwind' => 'Basic Theme using tailwind.css', 'inheritance' => 'Inherit from another theme', 'copy' => 'Copy another theme']
  121. );
  122. $this->component['template'] = $io->askQuestion($question);
  123. if ($this->component['template'] === 'inheritance') {
  124. $themes = $this->gpm->getInstalledThemes();
  125. $installedThemes = [];
  126. foreach ($themes as $key => $theme) {
  127. $installedThemes[] = $key;
  128. }
  129. $question = new ChoiceQuestion('Please choose a theme to extend', $installedThemes);
  130. $this->component['extends'] = $io->askQuestion($question);
  131. } elseif ($this->component['template'] === 'copy') {
  132. $themes = $this->gpm->getInstalledThemes();
  133. $installedThemes = [];
  134. foreach ($themes as $key => $theme) {
  135. $installedThemes[] = $key;
  136. }
  137. $question = new ChoiceQuestion(
  138. 'Please choose a theme to copy',
  139. $installedThemes
  140. );
  141. $this->component['copy'] = $io->askQuestion($question);
  142. }
  143. $this->createComponent();
  144. return 0;
  145. }
  146. }