2019-07-21 18:53:35 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module main
|
|
|
|
|
|
|
|
struct ModDepGraphNode {
|
2019-07-22 13:44:08 +03:00
|
|
|
mut:
|
2019-07-21 18:53:35 +03:00
|
|
|
name string
|
|
|
|
deps []string
|
2019-07-22 13:44:08 +03:00
|
|
|
last_cycle string
|
2019-07-21 18:53:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ModDepGraph {
|
|
|
|
pub:
|
|
|
|
mut:
|
|
|
|
acyclic bool
|
|
|
|
nodes []ModDepGraphNode
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DepSet {
|
|
|
|
mut:
|
|
|
|
items []string
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:44:08 +03:00
|
|
|
pub fn(dset mut DepSet) add(item string) {
|
|
|
|
dset.items << item
|
2019-07-21 18:53:35 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 13:44:08 +03:00
|
|
|
pub fn(dset &DepSet) diff(otherset DepSet) DepSet {
|
2019-07-21 18:53:35 +03:00
|
|
|
mut diff := DepSet{}
|
2019-07-22 13:44:08 +03:00
|
|
|
for item in dset.items {
|
2019-07-21 18:53:35 +03:00
|
|
|
if !item in otherset.items {
|
|
|
|
diff.items << item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:44:08 +03:00
|
|
|
pub fn(dset &DepSet) size() int {
|
|
|
|
return dset.items.len
|
2019-07-21 18:53:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_mod_dep_graph() *ModDepGraph {
|
|
|
|
return &ModDepGraph{
|
|
|
|
acyclic: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn(graph mut ModDepGraph) from_import_tables(import_tables []FileImportTable) {
|
|
|
|
for fit in import_tables {
|
|
|
|
mut deps := []string
|
|
|
|
for _, m in fit.imports {
|
|
|
|
deps << m
|
|
|
|
}
|
|
|
|
graph.add(fit.module_name, deps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn(graph mut ModDepGraph) add(mod string, deps []string) {
|
|
|
|
graph.nodes << ModDepGraphNode{
|
|
|
|
name: mod,
|
|
|
|
deps: deps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn(graph &ModDepGraph) resolve() *ModDepGraph {
|
|
|
|
mut node_names := map[string]ModDepGraphNode{}
|
|
|
|
mut node_deps := map[string]DepSet{}
|
|
|
|
|
|
|
|
for _, node in graph.nodes {
|
|
|
|
node_names[node.name] = node
|
|
|
|
|
|
|
|
mut dep_set := DepSet{}
|
|
|
|
for _, dep in node.deps {
|
|
|
|
dep_set.add(dep)
|
|
|
|
}
|
|
|
|
node_deps[node.name] = dep_set
|
|
|
|
}
|
|
|
|
|
|
|
|
mut resolved := new_mod_dep_graph()
|
|
|
|
for node_deps.size != 0 {
|
|
|
|
mut ready_set := DepSet{}
|
|
|
|
for name, deps in node_deps {
|
|
|
|
if deps.size() == 0 {
|
|
|
|
ready_set.add(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ready_set.size() == 0 {
|
|
|
|
mut g := new_mod_dep_graph()
|
|
|
|
g.acyclic = false
|
2019-07-22 13:44:08 +03:00
|
|
|
ndk := node_deps.keys()
|
2019-07-21 18:53:35 +03:00
|
|
|
for name, _ in node_deps {
|
2019-07-22 13:44:08 +03:00
|
|
|
mut node := node_names[name]
|
|
|
|
if name == ndk[node_deps.size-1] {
|
|
|
|
node.last_cycle = node_deps[name].items[node_deps[name].items.len-1]
|
|
|
|
}
|
|
|
|
g.nodes << node
|
2019-07-21 18:53:35 +03:00
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:50:51 +03:00
|
|
|
for name in ready_set.items {
|
|
|
|
node_deps.delete(name)
|
|
|
|
resolved.nodes << node_names[name]
|
2019-07-21 18:53:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, deps in node_deps {
|
|
|
|
node_deps[name] = deps.diff(ready_set)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn(graph &ModDepGraph) imports() []string {
|
|
|
|
mut mods := []string
|
|
|
|
for node in graph.nodes {
|
|
|
|
if node.name == 'main' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mods << node.name
|
|
|
|
}
|
|
|
|
return mods
|
|
|
|
}
|
|
|
|
|
2019-07-29 17:35:56 +03:00
|
|
|
pub fn(graph &ModDepGraph) last_node() ModDepGraphNode {
|
2019-07-21 18:53:35 +03:00
|
|
|
return graph.nodes[graph.nodes.len-1]
|
|
|
|
}
|
|
|
|
|
2019-07-26 19:02:58 +03:00
|
|
|
pub fn(graph &ModDepGraph) last_cycle() string {
|
2019-07-31 11:50:51 +03:00
|
|
|
return graph.last_node().last_cycle
|
2019-07-26 19:02:58 +03:00
|
|
|
}
|
|
|
|
|
2019-07-21 18:53:35 +03:00
|
|
|
pub fn(graph &ModDepGraph) display() {
|
2019-07-22 13:44:08 +03:00
|
|
|
for i:=0; i<graph.nodes.len; i++ {
|
|
|
|
node := graph.nodes[i]
|
|
|
|
for dep in node.deps {
|
|
|
|
mut out := ' * $node.name -> $dep'
|
|
|
|
if !graph.acyclic && i == graph.nodes.len-1 && dep == node.last_cycle {
|
|
|
|
out += ' <-- last cycle'
|
|
|
|
}
|
|
|
|
println(out)
|
2019-07-21 18:53:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|