2013-04-15 13:20:00 +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;
|
|
|
|
|
2014-07-20 17:46:51 +00:00
|
|
|
use Composer\Script\CommandEvent;
|
2013-04-15 13:20:00 +00:00
|
|
|
use Composer\Script\ScriptEvents;
|
2016-02-15 19:47:43 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2013-04-15 13:20:00 +00:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fabien Potencier <fabien.potencier@gmail.com>
|
|
|
|
*/
|
2016-02-19 22:56:46 +00:00
|
|
|
class RunScriptCommand extends BaseCommand
|
2013-04-15 13:20:00 +00:00
|
|
|
{
|
2014-02-19 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* @var array Array with command events
|
|
|
|
*/
|
2015-02-23 15:31:54 +00:00
|
|
|
protected $scriptEvents = array(
|
2014-02-19 12:38:51 +00:00
|
|
|
ScriptEvents::PRE_INSTALL_CMD,
|
|
|
|
ScriptEvents::POST_INSTALL_CMD,
|
|
|
|
ScriptEvents::PRE_UPDATE_CMD,
|
|
|
|
ScriptEvents::POST_UPDATE_CMD,
|
|
|
|
ScriptEvents::PRE_STATUS_CMD,
|
|
|
|
ScriptEvents::POST_STATUS_CMD,
|
|
|
|
ScriptEvents::POST_ROOT_PACKAGE_INSTALL,
|
2015-02-23 15:31:54 +00:00
|
|
|
ScriptEvents::POST_CREATE_PROJECT_CMD,
|
2014-02-19 12:38:51 +00:00
|
|
|
ScriptEvents::PRE_ARCHIVE_CMD,
|
|
|
|
ScriptEvents::POST_ARCHIVE_CMD,
|
|
|
|
ScriptEvents::PRE_AUTOLOAD_DUMP,
|
2015-02-23 15:31:54 +00:00
|
|
|
ScriptEvents::POST_AUTOLOAD_DUMP,
|
2014-02-19 12:38:51 +00:00
|
|
|
);
|
|
|
|
|
2013-04-15 13:20:00 +00:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('run-script')
|
|
|
|
->setDescription('Run the scripts defined in composer.json.')
|
|
|
|
->setDefinition(array(
|
2015-02-03 12:44:42 +00:00
|
|
|
new InputArgument('script', InputArgument::OPTIONAL, 'Script name to run.'),
|
2014-07-20 19:55:24 +00:00
|
|
|
new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
|
2016-02-15 19:47:43 +00:00
|
|
|
new InputOption('timeout', null, InputOption::VALUE_REQUIRED, 'Sets script timeout in seconds, or 0 for never.'),
|
2013-04-15 13:20:00 +00:00
|
|
|
new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
|
|
|
|
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
|
2015-02-03 12:44:42 +00:00
|
|
|
new InputOption('list', 'l', InputOption::VALUE_NONE, 'List scripts.'),
|
2013-04-15 13:20:00 +00:00
|
|
|
))
|
|
|
|
->setHelp(<<<EOT
|
|
|
|
The <info>run-script</info> command runs scripts defined in composer.json:
|
|
|
|
|
|
|
|
<info>php composer.phar run-script post-update-cmd</info>
|
|
|
|
EOT
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2015-02-03 12:44:42 +00:00
|
|
|
if ($input->getOption('list')) {
|
2015-02-06 12:52:44 +00:00
|
|
|
return $this->listScripts();
|
2015-02-03 12:44:42 +00:00
|
|
|
} elseif (!$input->getArgument('script')) {
|
2016-04-06 22:52:50 +00:00
|
|
|
throw new \RuntimeException('Missing required argument "script"');
|
2015-02-03 12:44:42 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 13:20:00 +00:00
|
|
|
$script = $input->getArgument('script');
|
2015-02-23 15:31:54 +00:00
|
|
|
if (!in_array($script, $this->scriptEvents)) {
|
2013-04-15 13:20:00 +00:00
|
|
|
if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 17:46:51 +00:00
|
|
|
$composer = $this->getComposer();
|
|
|
|
$hasListeners = $composer->getEventDispatcher()->hasEventListeners(new CommandEvent($script, $composer, $this->getIO()));
|
|
|
|
if (!$hasListeners) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Script "%s" is not defined in this package', $script));
|
|
|
|
}
|
2013-04-15 13:20:00 +00:00
|
|
|
|
2014-09-05 16:28:50 +00:00
|
|
|
$args = $input->getArgument('args');
|
2014-07-20 19:55:24 +00:00
|
|
|
|
2016-02-15 19:47:43 +00:00
|
|
|
if (!is_null($timeout = $input->getOption('timeout'))) {
|
|
|
|
if (!ctype_digit($timeout)) {
|
|
|
|
throw new \RuntimeException('Timeout value must be numeric and positive if defined, or 0 for forever');
|
|
|
|
}
|
|
|
|
// Override global timeout set before in Composer by environment or config
|
2016-02-25 10:04:44 +00:00
|
|
|
ProcessExecutor::setTimeout((int) $timeout);
|
2016-02-15 19:47:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 16:28:50 +00:00
|
|
|
return $composer->getEventDispatcher()->dispatchScript($script, $input->getOption('dev') || !$input->getOption('no-dev'), $args);
|
2013-04-15 13:20:00 +00:00
|
|
|
}
|
2015-02-03 12:44:42 +00:00
|
|
|
|
2015-02-06 12:52:44 +00:00
|
|
|
protected function listScripts()
|
2015-02-03 12:44:42 +00:00
|
|
|
{
|
|
|
|
$scripts = $this->getComposer()->getPackage()->getScripts();
|
|
|
|
|
|
|
|
if (!count($scripts)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:30:09 +00:00
|
|
|
$io = $this->getIO();
|
|
|
|
$io->writeError('<info>scripts:</info>');
|
2015-02-03 12:44:42 +00:00
|
|
|
foreach ($scripts as $name => $script) {
|
2015-09-13 21:30:09 +00:00
|
|
|
$io->write(' ' . $name);
|
2015-02-03 12:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-15 13:20:00 +00:00
|
|
|
}
|