4 ini_set("log_errors", false);
5 ini_set("display_errors", true);
8 if ($argv[1] === "-") {
13 } elseif (stdin_is_readable()) {
14 $file = "php://stdin";
16 $file = "./package.xml";
19 if (($xml = simplexml_load_file($file))) {
20 $xml_files = xmllist($xml->contents
[0]);
22 while ($dir = array_shift($dirs)) {
23 foreach (dirlist($dir) as $file) {
24 if (is_gitignored($file)) {
27 if (is_commonly_ignored($file)) {
31 if (!in_array($file, $xml_files)) {
32 echo "Missing file $file\n";
35 $base = basename($file);
36 if ($base{0} !== ".") {
37 array_push($dirs, $file);
42 foreach ($xml_files as $file) {
43 if (!file_exists($file)) {
44 echo "Extraneous file $file\n";
51 function error($fmt) {
52 trigger_error(call_user_func_array("sprintf", func_get_args()));
55 function stdin_is_readable() {
56 $r = [STDIN
]; $w = $e = [];
57 return stream_select($r, $w, $e, 0);
60 function is_gitignored($file) {
61 static $gitignore, $gitmodules;
63 if (!isset($gitmodules)) {
64 if (is_readable("./.gitmodules")) {
65 $gitmodules = explode("\n", `git submodule status | awk
'{printf$2}'`
);
70 if (!isset($gitignore)) {
71 if (is_readable("./.gitignore")) {
72 $ignore_submodules = $gitmodules ?
" ! -path './".implode("/*' ! -path './", $gitmodules)."/*'" : "";
73 $gitignore = explode("\n", `find
. $ignore_submodules | git check
-ignore
--stdin`
);
79 if (in_array($file, $gitignore)) {
84 foreach ($gitmodules as $module) {
85 if (fnmatch("./$module/*", $file)) {
93 function is_commonly_ignored($file) {
94 return fnmatch("./.git*", $file)
95 ||
in_array($file, ["./package.xml", "./package2.xml", "./.travis.yml", "./.editorconfig"], true);
98 function xmllist(SimpleXmlElement
$dir, $p = ".", &$a = null) {
100 $p = trim($p, "/") . "/" . trim($dir["name"], "/") . "/";
101 foreach ($dir as $file) {
102 switch ($file->getName()) {
104 xmllist($file, $p, $a);
107 $a[] = sprintf("%s/%s", trim($p, "/"), trim($file["name"]));
110 error("Unknown content type: %s", $file->getName());
117 function dirlist($dir, $p = null) {
118 $p = implode("/", array_filter([trim($p, "/"), trim($dir, "/")]));
119 foreach (scandir($p) as $file) {