comparison vendor/squizlabs/php_codesniffer/scripts/build-phar.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
19 if (ini_get('phar.readonly') === '1') { 19 if (ini_get('phar.readonly') === '1') {
20 echo 'Unable to build, phar.readonly in php.ini is set to read only.'.PHP_EOL; 20 echo 'Unable to build, phar.readonly in php.ini is set to read only.'.PHP_EOL;
21 exit(1); 21 exit(1);
22 } 22 }
23 23
24 $cwd = getCwd(); 24 $scripts = [
25 require_once __DIR__.'/../CodeSniffer.php'; 25 'phpcs',
26 26 'phpcbf',
27 $scripts = array( 27 ];
28 'phpcs',
29 'phpcbf',
30 );
31 28
32 foreach ($scripts as $script) { 29 foreach ($scripts as $script) {
33 echo "Building $script phar".PHP_EOL; 30 echo "Building $script phar".PHP_EOL;
34 31
35 $pharFile = $cwd.'/'.$script.'.phar'; 32 $pharName = $script.'.phar';
33 $pharFile = getcwd().'/'.$pharName;
36 echo "\t=> $pharFile".PHP_EOL; 34 echo "\t=> $pharFile".PHP_EOL;
37 if (file_exists($pharFile) === true) { 35 if (file_exists($pharFile) === true) {
38 echo "\t** file exists, removing **".PHP_EOL; 36 echo "\t** file exists, removing **".PHP_EOL;
39 unlink($pharFile); 37 unlink($pharFile);
40 } 38 }
41 39
42 $phar = new Phar($pharFile, 0, $script.'.phar'); 40 $phar = new Phar($pharFile, 0, $pharName);
43 41
44 echo "\t=> adding files from package.xml... "; 42 /*
45 buildFromPackage($phar); 43 Add the files.
44 */
45
46 echo "\t=> adding files... ";
47
48 $srcDir = realpath(__DIR__.'/../src');
49 $srcDirLen = strlen($srcDir);
50
51 $rdi = new \RecursiveDirectoryIterator($srcDir, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
52 $di = new \RecursiveIteratorIterator($rdi, 0, \RecursiveIteratorIterator::CATCH_GET_CHILD);
53
54 foreach ($di as $file) {
55 $filename = $file->getFilename();
56
57 // Skip hidden files.
58 if (substr($filename, 0, 1) === '.') {
59 continue;
60 }
61
62 $fullpath = $file->getPathname();
63 if (strpos($fullpath, '/Tests/') !== false) {
64 continue;
65 }
66
67 $path = 'src'.substr($fullpath, $srcDirLen);
68
69 $phar->addFromString($path, php_strip_whitespace($fullpath));
70 }
71
72 // Add autoloader.
73 $phar->addFromString('autoload.php', php_strip_whitespace(realpath(__DIR__.'/../autoload.php')));
74
75 // Add licence file.
76 $phar->addFromString('licence.txt', php_strip_whitespace(realpath(__DIR__.'/../licence.txt')));
77
46 echo 'done'.PHP_EOL; 78 echo 'done'.PHP_EOL;
79
80 /*
81 Add the stub.
82 */
47 83
48 echo "\t=> adding stub... "; 84 echo "\t=> adding stub... ";
49 $stub = '#!/usr/bin/env php'."\n"; 85 $stub = '#!/usr/bin/env php'."\n";
50 $stub .= '<?php'."\n"; 86 $stub .= '<?php'."\n";
51 $stub .= 'Phar::mapPhar(\''.$script.'.phar\');'."\n"; 87 $stub .= 'Phar::mapPhar(\''.$pharName.'\');'."\n";
52 $stub .= 'require_once "phar://'.$script.'.phar/CodeSniffer/CLI.php";'."\n"; 88 $stub .= 'require_once "phar://'.$pharName.'/autoload.php";'."\n";
53 $stub .= '$cli = new PHP_CodeSniffer_CLI();'."\n"; 89 $stub .= '$runner = new PHP_CodeSniffer\Runner();'."\n";
54 $stub .= '$cli->run'.$script.'();'."\n"; 90 $stub .= '$exitCode = $runner->run'.$script.'();'."\n";
91 $stub .= 'exit($exitCode);'."\n";
55 $stub .= '__HALT_COMPILER();'; 92 $stub .= '__HALT_COMPILER();';
56 $phar->setStub($stub); 93 $phar->setStub($stub);
94
57 echo 'done'.PHP_EOL; 95 echo 'done'.PHP_EOL;
58 }//end foreach 96 }//end foreach
59
60
61 /**
62 * Build from a package list.
63 *
64 * @param object $phar The Phar class.
65 *
66 * @return void
67 */
68 function buildFromPackage(&$phar)
69 {
70 $packageFile = realpath(__DIR__.'/../package.xml');
71 $dom = new DOMDocument('1.0', 'utf-8');
72 $loaded = $dom->loadXML(file_get_contents($packageFile));
73 if ($loaded === false) {
74 echo "Unable to load package file: $packageFile".PHP_EOL;
75 exit(1);
76 }
77
78 $contents = $dom->getElementsByTagName('contents');
79 $topLevels = $contents->item(0)->childNodes;
80 $tlLength = $topLevels->length;
81 for ($l = 0; $l < $tlLength; $l++) {
82 $currentLevel = $topLevels->item($l);
83 buildFromNode($phar, $currentLevel, '');
84 }
85
86 // Add licence file.
87 $phar->addFile(realpath(__DIR__.'/../licence.txt'), 'licence.txt');
88 $phar['licence.txt']->compress(Phar::GZ);
89
90 }//end buildFromPackage()
91
92
93 /**
94 * Add from a node.
95 *
96 * @param object $phar The Phar class.
97 * @param object $node The node to add.
98 * @param string $prefix The prefix of the structure.
99 *
100 * @return void
101 */
102 function buildFromNode(&$phar, $node, $prefix='')
103 {
104 $nodeName = $node->nodeName;
105 if ($nodeName !== 'dir' && $nodeName !== 'file') {
106 // Invalid node.
107 return;
108 }
109
110 $path = $prefix.$node->getAttribute('name');
111 if ($node->getAttribute('role') === 'php' || $node->getAttribute('role') === 'data') {
112 $path = ltrim($path, '/');
113 $phar->addFile(realpath(__DIR__.'/../'.$path), $path);
114 $phar[$path]->compress(Phar::GZ);
115 }
116
117 if ($nodeName === 'dir') {
118 // Descend into the depths.
119 $path = rtrim($path, '/').'/';
120 $children = $node->childNodes;
121 $childLn = $children->length;
122 for ($c = 0; $c < $childLn; $c++) {
123 $child = $children->item($c);
124 buildFromNode($phar, $child, $path);
125 }
126 }
127
128 }//end buildFromNode()