github.com/tiagovtristao/plz@v13.4.0+incompatible/tools/please_maven/maven/print.go (about) 1 package maven 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "text/template" 8 ) 9 10 const mavenJarTemplate = ` 11 {{- if Needed .GroupID .ArtifactID -}} 12 maven_jar( 13 name = '{{ .ArtifactID }}', 14 id = '{{ .GroupID }}:{{ .ArtifactID }}:{{ .Version }}', 15 hash = '',{{ if .Dependencies.Dependency }} 16 deps = [ 17 {{ range .Dependencies.Dependency }} '{{ Dep .GroupID .ArtifactID }}', 18 {{ end }} ],{{ end }} 19 ) 20 {{- end -}} 21 ` 22 23 // AllDependencies returns all the dependencies of these artifacts in a short format 24 // that we consume later. The format is vaguely akin to a Maven id, although we consider 25 // it an internal detail - it must agree between this and the maven_jars build rule that 26 // consumes it, but we don't hold it stable between different Please versions. The format is: 27 // group_id:artifact_id:version:{src|no_src}[:licence|licence|...] 28 // 29 // Alternatively if buildRules is true, it will return a series of maven_jar rules 30 // that could be pasted into a BUILD file. 31 func AllDependencies(f *Fetch, artifacts []Artifact, concurrency int, indent, buildRules bool, graph *Graph) []string { 32 f.Resolver.Run(artifacts, concurrency) 33 f.Resolver.Mediate() 34 graph.BuildMapping() 35 36 done := map[unversioned]bool{} 37 ret := []string{} 38 for _, a := range artifacts { 39 ret = append(ret, allDeps(f.Pom(&a), indent, buildRules, done, graph)...) 40 } 41 return ret 42 } 43 44 func allDeps(pom *PomXML, indent, buildRules bool, done map[unversioned]bool, graph *Graph) []string { 45 if buildRules { 46 tmpl := template.Must(template.New("maven_jar").Funcs(template.FuncMap{ 47 "Dep": graph.Dep, 48 "Needed": graph.Needed, 49 }).Parse(mavenJarTemplate)) 50 return allDependencies(pom, "", "", tmpl, done) 51 } 52 53 indentIncrement := "" 54 if indent { 55 indentIncrement = " " 56 } 57 // Just run through dependencies here, not the top-level pom itself. 58 ret := []string{} 59 for _, dep := range pom.AllDependencies() { 60 if !done[dep.unversioned] { 61 done[dep.unversioned] = true 62 ret = append(ret, allDependencies(dep, "", indentIncrement, nil, done)...) 63 } 64 } 65 return ret 66 } 67 68 // allDependencies implements the logic of AllDependencies with indenting. 69 func allDependencies(pom *PomXML, currentIndent, indentIncrement string, tmpl *template.Template, done map[unversioned]bool) []string { 70 ret := []string{} 71 if tmpl != nil { 72 var buf bytes.Buffer 73 if err := tmpl.Execute(&buf, pom); err != nil { 74 log.Fatalf("%s\n", err) 75 } else if buf.Len() > 0 { 76 ret = []string{buf.String()} 77 } 78 } else { 79 ret = []string{fmt.Sprintf("%s%s:%s", currentIndent, pom.Artifact, source(pom))} 80 if licences := pom.AllLicences(); len(licences) > 0 { 81 ret[0] += ":" + strings.Join(licences, "|") 82 } 83 } 84 for _, dep := range pom.AllDependencies() { 85 if !done[dep.unversioned] { 86 done[dep.unversioned] = true 87 ret = append(ret, allDependencies(dep, currentIndent+indentIncrement, indentIncrement, tmpl, done)...) 88 } 89 } 90 return ret 91 } 92 93 // source returns the src / no_src indicator for a single pom. 94 func source(pom *PomXML) string { 95 if pom.HasSources { 96 return "src" 97 } 98 return "no_src" 99 }