Merge branch 'R_2_1'
[m6w6/ext-http] / check_package-xml.php
1 #!/usr/bin/env php
2 <?php
3
4 ini_set("log_errors", false);
5 ini_set("display_errors", true);
6
7 if ($argc > 1) {
8 if ($argv[1] === "-") {
9 $file = "php://stdin";
10 } else {
11 $file = $argv[1];
12 }
13 } elseif (stdin_is_readable()) {
14 $file = "php://stdin";
15 } else {
16 $file = "./package.xml";
17 }
18
19 if (($xml = simplexml_load_file($file))) {
20 $xml_files = xmllist($xml->contents[0]);
21 $dirs = ["."];
22 while ($dir = array_shift($dirs)) {
23 foreach (dirlist($dir) as $file) {
24 if (is_gitignored($file)) {
25 continue;
26 }
27 if (!is_dir($file)) {
28 if (!in_array($file, $xml_files)) {
29 echo "Missing file $file\n";
30 }
31 } else {
32 $base = basename($file);
33 if ($base{0} !== ".") {
34 array_push($dirs, $file);
35 }
36 }
37 }
38 }
39 }
40
41 ###
42
43 function error($fmt) {
44 trigger_error(call_user_func_array("sprintf", func_get_args()));
45 }
46
47 function stdin_is_readable() {
48 $r = [STDIN]; $w = $e = [];
49 return stream_select($r, $w, $e, 0);
50 }
51
52 function is_gitignored($file) {
53 static $gitignore;
54
55 if (!isset($gitignore)) {
56 if (is_readable(".gitignore")) {
57 $gitignore = explode("\n", `find | git check-ignore --stdin`);
58 } else {
59 $gitignore = false;
60 }
61 }
62 if ($gitignore) {
63 return in_array($file, $gitignore);
64 }
65 return false;
66 }
67
68 function xmllist(SimpleXmlElement $dir, $p = ".", &$a = null) {
69 settype($a, "array");
70 $p = trim($p, "/") . "/" . trim($dir["name"], "/") . "/";
71 foreach ($dir as $file) {
72 switch ($file->getName()) {
73 case "dir":
74 xmllist($file, $p, $a);
75 break;
76 case "file":
77 $a[] = sprintf("%s/%s", trim($p, "/"), trim($file["name"]));
78 break;
79 default:
80 error("Unknown content type: %s", $file->getName());
81 break;
82 }
83 }
84 return $a;
85 }
86
87 function dirlist($dir, $p = null) {
88 $p = implode("/", array_filter([trim($p, "/"), trim($dir, "/")]));
89 foreach (scandir($p) as $file) {
90 yield $p."/".$file;
91 }
92 }