/
print.go
89 lines (81 loc) · 2.72 KB
/
print.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
package maven
import (
"bytes"
"fmt"
"strings"
"text/template"
)
const mavenJarTemplate = `maven_jar(
name = '{{ .ArtifactID }}',
id = '{{ .GroupID }}:{{ .ArtifactID }}:{{ .Version }}',
hash = '',{{ if .Dependencies.Dependency }}
deps = [
{{ range .Dependencies.Dependency }} ':{{ .ArtifactID }}',
{{ end }} ],{{ end }}
)`
// AllDependencies returns all the dependencies of these artifacts in a short format
// that we consume later. The format is vaguely akin to a Maven id, although we consider
// it an internal detail - it must agree between this and the maven_jars build rule that
// consumes it, but we don't hold it stable between different Please versions. The format is:
// group_id:artifact_id:version:{src|no_src}[:licence|licence|...]
//
// Alternatively if buildRules is true, it will return a series of maven_jar rules
// that could be pasted into a BUILD file.
func AllDependencies(f *Fetch, artifacts []Artifact, concurrency int, indent, buildRules bool) []string {
f.Resolver.Run(artifacts, concurrency)
f.Resolver.Mediate()
done := map[unversioned]bool{}
ret := []string{}
for _, a := range artifacts {
ret = append(ret, allDeps(f.Pom(&a), indent, buildRules, done)...)
}
return ret
}
func allDeps(pom *PomXML, indent, buildRules bool, done map[unversioned]bool) []string {
if buildRules {
tmpl := template.Must(template.New("maven_jar").Parse(mavenJarTemplate))
return allDependencies(pom, "", "", tmpl, done)
}
indentIncrement := ""
if indent {
indentIncrement = " "
}
// Just run through dependencies here, not the top-level pom itself.
ret := []string{}
for _, dep := range pom.AllDependencies() {
if !done[dep.unversioned] {
done[dep.unversioned] = true
ret = append(ret, allDependencies(dep, "", indentIncrement, nil, done)...)
}
}
return ret
}
// allDependencies implements the logic of AllDependencies with indenting.
func allDependencies(pom *PomXML, currentIndent, indentIncrement string, tmpl *template.Template, done map[unversioned]bool) []string {
ret := []string{
fmt.Sprintf("%s%s:%s", currentIndent, pom.Artifact, source(pom)),
}
if tmpl != nil {
var buf bytes.Buffer
if err := tmpl.Execute(&buf, pom); err != nil {
log.Fatalf("%s\n", err)
}
ret[0] = buf.String()
} else if licences := pom.AllLicences(); len(licences) > 0 {
ret[0] += ":" + strings.Join(licences, "|")
}
for _, dep := range pom.AllDependencies() {
if !done[dep.unversioned] {
done[dep.unversioned] = true
ret = append(ret, allDependencies(dep, currentIndent+indentIncrement, indentIncrement, tmpl, done)...)
}
}
return ret
}
// source returns the src / no_src indicator for a single pom.
func source(pom *PomXML) string {
if pom.HasSources {
return "src"
}
return "no_src"
}