annotate trunk/src/cmd/ImportGraph.d @ 375:0bd21b746a04

- Added code to main() for recognizing options to the importgraph command. - Added HelpImportGraph message. - Added enum IGraphOption. - Added class Vertex inheriting from Module. Changed struct Edge to a class. - Modified a lot of code in cmd.ImportGraph; too many to list here. - Made some minor changes to dil.Parser.
author aziz
date Sat, 08 Sep 2007 16:42:02 +0000
parents ae4afb66768f
children 33b566df6af4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
1 /++
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
2 Author: Aziz Köksal
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
3 License: GPL3
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
4 +/
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
5 module cmd.ImportGraph;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
6 import dil.SyntaxTree;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
7 import dil.Declarations;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
8 import dil.Token;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
9 import dil.Parser, dil.Lexer;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
10 import dil.File;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
11 import dil.Module;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
12 import dil.Settings;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
13 import std.stdio : writefln, writef;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
14 import std.path : getDirName, dirSep = sep;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
15 import std.file : exists;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
16 import std.string : replace;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
17 import std.regexp;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
18
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
19 enum IGraphOption
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
20 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
21 None,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
22 IncludeUnlocatableModules = 1,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
23 HighlightCyclicEdges = 1<<1,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
24 HighlightCyclicVertices = 1<<2,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
25 PrintDot = 1<<3,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
26 PrintPaths = 1<<4,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
27 PrintList = 1<<5,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
28 GroupByPackageNames = 1<<6,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
29 GroupByFullPackageName = 1<<7,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
30 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
31
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
32 string findModulePath(string moduleFQN, string[] importPaths)
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
33 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
34 string modulePath;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
35 foreach (path; importPaths)
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
36 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
37 modulePath = path ~ (path[$-1] == dirSep[0] ? "" : dirSep) ~ moduleFQN ~ ".d";
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
38 if (exists(modulePath))
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
39 return modulePath;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
40 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
41 return null;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
42 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
43
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
44 class Edge
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
45 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
46 Vertex outgoing;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
47 Vertex incoming;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
48 bool isCyclic;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
49 this(Vertex o, Vertex i)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
50 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
51 this.outgoing = o;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
52 this.incoming = i;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
53 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
54 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
55
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
56 class Vertex : Module
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
57 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
58 uint id;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
59 Vertex[] incoming;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
60 bool isCyclic; /// Whether this vertex is in a cyclic relationship with other vertices.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
61
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
62 this(string filePath)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
63 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
64 super(filePath, true);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
65 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
66
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
67 Vertex[] outgoing()
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
68 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
69 return cast(Vertex[])super.modules;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
70 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
71 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
72
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
73 void execute(string filePath, string[] importPaths, string[] strRegexps, uint levels, IGraphOption options)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
74 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
75 // Init regular expressions.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
76 RegExp[] regexps;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
77 foreach (strRegexp; strRegexps)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
78 regexps ~= new RegExp(strRegexp);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
79
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
80 // Add directory of file and global directories to import paths.
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
81 auto fileDir = getDirName(filePath);
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
82 if (fileDir.length)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
83 importPaths ~= fileDir;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
84 importPaths ~= GlobalSettings.importPaths;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
85
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
86 Vertex[string] loadedModules;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
87 Vertex[] loadedModulesList; // Ordered list of loaded modules.
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
88 Edge edges[];
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
89
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
90 Vertex loadModule(string moduleFQNPath)
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
91 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
92 auto mod_ = moduleFQNPath in loadedModules;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
93 if (mod_ !is null)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
94 return *mod_; // Return already loaded module.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
95
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
96 // Ignore module names matching regular expressions.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
97 foreach (rx; regexps)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
98 if (rx.test(replace(moduleFQNPath, dirSep, ".")))
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
99 return null;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
100
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
101 auto modulePath = findModulePath(moduleFQNPath, importPaths);
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
102 Vertex mod;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
103 if (modulePath is null)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
104 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
105 if (options & IGraphOption.IncludeUnlocatableModules)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
106 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
107 mod = new Vertex(null);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
108 mod.setFQN(replace(moduleFQNPath, dirSep, "."));
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
109 loadedModules[moduleFQNPath] = mod;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
110 loadedModulesList ~= mod;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
111 mod.id = loadedModulesList.length -1;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
112 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
113 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
114 else
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
115 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
116 // writefln(modulePath);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
117 mod = new Vertex(modulePath);
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
118 mod.parse();
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
119
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
120 loadedModules[moduleFQNPath] = mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
121 loadedModulesList ~= mod;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
122 mod.id = loadedModulesList.length -1;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
123
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
124 auto moduleFQNs = mod.getImports();
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
125
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
126 foreach (moduleFQN_; moduleFQNs)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
127 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
128 auto loaded_mod = loadModule(moduleFQN_);
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
129 if (loaded_mod !is null)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
130 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
131 edges ~= new Edge(mod, loaded_mod);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
132 mod.modules ~= loaded_mod;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
133 loaded_mod.incoming ~= mod;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
134 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
135 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
136 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
137 return mod;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
138 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
139
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
140 auto mod = new Vertex(filePath);
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
141 mod.parse();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
142
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
143 auto moduleFQNs = mod.getImports();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
144
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
145 loadedModules[mod.getFQNPath()] = mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
146 loadedModulesList ~= mod;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
147 mod.id = 0; // loadedModulesList.length -1
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
148
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
149 foreach (moduleFQN_; moduleFQNs)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
150 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
151 auto loaded_mod = loadModule(moduleFQN_);
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
152 if (loaded_mod !is null)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
153 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
154 edges ~= new Edge(mod, loaded_mod);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
155 mod.modules ~= loaded_mod;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
156 loaded_mod.incoming ~= mod;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
157 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
158 }
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
159 // Finished loading modules.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
160
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
161
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
162 if (options & IGraphOption.PrintPaths)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
163 printPaths(loadedModulesList, levels+1, "");
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
164 else if (options & IGraphOption.PrintList)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
165 printList(loadedModulesList, levels+1, "");
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
166 else if (options & IGraphOption.PrintDot)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
167 printDot(loadedModulesList, edges, options);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
168 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
169
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
170 void printPaths(Vertex[] vertices, uint level, char[] indent)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
171 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
172 if (!level)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
173 return;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
174 foreach (vertex; vertices)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
175 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
176 writefln(indent, vertex.filePath);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
177 if (vertex.outgoing.length)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
178 printPaths(vertex.outgoing, level-1, indent~" ");
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
179 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
180 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
181
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
182 void printList(Vertex[] vertices, uint level, char[] indent)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
183 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
184 if (!level)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
185 return;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
186 foreach (vertex; vertices)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
187 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
188 writefln(indent, vertex.getFQN());
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
189 if (vertex.outgoing.length)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
190 printList(vertex.outgoing, level-1, indent~" ");
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
191 }
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
192 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
193
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
194 void printDot(Vertex[] loadedModulesList, Edge[] edges, IGraphOption options)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
195 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
196 Vertex[][string] verticesByPckgName;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
197 if (options & IGraphOption.GroupByFullPackageName)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
198 foreach (module_; loadedModulesList)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
199 verticesByPckgName[module_.packageName] ~= module_;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
200
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
201 if (options & (IGraphOption.HighlightCyclicVertices |
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
202 IGraphOption.HighlightCyclicEdges))
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
203 analyzeGraph(loadedModulesList, edges.dup);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
204
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
205 writefln("Digraph ModuleDependencies\n{");
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
206
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
207 if (options & IGraphOption.HighlightCyclicVertices)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
208 foreach (i, module_; loadedModulesList)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
209 writefln(` n%d [label="%s"%s];`, i, module_.getFQN(), (module_.isCyclic ? ",style=filled,fillcolor=tomato" : ""));
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
210 else
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
211 foreach (i, module_; loadedModulesList)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
212 writefln(` n%d [label="%s"];`, i, module_.getFQN());
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
213
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
214 foreach (edge; edges)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
215 writefln(` n%d -> n%d%s;`, edge.outgoing.id, edge.incoming.id, (edge.isCyclic ? "[color=red]" : ""));
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
216
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
217 if (options & IGraphOption.GroupByFullPackageName)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
218 foreach (packageName, vertices; verticesByPckgName)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
219 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
220 writef(` subgraph "cluster_%s" {`\n` label="%s";color=blue;`"\n ", packageName, packageName);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
221 foreach (module_; vertices)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
222 writef(`n%d;`, module_.id);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
223 writefln("\n }");
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
224 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
225
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
226 writefln("}");
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
227 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
228
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
229 void analyzeGraph(Vertex[] vertices, Edge[] edges)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
230 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
231 void recursive(Vertex[] modules)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
232 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
233 foreach (idx, vertex; vertices)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
234 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
235 uint outgoing, incoming;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
236 foreach (j, edge; edges)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
237 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
238 if (edge.outgoing is vertex)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
239 outgoing++;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
240 if (edge.incoming is vertex)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
241 incoming++;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
242 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
243
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
244 if (outgoing == 0)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
245 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
246 if (incoming != 0)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
247 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
248 // Vertex is a sink.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
249 alias outgoing i; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
250 alias incoming j; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
251 // Remove edges.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
252 for (i=j=0; i < edges.length; i++)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
253 if (edges[i].incoming !is vertex)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
254 edges[j++] = edges[i];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
255 edges.length = j;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
256 vertices = vertices[0..idx] ~ vertices[idx+1..$];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
257 return recursive(modules);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
258 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
259 else
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
260 assert(0, "orphaned module: "~vertex.getFQN()~" (has no edges in graph)"); // orphaned vertex (module) in graph
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
261 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
262 else if (incoming == 0)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
263 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
264 // Vertex is a source
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
265 alias outgoing i; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
266 alias incoming j; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
267 // Remove edges.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
268 for (i=j=0; i < edges.length; i++)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
269 if (edges[i].outgoing !is vertex)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
270 edges[j++] = edges[i];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
271 edges.length = j;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
272 vertices = vertices[0..idx] ~ vertices[idx+1..$];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
273 return recursive(modules);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
274 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
275 // else
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
276 // {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
277 // // source && sink
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
278 // }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
279 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
280
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
281 // When reaching this point it means only cylic edges and vertices are left.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
282 foreach (vertex; vertices)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
283 vertex.isCyclic = true;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
284 foreach (edge; edges)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
285 if (edge)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
286 edge.isCyclic = true;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
287 }
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
288 recursive(vertices);
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
289 }