Skip to content

Commit

Permalink
Add top level aliases for nonstandard scripts and allow parameter pas…
Browse files Browse the repository at this point in the history
…sing, refs #2516
  • Loading branch information
Seldaek committed Jul 20, 2014
1 parent de55943 commit cec1627
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 14 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -47,5 +47,8 @@
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"scripts": {
"test": "phpunit"
}
}
7 changes: 5 additions & 2 deletions src/Composer/Command/RunScriptCommand.php
Expand Up @@ -55,6 +55,7 @@ protected function configure()
->setDescription('Run the scripts defined in composer.json.')
->setDefinition(array(
new InputArgument('script', InputArgument::REQUIRED, 'Script name to run.'),
new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
))
Expand Down Expand Up @@ -88,10 +89,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
putenv('PATH='.realpath($binDir).PATH_SEPARATOR.getenv('PATH'));
}

$args = $input->getArguments();

This comment has been minimized.

Copy link
@stof

stof Aug 13, 2014

Contributor

why not just using $input->getArgument('args') here ?


if (in_array($script, $this->commandEvents)) {
return $composer->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev'));
return $composer->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev'), $args['args']);
}

return $composer->getEventDispatcher()->dispatchScript($script, $input->getOption('dev') || !$input->getOption('no-dev'));
return $composer->getEventDispatcher()->dispatchScript($script, $input->getOption('dev') || !$input->getOption('no-dev'), $args['args']);
}
}
67 changes: 67 additions & 0 deletions src/Composer/Command/ScriptAliasCommand.php
@@ -0,0 +1,67 @@
<?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 Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ScriptAliasCommand extends Command
{
private $script;

public function __construct($script)
{
$this->script = $script;

parent::__construct();
}

protected function configure()
{
$this
->setName($this->script)
->setDescription('Run the '.$this->script.' script as defined in composer.json.')
->setDefinition(array(
new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
))
->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)
{
$composer = $this->getComposer();

// add the bin dir to the PATH to make local binaries of deps usable in scripts
$binDir = $composer->getConfig()->get('bin-dir');
if (is_dir($binDir)) {
putenv('PATH='.realpath($binDir).PATH_SEPARATOR.getenv('PATH'));
}

$args = $input->getArguments();

return $composer->getEventDispatcher()->dispatchScript($this->script, $input->getOption('dev') || !$input->getOption('no-dev'), $args['args']);
}
}
9 changes: 9 additions & 0 deletions src/Composer/Console/Application.php
Expand Up @@ -104,6 +104,15 @@ public function doRun(InputInterface $input, OutputInterface $output)
$input->setInteractive(false);
}

// add non-standard scripts as own commands
if ($composer = $this->getComposer(false)) {
foreach ($composer->getPackage()->getScripts() as $script => $dummy) {
if (!defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
$this->add(new Command\ScriptAliasCommand($script));
}
}

This comment has been minimized.

Copy link
@stof

stof Aug 13, 2014

Contributor

you should also exclude any script name matching a builtin composer command (it is currently possible to define a script install, which will then replace the composer install command for instance)

This comment has been minimized.

Copy link
@Seldaek

Seldaek Sep 5, 2014

Author Member

Thanks, both comments fixed in a8adbfe

}

