/
query.go
240 lines (183 loc) · 6.07 KB
/
query.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"github.com/ajacward/maven-client/maven"
"golang.org/x/text/encoding/ianaindex"
)
func defaultString(value string, defaultValue string) string {
result := value
if len(value) == 0 {
result = defaultValue
}
return result
}
func buildProps(project maven.Project, deps map[string]maven.Project) map[string]string {
props := make(map[string]string, 0)
if len(project.Parent.ArtifactId) > 0 {
props = buildProps(deps[project.Parent.GavForm()], deps)
props["project.parent.version"] = project.Parent.Version
}
for k, v := range project.Properties {
props[k] = v
}
props["project.version"] = project.Version
r, _ := regexp.Compile(`\$\{(.+)\}`)
for k, v := range props {
if strings.Contains(v, "${") {
token := r.FindStringSubmatch(v)[1]
props[k] = props[token]
}
}
return props
}
func findInheritedValue(dep maven.Dependency, parent maven.Project, deps map[string]maven.Project) maven.Dependency {
var inheritedValue maven.Dependency
for _, d := range parent.DependencyManagement.Dependencies {
if d.ArtifactId == dep.ArtifactId && d.GroupId == dep.GroupId {
inheritedValue = d
break
}
}
if len(inheritedValue.ArtifactId) == 0 && len(parent.Parent.ArtifactId) > 0 {
inheritedValue = findInheritedValue(dep, deps[parent.Parent.GavForm()], deps)
}
return inheritedValue
}
func findInheritedValues(dep maven.Dependency, parent maven.Project, deps map[string]maven.Project) maven.Dependency {
inheritedValues := findInheritedValue(dep, parent, deps)
props := buildProps(parent, deps)
r, _ := regexp.Compile(`\$\{(.+)\}`)
if strings.Contains(inheritedValues.Version, "${") {
versionToken := r.FindStringSubmatch(inheritedValues.Version)[1]
inheritedValues.Version = props[versionToken]
}
return inheritedValues
}
func Unmarshal(data []byte, v interface{}) error {
decoder := xml.NewDecoder(bytes.NewReader(data))
decoder.CharsetReader = func(charset string, reader io.Reader) (io.Reader, error) {
enc, err := ianaindex.IANA.Encoding(charset)
if err != nil {
return nil, fmt.Errorf("charset %s: %s", charset, err.Error())
}
if enc == nil {
// Assume it's compatible with (a subset of) UTF-8 encoding
// Bug: https://github.com/golang/go/issues/19421
return reader, nil
}
return enc.NewDecoder().Reader(reader), nil
}
return decoder.Decode(v)
}
func any(exclusions []maven.Exclusion, f func(maven.Exclusion) bool) bool {
exists := false
for _, exclusion := range exclusions {
if f(exclusion) {
exists = true
break
}
}
return exists
}
func queryCoordinate(coordinate string, exclusions []maven.Exclusion, config Config, dependencies map[string]maven.Project) {
_, prs := dependencies[coordinate]
if !prs {
d := maven.NewMavenCoordinate(coordinate)
url := fmt.Sprintf("%s/%s.pom", config.repoUrl, d.UrlPath())
fmt.Println(url)
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var project maven.Project
err = Unmarshal(body, &project)
if err != nil {
log.Fatalln("error:", err)
}
dependencies[coordinate] = project
if len(project.Parent.ArtifactId) > 0 {
queryCoordinate(project.Parent.GavForm(), []maven.Exclusion{}, config, dependencies)
}
parent, parentPrs := dependencies[project.Parent.GavForm()]
props := buildProps(project, dependencies)
if len(exclusions) == 0 && parentPrs {
exclusions = findInheritedValues(maven.Dependency{GroupId: project.GroupId, ArtifactId: project.ArtifactId}, parent, dependencies).Exclusions
}
bomDependencyManagement := make([]maven.Dependency, 0)
for _, dm := range project.DependencyManagement.Dependencies {
if dm.Scope == "import" {
r, _ := regexp.Compile(`\$\{(.+)\}`)
if strings.Contains(dm.GroupId, "${") {
groupToken := r.FindStringSubmatch(dm.GroupId)[1]
dm.GroupId = props[groupToken]
}
if strings.Contains(dm.Version, "${") {
versionToken := r.FindStringSubmatch(dm.Version)[1]
dm.Version = props[versionToken]
}
queryCoordinate(dm.GavForm(),
[]maven.Exclusion{},
config, dependencies)
bomProps := buildProps(dependencies[dm.GavForm()], dependencies)
for _, bmDm := range dependencies[dm.GavForm()].DependencyManagement.Dependencies {
if strings.Contains(bmDm.Version, "${") {
versionToken := r.FindStringSubmatch(bmDm.Version)[1]
bmDm.Version = bomProps[versionToken]
}
bomDependencyManagement = append(bomDependencyManagement, bmDm)
}
}
}
for _, dependency := range project.Dependencies {
inheritedValues := findInheritedValues(dependency, project, dependencies)
if dependency.Version == "" {
for _, bomDm := range bomDependencyManagement {
if bomDm.GroupId == dependency.GroupId && bomDm.ArtifactId == dependency.ArtifactId {
dependency.Version = bomDm.Version
break
}
}
}
if dependency.Scope == "" {
dependency.Scope = defaultString(inheritedValues.Scope, "compile")
}
if !dependency.Optional {
dependency.Optional = inheritedValues.Optional
}
isExcluded := any(exclusions, func(e maven.Exclusion) bool {
return e.GroupId == dependency.GroupId && e.ArtifactId == dependency.ArtifactId
})
if !isExcluded && (dependency.Scope == "compile" || dependency.Scope == "runtime") && !dependency.Optional {
r, _ := regexp.Compile(`\$\{(.+)\}`)
if strings.Contains(dependency.GroupId, "${") {
groupToken := r.FindStringSubmatch(dependency.GroupId)[1]
dependency.GroupId = props[groupToken]
}
if strings.Contains(dependency.Version, "${") {
versionToken := r.FindStringSubmatch(dependency.Version)[1]
dependency.Version = props[versionToken]
}
queryCoordinate(
fmt.Sprintf(
"%s:%s:%s",
defaultString(dependency.GroupId, inheritedValues.GroupId),
dependency.ArtifactId,
defaultString(defaultString(dependency.Version, inheritedValues.Version), d.Version)),
dependency.Exclusions,
config, dependencies)
}
}
}
}