-
Notifications
You must be signed in to change notification settings - Fork 513
/
AddCommand.php
125 lines (104 loc) · 3.75 KB
/
AddCommand.php
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
<?php
/*
* This file is part of Satis.
*
* (c) Sergey Kolodyazhnyy <sergey.kolodyazhnyy@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Satis\Console\Command;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Repository\VcsRepository;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Composer\Command\BaseCommand;
use Composer\Config;
use Composer\Json\JsonFile;
/**
* @author Sergey Kolodyazhnyy <sergey.kolodyazhnyy@gmail.com>
*/
class AddCommand extends BaseCommand
{
protected function configure()
{
$this
->setName('add')
->setDescription('Add repository URL to satis JSON file')
->setDefinition(array(
new InputArgument('url', InputArgument::REQUIRED, 'VCS repository URL'),
new InputArgument('file', InputArgument::OPTIONAL, 'JSON file to use', './satis.json')
))
->setHelp(<<<EOT
The <info>add</info> command adds given repository URL to the json file
(satis.json is used by default). You will need to run <comment>build</comment> command to
fetch updates from repository.
EOT
)
;
}
/**
* @param InputInterface $input The input instance
* @param OutputInterface $output The output instance
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var FormatterHelper $formatter */
$formatter = $this->getHelper('formatter');
$configFile = $input->getArgument('file');
$repositoryUrl = $input->getArgument('url');
if (preg_match('{^https?://}i', $configFile)) {
$output->writeln('<error>Unable to write to remote file '.$configFile.'</error>');
return 2;
}
$file = new JsonFile($configFile);
if (!$file->exists()) {
$output->writeln('<error>File not found: '.$configFile.'</error>');
return 1;
}
if (!$this->isRepositoryValid($repositoryUrl)) {
$output->writeln('<error>Invalid Repository URL: '.$repositoryUrl.'</error>');
return 3;
}
$config = $file->read();
if (!isset($config['repositories']) || !is_array($config['repositories'])) {
$config['repositories'] = array();
}
foreach ($config['repositories'] as $repository) {
if(isset($repository['url']) && $repository['url'] == $repositoryUrl) {
$output->writeln('<error>Repository already added to the file</error>');
return 4;
}
}
$config['repositories'][] = array('type' => 'vcs', 'url' => $repositoryUrl);
$file->write($config);
$output->writeln(array(
'',
$formatter->formatBlock('Your configuration file successfully updated! It\'s time to rebuild your repository', 'bg=blue;fg=white', true),
''
));
return 0;
}
/**
* Validate repository URL
*
* @param $repositoryUrl
* @return bool
*/
protected function isRepositoryValid($repositoryUrl)
{
$io = new NullIO();
$config = Factory::createConfig();
$io->loadConfiguration($config);
$repository = new VcsRepository(array('url' => $repositoryUrl), $io, $config);
if (!($driver = $repository->getDriver())) {
return false;
}
$information = $driver->getComposerInformation($driver->getRootIdentifier());
return !empty($information['name']);
}
}