2012-01-04 00:57:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Command;
|
|
|
|
|
|
|
|
use Composer\Json\JsonFile;
|
|
|
|
use Composer\Command\Helper\DialogHelper;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2012-01-08 22:01:49 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use Symfony\Component\Process\ExecutableFinder;
|
2012-01-04 00:57:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Justin Rainbow <justin.rainbow@gmail.com>
|
|
|
|
*/
|
|
|
|
class InitCommand extends Command
|
|
|
|
{
|
2012-01-08 22:01:49 +00:00
|
|
|
private $gitConfig;
|
|
|
|
|
|
|
|
public function parseAuthorString($author)
|
|
|
|
{
|
|
|
|
if (preg_match('/^(?P<name>[- \.,a-z0-9]+) <(?P<email>.+?)>$/i', $author, $match)) {
|
|
|
|
if ($match['email'] === filter_var($match['email'], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
return array(
|
|
|
|
'name' => trim($match['name']),
|
|
|
|
'email' => $match['email']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Invalid author string. Must be in the format:'.
|
|
|
|
' John Smith <john@example.com>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-01-04 00:57:31 +00:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('init')
|
|
|
|
->setDescription('Creates a basic composer.json file in current directory.')
|
|
|
|
->setDefinition(array(
|
|
|
|
new InputOption('name', null, InputOption::VALUE_NONE, 'Name of the package'),
|
|
|
|
new InputOption('description', null, InputOption::VALUE_NONE, 'Description of package'),
|
2012-01-08 22:01:49 +00:00
|
|
|
new InputOption('author', null, InputOption::VALUE_NONE, 'Author name of package'),
|
2012-01-04 00:57:31 +00:00
|
|
|
// new InputOption('version', null, InputOption::VALUE_NONE, 'Version of package'),
|
|
|
|
new InputOption('homepage', null, InputOption::VALUE_NONE, 'Homepage of package'),
|
2012-01-08 19:48:05 +00:00
|
|
|
new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'An array required packages'),
|
2012-01-04 00:57:31 +00:00
|
|
|
))
|
|
|
|
->setHelp(<<<EOT
|
2012-01-04 01:03:09 +00:00
|
|
|
The <info>init</info> command creates a basic composer.json file
|
|
|
|
in the current directory.
|
2012-01-04 00:57:31 +00:00
|
|
|
|
2012-01-04 01:03:09 +00:00
|
|
|
<info>php composer.phar init</info>
|
2012-01-04 00:57:31 +00:00
|
|
|
|
|
|
|
EOT
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$dialog = $this->getDialogHelper();
|
|
|
|
|
2012-01-08 22:01:49 +00:00
|
|
|
$whitelist = array('name', 'description', 'author', 'require');
|
2012-01-04 00:57:31 +00:00
|
|
|
|
2012-01-08 22:01:49 +00:00
|
|
|
$options = array_filter(array_intersect_key($input->getOptions(), array_flip($whitelist)));
|
|
|
|
|
|
|
|
if (isset($options['author'])) {
|
|
|
|
$options['authors'] = $this->formatAuthors($options['author']);
|
|
|
|
unset($options['author']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$options['require'] = isset($options['require']) ?
|
|
|
|
$this->formatRequirements($options['require']) :
|
|
|
|
new \stdClass;
|
2012-01-04 00:57:31 +00:00
|
|
|
|
2012-01-08 19:48:05 +00:00
|
|
|
$file = new JsonFile('composer.json');
|
2012-01-04 00:57:31 +00:00
|
|
|
|
2012-01-08 19:48:05 +00:00
|
|
|
$json = $file->encode($options);
|
2012-01-04 00:57:31 +00:00
|
|
|
|
|
|
|
if ($input->isInteractive()) {
|
|
|
|
$output->writeln(array(
|
|
|
|
'',
|
|
|
|
$json,
|
|
|
|
''
|
|
|
|
));
|
|
|
|
if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) {
|
|
|
|
$output->writeln('<error>Command aborted</error>');
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$file->write($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function interact(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2012-01-08 22:01:49 +00:00
|
|
|
$git = $this->getGitConfig();
|
|
|
|
|
2012-01-04 00:57:31 +00:00
|
|
|
$dialog = $this->getDialogHelper();
|
|
|
|
$dialog->writeSection($output, 'Welcome to the Composer config generator');
|
|
|
|
|
|
|
|
// namespace
|
|
|
|
$output->writeln(array(
|
|
|
|
'',
|
|
|
|
'This command will guide you through creating your composer.json config.',
|
|
|
|
'',
|
|
|
|
));
|
|
|
|
|
|
|
|
$cwd = realpath(".");
|
|
|
|
|
2012-01-08 22:01:49 +00:00
|
|
|
if (false === $name = $input->getOption('name')) {
|
|
|
|
$name = basename($cwd);
|
|
|
|
if (isset($git['github.user'])) {
|
|
|
|
$name = $git['github.user'] . '/' . $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 00:57:31 +00:00
|
|
|
$name = $dialog->ask(
|
|
|
|
$output,
|
|
|
|
$dialog->getQuestion('Package name', $name),
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
$input->setOption('name', $name);
|
|
|
|
|
|
|
|
$description = $input->getOption('description') ?: false;
|
|
|
|
$description = $dialog->ask(
|
|
|
|
$output,
|
|
|
|
$dialog->getQuestion('Description', $description)
|
|
|
|
);
|
|
|
|
$input->setOption('description', $description);
|
2012-01-08 19:48:05 +00:00
|
|
|
|
2012-01-08 22:01:49 +00:00
|
|
|
if (false === $author = $input->getOption('author')) {
|
|
|
|
if (isset($git['user.name']) && isset($git['user.email'])) {
|
|
|
|
$author = sprintf('%s <%s>', $git['user.name'], $git['user.email']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$self = $this;
|
|
|
|
$author = $dialog->askAndValidate(
|
|
|
|
$output,
|
|
|
|
$dialog->getQuestion('Author', $author),
|
|
|
|
function ($value) use ($self, $author) {
|
|
|
|
if (null === $value) {
|
|
|
|
return $author;
|
|
|
|
}
|
|
|
|
|
|
|
|
$author = $self->parseAuthorString($value);
|
|
|
|
|
|
|
|
return sprintf('%s <%s>', $author['name'], $author['email']);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$input->setOption('author', $author);
|
|
|
|
|
2012-01-08 19:48:05 +00:00
|
|
|
$output->writeln(array(
|
|
|
|
'',
|
|
|
|
'Define your dependencies.',
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
2012-01-08 22:01:49 +00:00
|
|
|
$requirements = array();
|
2012-01-08 19:48:05 +00:00
|
|
|
if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dependencies interactively', 'yes', '?'), true)) {
|
|
|
|
$requirements = $this->determineRequirements($input, $output);
|
|
|
|
}
|
2012-01-08 22:01:49 +00:00
|
|
|
$input->setOption('require', $requirements);
|
2012-01-04 00:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDialogHelper()
|
|
|
|
{
|
|
|
|
$dialog = $this->getHelperSet()->get('dialog');
|
|
|
|
if (!$dialog || get_class($dialog) !== 'Composer\Command\Helper\DialogHelper') {
|
|
|
|
$this->getHelperSet()->set($dialog = new DialogHelper());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dialog;
|
|
|
|
}
|
2012-01-08 19:48:05 +00:00
|
|
|
|
|
|
|
protected function findPackages($name)
|
|
|
|
{
|
|
|
|
$composer = $this->getComposer();
|
|
|
|
|
|
|
|
$packages = array();
|
|
|
|
|
|
|
|
// create local repo, this contains all packages that are installed in the local project
|
|
|
|
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
|
|
|
|
|
|
|
|
$token = strtolower($name);
|
|
|
|
foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
|
|
|
|
foreach ($repository->getPackages() as $package) {
|
|
|
|
if (false === ($pos = strpos($package->getName(), $token))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$packages[] = $package;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $packages;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function determineRequirements(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$dialog = $this->getDialogHelper();
|
|
|
|
$prompt = $dialog->getQuestion('Search for a package', false, ':');
|
|
|
|
|
|
|
|
$requires = $input->getOption('require') ?: array();
|
|
|
|
|
|
|
|
while (null !== $package = $dialog->ask($output, $prompt)) {
|
|
|
|
$matches = $this->findPackages($package);
|
|
|
|
|
|
|
|
if (count($matches)) {
|
|
|
|
$output->writeln(array(
|
|
|
|
'',
|
|
|
|
sprintf('Found <info>%s</info> packages matching <info>%s</info>', count($matches), $package),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
foreach ($matches as $position => $package) {
|
|
|
|
$output->writeln(sprintf(' <info>%5s</info> %s <comment>%s</comment>', "[$position]", $package->getPrettyName(), $package->getPrettyVersion()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->writeln('');
|
|
|
|
|
|
|
|
$validator = function ($selection) use ($matches) {
|
|
|
|
if ('' === $selection) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($matches[(int) $selection])) {
|
|
|
|
throw new \Exception('Not a valid selection');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[(int) $selection];
|
|
|
|
};
|
|
|
|
|
|
|
|
$package = $dialog->askAndValidate($output, $dialog->getQuestion('Enter package # to add', false, ':'), $validator, 3);
|
|
|
|
|
|
|
|
if (false !== $package) {
|
|
|
|
$requires[] = sprintf('%s %s', $package->getName(), $package->getPrettyVersion());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $requires;
|
|
|
|
}
|
|
|
|
|
2012-01-08 22:01:49 +00:00
|
|
|
protected function formatAuthors($author)
|
|
|
|
{
|
|
|
|
return array($this->parseAuthorString($author));
|
|
|
|
}
|
|
|
|
|
2012-01-08 19:48:05 +00:00
|
|
|
protected function formatRequirements(array $requirements)
|
|
|
|
{
|
|
|
|
$requires = array();
|
|
|
|
foreach ($requirements as $requirement) {
|
|
|
|
list($packageName, $packageVersion) = explode(" ", $requirement);
|
|
|
|
|
|
|
|
$requires[$packageName] = $packageVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return empty($requires) ? new \stdClass : $requires;
|
|
|
|
}
|
2012-01-08 22:01:49 +00:00
|
|
|
|
|
|
|
protected function getGitConfig()
|
|
|
|
{
|
|
|
|
if (null !== $this->gitConfig) {
|
|
|
|
return $this->gitConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new ExecutableFinder();
|
|
|
|
$gitBin = $finder->find('git');
|
|
|
|
|
|
|
|
$cmd = new Process(sprintf('%s config -l', $gitBin));
|
|
|
|
$cmd->run();
|
|
|
|
|
|
|
|
if ($cmd->isSuccessful()) {
|
|
|
|
return $this->gitConfig = parse_ini_string($cmd->getOutput(), false, INI_SCANNER_RAW);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->gitConfig = array();
|
|
|
|
}
|
2012-01-04 00:57:31 +00:00
|
|
|
}
|