PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/Build/BuilderElement/Task/Exec.php

http://github.com/matamouros/cintient
PHP | 245 lines | 174 code | 8 blank | 63 comment | 23 complexity | e48e45ec99d75210036f327b8e878f41 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. *
  4. * Cintient, Continuous Integration made simple.
  5. * Copyright (c) 2010-2012, Pedro Mata-Mouros <pedro.matamouros@gmail.com>
  6. *
  7. * This file is part of Cintient.
  8. *
  9. * Cintient is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Cintient is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with Cintient. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * The exec task is perhaps the single most universal and important task.
  25. * All other tasks could be, in some way or another be specified by an
  26. * exec task.
  27. *
  28. * Usage:
  29. *
  30. * $exec = new Build_BuilderElement_Task_Exec();
  31. * $exec->setExecutable('php');
  32. * $exec->setArgs('runMe.php arg1 arg2');
  33. * $exec->setDir('/tmp/');
  34. * $exec->setOutputProperty('fooBar');
  35. * echo $exec->toString('ant');
  36. *
  37. * @package Build
  38. * @subpackage Task
  39. * @author Pedro Mata-Mouros Fonseca <pedro.matamouros@gmail.com>
  40. * @copyright 2010-2011, Pedro Mata-Mouros Fonseca.
  41. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPLv3 or later.
  42. * @version $LastChangedRevision$
  43. * @link $HeadURL$
  44. * Changed by $LastChangedBy$
  45. * Changed on $LastChangedDate$
  46. */
  47. class Build_BuilderElement_Task_Exec extends Build_BuilderElement
  48. {
  49. protected $_executable;
  50. protected $_args; // The arguments to the executable command, if any, a space separated string
  51. protected $_baseDir; // The directory in which the command should be executed in
  52. protected $_outputProperty; // Log the command's output to the variable with this name
  53. public function __construct()
  54. {
  55. parent::__construct();
  56. $this->_executable = null;
  57. $this->_args = null;
  58. $this->_baseDir = null;
  59. $this->_outputProperty = null;
  60. }
  61. /**
  62. * Creates a new instance of this builder element, with default values.
  63. */
  64. static public function create()
  65. {
  66. return new self();
  67. }
  68. /**
  69. * Setter. Makes sure <code>$dir</code> always ends in a valid
  70. * <code>DIRECTORY_SEPARATOR</code> token.
  71. *
  72. * @param string $dir
  73. */
  74. public function setBaseDir($dir)
  75. {
  76. if (!empty($dir) && strpos($dir, DIRECTORY_SEPARATOR, (strlen($dir)-1)) === false) {
  77. $dir .= DIRECTORY_SEPARATOR;
  78. }
  79. $this->_baseDir = $dir;
  80. }
  81. public function toAnt()
  82. {
  83. if (!$this->isActive()) {
  84. return true;
  85. }
  86. if (!$this->getExecutable()) {
  87. SystemEvent::raise(SystemEvent::ERROR, 'Executable not set for exec task.', __METHOD__);
  88. return false;
  89. }
  90. $xml = new XmlDoc();
  91. $xml->startElement('exec');
  92. if ($this->getOutputProperty()) {
  93. $xml->writeAttribute('outputproperty', $this->getOutputProperty());
  94. }
  95. if ($this->getBaseDir()) {
  96. $xml->writeAttribute('dir', $this->getBaseDir());
  97. }
  98. if ($this->getFailOnError() !== null) {
  99. $xml->writeAttribute('failonerror', ($this->getFailOnError()?'true':'false'));
  100. }
  101. $xml->writeAttribute('executable', $this->getExecutable());
  102. if ($this->getArgs()) {
  103. $args = $this->getArgs();
  104. foreach ($args as $arg) {
  105. $xml->startElement('arg');
  106. $xml->writeAttribute('line', $arg);
  107. $xml->endElement();
  108. }
  109. }
  110. $xml->endElement();
  111. return $xml->flush();
  112. }
  113. public function toHtml(Array $_ = array(), Array $__ = array())
  114. {
  115. if (!$this->isVisible()) {
  116. return true;
  117. }
  118. $callbacks = array(
  119. array(
  120. 'cb' => 'getHtmlInputText',
  121. 'name' => 'executable',
  122. 'value' => $this->getExecutable()
  123. ),
  124. array(
  125. 'cb' => 'getHtmlInputText',
  126. 'name' => 'args',
  127. 'value' => $this->getArgs(),
  128. 'help' => 'Space separated.'
  129. ),
  130. array(
  131. 'cb' => 'getHtmlInputText',
  132. 'name' => 'basedir',
  133. 'label' =>
  134. 'Base dir',
  135. 'value' => $this->getBaseDir()
  136. ),
  137. array(
  138. 'cb' => 'getHtmlInputText',
  139. 'name' => 'outputProperty',
  140. 'label' => 'Output property',
  141. 'value' => $this->getOutputProperty()
  142. ),
  143. );
  144. parent::toHtml(array('title' => 'Exec'), $callbacks);
  145. }
  146. public function toPhing()
  147. {
  148. if (!$this->isActive()) {
  149. return '';
  150. }
  151. if (!$this->getExecutable()) {
  152. SystemEvent::raise(SystemEvent::ERROR, 'Executable not set for exec task.', __METHOD__);
  153. return false;
  154. }
  155. $xml = new XmlDoc();
  156. $xml->startElement('exec');
  157. if ($this->getOutputProperty()) {
  158. $xml->writeAttribute('outputProperty', $this->getOutputProperty());
  159. }
  160. if ($this->getBaseDir()) {
  161. $xml->writeAttribute('dir', $this->getBaseDir());
  162. }
  163. $args = '';
  164. if ($this->getArgs()) {
  165. $args = ' ' . implode(' ', $this->getArgs());
  166. }
  167. $xml->writeAttribute('command', $this->getExecutable() . $args);
  168. $xml->endElement();
  169. return $xml->flush();
  170. }
  171. public function toPhp(Array &$context = array())
  172. {
  173. if (!$this->isActive()) {
  174. return true;
  175. }
  176. $php = '';
  177. if (!$this->getExecutable()) {
  178. SystemEvent::raise(SystemEvent::ERROR, 'Executable not set for exec task.', __METHOD__);
  179. return false;
  180. }
  181. $php .= "
  182. \$GLOBALS['result']['task'] = 'exec';
  183. \$getBaseDir = '';
  184. ";
  185. if ($this->getBaseDir()) {
  186. $php .= "
  187. \$getBaseDir = \"cd \" . expandStr('{$this->getBaseDir()}') . \"; \";
  188. ";
  189. }
  190. $php .= "
  191. \$args = '';
  192. ";
  193. if ($this->getArgs()) {
  194. $php .= "
  195. \$getArgs = expandStr(' {$this->getArgs()}');
  196. ";
  197. }
  198. $php .= "
  199. \$getExecutable = expandStr('{$this->getExecutable()}');
  200. \$GLOBALS['result']['task'] = 'exec';
  201. output(\"Executing '\$getBaseDir\$getExecutable\$getArgs'.\");
  202. \$ret = exec(\"\$getBaseDir\$getExecutable\$getArgs\", \$lines, \$retval);
  203. foreach (\$lines as \$line) {
  204. output(\$line);
  205. }
  206. ";
  207. if ($this->getOutputProperty()) {
  208. $php .= "
  209. \$GLOBALS['properties']['{$this->getOutputProperty()}_{$context['id']}'] = \$ret;
  210. ";
  211. }
  212. $php .= "
  213. if (\$retval > 0) {
  214. output('Failed.');
  215. if ({$this->getFailOnError()}) {
  216. \$GLOBALS['result']['ok'] = false;
  217. return false;
  218. } else {
  219. \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;
  220. }
  221. } else {
  222. \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;
  223. output('Success.');
  224. }
  225. ";
  226. //TODO: bullet proof this for boolean falses (they're not showing up)
  227. /*
  228. $php .= "if ({$this->getFailOnError()} && !\$ret) {
  229. \$GLOBALS['result']['ok'] = false;
  230. return false;
  231. }
  232. \$GLOBALS['result']['ok'] = true;
  233. return true;
  234. ";*/
  235. return $php;
  236. }
  237. }