| 35 | | /** |
|---|
| 36 | | * Command to execute. |
|---|
| 37 | | * @var string |
|---|
| 38 | | */ |
|---|
| 39 | | protected $command; |
|---|
| 40 | | |
|---|
| 41 | | /** |
|---|
| 42 | | * Working directory. |
|---|
| 43 | | * @var File |
|---|
| 44 | | */ |
|---|
| 45 | | protected $dir; |
|---|
| 46 | | |
|---|
| 47 | | /** |
|---|
| 48 | | * Operating system. |
|---|
| 49 | | * @var string |
|---|
| 50 | | */ |
|---|
| 51 | | protected $os; |
|---|
| 52 | | |
|---|
| 53 | | /** |
|---|
| 54 | | * Whether to escape shell command using escapeshellcmd(). |
|---|
| 55 | | * @var boolean |
|---|
| 56 | | */ |
|---|
| 57 | | protected $escape = false; |
|---|
| 58 | | |
|---|
| 59 | | /** |
|---|
| 60 | | * Where to direct output. |
|---|
| 61 | | * @var File |
|---|
| 62 | | */ |
|---|
| 63 | | protected $output; |
|---|
| 64 | | |
|---|
| 65 | | /** |
|---|
| 66 | | * Whether to passthru the output |
|---|
| 67 | | * @var boolean |
|---|
| 68 | | */ |
|---|
| 69 | | protected $passthru = false; |
|---|
| 70 | | |
|---|
| 71 | | /** |
|---|
| 72 | | * Where to direct error output. |
|---|
| 73 | | * @var File |
|---|
| 74 | | */ |
|---|
| 75 | | protected $error; |
|---|
| 76 | | |
|---|
| | 35 | /** |
|---|
| | 36 | * Command to execute. |
|---|
| | 37 | * @var string |
|---|
| | 38 | */ |
|---|
| | 39 | protected $command; |
|---|
| | 40 | |
|---|
| | 41 | /** |
|---|
| | 42 | * Working directory. |
|---|
| | 43 | * @var File |
|---|
| | 44 | */ |
|---|
| | 45 | protected $dir; |
|---|
| | 46 | |
|---|
| | 47 | /** |
|---|
| | 48 | * Operating system. |
|---|
| | 49 | * @var string |
|---|
| | 50 | */ |
|---|
| | 51 | protected $os; |
|---|
| | 52 | |
|---|
| | 53 | /** |
|---|
| | 54 | * Whether to escape shell command using escapeshellcmd(). |
|---|
| | 55 | * @var boolean |
|---|
| | 56 | */ |
|---|
| | 57 | protected $escape = false; |
|---|
| | 58 | |
|---|
| | 59 | /** |
|---|
| | 60 | * Where to direct output. |
|---|
| | 61 | * @var File |
|---|
| | 62 | */ |
|---|
| | 63 | protected $output; |
|---|
| | 64 | |
|---|
| | 65 | /** |
|---|
| | 66 | * Whether to passthru the output |
|---|
| | 67 | * @var boolean |
|---|
| | 68 | */ |
|---|
| | 69 | protected $passthru = false; |
|---|
| | 70 | |
|---|
| | 71 | /** |
|---|
| | 72 | * Where to direct error output. |
|---|
| | 73 | * @var File |
|---|
| | 74 | */ |
|---|
| | 75 | protected $error; |
|---|
| | 76 | |
|---|
| 82 | | |
|---|
| 83 | | /** |
|---|
| 84 | | * Whether to check the return code. |
|---|
| 85 | | * @var boolean |
|---|
| 86 | | */ |
|---|
| 87 | | protected $checkreturn = false; |
|---|
| 88 | | |
|---|
| 89 | | /** |
|---|
| 90 | | * Main method: wraps execute() command. |
|---|
| 91 | | * @return void |
|---|
| 92 | | */ |
|---|
| 93 | | public function main() { |
|---|
| 94 | | $this->execute(); |
|---|
| 95 | | } |
|---|
| 96 | | |
|---|
| 97 | | /** |
|---|
| 98 | | * Executes a program and returns the return code. |
|---|
| 99 | | * Output from command is logged at INFO level. |
|---|
| 100 | | * @return int Return code from execution. |
|---|
| 101 | | */ |
|---|
| 102 | | public function execute() { |
|---|
| 103 | | |
|---|
| 104 | | // test if os match |
|---|
| 105 | | $myos = Phing::getProperty("os.name"); |
|---|
| 106 | | $this->log("Myos = " . $myos, Project::MSG_VERBOSE); |
|---|
| 107 | | if (($this->os !== null) && (strpos($this->os, $myos) === false)) { |
|---|
| 108 | | // this command will be executed only on the specified OS |
|---|
| 109 | | $this->log("Not found in " . $this->os, Project::MSG_VERBOSE); |
|---|
| 110 | | return 0; |
|---|
| 111 | | } |
|---|
| 112 | | |
|---|
| 113 | | if ($this->dir !== null) { |
|---|
| 114 | | if ($this->dir->isDirectory()) { |
|---|
| 115 | | $currdir = getcwd(); |
|---|
| 116 | | @chdir($this->dir->getPath()); |
|---|
| 117 | | } else { |
|---|
| 118 | | throw new BuildException("Can't chdir to:" . $this->dir->__toString()); |
|---|
| 119 | | } |
|---|
| 120 | | } |
|---|
| 121 | | |
|---|
| 122 | | |
|---|
| 123 | | if ($this->escape == true) { |
|---|
| 124 | | // FIXME - figure out whether this is correct behavior |
|---|
| 125 | | $this->command = escapeshellcmd($this->command); |
|---|
| 126 | | } |
|---|
| 127 | | |
|---|
| 128 | | if ($this->error !== null) { |
|---|
| 129 | | $this->command .= ' 2> ' . $this->error->getPath(); |
|---|
| 130 | | $this->log("Writing error output to: " . $this->error->getPath()); |
|---|
| 131 | | } |
|---|
| 132 | | |
|---|
| 133 | | if ($this->output !== null) { |
|---|
| 134 | | $this->command .= ' 1> ' . $this->output->getPath(); |
|---|
| 135 | | $this->log("Writing standard output to: " . $this->output->getPath()); |
|---|
| 136 | | } elseif ($this->spawn) { |
|---|
| | 82 | |
|---|
| | 83 | /** |
|---|
| | 84 | * Property name to set with return value from exec call. |
|---|
| | 85 | * |
|---|
| | 86 | * @var string |
|---|
| | 87 | */ |
|---|
| | 88 | protected $returnProperty; |
|---|
| | 89 | |
|---|
| | 90 | /** |
|---|
| | 91 | * Whether to check the return code. |
|---|
| | 92 | * @var boolean |
|---|
| | 93 | */ |
|---|
| | 94 | protected $checkreturn = false; |
|---|
| | 95 | |
|---|
| | 96 | /** |
|---|
| | 97 | * Main method: wraps execute() command. |
|---|
| | 98 | * @return void |
|---|
| | 99 | */ |
|---|
| | 100 | public function main() { |
|---|
| | 101 | $this->execute(); |
|---|
| | 102 | } |
|---|
| | 103 | |
|---|
| | 104 | /** |
|---|
| | 105 | * Executes a program and returns the return code. |
|---|
| | 106 | * Output from command is logged at INFO level. |
|---|
| | 107 | * @return int Return code from execution. |
|---|
| | 108 | */ |
|---|
| | 109 | public function execute() { |
|---|
| | 110 | |
|---|
| | 111 | // test if os match |
|---|
| | 112 | $myos = Phing::getProperty("os.name"); |
|---|
| | 113 | $this->log("Myos = " . $myos, Project::MSG_VERBOSE); |
|---|
| | 114 | if (($this->os !== null) && (strpos($this->os, $myos) === false)) { |
|---|
| | 115 | // this command will be executed only on the specified OS |
|---|
| | 116 | $this->log("Not found in " . $this->os, Project::MSG_VERBOSE); |
|---|
| | 117 | return 0; |
|---|
| | 118 | } |
|---|
| | 119 | |
|---|
| | 120 | if ($this->dir !== null) { |
|---|
| | 121 | if ($this->dir->isDirectory()) { |
|---|
| | 122 | $currdir = getcwd(); |
|---|
| | 123 | @chdir($this->dir->getPath()); |
|---|
| | 124 | } else { |
|---|
| | 125 | throw new BuildException("Can't chdir to:" . $this->dir->__toString()); |
|---|
| | 126 | } |
|---|
| | 127 | } |
|---|
| | 128 | |
|---|
| | 129 | |
|---|
| | 130 | if ($this->escape == true) { |
|---|
| | 131 | // FIXME - figure out whether this is correct behavior |
|---|
| | 132 | $this->command = escapeshellcmd($this->command); |
|---|
| | 133 | } |
|---|
| | 134 | |
|---|
| | 135 | if ($this->error !== null) { |
|---|
| | 136 | $this->command .= ' 2> ' . $this->error->getPath(); |
|---|
| | 137 | $this->log("Writing error output to: " . $this->error->getPath()); |
|---|
| | 138 | } |
|---|
| | 139 | |
|---|
| | 140 | if ($this->output !== null) { |
|---|
| | 141 | $this->command .= ' 1> ' . $this->output->getPath(); |
|---|
| | 142 | $this->log("Writing standard output to: " . $this->output->getPath()); |
|---|
| | 143 | } elseif ($this->spawn) { |
|---|
| 155 | | |
|---|
| 156 | | $output = array(); |
|---|
| 157 | | $return = null; |
|---|
| 158 | | exec($this->command, $output, $return); |
|---|
| 159 | | |
|---|
| 160 | | if ($this->dir !== null) { |
|---|
| 161 | | @chdir($currdir); |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | foreach($output as $line) { |
|---|
| 165 | | $this->log($line, ($this->passthru ? Project::MSG_INFO : Project::MSG_VERBOSE)); |
|---|
| 166 | | } |
|---|
| 167 | | |
|---|
| 168 | | if($return != 0 && $this->checkreturn) |
|---|
| 169 | | { |
|---|
| 170 | | throw new BuildException("Task exited with code $return"); |
|---|
| 171 | | } |
|---|
| 172 | | |
|---|
| 173 | | return $return; |
|---|
| 174 | | } |
|---|
| 175 | | |
|---|
| 176 | | /** |
|---|
| 177 | | * The command to use. |
|---|
| 178 | | * @param mixed $command String or string-compatible (e.g. w/ __toString()). |
|---|
| 179 | | */ |
|---|
| 180 | | function setCommand($command) { |
|---|
| 181 | | $this->command = "" . $command; |
|---|
| 182 | | } |
|---|
| 183 | | |
|---|
| 184 | | /** |
|---|
| 185 | | * Whether to use escapeshellcmd() to escape command. |
|---|
| 186 | | * @param boolean $escape |
|---|
| 187 | | */ |
|---|
| 188 | | function setEscape($escape) { |
|---|
| 189 | | $this->escape = (bool) $escape; |
|---|
| 190 | | } |
|---|
| 191 | | |
|---|
| 192 | | /** |
|---|
| 193 | | * Specify the working directory for executing this command. |
|---|
| 194 | | * @param PhingFile $dir |
|---|
| 195 | | */ |
|---|
| 196 | | function setDir(PhingFile $dir) { |
|---|
| 197 | | $this->dir = $dir; |
|---|
| 198 | | } |
|---|
| 199 | | |
|---|
| 200 | | /** |
|---|
| 201 | | * Specify OS (or muliple OS) that must match in order to execute this command. |
|---|
| 202 | | * @param string $os |
|---|
| 203 | | */ |
|---|
| 204 | | function setOs($os) { |
|---|
| 205 | | $this->os = (string) $os; |
|---|
| 206 | | } |
|---|
| 207 | | |
|---|
| 208 | | /** |
|---|
| 209 | | * File to which output should be written. |
|---|
| 210 | | * @param PhingFile $output |
|---|
| 211 | | */ |
|---|
| 212 | | function setOutput(PhingFile $f) { |
|---|
| 213 | | $this->output = $f; |
|---|
| 214 | | } |
|---|
| 215 | | |
|---|
| 216 | | /** |
|---|
| 217 | | * File to which error output should be written. |
|---|
| 218 | | * @param PhingFile $output |
|---|
| 219 | | */ |
|---|
| 220 | | function setError(PhingFile $f) { |
|---|
| 221 | | $this->error = $f; |
|---|
| 222 | | } |
|---|
| 223 | | |
|---|
| 224 | | /** |
|---|
| 225 | | * Whether to use passthru the output. |
|---|
| 226 | | * @param boolean $passthru |
|---|
| 227 | | */ |
|---|
| 228 | | function setPassthru($passthru) { |
|---|
| 229 | | $this->passthru = (bool) $passthru; |
|---|
| 230 | | } |
|---|
| 231 | | |
|---|
| | 162 | |
|---|
| | 163 | $output = array(); |
|---|
| | 164 | $return = null; |
|---|
| | 165 | exec($this->command, $output, $return); |
|---|
| | 166 | |
|---|
| | 167 | if ($this->dir !== null) { |
|---|
| | 168 | @chdir($currdir); |
|---|
| | 169 | } |
|---|
| | 170 | |
|---|
| | 171 | foreach($output as $line) { |
|---|
| | 172 | $this->log($line, ($this->passthru ? Project::MSG_INFO : Project::MSG_VERBOSE)); |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| | 175 | if ($this->returnProperty) { |
|---|
| | 176 | $this->project->setProperty($this->returnProperty, $return); |
|---|
| | 177 | } |
|---|
| | 178 | |
|---|
| | 179 | if($return != 0 && $this->checkreturn) { |
|---|
| | 180 | throw new BuildException("Task exited with code $return"); |
|---|
| | 181 | } |
|---|
| | 182 | |
|---|
| | 183 | return $return; |
|---|
| | 184 | } |
|---|
| | 185 | |
|---|
| | 186 | /** |
|---|
| | 187 | * The command to use. |
|---|
| | 188 | * @param mixed $command String or string-compatible (e.g. w/ __toString()). |
|---|
| | 189 | */ |
|---|
| | 190 | function setCommand($command) { |
|---|
| | 191 | $this->command = "" . $command; |
|---|
| | 192 | } |
|---|
| | 193 | |
|---|
| | 194 | /** |
|---|
| | 195 | * Whether to use escapeshellcmd() to escape command. |
|---|
| | 196 | * @param boolean $escape |
|---|
| | 197 | */ |
|---|
| | 198 | function setEscape($escape) { |
|---|
| | 199 | $this->escape = (bool) $escape; |
|---|
| | 200 | } |
|---|
| | 201 | |
|---|
| | 202 | /** |
|---|
| | 203 | * Specify the working directory for executing this command. |
|---|
| | 204 | * @param PhingFile $dir |
|---|
| | 205 | */ |
|---|
| | 206 | function setDir(PhingFile $dir) { |
|---|
| | 207 | $this->dir = $dir; |
|---|
| | 208 | } |
|---|
| | 209 | |
|---|
| | 210 | /** |
|---|
| | 211 | * Specify OS (or muliple OS) that must match in order to execute this command. |
|---|
| | 212 | * @param string $os |
|---|
| | 213 | */ |
|---|
| | 214 | function setOs($os) { |
|---|
| | 215 | $this->os = (string) $os; |
|---|
| | 216 | } |
|---|
| | 217 | |
|---|
| | 218 | /** |
|---|
| | 219 | * File to which output should be written. |
|---|
| | 220 | * @param PhingFile $output |
|---|
| | 221 | */ |
|---|
| | 222 | function setOutput(PhingFile $f) { |
|---|
| | 223 | $this->output = $f; |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | /** |
|---|
| | 227 | * File to which error output should be written. |
|---|
| | 228 | * @param PhingFile $output |
|---|
| | 229 | */ |
|---|
| | 230 | function setError(PhingFile $f) { |
|---|
| | 231 | $this->error = $f; |
|---|
| | 232 | } |
|---|
| | 233 | |
|---|
| | 234 | /** |
|---|
| | 235 | * Whether to use passthru the output. |
|---|
| | 236 | * @param boolean $passthru |
|---|
| | 237 | */ |
|---|
| | 238 | function setPassthru($passthru) { |
|---|
| | 239 | $this->passthru = (bool) $passthru; |
|---|
| | 240 | } |
|---|
| | 241 | |
|---|