Allow the binary finder to use the project's composer.json file

- Load JSON file and get bin-dir value: https://getcomposer.org/doc/articles/vendor-binaries.md

- Update finder behaviour to avoid code duplication

Signed-off-by: Stéphane HULARD <s.hulard@chstudio.fr>
This commit is contained in:
Stéphane HULARD 2014-07-04 11:02:27 +02:00
parent cf0cc172b7
commit 07f84a28d0
5 changed files with 78 additions and 78 deletions

View file

@ -268,7 +268,7 @@ class Builder implements LoggerAwareInterface
*/
public function findBinary($binary)
{
return $this->commandExecutor->findBinary($binary);
return $this->commandExecutor->findBinary($binary, $this->buildPath);
}
/**

View file

@ -10,6 +10,7 @@
namespace PHPCI\Helper;
use \PHPCI\Logging\BuildLogger;
use Psr\Log\LogLevel;
abstract class BaseCommandExecutor implements CommandExecutor
{
@ -32,13 +33,18 @@ abstract class BaseCommandExecutor implements CommandExecutor
public $logExecOutput = true;
/**
* The path which findBinary will look in.
* @var string
*/
protected $rootDir;
/**
* Current build path
* @var string
*/
protected $buildPath;
/**
* @param BuildLogger $logger
* @param string $rootDir
@ -104,5 +110,68 @@ abstract class BaseCommandExecutor implements CommandExecutor
* @param string $binary
* @return null|string
*/
abstract public function findBinary($binary);
public function findBinary($binary, $buildPath = null) {
$binaryPath = null;
$composerBin = $this->getComposerBinDir(realpath($buildPath));
if (is_string($binary)) {
$binary = array($binary);
}
foreach ($binary as $bin) {
$this->logger->log("Looking for binary: " . $bin, LogLevel::DEBUG);
if (is_dir($composerBin) && is_file($composerBin.'/'.$bin)) {
$this->logger->log("Found in ".$composerBin.": " . $bin, LogLevel::DEBUG);
$binaryPath = $composerBin . '/' . $bin;
break;
}
if (is_file($this->rootDir . $bin)) {
$this->logger->log("Found in root: " . $bin, LogLevel::DEBUG);
$binaryPath = $this->rootDir . $bin;
break;
}
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
$this->logger->log("Found in vendor/bin: " . $bin, LogLevel::DEBUG);
$binaryPath = $this->rootDir . 'vendor/bin/' . $bin;
break;
}
$findCmdResult = $this->findGlobalBinary($bin);
if (is_file($findCmdResult)) {
$this->logger->log("Found in " . $findCmdResult, LogLevel::DEBUG);
$binaryPath = $findCmdResult;
break;
}
}
return $binaryPath;
}
/**
* Find a binary which is installed globally on the system
* @param string $binary
* @return null|string
*/
abstract protected function findGlobalBinary($bin);
/**
* Try to load the composer.json file in the building project
* If the bin-dir is configured, return the full path to it
* @param string $path Current build path
* @return string|null
*/
public function getComposerBinDir($path) {
if (is_dir($path)) {
$composer = $path.'/composer.json';
if( is_file($composer) ) {
$json = json_decode(file_get_contents($composer));
if( isset($json->config->{"bin-dir"}) ) {
return $path.'/'.$json->config->{"bin-dir"};
}
}
}
return null;
}
}

View file

@ -26,7 +26,8 @@ interface CommandExecutor
/**
* Find a binary required by a plugin.
* @param string $binary
* @param string $buildPath the current build path
* @return null|string
*/
public function findBinary($binary);
public function findBinary($binary, $buildPath = null);
}

View file

@ -9,45 +9,10 @@
namespace PHPCI\Helper;
use Psr\Log\LogLevel;
class UnixCommandExecutor extends BaseCommandExecutor
{
/**
* Find a binary required by a plugin.
* @param string $binary
* @return null|string
*/
public function findBinary($binary)
protected function findGlobalBinary($binary)
{
$binaryPath = null;
if (is_string($binary)) {
$binary = array($binary);
}
foreach ($binary as $bin) {
$this->logger->log("Looking for binary: " . $bin, LogLevel::DEBUG);
if (is_file($this->rootDir . $bin)) {
$this->logger->log("Found in root: " . $bin, LogLevel::DEBUG);
$binaryPath = $this->rootDir . $bin;
break;
}
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
$this->logger->log("Found in vendor/bin: " . $bin, LogLevel::DEBUG);
$binaryPath = $this->rootDir . 'vendor/bin/' . $bin;
break;
}
$findCmdResult = trim(shell_exec('which ' . $bin));
if (!empty($findCmdResult)) {
$this->logger->log("Found in " . $findCmdResult, LogLevel::DEBUG);
$binaryPath = $findCmdResult;
break;
}
}
return $binaryPath;
return trim(shell_exec('which ' . $binary));
}
}

View file

@ -9,45 +9,10 @@
namespace PHPCI\Helper;
use Psr\Log\LogLevel;
class WindowsCommandExecutor extends BaseCommandExecutor
{
/**
* Find a binary required by a plugin.
* @param string $binary
* @return null|string
*/
public function findBinary($binary)
protected function findGlobalBinary($binary)
{
$binaryPath = null;
if (is_string($binary)) {
$binary = array($binary);
}
foreach ($binary as $bin) {
$this->logger->log("Looking for binary: " . $bin, LogLevel::DEBUG);
if (is_file($this->rootDir . $bin)) {
$this->logger->log("Found in root: " . $bin, LogLevel::DEBUG);
$binaryPath = $this->rootDir . $bin;
break;
}
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
$this->logger->log("Found in vendor/bin: " . $bin, LogLevel::DEBUG);
$binaryPath = $this->rootDir . 'vendor/bin/' . $bin;
break;
}
$findCmdResult = trim(shell_exec('where ' . $bin));
if (!empty($findCmdResult)) {
$this->logger->log("Found in " . $findCmdResult, LogLevel::DEBUG);
$binaryPath = $findCmdResult;
break;
}
}
return $binaryPath;
return trim(shell_exec('where ' . $binary));
}
}