This repository has been archived by the owner on Jul 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gd3tool.php
executable file
·158 lines (144 loc) · 3.72 KB
/
gd3tool.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env php
<?php
define('SRCDIR',dirname(realpath(__FILE__)));
require_once(SRCDIR."/utils/gendoc3.php");
require_once(SRCDIR."/utils/Spyc.php");
define('CMD',array_shift($argv));
error_reporting(E_ALL);
if (!count($argv)) die("Must specify a sub-command\n");
define('SUBCMD',array_shift($argv));
function subcmd_strip(array $args) {
$mode = 'view';
while (count($args)) {
switch ($args[0]) {
case "-v":
$mode = "view";
array_shift($args);
continue;
case "-w":
$mode = "write";
array_shift($args);
continue;
}
break;
}
//echo __FILE__.",".__LINE__.PHP_EOL;
foreach ($args as $f) {
if (($txt = file_get_contents($f)) === false)
die("$f: error reading file\n");
$txt = analyze_doc(explode("\n",$txt),true);
while (count($txt)>1 && $txt[count($txt)-1] == "") array_pop($txt);
$txt[] = "";
switch($mode) {
case "view":
if (count($argv) > 1) echo "$f =====\n";
echo implode("\n",$txt);
break;
case "write":
file_put_contents($f,implode("\n",$txt));
break;
default:
die("Invalid mode: $mode\n");
}
}
}
function subcmd_analyze(array $args) {
foreach ($args as $d) {
$data = [];
if (is_file($d)) {
echo "SOURCE: $d\n";
analyze_php($d,$data);
} if (is_dir($d)) {
echo "DIRECTORY: $d\n";
analyze_tree($d,$data);
}
print_r($data);
}
}
function subcmd_gen(array $args) {
$save = FALSE;
$doc = 'README.md';
$path = SRCDIR . PATH_SEPARATOR . SRCDIR . '/templates' . PATH_SEPARATOR . '{dirname}';
$ymlfile = 'plugin.yml';
while (count($args)) {
switch ($args[0]) {
case "-w":
$save = TRUE;
array_shift($args);
continue;
case "-d":
if (!isset($args[1])) die("Must specify document for -d\n");
$doc = $args[1];
array_shift($args);array_shift($args);
continue;
case "-I":
if (!isset($args[1])) die("Must specify document for -I\n");
if ($path == "")
$path = $args[1];
else
$path .= PATH_SEPARATOR . $args[1];
array_shift($args);array_shift($args);
continue;
case "-P":
if (!isset($args[1])) die("Must specify document for -P\n");
$path = $args[1];
array_shift($args);array_shift($args);
continue;
case "-y":
if (!isset($args[1])) die("Must specify document for -y\n");
$ymlfile = $args[1];
array_shift($args);array_shift($args);
continue;
}
break;
}
if (!count($args)) $args = [ "src" ];
$path = str_replace('{dirname}',dirname(realpath($doc)),$path);
fwrite(STDERR,"MODE: ".($save ? "SAVE" : "PREVIEW").PHP_EOL);
fwrite(STDERR,"DOC: ".$doc.PHP_EOL);
fwrite(STDERR,"INCPATH: ".$path.PHP_EOL);
fwrite(STDERR,"YMLFILE: ".$ymlfile.PHP_EOL);
if (!is_file($ymlfile)) die($ymlfile.": Not found\n");
$yaml = Spyc::YAMLLoad($ymlfile);
if (!is_file($doc)) die($doc.": Not found\n");
set_include_path(get_include_path().PATH_SEPARATOR.$path);
$snippets = [];
foreach ($args as $phpsrc) {
fwrite(STDERR,"Analyzing ".$phpsrc."...");
analyze_tree($phpsrc,$snippets);
}
$otxt = file_get_contents($doc);
$ntxt = analyze_doc(explode("\n",$otxt));
$ntxt = expand_tags($ntxt,$snippets,$yaml);
$ntxt = implode("\n",$ntxt);
if ($save) {
if ($otxt != $ntxt) {
file_put_contents($doc,$ntxt);
fwrite(STDERR,"Updaged ".$doc.PHP_EOL);
}
} else {
echo $ntxt;
}
}
switch (SUBCMD) {
case 'analyze':
subcmd_analyze($argv);
break;
case 'strip':
subcmd_strip($argv);
break;
case 'gen':
subcmd_gen($argv);
break;
case 'help':
?>
Usage:
- <?=CMD?> analyze <src> [...]
- <?=CMD?> strip <docfile>
- <?=CMD?> gen [-w] [-d <README.md>] [-I <dir>] [-P <path>] <src> [...]
<?php
echo PHP_EOL;
break;
default:
die("Unknown subcommand ".SUBCMD.PHP_EOL);
}