|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Forrest\CliCommand\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Forrest\CliCommand\Search\FileCommand; |
|
6
|
|
|
use Startwind\Forrest\CliCommand\Search\PatternCommand; |
|
7
|
|
|
use Startwind\Forrest\CliCommand\Search\ToolCommand; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
|
9
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
|
|
15
|
|
|
class RunCommand extends CommandCommand |
|
16
|
|
|
{ |
|
17
|
|
|
public const NAME = 'commands:run'; |
|
18
|
|
|
|
|
19
|
|
|
protected static $defaultName = self::NAME; |
|
20
|
|
|
protected static $defaultDescription = 'Run a specific command.'; |
|
21
|
|
|
|
|
22
|
|
|
protected function configure(): void |
|
23
|
|
|
{ |
|
24
|
|
|
parent::configure(); |
|
25
|
|
|
$this->setAliases(['run']); |
|
26
|
|
|
$this->addArgument('argument', InputArgument::IS_ARRAY, 'The commands identifier.'); |
|
27
|
|
|
// $this->addArgument('identifier', InputArgument::OPTIONAL, 'The commands identifier.', false); |
|
28
|
|
|
// $this->addArgument('pattern', InputArgument::OPTIONAL, 'Small filter', false); |
|
29
|
|
|
$this->addOption('force', null, InputOption::VALUE_NONE, 'Run the command without asking for permission.'); |
|
30
|
|
|
$this->addOption('parameters', 'p', InputOption::VALUE_OPTIONAL, 'Parameters as json string. E.g: -p \'{"dir_to_search_in":".", "number_on_days":"12"}\'', "{}"); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritDoc |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function doExecute(InputInterface $input, OutputInterface $output): int |
|
37
|
|
|
{ |
|
38
|
|
|
$arguments = $input->getArgument('argument'); |
|
39
|
|
|
|
|
40
|
|
|
if (count($arguments) == 0) { |
|
41
|
|
|
$this->renderListCommand(); |
|
42
|
|
|
return SymfonyCommand::SUCCESS; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$commandName = array_pop($arguments); |
|
46
|
|
|
|
|
47
|
|
|
if (count($arguments) >= 1) { |
|
48
|
|
|
$pattern = $arguments; |
|
49
|
|
|
} else { |
|
50
|
|
|
$pattern = []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!str_contains($commandName, ':')) { |
|
54
|
|
|
$arguments = implode(' ', $input->getArgument('argument')); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$query = trim(implode(' ', $pattern) . ' ' . $commandName); |
|
57
|
|
|
|
|
58
|
|
|
if (file_exists($commandName)) { |
|
59
|
|
|
return $this->runSearchFileCommand($commandName, $pattern, $input->getOption('debug')); |
|
60
|
|
|
} elseif (!str_contains($query, ' ')) { |
|
61
|
|
|
return $this->runSearchToolCommand($query, $input->getOption('debug')); |
|
62
|
|
|
} else { |
|
63
|
|
|
$pattern[] = $commandName; |
|
64
|
|
|
return $this->runSearchPatternCommand($pattern, $input->getOption('debug')); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->enrichRepositories(); |
|
69
|
|
|
|
|
70
|
|
|
$command = $this->getRepositoryCollection()->getCommand($commandName); |
|
71
|
|
|
|
|
72
|
|
|
return $this->runCommand($command, $this->extractUserParameters($input)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The run command can also be applied to a file. This is a shortcut for the |
|
77
|
|
|
* search:file symfony console command. |
|
78
|
|
|
*/ |
|
79
|
|
|
private function runSearchFileCommand(string $filename, array $pattern, bool $debug): int |
|
80
|
|
|
{ |
|
81
|
|
|
$arguments = [ |
|
82
|
|
|
'filename' => $filename, |
|
83
|
|
|
'pattern' => $pattern, |
|
84
|
|
|
'--debug' => $debug |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
$fileArguments = new ArrayInput($arguments); |
|
88
|
|
|
$fileCommand = $this->getApplication()->find(FileCommand::COMMAND_NAME); |
|
89
|
|
|
|
|
90
|
|
|
return $fileCommand->run($fileArguments, $this->getOutput()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* The run command can also be applied to a file. This is a shortcut for the |
|
95
|
|
|
* search:file symfony console command. |
|
96
|
|
|
*/ |
|
97
|
|
|
private function runSearchToolCommand(string $tool, bool $debug): int |
|
98
|
|
|
{ |
|
99
|
|
|
$arguments = [ |
|
100
|
|
|
'tool' => $tool, |
|
101
|
|
|
'--debug' => $debug |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
$fileArguments = new ArrayInput($arguments); |
|
105
|
|
|
$fileCommand = $this->getApplication()->find(ToolCommand::COMMAND_NAME); |
|
106
|
|
|
|
|
107
|
|
|
return $fileCommand->run($fileArguments, $this->getOutput()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* The run command can also be applied to a file. This is a shortcut for the |
|
112
|
|
|
* search:file symfony console command. |
|
113
|
|
|
*/ |
|
114
|
|
|
private function runSearchPatternCommand(array $pattern, bool $debug): int |
|
115
|
|
|
{ |
|
116
|
|
|
$arguments = [ |
|
117
|
|
|
'pattern' => $pattern, |
|
118
|
|
|
'--debug' => $debug |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
$patternArguments = new ArrayInput($arguments); |
|
122
|
|
|
$command = $this->getApplication()->find(PatternCommand::COMMAND_NAME); |
|
123
|
|
|
return $command->run($patternArguments, $this->getOutput()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Return the parameters that are prefilled via the input option. |
|
128
|
|
|
*/ |
|
129
|
|
|
private function extractUserParameters(InputInterface $input): array |
|
130
|
|
|
{ |
|
131
|
|
|
return json_decode($input->getOption('parameters'), true); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|