SvnLastRevisionTask.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * $Id: SvnLastRevisionTask.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. require_once 'phing/tasks/ext/svn/SvnBaseTask.php';
  23. /**
  24. * Stores the number of the last revision of a workingcopy in a property
  25. *
  26. * @author Michiel Rook <michiel.rook@gmail.com>
  27. * @version $Id: SvnLastRevisionTask.php 905 2010-10-05 16:28:03Z mrook $
  28. * @package phing.tasks.ext.svn
  29. * @see VersionControl_SVN
  30. * @since 2.1.0
  31. */
  32. class SvnLastRevisionTask extends SvnBaseTask
  33. {
  34. private $propertyName = "svn.lastrevision";
  35. private $forceCompatible = false;
  36. /**
  37. * Sets the name of the property to use
  38. */
  39. function setPropertyName($propertyName)
  40. {
  41. $this->propertyName = $propertyName;
  42. }
  43. /**
  44. * Returns the name of the property to use
  45. */
  46. function getPropertyName()
  47. {
  48. return $this->propertyName;
  49. }
  50. /**
  51. * Sets whether to force compatibility with older SVN versions (< 1.2)
  52. */
  53. public function forceCompatible($force)
  54. {
  55. $this->forceCompatible = (bool) $force;
  56. }
  57. /**
  58. * The main entry point
  59. *
  60. * @throws BuildException
  61. */
  62. function main()
  63. {
  64. $this->setup('info');
  65. if ($this->forceCompatible)
  66. {
  67. $output = $this->run();
  68. if (preg_match('/Rev:[\s]+([\d]+)/', $output, $matches))
  69. {
  70. $this->project->setProperty($this->getPropertyName(), $matches[1]);
  71. }
  72. else
  73. {
  74. throw new BuildException("Failed to parse the output of 'svn info'.");
  75. }
  76. }
  77. else
  78. {
  79. $output = $this->run(array('--xml'));
  80. if ($xmlObj = @simplexml_load_string($output))
  81. {
  82. $lastRevision = (int)$xmlObj->entry['revision'];
  83. $this->project->setProperty($this->getPropertyName(), $lastRevision);
  84. }
  85. else
  86. {
  87. throw new BuildException("Failed to parse the output of 'svn info --xml'.");
  88. }
  89. }
  90. }
  91. }