annotate trunk/src/cmd/ImportGraph.d @ 699:4c0ea78a7f8b

Fixed cycle detection algorithm.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 26 Jan 2008 20:59:27 +0100
parents 1564e41f454e
children e10838e0b182
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;
580
fa6d3c52757d Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 576
diff changeset
6
fa6d3c52757d Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 576
diff changeset
7 import dil.ast.Node;
585
05c375fb2d5c Moved dil.Declarations to dil.ast.Declarations.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 580
diff changeset
8 import dil.ast.Declarations;
593
2848ce3becf5 Moved dil.Module to dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 585
diff changeset
9 import dil.semantic.Module;
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
10 import dil.parser.ImportParser;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
11 import dil.File;
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;
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
13 import tango.text.Regex : RegExp = Regex;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
14 import tango.io.FilePath;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
15 import tango.io.FileConst;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
16 import tango.text.Util;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
17 import common;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
18
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
19 alias FileConst.PathSeparatorChar dirSep;
375
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 enum IGraphOption
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
22 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
23 None,
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
24 IncludeUnlocatableModules = 1,
427
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
25 PrintDot = 1<<1,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
26 HighlightCyclicEdges = 1<<2,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
27 HighlightCyclicVertices = 1<<3,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
28 GroupByPackageNames = 1<<4,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
29 GroupByFullPackageName = 1<<5,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
30 PrintPaths = 1<<6,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
31 PrintList = 1<<7,
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
32 MarkCyclicModules = 1<<8,
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
33 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
34
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
35 string findModulePath(string moduleFQNPath, string[] importPaths)
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
36 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
37 auto filePath = new FilePath();
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
38 foreach (importPath; importPaths)
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
39 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
40 filePath.set(importPath);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
41 filePath.append(moduleFQNPath);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
42 foreach (moduleSuffix; [".d", ".di"/*interface file*/])
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
43 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
44 filePath.suffix(moduleSuffix);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
45 if (filePath.exists())
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
46 return filePath.toString();
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
47 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
48 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
49 return null;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
50 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
51
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
52 class Graph
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
53 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
54 Vertex[] vertices;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
55 Edge[] edges;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
56
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
57 void addVertex(Vertex vertex)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
58 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
59 vertex.id = vertices.length;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
60 vertices ~= vertex;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
61 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
62
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
63 void addEdge(Vertex from, Vertex to)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
64 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
65 edges ~= new Edge(from, to);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
66 from.outgoing ~= to;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
67 to.incoming ~= from;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
68 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
69
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
70 /// Walks the graph and marks cyclic vertices and edges.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
71 void detectCycles()
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
72 { // Cycles could also be detected in the GraphBuilder,
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
73 // but having the code here makes things much clearer.
699
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
74 bool visit(Vertex vertex)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
75 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
76 if (vertex.status == Vertex.Status.Visiting)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
77 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
78 vertex.isCyclic = true;
699
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
79 return true;
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
80 }
699
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
81 if (vertex.status == Vertex.Status.Visited)
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
82 return false;
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
83 // Flag as visiting.
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
84 vertex.status = Vertex.Status.Visiting;
699
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
85 // Visit successors.
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
86 foreach (outVertex; vertex.outgoing)
699
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
87 vertex.isCyclic |= visit(outVertex);
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
88 // Flag as visited.
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
89 vertex.status = Vertex.Status.Visited;
699
4c0ea78a7f8b Fixed cycle detection algorithm.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 698
diff changeset
90 return false;
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
91 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
92 // Start visiting vertices.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
93 visit(vertices[0]);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
94
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
95 foreach (edge; edges)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
96 if (edge.from.isCyclic && edge.to.isCyclic)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
97 edge.isCyclic = true;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
98 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
99 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
100
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
101 /// Represents a directed connection between two vertices.
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
102 class Edge
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
103 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
104 Vertex from; /// Coming from vertex.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
105 Vertex to; /// Going to vertex.
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
106 bool isCyclic;
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
107
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
108 this(Vertex from, Vertex to)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
109 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
110 this.from = from;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
111 this.to = to;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
112 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
113 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
114
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
115 /// Represents a module in the graph.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
116 class Vertex
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
117 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
118 Module modul; /// The module represented by this vertex.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
119 uint id; /// The nth vertex in the graph.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
120 Vertex[] incoming; /// Also called predecessors.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
121 Vertex[] outgoing; /// Also called successors.
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
122 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
123
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
124 enum Status : ubyte
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
125 { None, Visiting, Visited }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
126 Status status; /// Used by the cycle detection algorithm.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
127 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
128
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
129 class GraphBuilder
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
130 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
131 Graph graph;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
132 IGraphOption options;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
133 string[] importPaths; /// Where to look for modules.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
134 Vertex[string] loadedModulesTable; /// Maps FQN paths to modules.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
135 bool delegate(string) filterPredicate;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
136
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
137 this()
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
138 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
139 this.graph = new Graph;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
140 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
141
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
142 /// Start building the graph and return that.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
143 Graph start(string fileName)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
144 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
145 loadModule(fileName);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
146 return graph;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
147 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
148
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
149 /++
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
150 Loads all modules recursively and builds the graph at the same time.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
151 Params:
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
152 moduleFQNPath = e.g.: dil/ast/Node (module FQN = dil.ast.Node)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
153 +/
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
154 Vertex loadModule(string moduleFQNPath)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
155 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
156 // Filter out modules.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
157 if (filterPredicate(moduleFQNPath))
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
158 return null;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
159
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
160 // Look up in table if the module is already loaded.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
161 auto pVertex = moduleFQNPath in loadedModulesTable;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
162 if (pVertex !is null)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
163 return *pVertex; // Return already loaded module.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
164
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
165 // Locate the module in the file system.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
166 auto moduleFilePath = findModulePath(moduleFQNPath, importPaths);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
167
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
168 Vertex vertex;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
169
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
170 if (moduleFilePath is null)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
171 { // Module not found.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
172 if (options & IGraphOption.IncludeUnlocatableModules)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
173 { // Include module nevertheless.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
174 vertex = new Vertex;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
175 vertex.modul = new Module("");
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
176 vertex.modul.setFQN(replace(moduleFQNPath, dirSep, '.'));
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
177 graph.addVertex(vertex);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
178 loadedModulesTable[moduleFQNPath] = vertex;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
179 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
180 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
181 else
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
182 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
183 auto modul = new Module(moduleFilePath);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
184 // Use lightweight ImportParser.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
185 modul.parser = new ImportParser(loadFile(moduleFilePath), moduleFilePath);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
186 modul.parse();
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
187
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
188 vertex = new Vertex;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
189 vertex.modul = modul;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
190
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
191 graph.addVertex(vertex);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
192 loadedModulesTable[modul.getFQNPath()] = vertex;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
193 // Load modules which this module depends on.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
194 foreach (moduleFQNPath_; modul.getImportPaths())
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
195 {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
196 auto loaded = loadModule(moduleFQNPath_);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
197 if (loaded !is null)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
198 graph.addEdge(vertex, loaded);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
199 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
200 }
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
201 return vertex;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
202 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
203 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
204
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
205 void execute(string filePathString, string[] importPaths, string[] strRegexps, uint levels, IGraphOption options)
375
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 // Init regular expressions.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
208 RegExp[] regexps;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
209 foreach (strRegexp; strRegexps)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
210 regexps ~= new RegExp(strRegexp);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
211
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
212 // Add directory of file and global directories to import paths.
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
213 auto filePath = new FilePath(filePathString);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
214 auto fileDir = filePath.folder();
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
215 if (fileDir.length)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
216 importPaths ~= fileDir;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
217 importPaths ~= GlobalSettings.importPaths;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
218
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
219
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
220 auto gbuilder = new GraphBuilder;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
221
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
222 gbuilder.importPaths = importPaths;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
223 gbuilder.options = options;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
224 gbuilder.filterPredicate = (string moduleFQNPath) {
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
225 foreach (rx; regexps)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
226 // Replace slashes: dil/ast/Node -> dil.ast.Node
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
227 if (rx.test(replace(moduleFQNPath, dirSep, '.')))
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
228 return true;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
229 return false;
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
230 };
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
231
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
232 auto graph = gbuilder.start(filePath.name());
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
233
427
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
234 if (options & (IGraphOption.PrintList | IGraphOption.PrintPaths))
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
235 {
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
236 if (options & IGraphOption.MarkCyclicModules)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
237 graph.detectCycles();
427
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
238
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
239 if (options & IGraphOption.PrintPaths)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
240 printModulePaths(graph.vertices, levels+1, "");
427
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
241 else
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
242 printModuleList(graph.vertices, levels+1, "");
427
e2bbc6406a14 Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
243 }
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
244 else
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
245 printDotDocument(graph, options);
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
246 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
247
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
248 void printModulePaths(Vertex[] vertices, uint level, char[] indent)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
249 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
250 if (level == 0)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
251 return;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
252 foreach (vertex; vertices)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
253 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
254 Stdout(indent)((vertex.isCyclic?"*":"")~vertex.modul.filePath).newline;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
255 if (vertex.outgoing.length)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
256 printModulePaths(vertex.outgoing, level-1, indent~" ");
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
257 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
258 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
259
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
260 void printModuleList(Vertex[] vertices, uint level, char[] indent)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
261 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
262 if (level == 0)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
263 return;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
264 foreach (vertex; vertices)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
265 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
266 Stdout(indent)((vertex.isCyclic?"*":"")~vertex.modul.getFQN()).newline;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
267 if (vertex.outgoing.length)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
268 printModuleList(vertex.outgoing, level-1, indent~" ");
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
269 }
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
270 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
271
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
272 void printDotDocument(Graph graph, IGraphOption options)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
273 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
274 Vertex[][string] verticesByPckgName;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
275 if (options & IGraphOption.GroupByFullPackageName)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
276 foreach (vertex; graph.vertices)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
277 verticesByPckgName[vertex.modul.packageName] ~= vertex;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
278
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
279 if (options & (IGraphOption.HighlightCyclicVertices |
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
280 IGraphOption.HighlightCyclicEdges))
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
281 graph.detectCycles();
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
282
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
283 // Output header of the dot document.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
284 Stdout("Digraph ImportGraph\n{\n");
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
285 // Output nodes.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
286 // 'i' and vertex.id should be the same.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
287 foreach (i, vertex; graph.vertices)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
288 Stdout.formatln(` n{} [label="{}"{}];`, i, vertex.modul.getFQN(), (vertex.isCyclic ? ",style=filled,fillcolor=tomato" : ""));
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
289 // Output edges.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
290 foreach (edge; graph.edges)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
291 Stdout.formatln(` n{} -> n{}{};`, edge.from.id, edge.to.id, (edge.isCyclic ? "[color=red]" : ""));
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
292
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
293 if (options & IGraphOption.GroupByFullPackageName)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
294 foreach (packageName, vertices; verticesByPckgName)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
295 { // Output nodes in a cluster.
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
296 Stdout.format(` subgraph "cluster_{}" {`\n` label="{}";color=blue;`"\n ", packageName, packageName);
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
297 foreach (vertex; vertices)
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
298 Stdout.format(`n{};`, vertex.id);
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
299 Stdout("\n }\n");
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
300 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
301
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
302 Stdout("}\n");
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
303 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
304
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
305 /+
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
306 // This is the old algorithm that was used to detect cycles in a directed graph.
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
307 void analyzeGraph(Vertex[] vertices_init, Edge[] edges)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
308 {
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
309 void recursive(Vertex[] vertices)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
310 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
311 foreach (idx, vertex; vertices)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
312 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
313 uint outgoing, incoming;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
314 foreach (j, edge; edges)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
315 {
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
316 if (edge.from is vertex)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
317 outgoing++;
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
318 if (edge.to is vertex)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
319 incoming++;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
320 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
321
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
322 if (outgoing == 0)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
323 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
324 if (incoming != 0)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
325 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
326 // Vertex is a sink.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
327 alias outgoing i; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
328 alias incoming j; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
329 // Remove edges.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
330 for (i=j=0; i < edges.length; i++)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
331 if (edges[i].to !is vertex)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
332 edges[j++] = edges[i];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
333 edges.length = j;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
334 vertices = vertices[0..idx] ~ vertices[idx+1..$];
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
335 recursive(vertices);
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
336 return;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
337 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
338 else
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
339 {
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
340 // Edges to this vertex were removed previously.
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
341 // Only remove vertex now.
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
342 vertices = vertices[0..idx] ~ vertices[idx+1..$];
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
343 recursive(vertices);
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
344 return;
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
345 }
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
346 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
347 else if (incoming == 0)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
348 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
349 // Vertex is a source
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
350 alias outgoing i; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
351 alias incoming j; // Reuse
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
352 // Remove edges.
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
353 for (i=j=0; i < edges.length; i++)
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
354 if (edges[i].from !is vertex)
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
355 edges[j++] = edges[i];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
356 edges.length = j;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
357 vertices = vertices[0..idx] ~ vertices[idx+1..$];
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
358 recursive(vertices);
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
359 return;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
360 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
361 // else
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
362 // {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
363 // // source && sink
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
364 // // continue loop.
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
365 // }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
366 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
367
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
368 // 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
369 foreach (vertex; vertices)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
370 vertex.isCyclic = true;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
371 foreach (edge; edges)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
372 if (edge)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 370
diff changeset
373 edge.isCyclic = true;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
374 }
431
7a6bfa569a52 Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 427
diff changeset
375 recursive(vertices_init);
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
376 }
698
1564e41f454e Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
377 +/