BinCommand.php
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Bamarni\Composer\Bin;
use Composer\Console\Application as ComposerApplication;
use Composer\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Command\BaseCommand;
use Composer\Json\JsonFile;
class BinCommand extends BaseCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('bin')
->setDescription('Run a command inside a bin namespace')
->setDefinition(array(
new InputArgument('namespace', InputArgument::REQUIRED),
new InputArgument('args', InputArgument::REQUIRED | InputArgument::IS_ARRAY),
))
->ignoreValidationErrors()
;
}
/**
* {@inheritDoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$config = new Config($this->getComposer());
$this->resetComposers($application = $this->getApplication());
/** @var ComposerApplication $application */
if ($config->binLinksAreEnabled()) {
putenv('COMPOSER_BIN_DIR='.$this->createConfig()->get('bin-dir'));
}
$vendorRoot = $config->getTargetDirectory();
$namespace = $input->getArgument('namespace');
$input = new StringInput(preg_replace(
sprintf('/bin\s+(--ansi\s)?%s(\s.+)/', preg_quote($namespace, '/')),
'$1$2',
(string) $input,
1
));
return ('all' !== $namespace)
? $this->executeInNamespace($application, $vendorRoot.'/'.$namespace, $input, $output)
: $this->executeAllNamespaces($application, $vendorRoot, $input, $output)
;
}
/**
* @param ComposerApplication $application
* @param string $binVendorRoot
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int Exit code
*/
private function executeAllNamespaces(ComposerApplication $application, $binVendorRoot, InputInterface $input, OutputInterface $output)
{
$binRoots = glob($binVendorRoot.'/*', GLOB_ONLYDIR);
if (empty($binRoots)) {
$this->getIO()->writeError('<warning>Couldn\'t find any bin namespace.</warning>');
return 0; // Is a valid scenario: the user may not have setup any bin namespace yet
}
$originalWorkingDir = getcwd();
$exitCode = 0;
foreach ($binRoots as $binRoot) {
$exitCode += $this->executeInNamespace($application, $binRoot, $input, $output);
chdir($originalWorkingDir);
$this->resetComposers($application);
}
return min($exitCode, 255);
}
/**
* @param ComposerApplication $application
* @param string $namespace
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int Exit code
*/
private function executeInNamespace(ComposerApplication $application, $namespace, InputInterface $input, OutputInterface $output)
{
if (!file_exists($namespace)) {
mkdir($namespace, 0777, true);
}
$this->chdir($namespace);
$input = new StringInput((string) $input . ' --working-dir=.');
return $application->doRun($input, $output);
}
/**
* {@inheritDoc}
*/
public function isProxyCommand()
{
return true;
}
/**
* Resets all Composer references in the application.
*
* @param ComposerApplication $application
*/
private function resetComposers(ComposerApplication $application)
{
$application->resetComposer();
foreach ($this->getApplication()->all() as $command) {
if ($command instanceof BaseCommand) {
$command->resetComposer();
}
}
}
private function chdir($dir)
{
chdir($dir);
$this->getIO()->writeError('<info>Changed current directory to ' . $dir . '</info>');
}
private function createConfig()
{
$config = Factory::createConfig();
$file = new JsonFile(Factory::getComposerFile());
if (!$file->exists()) {
return $config;
}
$file->validateSchema(JsonFile::LAX_SCHEMA);
$config->merge($file->read());
return $config;
}
}