Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic path for composer task #701

Merged
merged 29 commits into from Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 51 additions & 26 deletions classes/phing/tasks/ext/ComposerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,31 @@
class ComposerTask extends Task
{
/**
* @var string the path to php interpreter
* Path to php interpreter
* @var string
*/
private $php = '';

/**
*
* @var string the Composer command to execute
* Composer command to execute
* @var string
*/
private $command = null;

/**
*
* Commandline object
* @var Commandline
*/
private $commandLine = null;

/**
*
* @var string path to Composer application
* Path to Composer application
* @var string
*/
private $composer = 'composer.phar';

/**
*
* Constructor.
*/
public function __construct()
{
Expand All @@ -64,7 +66,7 @@ public function __construct()
}

/**
* Initialize the interpreter with the Phing property php.interpreter
* Initialize the interpreter with the Phing property php.interpreter.
*/
public function init()
{
Expand All @@ -82,7 +84,7 @@ public function setPhp($php)
}

/**
* gets the path to php executable.
* Gets the path to php executable.
*
* @return string
*/
Expand All @@ -92,7 +94,8 @@ public function getPhp()
}

/**
* sets the Composer command to execute
* Sets the Composer command to execute.
*
* @param string $command
*/
public function setCommand($command)
Expand All @@ -101,7 +104,8 @@ public function setCommand($command)
}

/**
* return the Composer command to execute
* Return the Composer command to execute.
*
* @return String
*/
public function getCommand()
Expand All @@ -110,7 +114,8 @@ public function getCommand()
}

/**
* sets the path to Composer application
* Sets the path to Composer application.
*
* @param string $console
*/
public function setComposer($console)
Expand All @@ -119,16 +124,31 @@ public function setComposer($console)
}

/**
* returns the path to Composer application
* Returns the path to Composer application.
*
* If the filepath is non existent, try to find it on the system.
*
* @return string
*/
public function getComposer()
{
$composerFile = new SplFileInfo($this->composer);
if (false === $composerFile->isFile()) {
$message = sprintf('Composer binary not found at "%s"', $composerFile);
$this->log($message, Project::MSG_WARN);
$find = $this->isWindows() ? 'where' : 'which';
exec($find . ' composer', $composerLocation);
if (!empty($composerLocation[0])) {
$message = sprintf('Composer binary found at "%s", updating location', $composerLocation[0]);
$this->log($message, Project::MSG_INFO);
$this->setComposer($composerLocation[0]);
}
}
return $this->composer;
}

/**
* creates a nested arg task
* Creates a nested arg task.
*
* @return Arg Argument object
*/
Expand All @@ -139,7 +159,19 @@ public function createArg()
}

/**
* Prepares the command string to be executed
* Check is php is running on Windows.
*
* @return boolean
*/
public function isWindows()
{
$operatingSystemName = php_uname('s');
return strtoupper(substr($operatingSystemName, 0, 3)) === 'WIN';
}

/**
* Prepares the command string to be executed.
*
* @return string
*/
private function prepareCommandLine()
Expand All @@ -156,22 +188,15 @@ private function prepareCommandLine()
}

/**
* executes the Composer task
* Executes the Composer task.
*/
public function main()
{
$commandLine = $this->prepareCommandLine();
$this->log("executing " . $commandLine);

$composerFile = new SplFileInfo($this->getComposer());
if (false === $composerFile->isFile()) {
throw new BuildException(sprintf('Composer binary not found, path is "%s"', $composerFile));
}

$return = 0;
passthru($commandLine, $return);
$this->log("Executing " . $commandLine);
passthru($commandLine, $returnCode);

if ($return > 0) {
if ($returnCode > 0) {
throw new BuildException("Composer execution failed");
}
}
Expand Down
19 changes: 15 additions & 4 deletions test/classes/phing/tasks/ext/ComposerTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ComposerTaskTest extends \PHPUnit\Framework\TestCase
protected function setUp()
{
$this->object = new ComposerTask();
$this->object->setProject(new Project());
}

/**
Expand Down Expand Up @@ -77,9 +78,18 @@ public function testSetGetPhp()
*/
public function testSetGetComposer()
{
$composer = 'foo';
$o = $this->object;
$o->setComposer('foo');
$this->assertEquals('foo', $o->getComposer());
$o->setComposer($composer);
$composerFile = new SplFileInfo($composer);
if (false === $composerFile->isFile()) {
$find = $o->isWindows() ? 'where' : 'which';
exec($find . ' composer', $composerLocation);
if (!empty($composerLocation[0])) {
$composer = $composerLocation[0];
}
}
$this->assertEquals($composer, $o->getComposer());
}

/**
Expand All @@ -98,11 +108,12 @@ public function testMultipleCalls()
$o->setPhp('php');
$o->setCommand('install');
$o->createArg()->setValue('--dry-run');
$composer = $o->getComposer();
$method = new ReflectionMethod('ComposerTask', 'prepareCommandLine');
$method->setAccessible(true);
$this->assertEquals('php composer.phar install --dry-run', (string)$method->invoke($o));
$this->assertEquals('php ' . $composer . ' install --dry-run', (string)$method->invoke($o));
$o->setCommand('update');
$o->createArg()->setValue('--dev');
$this->assertEquals('php composer.phar update --dev', (string)$method->invoke($o));
$this->assertEquals('php ' . $composer . ' update --dev', (string)$method->invoke($o));
}
}