-
Notifications
You must be signed in to change notification settings - Fork 19
/
graph.go
60 lines (50 loc) · 1.24 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package depgraph
import (
"github.com/Helcaraxan/gomod/internal/graph"
"github.com/Helcaraxan/gomod/internal/logger"
"github.com/Helcaraxan/gomod/internal/modules"
)
// DepGraph represents a Go module's dependency graph.
type DepGraph struct {
Path string
Main *Module
Graph *graph.HierarchicalDigraph
replaces map[string]string
}
type Level uint8
const (
LevelModules Level = iota
LevelPackages
)
func NewGraph(log *logger.Logger, path string, main *modules.ModuleInfo) *DepGraph {
g := &DepGraph{
Path: path,
Graph: graph.NewHierarchicalDigraph(log),
replaces: map[string]string{},
}
g.Main = g.AddModule(main)
return g
}
func (g *DepGraph) getModule(name string) (*Module, bool) {
if replaced, ok := g.replaces[name]; ok {
name = replaced
}
node, err := g.Graph.GetNode(moduleHash(name))
if err != nil {
return nil, false
}
return node.(*Module), true
}
func (g *DepGraph) AddModule(module *modules.ModuleInfo) *Module {
if module == nil {
return nil
} else if node, _ := g.Graph.GetNode(moduleHash(module.Path)); node != nil {
return node.(*Module)
}
newModule := NewModule(module)
_ = g.Graph.AddNode(newModule)
if module.Replace != nil {
g.replaces[module.Replace.Path] = module.Path
}
return newModule
}