Merge pull request #404 from corpsee/fixes-for-windows
Init abstraction of command execution on different operation systems
This commit is contained in:
commit
f583d045cf
|
@ -10,7 +10,6 @@
|
||||||
namespace PHPCI;
|
namespace PHPCI;
|
||||||
|
|
||||||
use PHPCI\Helper\BuildInterpolator;
|
use PHPCI\Helper\BuildInterpolator;
|
||||||
use PHPCI\Helper\CommandExecutor;
|
|
||||||
use PHPCI\Helper\MailerFactory;
|
use PHPCI\Helper\MailerFactory;
|
||||||
use PHPCI\Logging\BuildLogger;
|
use PHPCI\Logging\BuildLogger;
|
||||||
use PHPCI\Model\Build;
|
use PHPCI\Model\Build;
|
||||||
|
@ -118,7 +117,12 @@ class Builder implements LoggerAwareInterface
|
||||||
$pluginFactory->addConfigFromFile(PHPCI_DIR . "/pluginconfig.php");
|
$pluginFactory->addConfigFromFile(PHPCI_DIR . "/pluginconfig.php");
|
||||||
$this->pluginExecutor = new Plugin\Util\Executor($pluginFactory, $this->buildLogger);
|
$this->pluginExecutor = new Plugin\Util\Executor($pluginFactory, $this->buildLogger);
|
||||||
|
|
||||||
$this->commandExecutor = new CommandExecutor(
|
$executorClass = 'PHPCI\Helper\UnixCommandExecutor';
|
||||||
|
if (IS_WIN) {
|
||||||
|
$executorClass = 'PHPCI\Helper\WindowsCommandExecutor';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->commandExecutor = new $executorClass(
|
||||||
$this->buildLogger,
|
$this->buildLogger,
|
||||||
PHPCI_DIR,
|
PHPCI_DIR,
|
||||||
$this->quiet,
|
$this->quiet,
|
||||||
|
@ -126,7 +130,6 @@ class Builder implements LoggerAwareInterface
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->interpolator = new BuildInterpolator();
|
$this->interpolator = new BuildInterpolator();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
101
PHPCI/Helper/BaseCommandExecutor.php
Normal file
101
PHPCI/Helper/BaseCommandExecutor.php
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPCI\Helper;
|
||||||
|
|
||||||
|
use \PHPCI\Logging\BuildLogger;
|
||||||
|
|
||||||
|
abstract class BaseCommandExecutor implements CommandExecutor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \PHPCI\Logging\BuildLogger
|
||||||
|
*/
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $quiet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $verbose;
|
||||||
|
|
||||||
|
protected $lastOutput;
|
||||||
|
|
||||||
|
public $logExecOutput = true;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path which findBinary will look in.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $rootDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param BuildLogger $logger
|
||||||
|
* @param string $rootDir
|
||||||
|
* @param bool $quiet
|
||||||
|
* @param bool $verbose
|
||||||
|
*/
|
||||||
|
public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false)
|
||||||
|
{
|
||||||
|
$this->logger = $logger;
|
||||||
|
$this->quiet = $quiet;
|
||||||
|
$this->verbose = $verbose;
|
||||||
|
|
||||||
|
$this->lastOutput = array();
|
||||||
|
|
||||||
|
$this->rootDir = $rootDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes shell commands.
|
||||||
|
* @param array $args
|
||||||
|
* @return bool Indicates success
|
||||||
|
*/
|
||||||
|
public function executeCommand($args = array())
|
||||||
|
{
|
||||||
|
$this->lastOutput = array();
|
||||||
|
|
||||||
|
$command = call_user_func_array('sprintf', $args);
|
||||||
|
|
||||||
|
if ($this->quiet) {
|
||||||
|
$this->logger->log('Executing: ' . $command);
|
||||||
|
}
|
||||||
|
|
||||||
|
$status = 0;
|
||||||
|
exec($command, $this->lastOutput, $status);
|
||||||
|
|
||||||
|
foreach ($this->lastOutput as &$lastOutput) {
|
||||||
|
$lastOutput = trim($lastOutput, '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->logExecOutput && !empty($this->lastOutput) && ($this->verbose|| $status != 0)) {
|
||||||
|
$this->logger->log($this->lastOutput);
|
||||||
|
}
|
||||||
|
|
||||||
|
$rtn = false;
|
||||||
|
|
||||||
|
if ($status == 0) {
|
||||||
|
$rtn = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rtn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the output from the last command run.
|
||||||
|
*/
|
||||||
|
public function getLastOutput()
|
||||||
|
{
|
||||||
|
return implode(PHP_EOL, $this->lastOutput);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a binary required by a plugin.
|
||||||
|
* @param string $binary
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
abstract public function findBinary($binary);
|
||||||
|
}
|
|
@ -2,133 +2,24 @@
|
||||||
|
|
||||||
namespace PHPCI\Helper;
|
namespace PHPCI\Helper;
|
||||||
|
|
||||||
|
interface CommandExecutor
|
||||||
use \PHPCI\Logging\BuildLogger;
|
|
||||||
use Psr\Log\LogLevel;
|
|
||||||
|
|
||||||
class CommandExecutor
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \PHPCI\Logging\BuildLogger
|
* Executes shell commands. Accepts multiple arguments the first
|
||||||
*/
|
* is the template and everything else is inserted in. c.f. sprintf
|
||||||
protected $logger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $quiet;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $verbose;
|
|
||||||
|
|
||||||
protected $lastOutput;
|
|
||||||
|
|
||||||
public $logExecOutput = true;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The path which findBinary will look in.
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $rootDir;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param BuildLogger $logger
|
|
||||||
* @param $rootDir
|
|
||||||
* @param bool $quiet
|
|
||||||
* @param bool $verbose
|
|
||||||
*/
|
|
||||||
public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false)
|
|
||||||
{
|
|
||||||
$this->logger = $logger;
|
|
||||||
$this->quiet = $quiet;
|
|
||||||
$this->verbose = $verbose;
|
|
||||||
|
|
||||||
$this->lastOutput = array();
|
|
||||||
|
|
||||||
$this->rootDir = $rootDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes shell commands.
|
|
||||||
* @param array $args
|
|
||||||
* @return bool Indicates success
|
* @return bool Indicates success
|
||||||
*/
|
*/
|
||||||
public function executeCommand($args = array())
|
public function executeCommand();
|
||||||
{
|
|
||||||
$this->lastOutput = array();
|
|
||||||
|
|
||||||
$command = call_user_func_array('sprintf', $args);
|
|
||||||
|
|
||||||
if ($this->quiet) {
|
|
||||||
$this->logger->log('Executing: ' . $command);
|
|
||||||
}
|
|
||||||
|
|
||||||
$status = 0;
|
|
||||||
exec($command, $this->lastOutput, $status);
|
|
||||||
|
|
||||||
foreach ($this->lastOutput as &$lastOutput) {
|
|
||||||
$lastOutput = trim($lastOutput, '"');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->logExecOutput && !empty($this->lastOutput) && ($this->verbose|| $status != 0)) {
|
|
||||||
$this->logger->log($this->lastOutput);
|
|
||||||
}
|
|
||||||
|
|
||||||
$rtn = false;
|
|
||||||
|
|
||||||
if ($status == 0) {
|
|
||||||
$rtn = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $rtn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the output from the last command run.
|
* Returns the output from the last command run.
|
||||||
*/
|
*/
|
||||||
public function getLastOutput()
|
public function getLastOutput();
|
||||||
{
|
|
||||||
return implode(PHP_EOL, $this->lastOutput);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a binary required by a plugin.
|
* Find a binary required by a plugin.
|
||||||
* @param $binary
|
* @param string $binary
|
||||||
* @return null|string
|
* @return null|string
|
||||||
*/
|
*/
|
||||||
public function findBinary($binary)
|
public function findBinary($binary);
|
||||||
{
|
|
||||||
if (is_string($binary)) {
|
|
||||||
$binary = array($binary);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($binary as $bin) {
|
|
||||||
$this->logger->log("Looking for binary: " . $bin, LogLevel::DEBUG);
|
|
||||||
// Check project root directory:
|
|
||||||
if (is_file($this->rootDir . $bin)) {
|
|
||||||
$this->logger->log("Found in root: " . $bin, LogLevel::DEBUG);
|
|
||||||
return $this->rootDir . $bin;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check Composer bin dir:
|
|
||||||
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
|
|
||||||
$this->logger->log("Found in vendor/bin: " . $bin, LogLevel::DEBUG);
|
|
||||||
return $this->rootDir . 'vendor/bin/' . $bin;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use "where" for windows and "which" for other OS
|
|
||||||
$findCmd = IS_WIN ? 'where' : 'which';
|
|
||||||
$findCmdResult = trim(shell_exec($findCmd . ' ' . $bin));
|
|
||||||
|
|
||||||
if (!empty($findCmdResult)) {
|
|
||||||
$this->logger->log("Found in " . $findCmdResult, LogLevel::DEBUG);
|
|
||||||
return $findCmdResult;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
46
PHPCI/Helper/UnixCommandExecutor.php
Normal file
46
PHPCI/Helper/UnixCommandExecutor.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
46
PHPCI/Helper/WindowsCommandExecutor.php
Normal file
46
PHPCI/Helper/WindowsCommandExecutor.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,6 +38,9 @@ class CleanBuild implements \PHPCI\Plugin
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
$cmd = 'rm -Rf "%s"';
|
$cmd = 'rm -Rf "%s"';
|
||||||
|
if (IS_WIN) {
|
||||||
|
$cmd = 'rmdir /S /Q "%s"';
|
||||||
|
}
|
||||||
$this->phpci->executeCommand($cmd, $this->phpci->buildPath . 'composer.phar');
|
$this->phpci->executeCommand($cmd, $this->phpci->buildPath . 'composer.phar');
|
||||||
$this->phpci->executeCommand($cmd, $this->phpci->buildPath . 'composer.lock');
|
$this->phpci->executeCommand($cmd, $this->phpci->buildPath . 'composer.lock');
|
||||||
|
|
||||||
|
|
|
@ -46,11 +46,17 @@ class CopyBuild implements \PHPCI\Plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
$cmd = 'mkdir -p "%s" && cp -R "%s" "%s"';
|
$cmd = 'mkdir -p "%s" && cp -R "%s" "%s"';
|
||||||
|
if (IS_WIN) {
|
||||||
|
$cmd = 'mkdir -p "%s" && xcopy /E "%s" "%s"';
|
||||||
|
}
|
||||||
$success = $this->phpci->executeCommand($cmd, $this->directory, $build, $this->directory);
|
$success = $this->phpci->executeCommand($cmd, $this->directory, $build, $this->directory);
|
||||||
|
|
||||||
if ($this->ignore) {
|
if ($this->ignore) {
|
||||||
foreach ($this->phpci->ignore as $file) {
|
foreach ($this->phpci->ignore as $file) {
|
||||||
$cmd = 'rm -Rf "%s/%s"';
|
$cmd = 'rm -Rf "%s/%s"';
|
||||||
|
if (IS_WIN) {
|
||||||
|
$cmd = 'rmdir /S /Q "%s\%s"';
|
||||||
|
}
|
||||||
$this->phpci->executeCommand($cmd, $this->directory, $file);
|
$this->phpci->executeCommand($cmd, $this->directory, $file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,10 @@ class Wipe implements \PHPCI\Plugin
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (is_dir($this->directory)) {
|
if (is_dir($this->directory)) {
|
||||||
$cmd = 'rm -rf %s*';
|
$cmd = 'rm -Rf "%s"';
|
||||||
|
if (IS_WIN) {
|
||||||
|
$cmd = 'rmdir /S /Q "%s"';
|
||||||
|
}
|
||||||
$success = $this->phpci->executeCommand($cmd, $this->directory);
|
$success = $this->phpci->executeCommand($cmd, $this->directory);
|
||||||
}
|
}
|
||||||
return $success;
|
return $success;
|
||||||
|
|
Loading…
Reference in a new issue