forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodlist.d
150 lines (131 loc) · 3.75 KB
/
modlist.d
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
#!/usr/bin/env rdmd
/**
* Copyright: Martin Nowak 2015-.
* License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: $(HTTP code.dawg.eu, Martin Nowak)
*/
import std.algorithm, std.file, std.getopt, std.path, std.stdio, std.string, std.range;
struct Tree
{
ref Tree insert(R)(R parts)
{
if (parts.front == "package")
{
pkgMod = true;
return this;
}
auto tail = leaves.find!((tr, pkg) => tr.name == pkg)(parts.front);
if (tail.empty)
{
leaves ~= Tree(parts.front);
tail = leaves[$-1 .. $];
}
parts.popFront();
return parts.empty ? this : tail.front.insert(parts);
}
void sort()
{
leaves = leaves.sort!((a, b) => a.name < b.name).release;
foreach (ref l; leaves)
l.sort();
}
void dumpRoot()
{
writeln();
writefln("$(MENU_W_SUBMENU $(TT %s))", name);
writefln("$(ITEMIZE");
dumpChildren([name]);
writeln(")");
writeln("$(MENU_W_SUBMENU_END)");
}
void dumpChildren(string[] pkgs)
{
foreach (i, ref l; leaves)
{
l.dump(pkgs);
writeln(i + 1 == leaves.length ? "" : ",");
}
}
void dump(string[] pkgs)
{
if (leaves.empty)
{
writef("%s$(MODULE%s %-(%s, %), %s)", indent(pkgs.length), pkgs.length + 1, pkgs, name);
}
else
{
if (pkgMod)
writefln("%s$(PACKAGE $(MODULE%s %-(%s, %), %s),", indent(pkgs.length), pkgs.length + 1, pkgs, name);
else
writefln("%s$(PACKAGE $(PACKAGE_NAME %s),", indent(pkgs.length), name);
dumpChildren(pkgs ~ name);
writef("%s)", indent(pkgs.length));
}
}
ref Tree opIndex(string part)
{
auto tail = leaves.find!((tr, pkg) => tr.name == pkg)(part);
assert(!tail.empty, part ~ " can't be found.");
return tail.front;
}
static string indent(size_t len)
{
static immutable spaces = " ";
return spaces[0 .. 2 * len];
}
string name;
bool pkgMod;
Tree[] leaves;
}
alias I(alias X) = X;
int main(string[] args)
{
string[] excludes, packages;
auto prog = getopt(args,
"ex", &excludes,
"dump", &packages,
);
if (prog.helpWanted || args.length <= 1)
{
defaultGetoptPrinter("./modlist <dir1> <dir2> ... <dirN>", prog.options);
return 1;
}
auto dirs = args[1 .. $];
bool included(string mod)
{
return !excludes.canFind!(e => mod.startsWith(e));
}
void add(string path, ref Tree tree)
{
auto files = dirEntries(path, "*.d", SpanMode.depth)
.filter!(de => de.isFile)
.map!(de => de.name.chompPrefix(path).chompPrefix(dirSeparator)) // relative to path
.map!(n => n.chomp(".d").replace(dirSeparator, ".")) // std/digest/sha.d => std.digest.sha
.filter!included;
foreach (string name; files)
tree.insert(name.splitter("."));
}
Tree tree;
foreach (dir; dirs)
{
// search for common root folders (fallback to the root directory)
["source", "src", ""]
.map!(f => buildPath(dir, f))
.filter!exists
.front // no UFCS for local symbols
.I!(name => add(name, tree));
}
tree.sort();
writeln("MODULE_MENU=");
foreach (part; packages)
{
// check whether it's a package or file
auto subTree = tree[part];
if (subTree.leaves.length)
subTree.dumpRoot;
else
writefln("$(MENU %s.html, $(TT %1$s))", part);
}
writeln("_=");
return 0;
}