Skip to content

Commit

Permalink
add an "all" namespace helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed May 9, 2016
1 parent ada41df commit 629eb4d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ This is done by registering a `bin` command, which can be used to run Composer c

## Installation

```sh
composer global require bamarni/composer-bin-plugin:0.*@dev
```
composer global require bamarni/composer-bin-plugin:0.*@dev

## Usage

Let's install Behat and PHPSpec inside a `bdd` namespace :
composer bin [namespace] [composer_command]

```sh
composer bin bdd require behat:behat:^3.0 phpspec/phpspec:^2.0
```
As an example, let's install Behat and PHPSpec inside a `bdd` namespace :

composer bin bdd require behat/behat:^3.0 phpspec/phpspec:^2.0

This command creates the following directory structure :

Expand Down Expand Up @@ -55,3 +53,18 @@ but they'll use an isolated set of dependencies.
Make sure to add the following line in your `.gitignore` :
vendor-bin/*/vendor
#### "all"
The "all" namespace has a special meaning, it runs a command for all
existing namespaces.
For instance, the following command would update all your tools :
composer bin all update
#### Composer global command
This plugin can also be used to manage your global tools :
composer global bin [namespace] [composer_command]
38 changes: 29 additions & 9 deletions src/BinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,40 @@ public function execute(InputInterface $input, OutputInterface $output)
{
putenv('COMPOSER_BIN_DIR='.Factory::createConfig()->get('bin-dir'));

$binVendorRoot = 'vendor-bin';
$binName = $input->getArgument('name');
$binRoot = 'vendor-bin/'.$binName;

if (!file_exists($binRoot)) {
mkdir($binRoot, 0777, true);
if ('all' === $binName) {
$binRoots = glob($binVendorRoot.'/*', GLOB_ONLYDIR);
$this->getApplication()->setAutoExit(false);
$originalWorkingDir = getcwd();
} else {
$binRoots = array($binVendorRoot.'/'.$binName);
if (!file_exists($binRoots[0])) {
mkdir($binRoots[0], 0777, true);
}
}
chdir($binRoot);
$this->getIO()->writeError('<info>Changed current directory to '.$binRoot.'</info>');

$this->resetComposer();
$exitCode = 0;
foreach ($binRoots as $binRoot) {
chdir($binRoot);

$this->getIO()->writeError('<info>Changed current directory to ' . $binRoot . '</info>');

$this->resetComposer();

$input = new StringInput(preg_replace('/bin\s+'.preg_quote($binName, '/').'/', '', $input->__toString(), 1));
$input = new StringInput(preg_replace('/bin\s+' . preg_quote($binName, '/') . '/', '', $input->__toString(), 1));

$exitCode += $this->getApplication()->run($input, $output);

chdir($originalWorkingDir);
foreach ($this->getApplication()->all() as $command) {
if ($command instanceof BaseCommand) {
$command->resetComposer();
}
}
}

return $this->getApplication()->run($input, $output);
return $exitCode;
}

/**
Expand Down

0 comments on commit 629eb4d

Please sign in to comment.