VersionTask.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /*
  3. * $Id: VersionTask.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. * VersionTask
  24. *
  25. * Increments a three-part version number from a given file
  26. * and writes it back to the file.
  27. * Incrementing is based on given releasetype, which can be one
  28. * of Major, Minor and Bugfix.
  29. * Resulting version number is also published under supplied property.
  30. *
  31. * @author Mike Wittje <mw@mike.wittje.de>
  32. * @version $Id: VersionTask.php 905 2010-10-05 16:28:03Z mrook $ $Rev $Revision: 905 $ $Date: 2010-10-05 18:28:03 +0200 (Tue, 05 Oct 2010) $ $Author: mrook $
  33. * @package phing.tasks.ext
  34. */
  35. class VersionTask extends Task
  36. {
  37. /**
  38. * Property for Releasetype
  39. * @var string $releasetype
  40. */
  41. private $releasetype;
  42. /**
  43. * Property for File
  44. * @var PhingFile file
  45. */
  46. private $file;
  47. /**
  48. * Property to be set
  49. * @var string $property
  50. */
  51. private $property;
  52. /* Allowed Releastypes */
  53. const RELEASETYPE_MAJOR = 'MAJOR';
  54. const RELEASETYPE_MINOR = 'MINOR';
  55. const RELEASETYPE_BUGFIX = 'BUGFIX';
  56. /**
  57. * Set Property for Releasetype (Minor, Major, Bugfix)
  58. * @param string $releasetype
  59. */
  60. public function setReleasetype($releasetype)
  61. {
  62. $this->releasetype = strtoupper($releasetype);
  63. }
  64. /**
  65. * Set Property for File containing versioninformation
  66. * @param PhingFile $file
  67. */
  68. public function setFile($file)
  69. {
  70. $this->file = $file;
  71. }
  72. /**
  73. * Set
  74. * @param $property
  75. * @return
  76. */
  77. public function setProperty($property)
  78. {
  79. $this->property = $property;
  80. }
  81. /**
  82. * Main-Method for the Task
  83. *
  84. * @return void
  85. * @throws BuildException
  86. */
  87. public function main()
  88. {
  89. // check supplied attributes
  90. $this->checkReleasetype();
  91. $this->checkFile();
  92. $this->checkProperty();
  93. // read file
  94. $filecontent = file_get_contents($this->file);
  95. // get new version
  96. $newVersion = $this->getVersion($filecontent);
  97. // write new Version to file
  98. file_put_contents($this->file, $newVersion);
  99. // publish new version number as property
  100. $this->project->setProperty($this->property, $newVersion);
  101. }
  102. /**
  103. * Returns new version number corresponding to Release type
  104. *
  105. * @param string $filecontent
  106. * @return string
  107. */
  108. private function getVersion($filecontent)
  109. {
  110. // init
  111. $newVersion = '';
  112. // Extract version
  113. list($major, $minor, $bugfix) = explode(".", $filecontent);
  114. // Return new version number
  115. switch ($this->releasetype) {
  116. case self::RELEASETYPE_MAJOR:
  117. $newVersion = sprintf("%d.%d.%d", ++$major,
  118. 0,
  119. 0);
  120. break;
  121. case self::RELEASETYPE_MINOR:
  122. $newVersion = sprintf("%d.%d.%d", $major,
  123. ++$minor,
  124. 0);
  125. break;
  126. case self::RELEASETYPE_BUGFIX:
  127. $newVersion = sprintf("%d.%d.%d", $major,
  128. $minor,
  129. ++$bugfix);
  130. break;
  131. }
  132. return $newVersion;
  133. }
  134. /**
  135. * checks releasetype attribute
  136. * @return void
  137. * @throws BuildException
  138. */
  139. private function checkReleasetype()
  140. {
  141. // check Releasetype
  142. if (is_null($this->releasetype)) {
  143. throw new BuildException('releasetype attribute is required', $this->location);
  144. }
  145. // known releasetypes
  146. $releaseTypes = array(
  147. self::RELEASETYPE_MAJOR,
  148. self::RELEASETYPE_MINOR,
  149. self::RELEASETYPE_BUGFIX
  150. );
  151. if (!in_array($this->releasetype, $releaseTypes)) {
  152. throw new BuildException(sprintf('Unknown Releasetype %s..Must be one of Major, Minor or Bugfix',
  153. $this->releasetype), $this->location);
  154. }
  155. }
  156. /**
  157. * checks file attribute
  158. * @return void
  159. * @throws BuildException
  160. */
  161. private function checkFile()
  162. {
  163. // check File
  164. if ($this->file === null ||
  165. strlen($this->file) == 0) {
  166. throw new BuildException('You must specify a file containing the version number', $this->location);
  167. }
  168. $content = file_get_contents($this->file);
  169. if (strlen($content) == 0) {
  170. throw new BuildException(sprintf('Supplied file %s is empty', $this->file), $this->location);
  171. }
  172. // check for three-part number
  173. $split = explode('.', $content);
  174. if (count($split) !== 3) {
  175. throw new BuildException('Unknown version number format', $this->location);
  176. }
  177. }
  178. /**
  179. * checks property attribute
  180. * @return void
  181. * @throws BuildException
  182. */
  183. private function checkProperty()
  184. {
  185. if (is_null($this->property) ||
  186. strlen($this->property) === 0) {
  187. throw new BuildException('Property for publishing version number is not set', $this->location);
  188. }
  189. }
  190. }