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