RunScriptCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(
  56. <<<EOT
  57. The <info>run-script</info> command runs scripts defined in composer.json:
  58. <info>php composer.phar run-script post-update-cmd</info>
  59. Read more at https://getcomposer.org/doc/03-cli.md#run-script
  60. EOT
  61. )
  62. ;
  63. }
  64. protected function execute(InputInterface $input, OutputInterface $output)
  65. {
  66. if ($input->getOption('list')) {
  67. return $this->listScripts($output);
  68. } elseif (!$input->getArgument('script')) {
  69. throw new \RuntimeException('Missing required argument "script"');
  70. }
  71. $script = $input->getArgument('script');
  72. if (!in_array($script, $this->scriptEvents)) {
  73. if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) {
  74. throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script));
  75. }
  76. }
  77. $composer = $this->getComposer();
  78. $devMode = $input->getOption('dev') || !$input->getOption('no-dev');
  79. $event = new ScriptEvent($script, $composer, $this->getIO(), $devMode);
  80. $hasListeners = $composer->getEventDispatcher()->hasEventListeners($event);
  81. if (!$hasListeners) {
  82. throw new \InvalidArgumentException(sprintf('Script "%s" is not defined in this package', $script));
  83. }
  84. $args = $input->getArgument('args');
  85. if (null !== $timeout = $input->getOption('timeout')) {
  86. if (!ctype_digit($timeout)) {
  87. throw new \RuntimeException('Timeout value must be numeric and positive if defined, or 0 for forever');
  88. }
  89. // Override global timeout set before in Composer by environment or config
  90. ProcessExecutor::setTimeout((int) $timeout);
  91. }
  92. return $composer->getEventDispatcher()->dispatchScript($script, $devMode, $args);
  93. }
  94. protected function listScripts(OutputInterface $output)
  95. {
  96. $scripts = $this->getComposer()->getPackage()->getScripts();
  97. if (!count($scripts)) {
  98. return 0;
  99. }
  100. $io = $this->getIO();
  101. $io->writeError('<info>scripts:</info>');
  102. $table = array();
  103. foreach ($scripts as $name => $script) {
  104. $description = '';
  105. try {
  106. $cmd = $this->getApplication()->find($name);
  107. if ($cmd instanceof ScriptAliasCommand) {
  108. $description = $cmd->getDescription();
  109. }
  110. } catch (\Symfony\Component\Console\Exception\CommandNotFoundException $e) {
  111. // ignore scripts that have no command associated, like native Composer script listeners
  112. }
  113. $table[] = array(' '.$name, $description);
  114. }
  115. $renderer = new Table($output);
  116. $renderer->setStyle('compact');
  117. $rendererStyle = $renderer->getStyle();
  118. $rendererStyle->setVerticalBorderChar('');
  119. $rendererStyle->setCellRowContentFormat('%s ');
  120. $renderer->setRows($table)->render();
  121. return 0;
  122. }
  123. }