ExecTask.php 7.9 KB

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