build-phar.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Build a PHPCS phar.
  5. *
  6. * PHP version 5
  7. *
  8. * @category PHP
  9. * @package PHP_CodeSniffer
  10. * @author Benjamin Pearson <bpearson@squiz.com.au>
  11. * @author Greg Sherwood <gsherwood@squiz.net>
  12. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  13. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. error_reporting(E_ALL | E_STRICT);
  17. if (ini_get('phar.readonly') === '1') {
  18. echo 'Unable to build, phar.readonly in php.ini is set to read only.'.PHP_EOL;
  19. exit(1);
  20. }
  21. $scripts = [
  22. 'phpcs',
  23. 'phpcbf',
  24. ];
  25. foreach ($scripts as $script) {
  26. echo "Building $script phar".PHP_EOL;
  27. $pharName = $script.'.phar';
  28. $pharFile = getcwd().'/'.$pharName;
  29. echo "\t=> $pharFile".PHP_EOL;
  30. if (file_exists($pharFile) === true) {
  31. echo "\t** file exists, removing **".PHP_EOL;
  32. unlink($pharFile);
  33. }
  34. $phar = new Phar($pharFile, 0, $pharName);
  35. /*
  36. Add the files.
  37. */
  38. echo "\t=> adding files... ";
  39. $srcDir = realpath(__DIR__.'/../src');
  40. $srcDirLen = strlen($srcDir);
  41. $rdi = new \RecursiveDirectoryIterator($srcDir, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
  42. $di = new \RecursiveIteratorIterator($rdi, 0, \RecursiveIteratorIterator::CATCH_GET_CHILD);
  43. foreach ($di as $file) {
  44. $filename = $file->getFilename();
  45. // Skip hidden files.
  46. if (substr($filename, 0, 1) === '.') {
  47. continue;
  48. }
  49. $fullpath = $file->getPathname();
  50. if (strpos($fullpath, '/Tests/') !== false) {
  51. continue;
  52. }
  53. $path = 'src'.substr($fullpath, $srcDirLen);
  54. $phar->addFromString($path, php_strip_whitespace($fullpath));
  55. }
  56. // Add autoloader.
  57. $phar->addFromString('autoload.php', php_strip_whitespace(realpath(__DIR__.'/../autoload.php')));
  58. // Add licence file.
  59. $phar->addFromString('licence.txt', php_strip_whitespace(realpath(__DIR__.'/../licence.txt')));
  60. echo 'done'.PHP_EOL;
  61. /*
  62. Add the stub.
  63. */
  64. echo "\t=> adding stub... ";
  65. $stub = '#!/usr/bin/env php'."\n";
  66. $stub .= '<?php'."\n";
  67. $stub .= 'Phar::mapPhar(\''.$pharName.'\');'."\n";
  68. $stub .= 'require_once "phar://'.$pharName.'/autoload.php";'."\n";
  69. $stub .= '$runner = new PHP_CodeSniffer\Runner();'."\n";
  70. $stub .= '$exitCode = $runner->run'.$script.'();'."\n";
  71. $stub .= 'exit($exitCode);'."\n";
  72. $stub .= '__HALT_COMPILER();';
  73. $phar->setStub($stub);
  74. echo 'done'.PHP_EOL;
  75. }//end foreach