if ($input->hasParameterOption('--profile')) {
$startTime = microtime(true);
$this->io->enableDebugging($startTime);
Expand Down
21 changes: 19 additions & 2 deletions src/Composer/EventDispatcher/Event.php
Expand Up @@ -24,6 +24,11 @@ class Event
*/
protected $name;

/**
* @var array Arguments passed by the user
*/
protected $args;

/**
* @var boolean Whether the event should not be passed to more listeners
*/
Expand All @@ -32,11 +37,13 @@ class Event
/**
* Constructor.
*
* @param string $name The event name
* @param string $name The event name
* @param array $events Arguments passed by the user
*/
public function __construct($name)
public function __construct($name, array $args = array())
{
$this->name = $name;
$this->args = $args;
}

/**
Expand All @@ -49,6 +56,16 @@ public function getName()
return $this->name;
}

/**
* Returns the event's arguments.
*
* @return array The event arguments
*/
public function getArguments()
{
return $this->args;
}

/**
* Checks if stopPropagation has been called
*
Expand Down
14 changes: 9 additions & 5 deletions src/Composer/EventDispatcher/EventDispatcher.php
Expand Up @@ -76,12 +76,13 @@ public function dispatch($eventName, Event $event = null)
*
* @param string $eventName The constant in ScriptEvents
* @param Script\Event $event
* @param array $additionalArgs
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
*/
public function dispatchScript($eventName, $devMode = false)
public function dispatchScript($eventName, $devMode = false, $additionalArgs = array())
{
return $this->doDispatch(new Script\Event($eventName, $this->composer, $this->io, $devMode));
return $this->doDispatch(new Script\Event($eventName, $this->composer, $this->io, $devMode, $additionalArgs));
}

/**
Expand All @@ -103,18 +104,20 @@ public function dispatchPackageEvent($eventName, $devMode, OperationInterface $o
*
* @param string $eventName The constant in ScriptEvents
* @param boolean $devMode Whether or not we are in dev mode
* @param array $additionalArgs
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
*/
public function dispatchCommandEvent($eventName, $devMode)
public function dispatchCommandEvent($eventName, $devMode, $additionalArgs = array())
{
return $this->doDispatch(new CommandEvent($eventName, $this->composer, $this->io, $devMode));
return $this->doDispatch(new CommandEvent($eventName, $this->composer, $this->io, $devMode, $additionalArgs));
}

/**
* Triggers the listeners of an event.
*
* @param Event $event The event object to pass to the event handlers/listeners.
* @param string $additionalArgs
* @return int return code of the executed script if any, for php scripts a false return
* value is changed to 1, anything else to 0
* @throws \RuntimeException
Expand Down Expand Up @@ -149,7 +152,8 @@ protected function doDispatch(Event $event)
throw $e;
}
} else {
if (0 !== ($exitCode = $this->process->execute($callable))) {
$args = implode(' ', array_map('escapeshellarg', $event->getArguments()));
if (0 !== ($exitCode = $this->process->execute($callable . ($args === '' ? '' : ' '.$args)))) {
$event->getIO()->write(sprintf('<error>Script %s handling the %s event returned with an error</error>', $callable, $event->getName()));

throw new \RuntimeException('Error Output: '.$this->process->getErrorOutput(), $exitCode);
Expand Down
5 changes: 3 additions & 2 deletions src/Composer/Plugin/CommandEvent.php
Expand Up @@ -45,10 +45,11 @@ class CommandEvent extends Event
* @param string $commandName The command name
* @param InputInterface $input
* @param OutputInterface $output
* @param array $events Arguments passed by the user
*/
public function __construct($name, $commandName, $input, $output)
public function __construct($name, $commandName, $input, $output, array $args = array())
{
parent::__construct($name);
parent::__construct($name, $args);
$this->commandName = $commandName;
$this->input = $input;
$this->output = $output;
Expand Down
8 changes: 5 additions & 3 deletions src/Composer/Script/Event.php
Expand Up @@ -14,14 +14,15 @@

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\EventDispatcher\Event as BaseEvent;

/**
* The script event class
*
* @author François Pluchino <francois.pluchino@opendisplay.com>
* @author Nils Adermann <naderman@naderman.de>
*/
class Event extends \Composer\EventDispatcher\Event
class Event extends BaseEvent
{
/**
* @var Composer The composer instance
Expand All @@ -45,10 +46,11 @@ class Event extends \Composer\EventDispatcher\Event
* @param Composer $composer The composer object
* @param IOInterface $io The IOInterface object
* @param boolean $devMode Whether or not we are in dev mode
* @param array $events Arguments passed by the user
*/
public function __construct($name, Composer $composer, IOInterface $io, $devMode = false)
public function __construct($name, Composer $composer, IOInterface $io, $devMode = false, array $args = array())
{
parent::__construct($name);
parent::__construct($name, $args);
$this->composer = $composer;
$this->io = $io;
$this->devMode = $devMode;
Expand Down

0 comments on commit cec1627

Please sign in to comment.