Skip to content

Commit

Permalink
Merge config from local composer.json if present
Browse files Browse the repository at this point in the history
Fixes bamarni#4
  • Loading branch information
bamarni committed Sep 4, 2016
1 parent 0f54319 commit 3111751
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ php:
- '7.0'

before_install:
- composer global require bamarni/composer-bin-plugin:0.*@dev
- composer global config repositories.bin path $PWD
- composer global require bamarni/composer-bin-plugin:dev-master

install:
- composer install
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This command creates the following directory structure :
└── vendor


You can continue to run `./vendor/bin/phpspec` and `./vendor/bin/phpspec`,
You can continue to run `./vendor/bin/behat` and `./vendor/bin/phpspec`,
but they'll use an isolated set of dependencies.

### The "all" namespace
Expand Down
20 changes: 18 additions & 2 deletions src/BinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Command\BaseCommand;
use Composer\Json\JsonFile;

class BinCommand extends BaseCommand
{
Expand All @@ -28,7 +29,7 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$this->resetComposers();

putenv('COMPOSER_BIN_DIR='.Factory::createConfig()->get('bin-dir'));
putenv('COMPOSER_BIN_DIR='.$this->createConfig()->get('bin-dir'));

$binVendorRoot = 'vendor-bin';
$binNamespace = $input->getArgument('namespace');
Expand Down Expand Up @@ -89,4 +90,19 @@ private function chdir($dir)
chdir($dir);
$this->getIO()->writeError('<info>Changed current directory to ' . $dir . '</info>');
}
}

private function createConfig()
{
$config = Factory::createConfig();

$file = new JsonFile(Factory::getComposerFile());
if (!$file->exists()) {
return $config;
}
$file->validateSchema(JsonFile::LAX_SCHEMA);

$config->merge($file->read());

return $config;
}
}
22 changes: 21 additions & 1 deletion tests/BinCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Bamarni\Composer\Bin\Tests\Fixtures\MyTestCommand;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\ConsoleOutput;

class BinCommandTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -21,6 +20,8 @@ protected function setUp()
mkdir($this->rootDir);
chdir($this->rootDir);

file_put_contents($this->rootDir.'/composer.json', '{}');

$this->application = new Application();
$this->application->addCommands(array(
new BinCommand(),
Expand Down Expand Up @@ -87,4 +88,23 @@ public function testAllNamespaceCommand()
$this->assertEquals($dataSet['vendor-dir'], $this->rootDir . '/vendor-bin/'.$ns.'/vendor');
}
}

public function testBinDirFromLocalConfig()
{
$binDir = 'bin';
$composer = array(
'config' => array(
'bin-dir' => $binDir
)
);
file_put_contents($this->rootDir.'/composer.json', json_encode($composer));

$input = new StringInput('bin theirspace mytest');
$output = new NullOutput();
$this->application->doRun($input, $output);

$this->assertCount(1, $this->myTestCommand->data);
$dataSet = array_shift($this->myTestCommand->data);
$this->assertEquals($dataSet['bin-dir'], $this->rootDir.'/'.$binDir);
}
}
11 changes: 10 additions & 1 deletion tests/Fixtures/MyTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Command\BaseCommand;
use Composer\Factory;
use Composer\IO\NullIO;

class MyTestCommand extends BaseCommand
{
Expand All @@ -26,16 +27,24 @@ public function __construct(\PHPUnit_Framework_Assert $assert)

public function execute(InputInterface $input, OutputInterface $output)
{
// make sure the proxy command didn't instantiate Composer
$this->assert->assertNull($this->getComposer(false));
$this->assert->assertNull($this->getApplication()->getComposer(false));

$config = Factory::createConfig();
// put a dummy composer.json to be able to create Composer
file_put_contents(getcwd().'/composer.json', '{}');

$factory = Factory::create(new NullIO());
$config = $factory->getConfig();

$this->data[] = array(
'bin-dir' => $config->get('bin-dir'),
'cwd' => getcwd(),
'vendor-dir' => $config->get('vendor-dir'),
);

$this->resetComposer();
$this->getApplication()->resetComposer();
}
}

0 comments on commit 3111751

Please sign in to comment.