Skip to content

Instantly share code, notes, and snippets.

@dig412
Last active December 14, 2015 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dig412/5057536 to your computer and use it in GitHub Desktop.
Save dig412/5057536 to your computer and use it in GitHub Desktop.
Phing condition for testing if an executable is available on the system.
<?php
/**
* Phing task to test if a given external program is avilable on the system
*
* Internally this uses "which" on Linux / OSX and "where" on Windows to locate the executable
*
* Usage:
* Put the file in a subfolder called "tasks", and use as follows:
*
* <typedef name="exists" classname="tasks.ExistsCondition"/>
* <targetname="test">
* <if>
* <exists command="uglifyjs">
* <then>
* ...
* </if>
* </target>
*
* @author Doug Fitzmaurice
* @link https://gist.github.com/dig412
*/
class ExistsCondition implements Condition {
/**
* The command we are testing the existance of
* @var string
*/
private $command = null;
/**
* The system command for locating executables (Which or Where)
* @var string
*/
private $whichCommand = null;
/**
* Setter for the "command" proerty
* @param string $command Name of the command that will be located (e.g. "uglifyjs")
*/
public function setCommand($command) {
$this->command = $command;
}
public function evaluate() {
$this->setWhichCommand();
exec("{$this->whichCommand} {$this->command} 2>&1", $output, $returnCode);
if($returnCode == 0) {
return true;
}
else {
return false;
}
}
/**
* Sets the "which" implementation for the current OS
*/
private function setWhichCommand() {
$os = Phing::getProperty("host.os");
switch($os) {
case "WINNT":
$this->whichCommand = "where";
break;
case "Linux":
case "Darwin":
$this->whichCommand = "which";
break;
default:
throw new BuildException("Not a known OS - $os");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment