RunScriptCommand.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Event as ScriptEvent;
  13. use Composer\Script\ScriptEvents;
  14. use Composer\Util\ProcessExecutor;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Helper\Table;
  20. /**
  21. * @author Fabien Potencier <fabien.potencier@gmail.com>
  22. */
  23. class RunScriptCommand extends BaseCommand
  24. {
  25. /**
  26. * @var array Array with command events
  27. */
  28. protected $scriptEvents = array(
  29. ScriptEvents::PRE_INSTALL_CMD,
  30. ScriptEvents::POST_INSTALL_CMD,
  31. ScriptEvents::PRE_UPDATE_CMD,
  32. ScriptEvents::POST_UPDATE_CMD,
  33. ScriptEvents::PRE_STATUS_CMD,
  34. ScriptEvents::POST_STATUS_CMD,
  35. ScriptEvents::POST_ROOT_PACKAGE_INSTALL,
  36. ScriptEvents::POST_CREATE_PROJECT_CMD,
  37. ScriptEvents::PRE_ARCHIVE_CMD,
  38. ScriptEvents::POST_ARCHIVE_CMD,
  39. ScriptEvents::PRE_AUTOLOAD_DUMP,
  40. ScriptEvents::POST_AUTOLOAD_DUMP,
  41. );
  42. protected function configure()
  43. {
  44. $this
  45. ->setName('run-script')
  46. ->setDescription('Runs the scripts defined in composer.json.')
  47. ->setDefinition(array(
  48. new InputArgument('script', InputArgument::OPTIONAL, 'Script name to run.'),
  49. new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
  50. new InputOption('timeout', null, InputOption::VALUE_REQUIRED, 'Sets script timeout in seconds, or 0 for never.'),
  51. new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'),
  52. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'),
  53. new InputOption('list', 'l', InputOption::VALUE_NONE, 'List scripts.'),
  54. ))
  55. ->setHelp(<<<EOT
  56. The <info>run-script</info> command runs scripts defined in composer.json:
  57. <info>php composer.phar run-script post-update-cmd</info>
  58. EOT
  59. )
  60. ;
  61. }
  62. protected function execute(InputInterface $input, OutputInterface $output)
  63. {
  64. if ($input->getOption('list')) {
  65. return $this->listScripts($output);
  66. } elseif (!$input->getArgument('script')) {
  67. throw new \RuntimeException('Missing required argument "script"');
  68. }
  69. $script = $input->getArgument('script');
  70. if (!in_array($script, $this->scriptEvents)) {
  71. if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  72. throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
  73. }
  74. }
  75. $composer = $this->getComposer();
  76. $devMode = $input->getOption('dev') || !$input->getOption('no-dev');
  77. $event = new ScriptEvent($script, $composer, $this->getIO(), $devMode);
  78. $hasListeners = $composer->getEventDispatcher()->hasEventListeners($event);
  79. if (!$hasListeners) {
  80. throw new \InvalidArgumentException(sprintf('Script "%s" is not defined in this package', $script));
  81. }
  82. $args = $input->getArgument('args');
  83. if (null !== $timeout = $input->getOption('timeout')) {
  84. if (!ctype_digit($timeout)) {
  85. throw new \RuntimeException('Timeout value must be numeric and positive if defined, or 0 for forever');
  86. }
  87. // Override global timeout set before in Composer by environment or config
  88. ProcessExecutor::setTimeout((int) $timeout);
  89. }
  90. return $composer->getEventDispatcher()->dispatchScript($script, $devMode, $args);
  91. }
  92. protected function listScripts(OutputInterface $output)
  93. {
  94. $scripts = $this->getComposer()->getPackage()->getScripts();
  95. if (!count($scripts)) {
  96. return 0;
  97. }
  98. $io = $this->getIO();
  99. $io->writeError('<info>scripts:</info>');
  100. $table = array();
  101. foreach ($scripts as $name => $script) {
  102. $description = '';
  103. try {
  104. $cmd = $this->getApplication()->find($name);
  105. if ($cmd instanceof ScriptAliasCommand) {
  106. $description = $cmd->getDescription();
  107. }
  108. } catch (\Symfony\Component\Console\Exception\CommandNotFoundException $e) {
  109. // ignore scripts that have no command associated, like native Composer script listeners
  110. }
  111. $table[] = array(' '.$name, $description);
  112. }
  113. $renderer = new Table($output);
  114. $renderer->setStyle('compact');
  115. $renderer->getStyle()->setVerticalBorderChar('');
  116. $renderer->getStyle()->setCellRowContentFormat('%s ');
  117. $renderer->setRows($table)->render();
  118. return 0;
  119. }
  120. }