SvnBaseTask.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /*
  3. * $Id: SvnBaseTask.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. include_once 'phing/Task.php';
  22. /**
  23. * Base class for Subversion tasks
  24. *
  25. * @author Michiel Rook <michiel.rook@gmail.com>
  26. * @author Andrew Eddie <andrew.eddie@jamboworks.com>
  27. * @version $Id: SvnBaseTask.php 905 2010-10-05 16:28:03Z mrook $
  28. * @package phing.tasks.ext.svn
  29. * @see VersionControl_SVN
  30. * @since 2.2.0
  31. */
  32. abstract class SvnBaseTask extends Task
  33. {
  34. private $workingCopy = "";
  35. private $repositoryUrl = "";
  36. private $svnPath = "/usr/bin/svn";
  37. private $svn = NULL;
  38. private $mode = "";
  39. private $svnArgs = array();
  40. private $svnSwitches = array();
  41. private $toDir = "";
  42. /**
  43. * Initialize Task.
  44. * This method includes any necessary SVN libraries and triggers
  45. * appropriate error if they cannot be found. This is not done in header
  46. * because we may want this class to be loaded w/o triggering an error.
  47. */
  48. function init() {
  49. include_once 'VersionControl/SVN.php';
  50. if (!class_exists('VersionControl_SVN')) {
  51. throw new Exception("The SVN tasks depend on PEAR VersionControl_SVN package being installed.");
  52. }
  53. }
  54. /**
  55. * Sets the path to the workingcopy
  56. */
  57. function setWorkingCopy($workingCopy)
  58. {
  59. $this->workingCopy = $workingCopy;
  60. }
  61. /**
  62. * Returns the path to the workingcopy
  63. */
  64. function getWorkingCopy()
  65. {
  66. return $this->workingCopy;
  67. }
  68. /**
  69. * Sets the path/URI to the repository
  70. */
  71. function setRepositoryUrl($repositoryUrl)
  72. {
  73. $this->repositoryUrl = $repositoryUrl;
  74. }
  75. /**
  76. * Returns the path/URI to the repository
  77. */
  78. function getRepositoryUrl()
  79. {
  80. return $this->repositoryUrl;
  81. }
  82. /**
  83. * Sets the path to the SVN executable
  84. */
  85. function setSvnPath($svnPath)
  86. {
  87. $this->svnPath = $svnPath;
  88. }
  89. /**
  90. * Returns the path to the SVN executable
  91. */
  92. function getSvnPath()
  93. {
  94. return $this->svnPath;
  95. }
  96. //
  97. // Args
  98. //
  99. /**
  100. * Sets the path to export/checkout to
  101. */
  102. function setToDir($toDir)
  103. {
  104. $this->toDir = $toDir;
  105. }
  106. /**
  107. * Returns the path to export/checkout to
  108. */
  109. function getToDir()
  110. {
  111. return $this->toDir;
  112. }
  113. //
  114. // Switches
  115. //
  116. /**
  117. * Sets the force switch
  118. */
  119. function setForce($value)
  120. {
  121. $this->svnSwitches['force'] = $value;
  122. }
  123. /**
  124. * Returns the force switch
  125. */
  126. function getForce()
  127. {
  128. return isset( $this->svnSwitches['force'] ) ? $this->svnSwitches['force'] : '';
  129. }
  130. /**
  131. * Sets the username of the user to export
  132. */
  133. function setUsername($value)
  134. {
  135. $this->svnSwitches['username'] = $value;
  136. }
  137. /**
  138. * Returns the username
  139. */
  140. function getUsername()
  141. {
  142. return isset( $this->svnSwitches['username'] ) ? $this->svnSwitches['username'] : '';
  143. }
  144. /**
  145. * Sets the password of the user to export
  146. */
  147. function setPassword($value)
  148. {
  149. $this->svnSwitches['password'] = $value;
  150. }
  151. /**
  152. * Returns the password
  153. */
  154. function getPassword()
  155. {
  156. return isset( $this->svnSwitches['password'] ) ? $this->svnSwitches['password'] : '';
  157. }
  158. /**
  159. * Sets the no-auth-cache switch
  160. */
  161. function setNoCache($value)
  162. {
  163. $this->svnSwitches['no-auth-cache'] = $value;
  164. }
  165. /**
  166. * Returns the no-auth-cache switch
  167. */
  168. function getNoCache()
  169. {
  170. return isset( $this->svnSwitches['no-auth-cache'] ) ? $this->svnSwitches['no-auth-cache'] : '';
  171. }
  172. /**
  173. * Sets the non-recursive switch
  174. */
  175. function setRecursive($value)
  176. {
  177. $this->svnSwitches['non-recursive'] = is_bool($value) ? !$value : TRUE;
  178. }
  179. /**
  180. * Returns the non-recursive switch
  181. */
  182. function getRecursive()
  183. {
  184. return isset( $this->svnSwitches['non-recursive'] ) ? $this->svnSwitches['non-recursive'] : '';
  185. }
  186. /**
  187. * Sets the ignore-externals switch
  188. */
  189. function setIgnoreExternals($value)
  190. {
  191. $this->svnSwitches['ignore-externals'] = $value;
  192. }
  193. /**
  194. * Returns the ignore-externals switch
  195. */
  196. function getIgnoreExternals()
  197. {
  198. return isset( $this->svnSwitches['ignore-externals'] ) ? $this->svnSwitches['ignore-externals'] : '';
  199. }
  200. /**
  201. * Creates a VersionControl_SVN class based on $mode
  202. *
  203. * @param string The SVN mode to use (info, export, checkout, ...)
  204. * @throws BuildException
  205. */
  206. protected function setup($mode)
  207. {
  208. $this->mode = $mode;
  209. // Set up runtime options. Will be passed to all
  210. // subclasses.
  211. $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_ASSOC, 'svn_path' => escapeshellarg($this->getSvnPath()));
  212. // Pass array of subcommands we need to factory
  213. $this->svn = VersionControl_SVN::factory($mode, $options);
  214. $this->svn->use_escapeshellcmd = false;
  215. if (!empty($this->repositoryUrl))
  216. {
  217. $this->svnArgs = array($this->repositoryUrl);
  218. }
  219. else
  220. if (!empty($this->workingCopy))
  221. {
  222. if (is_dir($this->workingCopy))
  223. {
  224. if (in_array(".svn", scandir($this->workingCopy)))
  225. {
  226. $this->svnArgs = array(escapeshellarg($this->workingCopy));
  227. }
  228. else
  229. {
  230. throw new BuildException("'".$this->workingCopy."' doesn't seem to be a working copy");
  231. }
  232. }
  233. else
  234. if ($mode=='info' )
  235. {
  236. if (is_file($this->workingCopy))
  237. {
  238. $this->svnArgs = array(escapeshellarg($this->workingCopy));
  239. }
  240. else
  241. {
  242. throw new BuildException("'".$this->workingCopy."' is not a directory nor a file");
  243. }
  244. }
  245. else
  246. {
  247. throw new BuildException("'".$this->workingCopy."' is not a directory");
  248. }
  249. }
  250. }
  251. /**
  252. * Executes the constructed VersionControl_SVN instance
  253. *
  254. * @param array Additional arguments to pass to SVN.
  255. * @param array Switches to pass to SVN.
  256. * @return string Output generated by SVN.
  257. */
  258. protected function run($args = array(), $switches = array())
  259. {
  260. $svnstack = PEAR_ErrorStack::singleton('VersionControl_SVN');
  261. $tempArgs = $this->svnArgs;
  262. $tempArgs = array_merge($tempArgs, $args);
  263. $tempSwitches = $this->svnSwitches;
  264. $tempSwitches = array_merge($tempSwitches, $switches);
  265. if ($output = $this->svn->run($tempArgs, $tempSwitches))
  266. {
  267. return $output;
  268. }
  269. else
  270. {
  271. if (count($errs = $svnstack->getErrors()))
  272. {
  273. $err = current($errs);
  274. throw new BuildException("Failed to run the 'svn " . $this->mode . "' command: " . $err['params']['errstr']);
  275. }
  276. }
  277. }
  278. }