|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Forrest\CliCommand\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
|
6
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
11
|
|
|
use Symfony\Component\Console\Question\Question; |
|
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
13
|
|
|
|
|
14
|
|
|
class CreateCommand extends RepositoryCommand |
|
15
|
|
|
{ |
|
16
|
|
|
protected static $defaultName = 'repository:create'; |
|
17
|
|
|
protected static $defaultDescription = 'Creates a boilerplate for a new command repository.'; |
|
18
|
|
|
|
|
19
|
|
|
protected function configure(): void |
|
20
|
|
|
{ |
|
21
|
|
|
parent::configure(); |
|
22
|
|
|
$this->addArgument('repositoryFileName', InputArgument::REQUIRED, 'The filename of new repository.'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function doExecute(InputInterface $input, OutputInterface $output): int |
|
26
|
|
|
{ |
|
27
|
|
|
$content = []; |
|
28
|
|
|
|
|
29
|
|
|
$repositoryFileName = $input->getArgument('repositoryFileName'); |
|
30
|
|
|
|
|
31
|
|
|
$output->writeln(''); |
|
32
|
|
|
|
|
33
|
|
|
/** @var QuestionHelper $questionHelper */ |
|
34
|
|
|
$questionHelper = $this->getHelper('question'); |
|
35
|
|
|
|
|
36
|
|
|
if (file_exists($repositoryFileName)) { |
|
37
|
|
|
$overwrite = $questionHelper->ask($input, $output, new ConfirmationQuestion('File already exists. Do you want to overwrite it? [y/n] ', false)); |
|
38
|
|
|
if (!$overwrite) { |
|
39
|
|
|
$this->renderErrorBox('No repository created. File already exists.'); |
|
40
|
|
|
return SymfonyCommand::FAILURE; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$name = $questionHelper->ask($input, $output, new Question('Name of the repository [default: "local commands"]: ', 'local commands')); |
|
45
|
|
|
$identifierSuggestion = $this->getIdentifierSuggestion($name); |
|
46
|
|
|
$description = $questionHelper->ask($input, $output, new Question('Description of the repository [default: "Commands for local usage"]: ', 'Commands for local usage')); |
|
47
|
|
|
$identifier = $questionHelper->ask($input, $output, new Question('Identifier of the repository [default: "' . $identifierSuggestion . '"]: ', $identifierSuggestion)); |
|
48
|
|
|
|
|
49
|
|
|
$content['repository'] = [ |
|
50
|
|
|
'name' => $name, |
|
51
|
|
|
'description' => $description, |
|
52
|
|
|
'identifier' => $identifier |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
$content['commands'] = [ |
|
56
|
|
|
'my-unique-command-name' => [ |
|
57
|
|
|
'name' => 'foo:bar', |
|
58
|
|
|
'description' => 'Command description', |
|
59
|
|
|
'prompt' => 'ls -lah' |
|
60
|
|
|
] |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
$this->saveRepositoryFile($repositoryFileName, $content); |
|
64
|
|
|
|
|
65
|
|
|
$this->renderInfoBox('Repository file "' . $repositoryFileName . '" successfully created.'); |
|
66
|
|
|
|
|
67
|
|
|
$register = $questionHelper->ask($input, $output, new ConfirmationQuestion('Do you already want to register the repository? [y/n] ', false)); |
|
68
|
|
|
|
|
69
|
|
|
if ($register) { |
|
70
|
|
|
$this->registerRepository($identifier, $name, $description, $repositoryFileName); |
|
71
|
|
|
$this->renderInfoBox('Repository file "' . $repositoryFileName . '" successfully registered.'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return SymfonyCommand::SUCCESS; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Store the repo file but make sure the directory exists. |
|
79
|
|
|
*/ |
|
80
|
|
|
private function saveRepositoryFile(string $filename, array $content): void |
|
81
|
|
|
{ |
|
82
|
|
|
$dir = dirname($filename); |
|
83
|
|
|
if (!is_dir($dir)) { |
|
84
|
|
|
mkdir($dir, 0777, true); |
|
85
|
|
|
} |
|
86
|
|
|
file_put_contents($filename, Yaml::dump($content, 4)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|