-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_index.d
72 lines (66 loc) · 2.64 KB
/
generate_index.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
import std.stdio;
import std.file;
import std.path;
import std.conv;
import std.string;
string toModuleName(string base, string path){
string[] splitted = [];
foreach (p; pathSplitter(stripExtension(relativePath(absolutePath(path), absolutePath(base)))))
splitted ~= p;
if (splitted[$-1] == "package")
splitted = splitted[0..$-1];
return splitted.join(".");
}
string toEnumName(string moduleName){
return moduleName.split(".").join("_");
}
void main(string[] args){
assert(args.length==2);
foreach (DirEntry d; dirEntries(args[1], SpanMode.breadth))
if (d.isDir) {
auto pkgFilePath = chainPath(d.name, "_index.d");
auto pkgFile = File(pkgFilePath,"w");
pkgFile.writeln("module "~toModuleName(args[1], d.name)~"._index;");
pkgFile.writeln();
pkgFile.writeln("struct Index {");
pkgFile.writeln(" enum packageName = \""~toModuleName(args[1], d.name)~"\";");
pkgFile.writeln();
pkgFile.write(" enum submodules");
string[] fileLines = [];
foreach(DirEntry d2; dirEntries(d.name, SpanMode.shallow)){
if (d2.isFile && d2.name.extension == ".d" && d2.name.baseName() != "_index.d" && d2.name.baseName() != "package.d"){
auto modName = toModuleName(args[1], d2.name);
auto enumName = toEnumName(modName);
fileLines ~= " "~enumName~" = \""~modName~"\",";
}
}
if (fileLines.length) {
pkgFile.writeln(" {");
foreach (line; fileLines)
pkgFile.writeln(line);
pkgFile.writeln(" }");
}
else
pkgFile.writeln(" ;");
pkgFile.writeln();
pkgFile.write(" enum subpackages");
string[] subdirLines = [];
foreach(DirEntry d2; dirEntries(d.name, SpanMode.shallow)){
if (d2.isDir){
auto modName = toModuleName(args[1], d2.name);
auto enumName = toEnumName(modName);
subdirLines ~= " "~enumName~" = \""~modName~"\",";
}
}
if (subdirLines.length) {
pkgFile.writeln(" {");
foreach (line; subdirLines)
pkgFile.writeln(line);
pkgFile.writeln(" }");
}
else
pkgFile.writeln(" ;");
pkgFile.writeln();
pkgFile.writeln("}");
}
}