Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 18 | "archive/zip" |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 19 | "bufio" |
| 20 | "bytes" |
| 21 | "encoding/xml" |
| 22 | "flag" |
| 23 | "fmt" |
| 24 | "io/ioutil" |
| 25 | "os" |
| 26 | "os/exec" |
| 27 | "path/filepath" |
| 28 | "regexp" |
| 29 | "sort" |
| 30 | "strings" |
| 31 | "text/template" |
| 32 | |
| 33 | "github.com/google/blueprint/proptools" |
| 34 | |
| 35 | "android/soong/bpfix/bpfix" |
| 36 | ) |
| 37 | |
| 38 | type RewriteNames []RewriteName |
| 39 | type RewriteName struct { |
| 40 | regexp *regexp.Regexp |
| 41 | repl string |
| 42 | } |
| 43 | |
| 44 | func (r *RewriteNames) String() string { |
| 45 | return "" |
| 46 | } |
| 47 | |
| 48 | func (r *RewriteNames) Set(v string) error { |
| 49 | split := strings.SplitN(v, "=", 2) |
| 50 | if len(split) != 2 { |
| 51 | return fmt.Errorf("Must be in the form of <regex>=<replace>") |
| 52 | } |
| 53 | regex, err := regexp.Compile(split[0]) |
| 54 | if err != nil { |
| 55 | return nil |
| 56 | } |
| 57 | *r = append(*r, RewriteName{ |
| 58 | regexp: regex, |
| 59 | repl: split[1], |
| 60 | }) |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | func (r *RewriteNames) MavenToBp(groupId string, artifactId string) string { |
| 65 | for _, r := range *r { |
| 66 | if r.regexp.MatchString(groupId + ":" + artifactId) { |
| 67 | return r.regexp.ReplaceAllString(groupId+":"+artifactId, r.repl) |
| 68 | } else if r.regexp.MatchString(artifactId) { |
| 69 | return r.regexp.ReplaceAllString(artifactId, r.repl) |
| 70 | } |
| 71 | } |
| 72 | return artifactId |
| 73 | } |
| 74 | |
| 75 | var rewriteNames = RewriteNames{} |
| 76 | |
| 77 | type ExtraDeps map[string][]string |
| 78 | |
| 79 | func (d ExtraDeps) String() string { |
| 80 | return "" |
| 81 | } |
| 82 | |
| 83 | func (d ExtraDeps) Set(v string) error { |
| 84 | split := strings.SplitN(v, "=", 2) |
| 85 | if len(split) != 2 { |
| 86 | return fmt.Errorf("Must be in the form of <module>=<module>[,<module>]") |
| 87 | } |
| 88 | d[split[0]] = strings.Split(split[1], ",") |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | var extraDeps = make(ExtraDeps) |
| 93 | |
| 94 | type Exclude map[string]bool |
| 95 | |
| 96 | func (e Exclude) String() string { |
| 97 | return "" |
| 98 | } |
| 99 | |
| 100 | func (e Exclude) Set(v string) error { |
| 101 | e[v] = true |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | var excludes = make(Exclude) |
| 106 | |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame^] | 107 | type HostModuleNames map[string]bool |
| 108 | |
| 109 | func (n HostModuleNames) IsHostModule(groupId string, artifactId string) bool { |
| 110 | _, found := n[groupId + ":" + artifactId] |
| 111 | return found |
| 112 | } |
| 113 | |
| 114 | func (n HostModuleNames) String() string { |
| 115 | return "" |
| 116 | } |
| 117 | |
| 118 | func (n HostModuleNames) Set(v string) error { |
| 119 | n[v] = true |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | var hostModuleNames = HostModuleNames{} |
| 124 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 125 | var sdkVersion string |
| 126 | var useVersion string |
| 127 | |
| 128 | func InList(s string, list []string) bool { |
| 129 | for _, l := range list { |
| 130 | if l == s { |
| 131 | return true |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | type Dependency struct { |
| 139 | XMLName xml.Name `xml:"dependency"` |
| 140 | |
| 141 | BpTarget string `xml:"-"` |
| 142 | |
| 143 | GroupId string `xml:"groupId"` |
| 144 | ArtifactId string `xml:"artifactId"` |
| 145 | Version string `xml:"version"` |
| 146 | Type string `xml:"type"` |
| 147 | Scope string `xml:"scope"` |
| 148 | } |
| 149 | |
| 150 | func (d Dependency) BpName() string { |
| 151 | if d.BpTarget == "" { |
| 152 | d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId) |
| 153 | } |
| 154 | return d.BpTarget |
| 155 | } |
| 156 | |
| 157 | type Pom struct { |
| 158 | XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"` |
| 159 | |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 160 | PomFile string `xml:"-"` |
| 161 | ArtifactFile string `xml:"-"` |
| 162 | BpTarget string `xml:"-"` |
| 163 | MinSdkVersion string `xml:"-"` |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 164 | |
| 165 | GroupId string `xml:"groupId"` |
| 166 | ArtifactId string `xml:"artifactId"` |
| 167 | Version string `xml:"version"` |
| 168 | Packaging string `xml:"packaging"` |
| 169 | |
| 170 | Dependencies []*Dependency `xml:"dependencies>dependency"` |
| 171 | } |
| 172 | |
| 173 | func (p Pom) IsAar() bool { |
| 174 | return p.Packaging == "aar" |
| 175 | } |
| 176 | |
| 177 | func (p Pom) IsJar() bool { |
| 178 | return p.Packaging == "jar" |
| 179 | } |
| 180 | |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame^] | 181 | func (p Pom) IsHostModule() bool { |
| 182 | return hostModuleNames.IsHostModule(p.GroupId, p.ArtifactId) |
| 183 | } |
| 184 | |
| 185 | func (p Pom) IsDeviceModule() bool { |
| 186 | return !p.IsHostModule() |
| 187 | } |
| 188 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 189 | func (p Pom) BpName() string { |
| 190 | if p.BpTarget == "" { |
| 191 | p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId) |
| 192 | } |
| 193 | return p.BpTarget |
| 194 | } |
| 195 | |
| 196 | func (p Pom) BpJarDeps() []string { |
| 197 | return p.BpDeps("jar", []string{"compile", "runtime"}) |
| 198 | } |
| 199 | |
| 200 | func (p Pom) BpAarDeps() []string { |
| 201 | return p.BpDeps("aar", []string{"compile", "runtime"}) |
| 202 | } |
| 203 | |
| 204 | func (p Pom) BpExtraDeps() []string { |
| 205 | return extraDeps[p.BpName()] |
| 206 | } |
| 207 | |
| 208 | // BpDeps obtains dependencies filtered by type and scope. The results of this |
| 209 | // method are formatted as Android.bp targets, e.g. run through MavenToBp rules. |
| 210 | func (p Pom) BpDeps(typeExt string, scopes []string) []string { |
| 211 | var ret []string |
| 212 | for _, d := range p.Dependencies { |
| 213 | if d.Type != typeExt || !InList(d.Scope, scopes) { |
| 214 | continue |
| 215 | } |
| 216 | name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId) |
| 217 | ret = append(ret, name) |
| 218 | } |
| 219 | return ret |
| 220 | } |
| 221 | |
| 222 | func (p Pom) SdkVersion() string { |
| 223 | return sdkVersion |
| 224 | } |
| 225 | |
| 226 | func (p *Pom) FixDeps(modules map[string]*Pom) { |
| 227 | for _, d := range p.Dependencies { |
| 228 | if d.Type == "" { |
| 229 | if depPom, ok := modules[d.BpName()]; ok { |
| 230 | // We've seen the POM for this dependency, use its packaging |
| 231 | // as the dependency type rather than Maven spec default. |
| 232 | d.Type = depPom.Packaging |
| 233 | } else { |
| 234 | // Dependency type was not specified and we don't have the POM |
| 235 | // for this artifact, use the default from Maven spec. |
| 236 | d.Type = "jar" |
| 237 | } |
| 238 | } |
| 239 | if d.Scope == "" { |
| 240 | // Scope was not specified, use the default from Maven spec. |
| 241 | d.Scope = "compile" |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 246 | // ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it |
| 247 | // to "current" if it is not present. |
| 248 | func (p *Pom) ExtractMinSdkVersion() error { |
| 249 | aar, err := zip.OpenReader(p.ArtifactFile) |
| 250 | if err != nil { |
| 251 | return err |
| 252 | } |
| 253 | defer aar.Close() |
| 254 | |
| 255 | var manifest *zip.File |
| 256 | for _, f := range aar.File { |
| 257 | if f.Name == "AndroidManifest.xml" { |
| 258 | manifest = f |
| 259 | break |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if manifest == nil { |
| 264 | return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile) |
| 265 | } |
| 266 | |
| 267 | r, err := manifest.Open() |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | defer r.Close() |
| 272 | |
| 273 | decoder := xml.NewDecoder(r) |
| 274 | |
| 275 | manifestData := struct { |
| 276 | XMLName xml.Name `xml:"manifest"` |
| 277 | Uses_sdk struct { |
| 278 | MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"` |
| 279 | } `xml:"uses-sdk"` |
| 280 | }{} |
| 281 | |
| 282 | err = decoder.Decode(&manifestData) |
| 283 | if err != nil { |
| 284 | return err |
| 285 | } |
| 286 | |
| 287 | p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion |
| 288 | if p.MinSdkVersion == "" { |
| 289 | p.MinSdkVersion = "current" |
| 290 | } |
| 291 | |
| 292 | return nil |
| 293 | } |
| 294 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 295 | var bpTemplate = template.Must(template.New("bp").Parse(` |
| 296 | {{if .IsAar}}android_library_import{{else}}java_import{{end}} { |
| 297 | name: "{{.BpName}}-nodeps", |
| 298 | {{if .IsAar}}aars{{else}}jars{{end}}: ["{{.ArtifactFile}}"], |
| 299 | sdk_version: "{{.SdkVersion}}",{{if .IsAar}} |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 300 | min_sdk_version: "{{.MinSdkVersion}}", |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 301 | static_libs: [{{range .BpAarDeps}} |
| 302 | "{{.}}",{{end}}{{range .BpExtraDeps}} |
| 303 | "{{.}}",{{end}} |
| 304 | ],{{end}} |
| 305 | } |
| 306 | |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame^] | 307 | {{if .IsAar}}android_library{{else}}{{if .IsDeviceModule}}java_library_static{{else}}java_library_host{{end}}{{end}} { |
| 308 | name: "{{.BpName}}",{{if .IsDeviceModule}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 309 | sdk_version: "{{.SdkVersion}}",{{if .IsAar}} |
Colin Cross | 461ba49 | 2018-07-10 13:45:30 -0700 | [diff] [blame] | 310 | min_sdk_version: "{{.MinSdkVersion}}", |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame^] | 311 | manifest: "manifests/{{.BpName}}/AndroidManifest.xml",{{end}}{{end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 312 | static_libs: [ |
| 313 | "{{.BpName}}-nodeps",{{range .BpJarDeps}} |
| 314 | "{{.}}",{{end}}{{range .BpAarDeps}} |
| 315 | "{{.}}",{{end}}{{range .BpExtraDeps}} |
| 316 | "{{.}}",{{end}} |
| 317 | ], |
| 318 | java_version: "1.7", |
| 319 | } |
| 320 | `)) |
| 321 | |
| 322 | func parse(filename string) (*Pom, error) { |
| 323 | data, err := ioutil.ReadFile(filename) |
| 324 | if err != nil { |
| 325 | return nil, err |
| 326 | } |
| 327 | |
| 328 | var pom Pom |
| 329 | err = xml.Unmarshal(data, &pom) |
| 330 | if err != nil { |
| 331 | return nil, err |
| 332 | } |
| 333 | |
| 334 | if useVersion != "" && pom.Version != useVersion { |
| 335 | return nil, nil |
| 336 | } |
| 337 | |
| 338 | if pom.Packaging == "" { |
| 339 | pom.Packaging = "jar" |
| 340 | } |
| 341 | |
| 342 | pom.PomFile = filename |
| 343 | pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging |
| 344 | |
| 345 | return &pom, nil |
| 346 | } |
| 347 | |
| 348 | func rerunForRegen(filename string) error { |
| 349 | buf, err := ioutil.ReadFile(filename) |
| 350 | if err != nil { |
| 351 | return err |
| 352 | } |
| 353 | |
| 354 | scanner := bufio.NewScanner(bytes.NewBuffer(buf)) |
| 355 | |
| 356 | // Skip the first line in the file |
| 357 | for i := 0; i < 2; i++ { |
| 358 | if !scanner.Scan() { |
| 359 | if scanner.Err() != nil { |
| 360 | return scanner.Err() |
| 361 | } else { |
| 362 | return fmt.Errorf("unexpected EOF") |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Extract the old args from the file |
| 368 | line := scanner.Text() |
| 369 | if strings.HasPrefix(line, "// pom2bp ") { |
| 370 | line = strings.TrimPrefix(line, "// pom2bp ") |
| 371 | } else if strings.HasPrefix(line, "// pom2mk ") { |
| 372 | line = strings.TrimPrefix(line, "// pom2mk ") |
| 373 | } else if strings.HasPrefix(line, "# pom2mk ") { |
| 374 | line = strings.TrimPrefix(line, "# pom2mk ") |
| 375 | } else { |
| 376 | return fmt.Errorf("unexpected second line: %q", line) |
| 377 | } |
| 378 | args := strings.Split(line, " ") |
| 379 | lastArg := args[len(args)-1] |
| 380 | args = args[:len(args)-1] |
| 381 | |
| 382 | // Append all current command line args except -regen <file> to the ones from the file |
| 383 | for i := 1; i < len(os.Args); i++ { |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 384 | if os.Args[i] == "-regen" || os.Args[i] == "--regen" { |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 385 | i++ |
| 386 | } else { |
| 387 | args = append(args, os.Args[i]) |
| 388 | } |
| 389 | } |
| 390 | args = append(args, lastArg) |
| 391 | |
| 392 | cmd := os.Args[0] + " " + strings.Join(args, " ") |
| 393 | // Re-exec pom2bp with the new arguments |
| 394 | output, err := exec.Command("/bin/sh", "-c", cmd).Output() |
| 395 | if exitErr, _ := err.(*exec.ExitError); exitErr != nil { |
| 396 | return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr)) |
| 397 | } else if err != nil { |
| 398 | return err |
| 399 | } |
| 400 | |
| 401 | // If the old file was a .mk file, replace it with a .bp file |
| 402 | if filepath.Ext(filename) == ".mk" { |
| 403 | os.Remove(filename) |
| 404 | filename = strings.TrimSuffix(filename, ".mk") + ".bp" |
| 405 | } |
| 406 | |
| 407 | return ioutil.WriteFile(filename, output, 0666) |
| 408 | } |
| 409 | |
| 410 | func main() { |
| 411 | flag.Usage = func() { |
| 412 | fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos |
| 413 | |
| 414 | The tool will extract the necessary information from *.pom files to create an Android.bp whose |
| 415 | aar libraries can be linked against when using AAPT2. |
| 416 | |
| 417 | Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>] |
| 418 | |
| 419 | -rewrite <regex>=<replace> |
| 420 | rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite |
| 421 | option can be specified multiple times. When determining the Android.bp module for a given Maven |
| 422 | project, mappings are searched in the order they were specified. The first <regex> matching |
| 423 | either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate |
| 424 | the Android.bp module name using <replace>. If no matches are found, <artifactId> is used. |
| 425 | -exclude <module> |
| 426 | Don't put the specified module in the Android.bp file. |
| 427 | -extra-deps <module>=<module>[,<module>] |
| 428 | Some Android.bp modules have transitive dependencies that must be specified when they are |
| 429 | depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat). |
| 430 | This may be specified multiple times to declare these dependencies. |
| 431 | -sdk-version <version> |
| 432 | Sets LOCAL_SDK_VERSION := <version> for all modules. |
| 433 | -use-version <version> |
| 434 | If the maven directory contains multiple versions of artifacts and their pom files, |
| 435 | -use-version can be used to only write Android.bp files for a specific version of those artifacts. |
| 436 | <dir> |
| 437 | The directory to search for *.pom files under. |
| 438 | The contents are written to stdout, to be put in the current directory (often as Android.bp) |
| 439 | -regen <file> |
| 440 | Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it |
| 441 | ends with .mk). |
| 442 | |
| 443 | `, os.Args[0]) |
| 444 | } |
| 445 | |
| 446 | var regen string |
| 447 | |
| 448 | flag.Var(&excludes, "exclude", "Exclude module") |
| 449 | flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module") |
| 450 | flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names") |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame^] | 451 | flag.Var(&hostModuleNames, "host", "Specifies that the corresponding module (specified in the form 'module.group:module.artifact') is a host module") |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 452 | flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION") |
| 453 | flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version") |
| 454 | flag.Bool("static-deps", false, "Ignored") |
| 455 | flag.StringVar(®en, "regen", "", "Rewrite specified file") |
| 456 | flag.Parse() |
| 457 | |
| 458 | if regen != "" { |
| 459 | err := rerunForRegen(regen) |
| 460 | if err != nil { |
| 461 | fmt.Fprintln(os.Stderr, err) |
| 462 | os.Exit(1) |
| 463 | } |
| 464 | os.Exit(0) |
| 465 | } |
| 466 | |
| 467 | if flag.NArg() == 0 { |
| 468 | fmt.Fprintln(os.Stderr, "Directory argument is required") |
| 469 | os.Exit(1) |
| 470 | } else if flag.NArg() > 1 { |
| 471 | fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " ")) |
| 472 | os.Exit(1) |
| 473 | } |
| 474 | |
| 475 | dir := flag.Arg(0) |
| 476 | absDir, err := filepath.Abs(dir) |
| 477 | if err != nil { |
| 478 | fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err) |
| 479 | os.Exit(1) |
| 480 | } |
| 481 | |
| 482 | var filenames []string |
| 483 | err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error { |
| 484 | if err != nil { |
| 485 | return err |
| 486 | } |
| 487 | |
| 488 | name := info.Name() |
| 489 | if info.IsDir() { |
| 490 | if strings.HasPrefix(name, ".") { |
| 491 | return filepath.SkipDir |
| 492 | } |
| 493 | return nil |
| 494 | } |
| 495 | |
| 496 | if strings.HasPrefix(name, ".") { |
| 497 | return nil |
| 498 | } |
| 499 | |
| 500 | if strings.HasSuffix(name, ".pom") { |
| 501 | path, err = filepath.Rel(absDir, path) |
| 502 | if err != nil { |
| 503 | return err |
| 504 | } |
| 505 | filenames = append(filenames, filepath.Join(dir, path)) |
| 506 | } |
| 507 | return nil |
| 508 | }) |
| 509 | if err != nil { |
| 510 | fmt.Fprintln(os.Stderr, "Error walking files:", err) |
| 511 | os.Exit(1) |
| 512 | } |
| 513 | |
| 514 | if len(filenames) == 0 { |
| 515 | fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir) |
| 516 | os.Exit(1) |
| 517 | } |
| 518 | |
| 519 | sort.Strings(filenames) |
| 520 | |
| 521 | poms := []*Pom{} |
| 522 | modules := make(map[string]*Pom) |
| 523 | duplicate := false |
| 524 | for _, filename := range filenames { |
| 525 | pom, err := parse(filename) |
| 526 | if err != nil { |
| 527 | fmt.Fprintln(os.Stderr, "Error converting", filename, err) |
| 528 | os.Exit(1) |
| 529 | } |
| 530 | |
| 531 | if pom != nil { |
| 532 | key := pom.BpName() |
| 533 | if excludes[key] { |
| 534 | continue |
| 535 | } |
| 536 | |
| 537 | if old, ok := modules[key]; ok { |
| 538 | fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile) |
| 539 | duplicate = true |
| 540 | } |
| 541 | |
| 542 | poms = append(poms, pom) |
| 543 | modules[key] = pom |
| 544 | } |
| 545 | } |
| 546 | if duplicate { |
| 547 | os.Exit(1) |
| 548 | } |
| 549 | |
| 550 | for _, pom := range poms { |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 551 | if pom.IsAar() { |
| 552 | err := pom.ExtractMinSdkVersion() |
| 553 | if err != nil { |
Colin Cross | fe5a3b7 | 2018-07-13 21:25:15 -0700 | [diff] [blame] | 554 | fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err) |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 555 | os.Exit(1) |
| 556 | } |
| 557 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 558 | pom.FixDeps(modules) |
| 559 | } |
| 560 | |
| 561 | buf := &bytes.Buffer{} |
| 562 | |
| 563 | fmt.Fprintln(buf, "// Automatically generated with:") |
| 564 | fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscape(os.Args[1:]), " ")) |
| 565 | |
| 566 | for _, pom := range poms { |
| 567 | var err error |
| 568 | err = bpTemplate.Execute(buf, pom) |
| 569 | if err != nil { |
| 570 | fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err) |
| 571 | os.Exit(1) |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | out, err := bpfix.Reformat(buf.String()) |
| 576 | if err != nil { |
| 577 | fmt.Fprintln(os.Stderr, "Error formatting output", err) |
| 578 | os.Exit(1) |
| 579 | } |
| 580 | |
| 581 | os.Stdout.WriteString(out) |
| 582 | } |