ExecCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. /**
  17. * @author Davey Shafik <me@daveyshafik.com>
  18. */
  19. class ExecCommand extends Command
  20. {
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('exec')
  25. ->setDescription('Execute a vendored binary/script')
  26. ->setDefinition(array(
  27. new InputOption('list', 'l', InputOption::VALUE_NONE),
  28. new InputArgument('script', InputArgument::OPTIONAL, 'The script to run, e.g. phpunit'),
  29. new InputArgument(
  30. 'args',
  31. InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
  32. 'Arguments to pass to the script. Use <info>--</info> to separate from composer arguments'
  33. ),
  34. ))
  35. ;
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $binDir = $this->getComposer()->getConfig()->get('bin-dir');
  40. if ($input->hasArgument('list') || !$input->hasArgument('script') || !$input->getArgument('script')) {
  41. $bins = glob($binDir . '/*');
  42. if (!$bins) {
  43. throw new \RuntimeException("No scripts found in bin-dir ($binDir)");
  44. }
  45. $this->getIO()->write(<<<EOT
  46. <comment>Available scripts:</comment>
  47. EOT
  48. );
  49. foreach ($bins as $bin) {
  50. $bin = basename($bin);
  51. $this->getIO()->write(<<<EOT
  52. <info>- $bin</info>
  53. EOT
  54. );
  55. }
  56. return;
  57. }
  58. $script = $input->getArgument('script');
  59. if (!file_exists($binDir . '/' . $script)) {
  60. throw new \RuntimeException("script '$script' not found in bin-dir ($binDir)");
  61. }
  62. if ($args = $input->getArgument('args')) {
  63. $args = " " . implode(" ", $args);
  64. }
  65. $this->getIO()->write(<<<EOT
  66. <comment>Executing $script$args:</comment>
  67. EOT
  68. );
  69. passthru($binDir . '/' . $script . $args);
  70. }
  71. }