Spaces:
Sleeping
Sleeping
File size: 6,616 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | <?php
/*
* $Id: ExecTask.php 3076 2006-12-18 08:52:12Z fabien $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/TaskPhing.php';
/**
* Executes a command on the shell.
*
* @author Andreas Aderhold <andi@binarycloud.com>
* @author Hans Lellelid <hans@xmpl.org>
* @version $Revision: 1.17 $
* @package phing.tasks.system
*/
class ExecTask extends TaskPhing {
/**
* Command to execute.
* @var string
*/
protected $command;
/**
* Working directory.
* @var File
*/
protected $dir;
/**
* Operating system.
* @var string
*/
protected $os;
/**
* Whether to escape shell command using escapeshellcmd().
* @var boolean
*/
protected $escape = false;
/**
* Where to direct output.
* @var File
*/
protected $output;
/**
* Whether to passthru the output
* @var boolean
*/
protected $passthru = false;
/**
* Where to direct error output.
* @var File
*/
protected $error;
/**
* If spawn is set then [unix] programs will redirect stdout and add '&'.
* @var boolean
*/
protected $spawn = false;
/**
* Whether to check the return code.
* @var boolean
*/
protected $checkreturn = false;
/**
* Main method: wraps execute() command.
* @return void
*/
public function main() {
$this->execute();
}
/**
* Executes a program and returns the return code.
* Output from command is logged at INFO level.
* @return int Return code from execution.
*/
public function execute() {
// test if os match
$myos = Phing::getProperty("os.name");
$this->log("Myos = " . $myos, PROJECT_MSG_VERBOSE);
if (($this->os !== null) && (strpos($os, $myos) === false)) {
// this command will be executed only on the specified OS
$this->log("Not found in " . $os, PROJECT_MSG_VERBOSE);
return 0;
}
if ($this->dir !== null) {
if ($this->dir->isDirectory()) {
$currdir = getcwd();
@chdir($this->dir->getPath());
} else {
throw new BuildException("Can't chdir to:" . $this->dir->__toString());
}
}
if ($this->escape == true) {
// FIXME - figure out whether this is correct behavior
$this->command = escapeshellcmd($this->command);
}
if ($this->error !== null) {
$this->command .= ' 2> ' . $this->error->getPath();
$this->log("Writing error output to: " . $this->error->getPath());
}
if ($this->output !== null) {
$this->command .= ' 1> ' . $this->output->getPath();
$this->log("Writing standard output to: " . $this->output->getPath());
} elseif ($this->spawn) {
$this->command .= ' 1>/dev/null';
$this->log("Sending ouptut to /dev/null");
}
// If neither output nor error are being written to file
// then we'll redirect error to stdout so that we can dump
// it to screen below.
if ($this->output === null && $this->error === null) {
$this->command .= ' 2>&1';
}
// we ignore the spawn boolean for windows
if ($this->spawn) {
$this->command .= ' &';
}
$this->log("Executing command: " . $this->command);
$output = array();
$return = null;
exec($this->command, $output, $return);
if ($this->dir !== null) {
@chdir($currdir);
}
foreach($output as $line) {
$this->log($line, ($this->passthru ? PROJECT_MSG_INFO : PROJECT_MSG_VERBOSE));
}
if($return != 0 && $this->checkreturn)
{
throw new BuildException("Task exited with code $return");
}
return $return;
}
/**
* The command to use.
* @param mixed $command String or string-compatible (e.g. w/ __toString()).
*/
function setCommand($command) {
$this->command = "" . $command;
}
/**
* Whether to use escapeshellcmd() to escape command.
* @param boolean $escape
*/
function setEscape($escape) {
$this->escape = (bool) $escape;
}
/**
* Specify the working directory for executing this command.
* @param PhingFile $dir
*/
function setDir(PhingFile $dir) {
$this->dir = $dir;
}
/**
* Specify OS (or muliple OS) that must match in order to execute this command.
* @param string $os
*/
function setOs($os) {
$this->os = (string) $os;
}
/**
* File to which output should be written.
* @param PhingFile $output
*/
function setOutput(PhingFile $f) {
$this->output = $f;
}
/**
* File to which error output should be written.
* @param PhingFile $output
*/
function setError(PhingFile $f) {
$this->error = $f;
}
/**
* Whether to use passthru the output.
* @param boolean $passthru
*/
function setPassthru($passthru) {
$this->passthru = (bool) $passthru;
}
/**
* Whether to suppress all output and run in the background.
* @param boolean $spawn
*/
function setSpawn($spawn) {
$this->spawn = (bool) $spawn;
}
/**
* Whether to check the return code.
* @param boolean $checkreturn
*/
function setCheckreturn($checkreturn) {
$this->checkreturn = (bool) $checkreturn;
}
}
|