RunScriptCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Script\CommandEvent;
  13. use Composer\Script\ScriptEvents;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * @author Fabien Potencier <fabien.potencier@gmail.com>
  20. */
  21. class RunScriptCommand extends Command
  22. {
  23. /**
  24. * @var array Array with command events
  25. */
  26. protected $scriptEvents = array(
  27. ScriptEvents::PRE_INSTALL_CMD,
  28. ScriptEvents::POST_INSTALL_CMD,
  29. ScriptEvents::PRE_UPDATE_CMD,
  30. ScriptEvents::POST_UPDATE_CMD,
  31. ScriptEvents::PRE_STATUS_CMD,
  32. ScriptEvents::POST_STATUS_CMD,
  33. ScriptEvents::POST_ROOT_PACKAGE_INSTALL,
  34. ScriptEvents::POST_CREATE_PROJECT_CMD,
  35. ScriptEvents::PRE_ARCHIVE_CMD,
  36. ScriptEvents::POST_ARCHIVE_CMD,
  37. ScriptEvents::PRE_AUTOLOAD_DUMP,
  38. ScriptEvents::POST_AUTOLOAD_DUMP,
  39. );
  40. protected function configure()
  41. {
  42. $this
  43. ->setName('run-script')
  44. ->setDescription('Run the scripts defined in composer.json.')
  45. ->setDefinition(array(
  46. new InputArgument('script', InputArgument::OPTIONAL, 'Script name to run.'),
  47. new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
  48. new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
  49. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
  50. new InputOption('list', 'l', InputOption::VALUE_NONE, 'List scripts.'),
  51. ))
  52. ->setHelp(<<<EOT
  53. The <info>run-script</info> command runs scripts defined in composer.json:
  54. <info>php composer.phar run-script post-update-cmd</info>
  55. EOT
  56. )
  57. ;
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output)
  60. {
  61. if ($input->getOption('list')) {
  62. return $this->listScripts();
  63. } elseif (!$input->getArgument('script')) {
  64. throw new \RunTimeException('Missing required argument "script"');
  65. }
  66. $script = $input->getArgument('script');
  67. if (!in_array($script, $this->scriptEvents)) {
  68. if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  69. throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
  70. }
  71. }
  72. $composer = $this->getComposer();
  73. $hasListeners = $composer->getEventDispatcher()->hasEventListeners(new CommandEvent($script, $composer, $this->getIO()));
  74. if (!$hasListeners) {
  75. throw new \InvalidArgumentException(sprintf('Script "%s" is not defined in this package', $script));
  76. }
  77. // add the bin dir to the PATH to make local binaries of deps usable in scripts
  78. $binDir = $composer->getConfig()->get('bin-dir');
  79. if (is_dir($binDir)) {
  80. $_SERVER['PATH'] = realpath($binDir).PATH_SEPARATOR.getenv('PATH');
  81. putenv('PATH='.$_SERVER['PATH']);
  82. }
  83. $args = $input->getArgument('args');
  84. return $composer->getEventDispatcher()->dispatchScript($script, $input->getOption('dev') || !$input->getOption('no-dev'), $args);
  85. }
  86. protected function listScripts()
  87. {
  88. $scripts = $this->getComposer()->getPackage()->getScripts();
  89. if (!count($scripts)) {
  90. return 0;
  91. }
  92. $this->getIO()->writeError('<info>scripts:</info>');
  93. foreach ($scripts as $name => $script) {
  94. $this->getIO()->write(' ' . $name);
  95. }
  96. return 0;
  97. }
  98. }