|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the slince/composer-alias package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Slince <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Slince\ComposerAlias\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Composer\Command\BaseCommand; |
|
14
|
|
|
use Slince\ComposerAlias\ComposerAlias; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
20
|
|
|
use Symfony\Component\Console\Question\Question; |
|
21
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
22
|
|
|
|
|
23
|
|
|
class AliasCommand extends BaseCommand |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ComposerAlias |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $composerAlias; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function configure() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->setName('alias') |
|
36
|
|
|
->addArgument('alias', InputArgument::OPTIONAL, 'The command alias you want') |
|
37
|
|
|
->addArgument('raw-command', InputArgument::OPTIONAL, 'The long command') |
|
38
|
|
|
->addOption('list', 'l', InputOption::VALUE_NONE, 'Display all alias') |
|
39
|
|
|
->addOption('unset', 'u', InputOption::VALUE_NONE, 'Remove an alias') |
|
40
|
|
|
->setDescription('Sets the alias for some commands'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(ComposerAlias $composerAlias) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->composerAlias = $composerAlias; |
|
46
|
|
|
parent::__construct(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
53
|
|
|
{ |
|
54
|
|
|
$alias = $input->getArgument('alias'); |
|
55
|
|
|
$rawCommand = $input->getArgument('raw-command'); |
|
56
|
|
|
if ($input->getOption('list')) { |
|
57
|
|
|
$this->showAliases($input, $output); |
|
58
|
|
|
|
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
if ($alias && !$rawCommand) { |
|
62
|
|
|
$rawCommand = $this->composerAlias->getAliases()->get($alias); |
|
63
|
|
|
if (null === $rawCommand) { |
|
64
|
|
|
throw new \InvalidArgumentException(sprintf('The alias "%s" is not found.', $alias)); |
|
65
|
|
|
} |
|
66
|
|
|
if ($input->getOption('unset')) { |
|
67
|
|
|
$this->composerAlias->getAliases()->remove($alias); |
|
68
|
|
|
$output->writeln('Remove OK!'); |
|
69
|
|
|
|
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
$this->showAliases($input, $output, [$alias => $rawCommand]); |
|
73
|
|
|
|
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
if (!$alias) { |
|
77
|
|
|
list($alias, $rawCommand) = $this->askAliasAndCommand($input, $output); |
|
78
|
|
|
} |
|
79
|
|
|
$this->composerAlias->getAliases()->add($alias, $rawCommand); |
|
80
|
|
|
$output->writeln('<info>Write OK!</info>'); |
|
81
|
|
|
|
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function askAliasAndCommand($input, $output) |
|
86
|
|
|
{ |
|
87
|
|
|
$helper = $this->getHelper('question'); |
|
88
|
|
|
$question = new Question('Enter an alias: '); |
|
89
|
|
|
$question->setValidator(function($answer) use ($helper, $input, $output){ |
|
90
|
|
|
if (preg_match('#\s#', $answer)) { |
|
91
|
|
|
throw new \InvalidArgumentException('alias can not have blank characters'); |
|
92
|
|
|
} |
|
93
|
|
|
if ($this->composerAlias->getAliases()->has($answer)) { |
|
94
|
|
|
$question = new ConfirmationQuestion(sprintf('The alias "%s" already exists, override it(y/N)?', $answer), false); |
|
95
|
|
|
if (!$helper->ask($input, $output, $question)) { |
|
96
|
|
|
throw new \Exception(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $answer; |
|
101
|
|
|
}); |
|
102
|
|
|
$alias = $helper->ask($input, $output, $question); |
|
103
|
|
|
$question = new Question('Enter raw command: '); |
|
104
|
|
|
$rawCommand = $helper->ask($input, $output, $question); |
|
105
|
|
|
|
|
106
|
|
|
return [$alias, $rawCommand]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function showAliases($input, $output, $aliases = []) |
|
110
|
|
|
{ |
|
111
|
|
|
$symfonyStyle = new SymfonyStyle($input, $output); |
|
112
|
|
|
$rows = []; |
|
113
|
|
|
foreach ($aliases ?: $this->composerAlias->getAliases() as $alias => $rawCommand) { |
|
114
|
|
|
$rows[] = ["<info>{$alias}</info>", $rawCommand]; |
|
115
|
|
|
} |
|
116
|
|
|
$symfonyStyle->table(['Alias', 'Command'], $rows); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getAliases() |
|
123
|
|
|
{ |
|
124
|
|
|
return [ |
|
125
|
|
|
'set-alias', |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|