-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprepackage.php
executable file
·111 lines (104 loc) · 3.03 KB
/
prepackage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env php
<?php
// modified from https://raw.githubusercontent.com/m6w6/ext-http/master/scripts/check_package-xml.php
ini_set("display_errors", true);
$xml = simplexml_load_file("./package.xml");
$xml_files = xmllist($xml->contents[0]);
$dirs = ["."];
$exit = 0;
while ($dir = array_shift($dirs)) {
foreach (dirlist($dir) as $file) {
if (is_git_ignored($file)) {
continue;
}
if (is_otherwise_ignored($file)) {
continue;
}
if (!is_dir($file)) {
if (!in_array($file, $xml_files)) {
echo "Missing file $file\n";
$exit = 1;
}
} else {
$base = basename($file);
if ($base{0} !== ".") {
array_push($dirs, $file);
}
}
}
}
foreach ($xml_files as $file) {
if (!file_exists($file)) {
echo "Extraneous file $file\n";
$exit = 1;
}
}
exit($exit);
###
function error($fmt) {
trigger_error(call_user_func_array("sprintf", func_get_args()));
}
function is_git_ignored($file) {
static $gitignore, $gitmodules;
if (!isset($gitmodules)) {
if (is_readable("./.gitmodules")) {
$gitmodules = explode("\n", `git submodule status | awk '{printf$2}'`);
} else {
$gitmodules = false;
}
}
if (!isset($gitignore)) {
if (is_readable("./.gitignore")) {
$ignore_submodules = $gitmodules ? " ! -path './".implode("/*' ! -path './", $gitmodules)."/*'" : "";
$gitignore = explode("\n", `find . $ignore_submodules | git check-ignore --stdin`);
} else {
$gitignore = false;
}
}
if ($gitignore) {
if (in_array($file, $gitignore)) {
return true;
}
}
if ($gitmodules) {
foreach ($gitmodules as $module) {
if (fnmatch("./$module/*", $file)) {
return true;
}
}
}
return false;
}
function is_otherwise_ignored($file) {
$ignored = [
"./package.xml", "./package2.xml", "./.travis.yml", "./.editorconfig",
"./run-tests.sh", "./reflect.php", "./clean.sh", "./prepackage.php",
"./appveyor.yml"
];
return fnmatch("./.git*", $file)
|| in_array($file, $ignored, true);
}
function xmllist(SimpleXmlElement $dir, $p = ".", &$a = null) {
settype($a, "array");
$p = trim($p, "/") . "/" . trim($dir["name"], "/") . "/";
foreach ($dir as $file) {
switch ($file->getName()) {
case "dir":
xmllist($file, $p, $a);
break;
case "file":
$a[] = sprintf("%s/%s", trim($p, "/"), trim($file["name"]));
break;
default:
trigger_error("Unknown content type: " . $file->getName());
exit(1);
}
}
return $a;
}
function dirlist($dir, $p = null) {
$p = implode("/", array_filter([trim($p, "/"), trim($dir, "/")]));
foreach (scandir($p) as $file) {
yield $p."/".$file;
}
}