NewPluginCommand.php 4.4 KB

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