PearPackage2Task.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /*
  3. * $Id: PearPackage2Task.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/tasks/ext/PearPackageTask.php';
  22. /**
  23. * A task to create a PEAR package.xml version 2.0 file.
  24. *
  25. * This class uses the PEAR_PackageFileManager2 class to perform the work.
  26. *
  27. * This class is designed to be very flexible -- i.e. account for changes to the package.xml w/o
  28. * requiring changes to this class. We've accomplished this by having generic <option> and <mapping>
  29. * nested elements. All options are set using PEAR_PackageFileManager2::setOptions().
  30. *
  31. * The <option> tag is used to set a simple option value.
  32. * <code>
  33. * <option name="option_name" value="option_value"/>
  34. * or <option name="option_name">option_value</option>
  35. * </code>
  36. *
  37. * The <mapping> tag represents a complex data type. You can use nested <element> (and nested <element> with
  38. * <element> tags) to represent the full complexity of the structure. Bear in mind that what you are creating
  39. * will be mapped to an associative array that will be passed in via PEAR_PackageFileManager2::setOptions().
  40. * <code>
  41. * <mapping name="option_name">
  42. * <element key="key_name" value="key_val"/>
  43. * <element key="key_name" value="key_val"/>
  44. * </mapping>
  45. * </code>
  46. *
  47. * Here's an over-simple example of how this could be used:
  48. * <code>
  49. * <pearpkg2 name="phing" dir="${build.src.dir}">
  50. * <fileset dir="src">
  51. * <include name="**"/>
  52. * </fileset>
  53. * <option name="outputdirectory" value="./build"/>
  54. * <option name="packagefile" value="package2.xml"/>
  55. * <option name="packagedirectory" value="./${build.dist.dir}"/>
  56. * <option name="baseinstalldir" value="${pkg.prefix}"/>
  57. * <option name="channel" value="my.pear-channel.com"/>
  58. * <option name="summary" value="${pkg.summary}"/>
  59. * <option name="description" value="${pkg.description}"/>
  60. * <option name="apiversion" value="${pkg.version}"/>
  61. * <option name="apistability" value="beta"/>
  62. * <option name="releaseversion" value="${pkg.version}"/>
  63. * <option name="releasestability" value="beta"/>
  64. * <option name="license" value="none"/>
  65. * <option name="phpdep" value="5.0.0"/>
  66. * <option name="pearinstallerdep" value="1.4.6"/>
  67. * <option name="packagetype" value="php"/>
  68. * <option name="notes" value="${pkg.relnotes}"/>
  69. * <mapping name="maintainers">
  70. * <element>
  71. * <element key="handle" value="hlellelid"/>
  72. * <element key="name" value="Hans"/>
  73. * <element key="email" value="hans@xmpl.org"/>
  74. * <element key="role" value="lead"/>
  75. * </element>
  76. * </mapping>
  77. * </pearpkg2>
  78. * </code>
  79. *
  80. * Look at the build.xml in the Phing base directory (assuming you have the full distro / CVS version of Phing) to
  81. * see a more complete example of how to call this script.
  82. *
  83. * @author Stuart Binge <stuart.binge@complinet.com>
  84. * @author Hans Lellelid <hans@xmpl.org>
  85. * @package phing.tasks.ext
  86. * @version $Id: PearPackage2Task.php 905 2010-10-05 16:28:03Z mrook $
  87. */
  88. class PearPackage2Task extends PearPackageTask {
  89. public function init() {
  90. include_once 'PEAR/PackageFileManager2.php';
  91. if (!class_exists('PEAR_PackageFileManager2')) {
  92. throw new BuildException("You must have installed PEAR_PackageFileManager in order to create a PEAR package.xml version 2.0 file.");
  93. }
  94. }
  95. protected function setVersion2Options()
  96. {
  97. $this->pkg->setPackage($this->package);
  98. $this->pkg->setDate(strftime('%Y-%m-%d'));
  99. $this->pkg->setTime(strftime('%H:%M:%S'));
  100. $newopts = array();
  101. foreach ($this->options as $opt) {
  102. switch ($opt->getName()) {
  103. case 'summary':
  104. $this->pkg->setSummary($opt->getValue());
  105. break;
  106. case 'description':
  107. $this->pkg->setDescription($opt->getValue());
  108. break;
  109. case 'uri':
  110. $this->pkg->setUri($opt->getValue());
  111. break;
  112. case 'license':
  113. $this->pkg->setLicense($opt->getValue());
  114. break;
  115. case 'channel':
  116. $this->pkg->setChannel($opt->getValue());
  117. break;
  118. case 'apiversion':
  119. $this->pkg->setAPIVersion($opt->getValue());
  120. break;
  121. case 'releaseversion':
  122. $this->pkg->setReleaseVersion($opt->getValue());
  123. break;
  124. case 'releasestability':
  125. $this->pkg->setReleaseStability($opt->getValue());
  126. break;
  127. case 'apistability':
  128. $this->pkg->setAPIStability($opt->getValue());
  129. break;
  130. case 'notes':
  131. $this->pkg->setNotes($opt->getValue());
  132. break;
  133. case 'packagetype':
  134. $this->pkg->setPackageType($opt->getValue());
  135. break;
  136. case 'phpdep':
  137. $this->pkg->setPhpDep($opt->getValue());
  138. break;
  139. case 'pearinstallerdep':
  140. $this->pkg->setPearinstallerDep($opt->getValue());
  141. break;
  142. default:
  143. $newopts[] = $opt;
  144. break;
  145. }
  146. }
  147. $this->options = $newopts;
  148. $newmaps = array();
  149. foreach ($this->mappings as $map) {
  150. switch ($map->getName()) {
  151. case 'deps':
  152. $deps = $map->getValue();
  153. foreach ($deps as $dep) {
  154. $type = isset($dep['optional']) ? 'optional' : 'required';
  155. $min = isset($dep['min']) ? $dep['min'] : $dep['version'];
  156. $max = isset($dep['max']) ? $dep['max'] : $dep['version'];
  157. $rec = isset($dep['recommended']) ? $dep['recommended'] : $dep['version'];
  158. $channel = isset($dep['channel']) ? $dep['channel'] : false;
  159. $uri = isset($dep['uri']) ? $dep['uri'] : false;
  160. if (!empty($channel)) {
  161. $this->pkg->addPackageDepWithChannel(
  162. $type, $dep['name'], $channel, $min, $max, $rec
  163. );
  164. } elseif (!empty($uri)) {
  165. $this->pkg->addPackageDepWithUri(
  166. $type, $dep['name'], $uri
  167. );
  168. }
  169. };
  170. break;
  171. case 'extdeps':
  172. $deps = $map->getValue();
  173. foreach ($deps as $dep) {
  174. $type = isset($dep['optional']) ? 'optional' : 'required';
  175. $min = isset($dep['min']) ? $dep['min'] : $dep['version'];
  176. $max = isset($dep['max']) ? $dep['max'] : $dep['version'];
  177. $rec = isset($dep['recommended']) ? $dep['recommended'] : $dep['version'];
  178. $this->pkg->addExtensionDep(
  179. $type, $dep['name'], $min, $max, $rec
  180. );
  181. };
  182. break;
  183. case 'maintainers':
  184. $maintainers = $map->getValue();
  185. foreach ($maintainers as $maintainer) {
  186. if (!isset($maintainer['active'])) {
  187. $maintainer['active'] = 'yes';
  188. }
  189. $this->pkg->addMaintainer(
  190. $maintainer['role'],
  191. $maintainer['handle'],
  192. $maintainer['name'],
  193. $maintainer['email'],
  194. $maintainer['active']
  195. );
  196. }
  197. break;
  198. case 'replacements':
  199. $replacements = $map->getValue();
  200. foreach($replacements as $replacement) {
  201. $this->pkg->addReplacement(
  202. $replacement['path'],
  203. $replacement['type'],
  204. $replacement['from'],
  205. $replacement['to']
  206. );
  207. }
  208. break;
  209. default:
  210. $newmaps[] = $map;
  211. }
  212. }
  213. $this->mappings = $newmaps;
  214. }
  215. /**
  216. * Main entry point.
  217. * @return void
  218. */
  219. public function main()
  220. {
  221. if ($this->dir === null) {
  222. throw new BuildException("You must specify the \"dir\" attribute for PEAR package 2 task.");
  223. }
  224. if ($this->package === null) {
  225. throw new BuildException("You must specify the \"name\" attribute for PEAR package 2 task.");
  226. }
  227. $this->pkg = new PEAR_PackageFileManager2();
  228. $this->setVersion2Options();
  229. $this->setOptions();
  230. $this->pkg->addRelease();
  231. $this->pkg->generateContents();
  232. $e = $this->pkg->writePackageFile();
  233. if (PEAR::isError($e)) {
  234. throw new BuildException("Unable to write package file.", new Exception($e->getMessage()));
  235. }
  236. }
  237. }