pom2bp: clean up templates

Move some of the more complicated conditional logic out to helper
functions.  Use the {{- }} syntax to strip previous whitespace to
allow spacing out the remaning conditionals in the templates.

Test: (cd prebuilts/sdk/current/androidx && pom2bp -regen Android.bp)
Change-Id: I766bd0e1837aa04375f322fbe796d923cd99ecde
This commit is contained in:
Colin Cross 2018-08-29 16:17:55 -07:00
parent 42d48b7b8b
commit 632987ac21

View file

@ -186,6 +186,34 @@ func (p Pom) IsDeviceModule() bool {
return !p.IsHostModule() return !p.IsHostModule()
} }
func (p Pom) ModuleType() string {
if p.IsAar() {
return "android_library"
} else if p.IsHostModule() {
return "java_library_host"
} else {
return "java_library_static"
}
}
func (p Pom) ImportModuleType() string {
if p.IsAar() {
return "android_library_import"
} else if p.IsHostModule() {
return "java_import_host"
} else {
return "java_import"
}
}
func (p Pom) ImportProperty() string {
if p.IsAar() {
return "aars"
} else {
return "jars"
}
}
func (p Pom) BpName() string { func (p Pom) BpName() string {
if p.BpTarget == "" { if p.BpTarget == "" {
p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId) p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
@ -293,27 +321,43 @@ func (p *Pom) ExtractMinSdkVersion() error {
} }
var bpTemplate = template.Must(template.New("bp").Parse(` var bpTemplate = template.Must(template.New("bp").Parse(`
{{if .IsAar}}android_library_import{{else if .IsDeviceModule}}java_import{{else}}java_import_host{{end}} { {{.ImportModuleType}} {
name: "{{.BpName}}-nodeps", name: "{{.BpName}}-nodeps",
{{if .IsAar}}aars{{else}}jars{{end}}: ["{{.ArtifactFile}}"], {{.ImportProperty}}: ["{{.ArtifactFile}}"],
sdk_version: "{{.SdkVersion}}",{{if .IsAar}} sdk_version: "{{.SdkVersion}}",
{{- if .IsAar}}
min_sdk_version: "{{.MinSdkVersion}}", min_sdk_version: "{{.MinSdkVersion}}",
static_libs: [{{range .BpAarDeps}} static_libs: [
"{{.}}",{{end}}{{range .BpExtraDeps}} {{- range .BpAarDeps}}
"{{.}}",{{end}} "{{.}}",
],{{end}} {{- end}}
{{- range .BpExtraDeps}}
"{{.}}",
{{- end}}
],
{{- end}}
} }
{{if .IsAar}}android_library{{else if .IsDeviceModule}}java_library_static{{else}}java_library_host{{end}} { {{.ModuleType}} {
name: "{{.BpName}}",{{if .IsDeviceModule}} name: "{{.BpName}}",
sdk_version: "{{.SdkVersion}}",{{if .IsAar}} {{- if .IsDeviceModule}}
sdk_version: "{{.SdkVersion}}",
{{- if .IsAar}}
min_sdk_version: "{{.MinSdkVersion}}", min_sdk_version: "{{.MinSdkVersion}}",
manifest: "manifests/{{.BpName}}/AndroidManifest.xml",{{end}}{{end}} manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
{{- end}}
{{- end}}
static_libs: [ static_libs: [
"{{.BpName}}-nodeps",{{range .BpJarDeps}} "{{.BpName}}-nodeps",
"{{.}}",{{end}}{{range .BpAarDeps}} {{- range .BpJarDeps}}
"{{.}}",{{end}}{{range .BpExtraDeps}} "{{.}}",
"{{.}}",{{end}} {{- end}}
{{- range .BpAarDeps}}
"{{.}}",
{{- end}}
{{- range .BpExtraDeps}}
"{{.}}",
{{- end}}
], ],
java_version: "1.7", java_version: "1.7",
} }