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