PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/guzzle/guzzle/phing/tasks/GuzzlePearPharPackageTask.php

https://gitlab.com/techniconline/kmc
PHP | 338 lines | 279 code | 40 blank | 19 comment | 37 complexity | 50c8539a43f78f48025d57717350503d MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of Guzzle's build process.
  4. *
  5. * @copyright 2012 Clay Loveless <clay@php.net>
  6. * @license http://claylo.mit-license.org/2012/ MIT License
  7. */
  8. require_once 'phing/Task.php';
  9. require_once 'PEAR/PackageFileManager2.php';
  10. require_once 'PEAR/PackageFileManager/File.php';
  11. require_once 'PEAR/Packager.php';
  12. class GuzzlePearPharPackageTask extends Task
  13. {
  14. private $version;
  15. private $deploy = true;
  16. private $makephar = true;
  17. private $subpackages = array();
  18. public function setVersion($str)
  19. {
  20. $this->version = $str;
  21. }
  22. public function getVersion()
  23. {
  24. return $this->version;
  25. }
  26. public function setDeploy($deploy)
  27. {
  28. $this->deploy = (bool) $deploy;
  29. }
  30. public function getDeploy()
  31. {
  32. return $this->deploy;
  33. }
  34. public function setMakephar($makephar)
  35. {
  36. $this->makephar = (bool) $makephar;
  37. }
  38. public function getMakephar()
  39. {
  40. return $this->makephar;
  41. }
  42. private $basedir;
  43. private $guzzleinfo;
  44. private $changelog_release_date;
  45. private $changelog_notes = '-';
  46. public function main()
  47. {
  48. $this->basedir = $this->getProject()->getBasedir();
  49. if (!is_dir((string) $this->basedir.'/.subsplit')) {
  50. throw new BuildException('PEAR packaging requires .subsplit directory');
  51. }
  52. // main composer file
  53. $composer_file = file_get_contents((string) $this->basedir.'/.subsplit/composer.json');
  54. $this->guzzleinfo = json_decode($composer_file, true);
  55. // make sure we have a target
  56. $pearwork = (string) $this->basedir . '/build/pearwork';
  57. if (!is_dir($pearwork)) {
  58. mkdir($pearwork, 0777, true);
  59. }
  60. $pearlogs = (string) $this->basedir . '/build/artifacts/logs';
  61. if (!is_dir($pearlogs)) {
  62. mkdir($pearlogs, 0777, true);
  63. }
  64. $version = $this->getVersion();
  65. $this->grabChangelog();
  66. if ($version[0] == '2') {
  67. $this->log('building single PEAR package');
  68. $this->buildSinglePackage();
  69. } else {
  70. // $this->log("building PEAR subpackages");
  71. // $this->createSubPackages();
  72. // $this->log("building PEAR bundle package");
  73. $this->buildSinglePackage();
  74. }
  75. if ($this->getMakephar()) {
  76. $this->log("building PHAR");
  77. $this->getProject()->executeTarget('package-phar');
  78. }
  79. if ($this->getDeploy()) {
  80. $this->doDeployment();
  81. }
  82. }
  83. public function doDeployment()
  84. {
  85. $basedir = (string) $this->basedir;
  86. $this->log('beginning PEAR/PHAR deployment');
  87. chdir($basedir . '/build/pearwork');
  88. if (!is_dir('./channel')) {
  89. mkdir('./channel');
  90. }
  91. // Pull the PEAR channel down locally
  92. passthru('aws s3 sync s3://pear.guzzlephp.org ./channel');
  93. // add PEAR packages
  94. foreach (scandir('./') as $file) {
  95. if (substr($file, -4) == '.tgz') {
  96. passthru('pirum add ./channel ' . $file);
  97. }
  98. }
  99. // if we have a new phar, add it
  100. if ($this->getMakephar() && file_exists($basedir . '/build/artifacts/guzzle.phar')) {
  101. rename($basedir . '/build/artifacts/guzzle.phar', './channel/guzzle.phar');
  102. }
  103. // Sync up with the S3 bucket
  104. chdir($basedir . '/build/pearwork/channel');
  105. passthru('aws s3 sync . s3://pear.guzzlephp.org');
  106. }
  107. public function buildSinglePackage()
  108. {
  109. $v = $this->getVersion();
  110. $apiversion = $v[0] . '.0.0';
  111. $opts = array(
  112. 'packagedirectory' => (string) $this->basedir . '/.subsplit/src/',
  113. 'filelistgenerator' => 'file',
  114. 'ignore' => array('*composer.json'),
  115. 'baseinstalldir' => '/',
  116. 'packagefile' => 'package.xml'
  117. //'outputdirectory' => (string) $this->basedir . '/build/pearwork/'
  118. );
  119. $pfm = new PEAR_PackageFileManager2();
  120. $pfm->setOptions($opts);
  121. $pfm->addRole('md', 'doc');
  122. $pfm->addRole('pem', 'php');
  123. $pfm->setPackage('Guzzle');
  124. $pfm->setSummary("Object-oriented PHP HTTP Client for PHP 5.3+");
  125. $pfm->setDescription($this->guzzleinfo['description']);
  126. $pfm->setPackageType('php');
  127. $pfm->setChannel('guzzlephp.org/pear');
  128. $pfm->setAPIVersion($apiversion);
  129. $pfm->setReleaseVersion($this->getVersion());
  130. $pfm->setAPIStability('stable');
  131. $pfm->setReleaseStability('stable');
  132. $pfm->setNotes($this->changelog_notes);
  133. $pfm->setPackageType('php');
  134. $pfm->setLicense('MIT', 'http://github.com/guzzle/guzzle/blob/master/LICENSE');
  135. $pfm->addMaintainer('lead', 'mtdowling', 'Michael Dowling', 'mtdowling@gmail.com', 'yes');
  136. $pfm->setDate($this->changelog_release_date);
  137. $pfm->generateContents();
  138. $phpdep = $this->guzzleinfo['require']['php'];
  139. $phpdep = str_replace('>=', '', $phpdep);
  140. $pfm->setPhpDep($phpdep);
  141. $pfm->addExtensionDep('required', 'curl');
  142. $pfm->setPearinstallerDep('1.4.6');
  143. $pfm->addPackageDepWithChannel('required', 'EventDispatcher', 'pear.symfony.com', '2.1.0');
  144. if (!empty($this->subpackages)) {
  145. foreach ($this->subpackages as $package) {
  146. $pkg = dirname($package);
  147. $pkg = str_replace('/', '_', $pkg);
  148. $pfm->addConflictingPackageDepWithChannel($pkg, 'guzzlephp.org/pear', false, $apiversion);
  149. }
  150. }
  151. ob_start();
  152. $startdir = getcwd();
  153. chdir((string) $this->basedir . '/build/pearwork');
  154. echo "DEBUGGING GENERATED PACKAGE FILE\n";
  155. $result = $pfm->debugPackageFile();
  156. if ($result) {
  157. $out = $pfm->writePackageFile();
  158. echo "\n\n\nWRITE PACKAGE FILE RESULT:\n";
  159. var_dump($out);
  160. // load up package file and build package
  161. $packager = new PEAR_Packager();
  162. echo "\n\n\nBUILDING PACKAGE FROM PACKAGE FILE:\n";
  163. $dest_package = $packager->package($opts['packagedirectory'].'package.xml');
  164. var_dump($dest_package);
  165. } else {
  166. echo "\n\n\nDEBUGGING RESULT:\n";
  167. var_dump($result);
  168. }
  169. echo "removing package.xml";
  170. unlink($opts['packagedirectory'].'package.xml');
  171. $log = ob_get_clean();
  172. file_put_contents((string) $this->basedir . '/build/artifacts/logs/pear_package.log', $log);
  173. chdir($startdir);
  174. }
  175. public function createSubPackages()
  176. {
  177. $this->findComponents();
  178. foreach ($this->subpackages as $package) {
  179. $baseinstalldir = dirname($package);
  180. $dir = (string) $this->basedir.'/.subsplit/src/' . $baseinstalldir;
  181. $composer_file = file_get_contents((string) $this->basedir.'/.subsplit/src/'. $package);
  182. $package_info = json_decode($composer_file, true);
  183. $this->log('building ' . $package_info['target-dir'] . ' subpackage');
  184. $this->buildSubPackage($dir, $baseinstalldir, $package_info);
  185. }
  186. }
  187. public function buildSubPackage($dir, $baseinstalldir, $info)
  188. {
  189. $package = str_replace('/', '_', $baseinstalldir);
  190. $opts = array(
  191. 'packagedirectory' => $dir,
  192. 'filelistgenerator' => 'file',
  193. 'ignore' => array('*composer.json', '*package.xml'),
  194. 'baseinstalldir' => '/' . $info['target-dir'],
  195. 'packagefile' => 'package.xml'
  196. );
  197. $pfm = new PEAR_PackageFileManager2();
  198. $pfm->setOptions($opts);
  199. $pfm->setPackage($package);
  200. $pfm->setSummary($info['description']);
  201. $pfm->setDescription($info['description']);
  202. $pfm->setPackageType('php');
  203. $pfm->setChannel('guzzlephp.org/pear');
  204. $pfm->setAPIVersion('3.0.0');
  205. $pfm->setReleaseVersion($this->getVersion());
  206. $pfm->setAPIStability('stable');
  207. $pfm->setReleaseStability('stable');
  208. $pfm->setNotes($this->changelog_notes);
  209. $pfm->setPackageType('php');
  210. $pfm->setLicense('MIT', 'http://github.com/guzzle/guzzle/blob/master/LICENSE');
  211. $pfm->addMaintainer('lead', 'mtdowling', 'Michael Dowling', 'mtdowling@gmail.com', 'yes');
  212. $pfm->setDate($this->changelog_release_date);
  213. $pfm->generateContents();
  214. $phpdep = $this->guzzleinfo['require']['php'];
  215. $phpdep = str_replace('>=', '', $phpdep);
  216. $pfm->setPhpDep($phpdep);
  217. $pfm->setPearinstallerDep('1.4.6');
  218. foreach ($info['require'] as $type => $version) {
  219. if ($type == 'php') {
  220. continue;
  221. }
  222. if ($type == 'symfony/event-dispatcher') {
  223. $pfm->addPackageDepWithChannel('required', 'EventDispatcher', 'pear.symfony.com', '2.1.0');
  224. }
  225. if ($type == 'ext-curl') {
  226. $pfm->addExtensionDep('required', 'curl');
  227. }
  228. if (substr($type, 0, 6) == 'guzzle') {
  229. $gdep = str_replace('/', ' ', $type);
  230. $gdep = ucwords($gdep);
  231. $gdep = str_replace(' ', '_', $gdep);
  232. $pfm->addPackageDepWithChannel('required', $gdep, 'guzzlephp.org/pear', $this->getVersion());
  233. }
  234. }
  235. // can't have main Guzzle package AND sub-packages
  236. $pfm->addConflictingPackageDepWithChannel('Guzzle', 'guzzlephp.org/pear', false, $apiversion);
  237. ob_start();
  238. $startdir = getcwd();
  239. chdir((string) $this->basedir . '/build/pearwork');
  240. echo "DEBUGGING GENERATED PACKAGE FILE\n";
  241. $result = $pfm->debugPackageFile();
  242. if ($result) {
  243. $out = $pfm->writePackageFile();
  244. echo "\n\n\nWRITE PACKAGE FILE RESULT:\n";
  245. var_dump($out);
  246. // load up package file and build package
  247. $packager = new PEAR_Packager();
  248. echo "\n\n\nBUILDING PACKAGE FROM PACKAGE FILE:\n";
  249. $dest_package = $packager->package($opts['packagedirectory'].'/package.xml');
  250. var_dump($dest_package);
  251. } else {
  252. echo "\n\n\nDEBUGGING RESULT:\n";
  253. var_dump($result);
  254. }
  255. echo "removing package.xml";
  256. unlink($opts['packagedirectory'].'/package.xml');
  257. $log = ob_get_clean();
  258. file_put_contents((string) $this->basedir . '/build/artifacts/logs/pear_package_'.$package.'.log', $log);
  259. chdir($startdir);
  260. }
  261. public function findComponents()
  262. {
  263. $ds = new DirectoryScanner();
  264. $ds->setBasedir((string) $this->basedir.'/.subsplit/src');
  265. $ds->setIncludes(array('**/composer.json'));
  266. $ds->scan();
  267. $files = $ds->getIncludedFiles();
  268. $this->subpackages = $files;
  269. }
  270. public function grabChangelog()
  271. {
  272. $cl = file((string) $this->basedir.'/.subsplit/CHANGELOG.md');
  273. $notes = '';
  274. $in_version = false;
  275. $release_date = null;
  276. foreach ($cl as $line) {
  277. $line = trim($line);
  278. if (preg_match('/^\* '.$this->getVersion().' \(([0-9\-]+)\)$/', $line, $matches)) {
  279. $release_date = $matches[1];
  280. $in_version = true;
  281. continue;
  282. }
  283. if ($in_version && empty($line) && empty($notes)) {
  284. continue;
  285. }
  286. if ($in_version && ! empty($line)) {
  287. $notes .= $line."\n";
  288. }
  289. if ($in_version && empty($line) && !empty($notes)) {
  290. $in_version = false;
  291. }
  292. }
  293. $this->changelog_release_date = $release_date;
  294. if (! empty($notes)) {
  295. $this->changelog_notes = $notes;
  296. }
  297. }
  298. }