Changeset View
Changeset View
Standalone View
Standalone View
src/future/exec/PhutilExecPassthru.php
Show All 11 Lines | |||||
* | * | ||||
* $exec = new PhutilExecPassthru('ls %s', $dir); | * $exec = new PhutilExecPassthru('ls %s', $dir); | ||||
* $err = $exec->execute(); | * $err = $exec->execute(); | ||||
* | * | ||||
* You can set the current working directory for the command with | * You can set the current working directory for the command with | ||||
* @{method:setCWD}, and set the environment with @{method:setEnv}. | * @{method:setCWD}, and set the environment with @{method:setEnv}. | ||||
* | * | ||||
* @task command Executing Passthru Commands | * @task command Executing Passthru Commands | ||||
* @task config Configuring Passthru Commands | |||||
*/ | */ | ||||
final class PhutilExecPassthru extends Phobject { | final class PhutilExecPassthru extends PhutilExecutableFuture { | ||||
private $command; | private $command; | ||||
private $env; | private $passthruResult; | ||||
private $cwd; | |||||
/* -( Executing Passthru Commands )---------------------------------------- */ | /* -( Executing Passthru Commands )---------------------------------------- */ | ||||
/** | /** | ||||
* Build a new passthru command. | * Build a new passthru command. | ||||
* | * | ||||
Show All 32 Lines | public function execute() { | ||||
$pipes = array(); | $pipes = array(); | ||||
if ($command instanceof PhutilCommandString) { | if ($command instanceof PhutilCommandString) { | ||||
$unmasked_command = $command->getUnmaskedString(); | $unmasked_command = $command->getUnmaskedString(); | ||||
} else { | } else { | ||||
$unmasked_command = $command; | $unmasked_command = $command; | ||||
} | } | ||||
$env = $this->env; | if ($this->hasEnv()) { | ||||
$cwd = $this->cwd; | $env = $this->getEnv(); | ||||
} else { | |||||
$env = null; | |||||
} | |||||
$cwd = $this->getCWD(); | |||||
$options = array(); | $options = array(); | ||||
if (phutil_is_windows()) { | if (phutil_is_windows()) { | ||||
// Without 'bypass_shell', things like launching vim don't work properly, | // Without 'bypass_shell', things like launching vim don't work properly, | ||||
// and we can't execute commands with spaces in them, and all commands | // and we can't execute commands with spaces in them, and all commands | ||||
// invoked from git bash fail horridly, and everything is a mess in | // invoked from git bash fail horridly, and everything is a mess in | ||||
// general. | // general. | ||||
$options['bypass_shell'] = true; | $options['bypass_shell'] = true; | ||||
Show All 25 Lines | $profiler->endServiceCall( | ||||
array( | array( | ||||
'err' => $err, | 'err' => $err, | ||||
)); | )); | ||||
return $err; | return $err; | ||||
} | } | ||||
/* -( Configuring Passthru Commands )-------------------------------------- */ | /* -( Future )------------------------------------------------------------- */ | ||||
/** | public function isReady() { | ||||
* Set environmental variables for the subprocess. | // This isn't really a future because it executes synchronously and has | ||||
* | // full control of the console. We're just implementing the interfaces to | ||||
* By default, variables are added to the environment of this process. You | // make it easier to share code with ExecFuture. | ||||
* can optionally wipe the environment and pass only the specified values. | |||||
* | if ($this->passthruResult === null) { | ||||
* // Env will have "X" and current env ("PATH", etc.) | $this->passthruResult = $this->execute(); | ||||
* $exec->setEnv(array('X' => 'y')); | |||||
* | |||||
* // Env will have ONLY "X". | |||||
* $exec->setEnv(array('X' => 'y'), $wipe_process_env = true); | |||||
* | |||||
* @param dict Dictionary of environmental variables. | |||||
* @param bool Optionally, pass true to wipe the existing environment clean. | |||||
* @return this | |||||
* | |||||
* @task config | |||||
*/ | |||||
public function setEnv(array $env, $wipe_process_env = false) { | |||||
if ($wipe_process_env) { | |||||
$this->env = $env; | |||||
} else { | |||||
$this->env = $env + $_ENV; | |||||
} | |||||
return $this->env; | |||||
} | } | ||||
return true; | |||||
} | |||||
/** | protected function getResult() { | ||||
* Set the current working directory for the subprocess (that is, set where | return $this->passthruResult; | ||||
* the subprocess will execute). If not set, the default value is the parent's | |||||
* current working directory. | |||||
* | |||||
* @param string Directory to execute the subprocess in. | |||||
* @return this | |||||
* | |||||
* @task config | |||||
*/ | |||||
public function setCWD($cwd) { | |||||
$this->cwd = $cwd; | |||||
return $this; | |||||
} | } | ||||
} | } |