From 0c8d9c0f74babfc7306f4b8cd9052815bc09b372 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Tue, 8 Oct 2013 08:50:10 +0100 Subject: [PATCH] Added findBinary, fixes #115 --- PHPCI/Builder.php | 33 ++++++++++++++++++++++++++++++++ PHPCI/Plugin/Atoum.php | 2 +- PHPCI/Plugin/Composer.php | 23 +--------------------- PHPCI/Plugin/Grunt.php | 2 +- PHPCI/Plugin/Pdepend.php | 9 ++++++++- PHPCI/Plugin/PhpCodeSniffer.php | 9 ++++++++- PHPCI/Plugin/PhpCpd.php | 9 ++++++++- PHPCI/Plugin/PhpCsFixer.php | 9 ++++++++- PHPCI/Plugin/PhpLoc.php | 9 ++++++++- PHPCI/Plugin/PhpMessDetector.php | 9 ++++++++- PHPCI/Plugin/PhpSpec.php | 10 +++++++++- PHPCI/Plugin/PhpUnit.php | 21 ++++++++++++++++++-- 12 files changed, 112 insertions(+), 33 deletions(-) diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index f62fd758..e21d1dbb 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -459,4 +459,37 @@ class Builder $value = json_encode($value); $this->store->setMeta($this->build->getProjectId(), $this->build->getId(), $key, $value); } + + /** + * Find a binary required by a plugin. + * @param $binary + * @return null|string + */ + public function findBinary($binary) + { + if (is_string($binary)) { + $binary = array($binary); + } + + foreach ($binary as $bin) { + // Check project root directory: + if (is_file(PHPCI_DIR . $bin)) { + return PHPCI_DIR . $bin; + } + + // Check Composer bin dir: + if (is_file(PHPCI_DIR . 'vendor/bin/' . $bin)) { + return PHPCI_DIR . 'vendor/bin/' . $bin; + } + + // Use "which" + $which = trim(shell_exec('which ' . $bin)); + + if (!empty($which)) { + return $which; + } + } + + return null; + } } diff --git a/PHPCI/Plugin/Atoum.php b/PHPCI/Plugin/Atoum.php index 54783b07..80dce4c3 100644 --- a/PHPCI/Plugin/Atoum.php +++ b/PHPCI/Plugin/Atoum.php @@ -15,7 +15,7 @@ class Atoum implements \PHPCI\Plugin if (isset($options['executable'])) { $this->executable = $this->phpci->buildPath . DIRECTORY_SEPARATOR.$options['executable']; } else { - $this->executable = PHPCI_BIN_DIR.'atoum'; + $this->executable = $this->phpci->findBinary('atoum'); } if (isset($options['args'])) { diff --git a/PHPCI/Plugin/Composer.php b/PHPCI/Plugin/Composer.php index 08060f83..ff1cbce4 100644 --- a/PHPCI/Plugin/Composer.php +++ b/PHPCI/Plugin/Composer.php @@ -36,7 +36,7 @@ class Composer implements \PHPCI\Plugin */ public function execute() { - $composerLocation = $this->whereIsComposer(); + $composerLocation = $this->phpci->findBinary(array('composer', 'composer.phar')); if (!$composerLocation) { $this->phpci->logFailure('Could not find Composer.'); @@ -47,25 +47,4 @@ class Composer implements \PHPCI\Plugin return $this->phpci->executeCommand($cmd, $this->directory, $this->action); } - - protected function whereIsComposer() - { - if (is_file(PHPCI_DIR . 'composer.phar')) { - return PHPCI_DIR . 'composer.phar'; - } - - $which = trim(shell_exec('which composer')); - - if (!empty($which)) { - return $which; - } - - $which = trim(shell_exec('which composer.phar')); - - if (!empty($which)) { - return $which; - } - - return null; - } } diff --git a/PHPCI/Plugin/Grunt.php b/PHPCI/Plugin/Grunt.php index 892d8303..ca024276 100644 --- a/PHPCI/Plugin/Grunt.php +++ b/PHPCI/Plugin/Grunt.php @@ -30,7 +30,7 @@ class Grunt implements \PHPCI\Plugin $this->phpci = $phpci; $this->directory = isset($options['directory']) ? $path . '/' . $options['directory'] : $path; $this->task = isset($options['task']) ? $options['task'] : null; - $this->grunt = isset($options['grunt']) ? $options['grunt'] : 'grunt'; + $this->grunt = isset($options['grunt']) ? $options['grunt'] : $this->phpci->findBinary('grunt'); $this->gruntfile = isset($options['gruntfile']) ? $options['gruntfile'] : 'Gruntfile.js'; } diff --git a/PHPCI/Plugin/Pdepend.php b/PHPCI/Plugin/Pdepend.php index 2a26cbb0..bb2fdea2 100644 --- a/PHPCI/Plugin/Pdepend.php +++ b/PHPCI/Plugin/Pdepend.php @@ -66,7 +66,14 @@ class Pdepend implements \PHPCI\Plugin throw new \Exception(sprintf('The location %s is not writable.', $this->location)); } - $cmd = PHPCI_BIN_DIR . 'pdepend --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; + $pdepend = $this->phpci->findBinary('pdepend'); + + if (!$pdepend) { + $this->phpci->logFailure('Could not find pdepend.'); + return false; + } + + $cmd = $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; $this->removeBuildArtifacts(); diff --git a/PHPCI/Plugin/PhpCodeSniffer.php b/PHPCI/Plugin/PhpCodeSniffer.php index 71a9008d..98d0c603 100755 --- a/PHPCI/Plugin/PhpCodeSniffer.php +++ b/PHPCI/Plugin/PhpCodeSniffer.php @@ -105,7 +105,14 @@ class PhpCodeSniffer implements \PHPCI\Plugin $encoding = ' --encoding='.$this->encoding; } - $cmd = PHPCI_BIN_DIR . 'phpcs %s %s %s %s %s "%s"'; + $phpcs = $this->phpci->findBinary('phpcs'); + + if (!$phpcs) { + $this->phpci->logFailure('Could not find phpcs.'); + return false; + } + + $cmd = $phpcs . ' %s %s %s %s %s "%s"'; $success = $this->phpci->executeCommand($cmd, $standard, $suffixes, $ignore, $tab_width, $encoding, $this->phpci->buildPath . $this->path); $output = $this->phpci->getLastOutput(); diff --git a/PHPCI/Plugin/PhpCpd.php b/PHPCI/Plugin/PhpCpd.php index 5a91dff4..5764258f 100755 --- a/PHPCI/Plugin/PhpCpd.php +++ b/PHPCI/Plugin/PhpCpd.php @@ -57,7 +57,14 @@ class PhpCpd implements \PHPCI\Plugin $ignore = implode('', $ignore); } - $success = $this->phpci->executeCommand(PHPCI_BIN_DIR . 'phpcpd %s "%s"', $ignore, $this->phpci->buildPath.$this->path); + $phpcpd = $this->phpci->findBinary('phpcpd'); + + if (!$phpcpd) { + $this->phpci->logFailure('Could not find phpcpd.'); + return false; + } + + $success = $this->phpci->executeCommand($phpcpd . ' %s "%s"', $ignore, $this->phpci->buildPath.$this->path); print $this->phpci->getLastOutput(); diff --git a/PHPCI/Plugin/PhpCsFixer.php b/PHPCI/Plugin/PhpCsFixer.php index 9aaa7536..6c42fcb8 100644 --- a/PHPCI/Plugin/PhpCsFixer.php +++ b/PHPCI/Plugin/PhpCsFixer.php @@ -42,7 +42,14 @@ class PhpCsFixer implements \PHPCI\Plugin $curdir = getcwd(); chdir($this->workingdir); - $cmd = PHPCI_BIN_DIR . 'php-cs-fixer fix . %s'; + $phpcsfixer = $this->phpci->findBinary('php-cs-fixer'); + + if (!$phpcsfixer) { + $this->phpci->logFailure('Could not find php-cs-fixer.'); + return false; + } + + $cmd = $phpcsfixer . ' fix . %s'; $success = $this->phpci->executeCommand($cmd, $this->args); chdir($curdir); diff --git a/PHPCI/Plugin/PhpLoc.php b/PHPCI/Plugin/PhpLoc.php index 51ba7108..6742de2b 100644 --- a/PHPCI/Plugin/PhpLoc.php +++ b/PHPCI/Plugin/PhpLoc.php @@ -47,7 +47,14 @@ class PhpLoc implements \PHPCI\Plugin $ignore = implode('', $ignore); } - $success = $this->phpci->executeCommand(PHPCI_BIN_DIR . 'phploc %s "%s"', $ignore, $this->phpci->buildPath); + $phploc = $this->phpci->findBinary('phploc'); + + if (!$phploc) { + $this->phpci->logFailure('Could not find phploc.'); + return false; + } + + $success = $this->phpci->executeCommand($phploc . ' %s "%s"', $ignore, $this->phpci->buildPath); $output = $this->phpci->getLastOutput(); if (preg_match_all('/\((LOC|CLOC|NCLOC|LLOC)\)\s+([0-9]+)/', $output, $matches)) { diff --git a/PHPCI/Plugin/PhpMessDetector.php b/PHPCI/Plugin/PhpMessDetector.php index c395c3dd..f1743b8d 100755 --- a/PHPCI/Plugin/PhpMessDetector.php +++ b/PHPCI/Plugin/PhpMessDetector.php @@ -82,7 +82,14 @@ class PhpMessDetector implements \PHPCI\Plugin $suffixes = ' --suffixes ' . implode(',', $this->suffixes); } - $cmd = PHPCI_BIN_DIR . 'phpmd "%s" text %s %s %s'; + $phpmd = $this->phpci->findBinary('phpmd'); + + if (!$phpmd) { + $this->phpci->logFailure('Could not find phpmd.'); + return false; + } + + $cmd = $phpmd . ' "%s" text %s %s %s'; $success = $this->phpci->executeCommand($cmd, $this->phpci->buildPath . $this->path, implode(',', $this->rules), $ignore, $suffixes); $errors = count(array_filter(explode(PHP_EOL, $this->phpci->getLastOutput()))); $this->phpci->storeBuildMeta('phpmd-warnings', $errors); diff --git a/PHPCI/Plugin/PhpSpec.php b/PHPCI/Plugin/PhpSpec.php index ace3f352..fbe981cc 100644 --- a/PHPCI/Plugin/PhpSpec.php +++ b/PHPCI/Plugin/PhpSpec.php @@ -31,7 +31,15 @@ class PhpSpec implements \PHPCI\Plugin { $curdir = getcwd(); chdir($this->phpci->buildPath); - $success = $this->phpci->executeCommand(PHPCI_BIN_DIR . 'phpspec'); + + $phpspec = $this->phpci->findBinary('phpspec'); + + if (!$phpspec) { + $this->phpci->logFailure('Could not find phpspec.'); + return false; + } + + $success = $this->phpci->executeCommand($phpspec); chdir($curdir); return $success; diff --git a/PHPCI/Plugin/PhpUnit.php b/PHPCI/Plugin/PhpUnit.php index 6925a772..dce241cf 100755 --- a/PHPCI/Plugin/PhpUnit.php +++ b/PHPCI/Plugin/PhpUnit.php @@ -102,7 +102,16 @@ class PhpUnit implements \PHPCI\Plugin chdir($this->phpci->buildPath.'/'.$this->runFrom); } - $cmd = PHPCI_BIN_DIR . 'phpunit %s -c "%s" ' . $this->coverage . $this->path; + + $phpunit = $this->phpci->findBinary('phpunit'); + + if (!$phpunit) { + $this->phpci->logFailure('Could not find phpunit.'); + return false; + } + + + $cmd = $phpunit . ' %s -c "%s" ' . $this->coverage . $this->path; $success = $this->phpci->executeCommand($cmd, $this->args, $this->phpci->buildPath . $configPath); if ($this->runFrom) { @@ -120,7 +129,15 @@ class PhpUnit implements \PHPCI\Plugin } else { $curdir = getcwd(); chdir($this->phpci->buildPath); - $cmd = PHPCI_BIN_DIR . 'phpunit %s "%s"'; + + $phpunit = $this->phpci->findBinary('phpunit'); + + if (!$phpunit) { + $this->phpci->logFailure('Could not find phpunit.'); + return false; + } + + $cmd = $phpunit . ' %s "%s"'; $success = $this->phpci->executeCommand($cmd, $this->args, $this->phpci->buildPath . $dirPath); chdir($curdir); return $success;