-
Notifications
You must be signed in to change notification settings - Fork 168
/
resolve_context.go
224 lines (202 loc) · 6.08 KB
/
resolve_context.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package maven
import (
"context"
"fmt"
"github.com/murphysecurity/murphysec/utils"
"github.com/vifraa/gopom"
"go.uber.org/zap"
)
type pomBuilder struct {
coordinate Coordinate
parentCoordinate *Coordinate
properties *properties
deps *pomDependencySet
depms *pomDependencySet
pom *UnresolvedPom
}
func newPomBuilder() *pomBuilder {
return &pomBuilder{
properties: newProperties(),
deps: newPomDependencySet(),
depms: newPomDependencySet(),
}
}
// listDependencyManagements 返回已解析属性的依赖管理列表
func (p *pomBuilder) listDependencyManagements() []gopom.Dependency {
var rs []gopom.Dependency
for _, dep := range p.depms.listAll() {
d := gopom.Dependency{
GroupID: p.properties.Resolve(dep.GroupID),
ArtifactID: p.properties.Resolve(dep.ArtifactID),
Version: p.properties.Resolve(dep.Version),
Type: p.properties.Resolve(dep.Type),
Classifier: p.properties.Resolve(dep.Classifier),
Scope: p.properties.Resolve(dep.Scope),
SystemPath: p.properties.Resolve(dep.SystemPath),
Exclusions: dep.Exclusions,
Optional: dep.Optional,
}
rs = append(rs, d)
}
return rs
}
func (p *pomBuilder) build() *Pom {
return &Pom{
dir: "",
project: p.pom.Project,
depSet: p.deps,
depmSet: p.depms,
Coordinate: p.coordinate,
properties: p.properties,
}
}
type resolveContext struct {
logger *zap.Logger
visited map[Coordinate]struct{}
resolver *PomResolver
}
func newResolveContext(ctx context.Context) *resolveContext {
return &resolveContext{
logger: utils.UseLogger(ctx),
visited: map[Coordinate]struct{}{},
}
}
func (r *resolveContext) visitEnter(coordinate Coordinate) bool {
if _, ok := r.visited[coordinate]; ok {
return true
}
return false
}
func (r *resolveContext) visitExit(coordinate Coordinate) {
delete(r.visited, coordinate)
}
func (r *resolveContext) _resolve(ctx context.Context, coordinate Coordinate) (*Pom, error) {
if coordinate.IsBad() {
return nil, ErrBadCoordinate.Detailed(coordinate.String())
}
if r.visitEnter(coordinate) {
return nil, ErrPomCircularDependent.Detailed(coordinate.String())
}
defer r.visitExit(coordinate)
builder := newPomBuilder()
rawPom, e := r.resolver.fetchPom(ctx, coordinate)
if e != nil {
return nil, e
}
builder.pom = rawPom
builder.parentCoordinate = rawPom.ParentCoordinate()
r.resolveInheritance(ctx, builder)
if e := r.resolveCoordinate(builder); e != nil {
return nil, e
}
// resolve & merge dependencyManagement.type==import
r.resolveDependencyManagementImport(ctx, builder)
// merge dependencyManagement into dependencies
builder.deps.mergeAll(builder.depms.listAll(), false, true)
return builder.build(), nil
}
func (r *resolveContext) resolveDependencyManagementImport(ctx context.Context, builder *pomBuilder) {
for _, dependency := range builder.listDependencyManagements() {
if dependency.Scope != "import" {
continue
}
coordinate := Coordinate{dependency.GroupID, dependency.ArtifactID, dependency.Version}
np, e := r._resolve(ctx, coordinate)
if e != nil {
continue
}
builder.depms.mergeAll(np.ListDependencyManagements(), false, false)
}
}
func (r *resolveContext) resolveInheritance(ctx context.Context, builder *pomBuilder) {
logger := r.logger
coordinate := builder.parentCoordinate
var visitedParents = make(map[Coordinate]struct{})
var poms []*UnresolvedPom
poms = append(poms, builder.pom)
for coordinate != nil {
if _, ok := visitedParents[*coordinate]; ok {
break
}
visitedParents[*coordinate] = struct{}{}
parent, e := r.resolver.fetchPom(ctx, *coordinate)
if e != nil {
logger.Warn("Fetch parent failed", zap.Any("coordinate", *coordinate), zap.Error(e))
break
}
poms = append(poms, parent)
coordinate = parent.ParentCoordinate()
}
utils.Reverse(poms)
for _, pom := range poms {
project := pom.Project
// merge property
builder.properties.PutMap(project.Properties.Entries)
// merge dependency management
builder.depms.mergeAll(project.DependencyManagement.Dependencies, false, false)
// merge dependencies
builder.deps.mergeAll(project.Dependencies, true, false)
}
// merge ${project.parent.*}
if c := builder.parentCoordinate; c != nil {
builder.properties.PutIfAbsent("project.parent.version", c.Version)
builder.properties.PutIfAbsent("project.parent.artifactId", c.ArtifactId)
builder.properties.PutIfAbsent("project.parent.groupId", c.GroupId)
}
}
func (r *resolveContext) resolveCoordinate(builder *pomBuilder) error {
pf := builder.pom.Project
g := pf.GroupID
if g == "" {
g = pf.Parent.GroupID
}
a := pf.ArtifactID
if a == "" {
a = pf.Parent.ArtifactID
}
v := pf.Version
if v == "" {
v = pf.Parent.Version
}
coordinate := Coordinate{
GroupId: builder.properties.Resolve(g),
ArtifactId: builder.properties.Resolve(a),
Version: builder.properties.Resolve(v),
}
if !coordinate.Complete() {
return ErrCouldNotResolve.Detailed(fmt.Sprintf("bad coordinate: %s", coordinate))
}
builder.coordinate = coordinate
// merge ${project.*} ${${project.artifactId.*}}
builder.properties.PutIfAbsent("project.version", coordinate.Version)
builder.properties.PutIfAbsent("project.artifactId", coordinate.ArtifactId)
builder.properties.PutIfAbsent("project.groupId", coordinate.GroupId)
builder.properties.PutIfAbsent(fmt.Sprintf("%s.groupId", coordinate.ArtifactId), coordinate.GroupId)
builder.properties.PutIfAbsent(fmt.Sprintf("%s.version", coordinate.ArtifactId), coordinate.Version)
return nil
}
type resolvedPomCache struct {
m map[Coordinate]*Pom
em map[Coordinate]error
}
func newResolvedPomCache() *resolvedPomCache {
return &resolvedPomCache{
m: map[Coordinate]*Pom{},
em: map[Coordinate]error{},
}
}
func (r *resolvedPomCache) storeErr(coordinate Coordinate, err error) {
r.em[coordinate] = err
}
func (r *resolvedPomCache) storePom(coordinate Coordinate, pom *Pom) {
r.m[coordinate] = pom
}
func (r *resolvedPomCache) get(coordinate Coordinate) (*Pom, error) {
if e, ok := r.em[coordinate]; ok {
return nil, e
}
if p, ok := r.m[coordinate]; ok {
return p, nil
}
return nil, nil
}