-
Notifications
You must be signed in to change notification settings - Fork 19
/
module.go
123 lines (101 loc) · 2.92 KB
/
module.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package depgraph
import (
"fmt"
"time"
"github.com/Helcaraxan/gomod/internal/graph"
"github.com/Helcaraxan/gomod/internal/modules"
)
// Module represents a module in a Go module's dependency graph.
type Module struct {
Info *modules.ModuleInfo
Indirects map[string]bool
VersionConstraints map[string]VersionConstraint
predecessors graph.NodeRefs
successors graph.NodeRefs
packages graph.NodeRefs
isNonTestDependency bool
}
type VersionConstraint struct {
Source string
Target string
}
func NewModule(info *modules.ModuleInfo) *Module {
return &Module{
Info: info,
Indirects: map[string]bool{},
VersionConstraints: map[string]VersionConstraint{},
predecessors: graph.NewNodeRefs(),
successors: graph.NewNodeRefs(),
packages: graph.NewNodeRefs(),
}
}
// Name of the module represented by this Dependency in the Graph instance.
func (m *Module) Name() string {
return m.Info.Path
}
func (m *Module) Hash() string {
return moduleHash(m.Info.Path)
}
func (m *Module) String() string {
return fmt.Sprintf("%s, preds: [%s], succs: [%s]", m.Hash(), m.predecessors, m.successors)
}
func moduleHash(name string) string {
return "module " + name
}
// SelectedVersion corresponds to the version of the dependency represented by this Dependency which
// was selected for use.
func (m *Module) SelectedVersion() string {
if m.Info.Replace != nil {
return m.Info.Replace.Version
}
return m.Info.Version
}
// Timestamp returns the time corresponding to the creation of the version at which this dependency
// is used.
func (m *Module) Timestamp() *time.Time {
if m.Info.Replace != nil {
return m.Info.Replace.Time
}
return m.Info.Time
}
func (m *Module) Parent() graph.Node {
return nil
}
func (m *Module) Predecessors() *graph.NodeRefs {
return &m.predecessors
}
func (m *Module) Successors() *graph.NodeRefs {
return &m.successors
}
func (m *Module) Children() *graph.NodeRefs {
return &m.packages
}
func (m *Module) NodeAttributes(annotate bool) []string {
var annotations []string
if annotate && m.SelectedVersion() != "" {
var replacement string
if m.Info.Replace != nil {
replacement = m.Info.Replace.Path + "<br />"
}
annotations = append(
annotations,
fmt.Sprintf("label=<%s<br /><font point-size=\"10\">%s%s</font>>", m.Name(), replacement, m.SelectedVersion()),
)
}
return annotations
}
func (m *Module) EdgeAttributes(target graph.Node, annotate bool) []string {
targetModule := target.(*Module)
var annotations []string
if m.Indirects[target.Name()] {
annotations = append(annotations, "style=dashed")
}
if c, ok := m.VersionConstraints[targetModule.Hash()]; ok && annotate {
annotations = append(annotations, fmt.Sprintf("label=<<font point-size=\"10\">%s</font>>", c.Target))
}
return annotations
}
var _ testAnnotated = &Module{}
func (m *Module) isTestDependency() bool {
return !m.isNonTestDependency
}