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 { |
Colin Cross | 86bc9d4 | 2018-08-29 15:36:33 -0700 | [diff] [blame] | 110 | _, found := n[groupId+":"+artifactId] |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame] | 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 | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 189 | func (p Pom) ModuleType() string { |
| 190 | if p.IsAar() { |
| 191 | return "android_library" |
| 192 | } else if p.IsHostModule() { |
| 193 | return "java_library_host" |
| 194 | } else { |
| 195 | return "java_library_static" |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func (p Pom) ImportModuleType() string { |
| 200 | if p.IsAar() { |
| 201 | return "android_library_import" |
| 202 | } else if p.IsHostModule() { |
| 203 | return "java_import_host" |
| 204 | } else { |
| 205 | return "java_import" |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func (p Pom) ImportProperty() string { |
| 210 | if p.IsAar() { |
| 211 | return "aars" |
| 212 | } else { |
| 213 | return "jars" |
| 214 | } |
| 215 | } |
| 216 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 217 | func (p Pom) BpName() string { |
| 218 | if p.BpTarget == "" { |
| 219 | p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId) |
| 220 | } |
| 221 | return p.BpTarget |
| 222 | } |
| 223 | |
| 224 | func (p Pom) BpJarDeps() []string { |
| 225 | return p.BpDeps("jar", []string{"compile", "runtime"}) |
| 226 | } |
| 227 | |
| 228 | func (p Pom) BpAarDeps() []string { |
| 229 | return p.BpDeps("aar", []string{"compile", "runtime"}) |
| 230 | } |
| 231 | |
| 232 | func (p Pom) BpExtraDeps() []string { |
| 233 | return extraDeps[p.BpName()] |
| 234 | } |
| 235 | |
| 236 | // BpDeps obtains dependencies filtered by type and scope. The results of this |
| 237 | // method are formatted as Android.bp targets, e.g. run through MavenToBp rules. |
| 238 | func (p Pom) BpDeps(typeExt string, scopes []string) []string { |
| 239 | var ret []string |
| 240 | for _, d := range p.Dependencies { |
| 241 | if d.Type != typeExt || !InList(d.Scope, scopes) { |
| 242 | continue |
| 243 | } |
| 244 | name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId) |
| 245 | ret = append(ret, name) |
| 246 | } |
| 247 | return ret |
| 248 | } |
| 249 | |
| 250 | func (p Pom) SdkVersion() string { |
| 251 | return sdkVersion |
| 252 | } |
| 253 | |
| 254 | func (p *Pom) FixDeps(modules map[string]*Pom) { |
| 255 | for _, d := range p.Dependencies { |
| 256 | if d.Type == "" { |
| 257 | if depPom, ok := modules[d.BpName()]; ok { |
| 258 | // We've seen the POM for this dependency, use its packaging |
| 259 | // as the dependency type rather than Maven spec default. |
| 260 | d.Type = depPom.Packaging |
| 261 | } else { |
| 262 | // Dependency type was not specified and we don't have the POM |
| 263 | // for this artifact, use the default from Maven spec. |
| 264 | d.Type = "jar" |
| 265 | } |
| 266 | } |
| 267 | if d.Scope == "" { |
| 268 | // Scope was not specified, use the default from Maven spec. |
| 269 | d.Scope = "compile" |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 274 | // ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it |
| 275 | // to "current" if it is not present. |
| 276 | func (p *Pom) ExtractMinSdkVersion() error { |
| 277 | aar, err := zip.OpenReader(p.ArtifactFile) |
| 278 | if err != nil { |
| 279 | return err |
| 280 | } |
| 281 | defer aar.Close() |
| 282 | |
| 283 | var manifest *zip.File |
| 284 | for _, f := range aar.File { |
| 285 | if f.Name == "AndroidManifest.xml" { |
| 286 | manifest = f |
| 287 | break |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if manifest == nil { |
| 292 | return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile) |
| 293 | } |
| 294 | |
| 295 | r, err := manifest.Open() |
| 296 | if err != nil { |
| 297 | return err |
| 298 | } |
| 299 | defer r.Close() |
| 300 | |
| 301 | decoder := xml.NewDecoder(r) |
| 302 | |
| 303 | manifestData := struct { |
| 304 | XMLName xml.Name `xml:"manifest"` |
| 305 | Uses_sdk struct { |
| 306 | MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"` |
| 307 | } `xml:"uses-sdk"` |
| 308 | }{} |
| 309 | |
| 310 | err = decoder.Decode(&manifestData) |
| 311 | if err != nil { |
| 312 | return err |
| 313 | } |
| 314 | |
| 315 | p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion |
| 316 | if p.MinSdkVersion == "" { |
| 317 | p.MinSdkVersion = "current" |
| 318 | } |
| 319 | |
| 320 | return nil |
| 321 | } |
| 322 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 323 | var bpTemplate = template.Must(template.New("bp").Parse(` |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 324 | {{.ImportModuleType}} { |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 325 | name: "{{.BpName}}-nodeps", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 326 | {{.ImportProperty}}: ["{{.ArtifactFile}}"], |
| 327 | sdk_version: "{{.SdkVersion}}", |
| 328 | {{- if .IsAar}} |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 329 | min_sdk_version: "{{.MinSdkVersion}}", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 330 | static_libs: [ |
| 331 | {{- range .BpAarDeps}} |
| 332 | "{{.}}", |
| 333 | {{- end}} |
| 334 | {{- range .BpExtraDeps}} |
| 335 | "{{.}}", |
| 336 | {{- end}} |
| 337 | ], |
| 338 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 341 | {{.ModuleType}} { |
| 342 | name: "{{.BpName}}", |
| 343 | {{- if .IsDeviceModule}} |
| 344 | sdk_version: "{{.SdkVersion}}", |
| 345 | {{- if .IsAar}} |
Colin Cross | 461ba49 | 2018-07-10 13:45:30 -0700 | [diff] [blame] | 346 | min_sdk_version: "{{.MinSdkVersion}}", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 347 | manifest: "manifests/{{.BpName}}/AndroidManifest.xml", |
| 348 | {{- end}} |
| 349 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 350 | static_libs: [ |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 351 | "{{.BpName}}-nodeps", |
| 352 | {{- range .BpJarDeps}} |
| 353 | "{{.}}", |
| 354 | {{- end}} |
| 355 | {{- range .BpAarDeps}} |
| 356 | "{{.}}", |
| 357 | {{- end}} |
| 358 | {{- range .BpExtraDeps}} |
| 359 | "{{.}}", |
| 360 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 361 | ], |
| 362 | java_version: "1.7", |
| 363 | } |
| 364 | `)) |
| 365 | |
| 366 | func parse(filename string) (*Pom, error) { |
| 367 | data, err := ioutil.ReadFile(filename) |
| 368 | if err != nil { |
| 369 | return nil, err |
| 370 | } |
| 371 | |
| 372 | var pom Pom |
| 373 | err = xml.Unmarshal(data, &pom) |
| 374 | if err != nil { |
| 375 | return nil, err |
| 376 | } |
| 377 | |
| 378 | if useVersion != "" && pom.Version != useVersion { |
| 379 | return nil, nil |
| 380 | } |
| 381 | |
| 382 | if pom.Packaging == "" { |
| 383 | pom.Packaging = "jar" |
| 384 | } |
| 385 | |
| 386 | pom.PomFile = filename |
| 387 | pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging |
| 388 | |
| 389 | return &pom, nil |
| 390 | } |
| 391 | |
| 392 | func rerunForRegen(filename string) error { |
| 393 | buf, err := ioutil.ReadFile(filename) |
| 394 | if err != nil { |
| 395 | return err |
| 396 | } |
| 397 | |
| 398 | scanner := bufio.NewScanner(bytes.NewBuffer(buf)) |
| 399 | |
| 400 | // Skip the first line in the file |
| 401 | for i := 0; i < 2; i++ { |
| 402 | if !scanner.Scan() { |
| 403 | if scanner.Err() != nil { |
| 404 | return scanner.Err() |
| 405 | } else { |
| 406 | return fmt.Errorf("unexpected EOF") |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Extract the old args from the file |
| 412 | line := scanner.Text() |
| 413 | if strings.HasPrefix(line, "// pom2bp ") { |
| 414 | line = strings.TrimPrefix(line, "// pom2bp ") |
| 415 | } else if strings.HasPrefix(line, "// pom2mk ") { |
| 416 | line = strings.TrimPrefix(line, "// pom2mk ") |
| 417 | } else if strings.HasPrefix(line, "# pom2mk ") { |
| 418 | line = strings.TrimPrefix(line, "# pom2mk ") |
| 419 | } else { |
| 420 | return fmt.Errorf("unexpected second line: %q", line) |
| 421 | } |
| 422 | args := strings.Split(line, " ") |
| 423 | lastArg := args[len(args)-1] |
| 424 | args = args[:len(args)-1] |
| 425 | |
| 426 | // Append all current command line args except -regen <file> to the ones from the file |
| 427 | for i := 1; i < len(os.Args); i++ { |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 428 | if os.Args[i] == "-regen" || os.Args[i] == "--regen" { |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 429 | i++ |
| 430 | } else { |
| 431 | args = append(args, os.Args[i]) |
| 432 | } |
| 433 | } |
| 434 | args = append(args, lastArg) |
| 435 | |
| 436 | cmd := os.Args[0] + " " + strings.Join(args, " ") |
| 437 | // Re-exec pom2bp with the new arguments |
| 438 | output, err := exec.Command("/bin/sh", "-c", cmd).Output() |
| 439 | if exitErr, _ := err.(*exec.ExitError); exitErr != nil { |
| 440 | return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr)) |
| 441 | } else if err != nil { |
| 442 | return err |
| 443 | } |
| 444 | |
| 445 | // If the old file was a .mk file, replace it with a .bp file |
| 446 | if filepath.Ext(filename) == ".mk" { |
| 447 | os.Remove(filename) |
| 448 | filename = strings.TrimSuffix(filename, ".mk") + ".bp" |
| 449 | } |
| 450 | |
| 451 | return ioutil.WriteFile(filename, output, 0666) |
| 452 | } |
| 453 | |
| 454 | func main() { |
| 455 | flag.Usage = func() { |
| 456 | fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos |
| 457 | |
| 458 | The tool will extract the necessary information from *.pom files to create an Android.bp whose |
| 459 | aar libraries can be linked against when using AAPT2. |
| 460 | |
| 461 | Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>] |
| 462 | |
| 463 | -rewrite <regex>=<replace> |
| 464 | rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite |
| 465 | option can be specified multiple times. When determining the Android.bp module for a given Maven |
| 466 | project, mappings are searched in the order they were specified. The first <regex> matching |
| 467 | either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate |
| 468 | the Android.bp module name using <replace>. If no matches are found, <artifactId> is used. |
| 469 | -exclude <module> |
| 470 | Don't put the specified module in the Android.bp file. |
| 471 | -extra-deps <module>=<module>[,<module>] |
| 472 | Some Android.bp modules have transitive dependencies that must be specified when they are |
| 473 | depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat). |
| 474 | This may be specified multiple times to declare these dependencies. |
| 475 | -sdk-version <version> |
| 476 | Sets LOCAL_SDK_VERSION := <version> for all modules. |
| 477 | -use-version <version> |
| 478 | If the maven directory contains multiple versions of artifacts and their pom files, |
| 479 | -use-version can be used to only write Android.bp files for a specific version of those artifacts. |
| 480 | <dir> |
| 481 | The directory to search for *.pom files under. |
| 482 | The contents are written to stdout, to be put in the current directory (often as Android.bp) |
| 483 | -regen <file> |
| 484 | Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it |
| 485 | ends with .mk). |
| 486 | |
| 487 | `, os.Args[0]) |
| 488 | } |
| 489 | |
| 490 | var regen string |
| 491 | |
| 492 | flag.Var(&excludes, "exclude", "Exclude module") |
| 493 | flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module") |
| 494 | flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names") |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame] | 495 | 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] | 496 | flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION") |
| 497 | flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version") |
| 498 | flag.Bool("static-deps", false, "Ignored") |
| 499 | flag.StringVar(®en, "regen", "", "Rewrite specified file") |
| 500 | flag.Parse() |
| 501 | |
| 502 | if regen != "" { |
| 503 | err := rerunForRegen(regen) |
| 504 | if err != nil { |
| 505 | fmt.Fprintln(os.Stderr, err) |
| 506 | os.Exit(1) |
| 507 | } |
| 508 | os.Exit(0) |
| 509 | } |
| 510 | |
| 511 | if flag.NArg() == 0 { |
| 512 | fmt.Fprintln(os.Stderr, "Directory argument is required") |
| 513 | os.Exit(1) |
| 514 | } else if flag.NArg() > 1 { |
| 515 | fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " ")) |
| 516 | os.Exit(1) |
| 517 | } |
| 518 | |
| 519 | dir := flag.Arg(0) |
| 520 | absDir, err := filepath.Abs(dir) |
| 521 | if err != nil { |
| 522 | fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err) |
| 523 | os.Exit(1) |
| 524 | } |
| 525 | |
| 526 | var filenames []string |
| 527 | err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error { |
| 528 | if err != nil { |
| 529 | return err |
| 530 | } |
| 531 | |
| 532 | name := info.Name() |
| 533 | if info.IsDir() { |
| 534 | if strings.HasPrefix(name, ".") { |
| 535 | return filepath.SkipDir |
| 536 | } |
| 537 | return nil |
| 538 | } |
| 539 | |
| 540 | if strings.HasPrefix(name, ".") { |
| 541 | return nil |
| 542 | } |
| 543 | |
| 544 | if strings.HasSuffix(name, ".pom") { |
| 545 | path, err = filepath.Rel(absDir, path) |
| 546 | if err != nil { |
| 547 | return err |
| 548 | } |
| 549 | filenames = append(filenames, filepath.Join(dir, path)) |
| 550 | } |
| 551 | return nil |
| 552 | }) |
| 553 | if err != nil { |
| 554 | fmt.Fprintln(os.Stderr, "Error walking files:", err) |
| 555 | os.Exit(1) |
| 556 | } |
| 557 | |
| 558 | if len(filenames) == 0 { |
| 559 | fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir) |
| 560 | os.Exit(1) |
| 561 | } |
| 562 | |
| 563 | sort.Strings(filenames) |
| 564 | |
| 565 | poms := []*Pom{} |
| 566 | modules := make(map[string]*Pom) |
| 567 | duplicate := false |
| 568 | for _, filename := range filenames { |
| 569 | pom, err := parse(filename) |
| 570 | if err != nil { |
| 571 | fmt.Fprintln(os.Stderr, "Error converting", filename, err) |
| 572 | os.Exit(1) |
| 573 | } |
| 574 | |
| 575 | if pom != nil { |
| 576 | key := pom.BpName() |
| 577 | if excludes[key] { |
| 578 | continue |
| 579 | } |
| 580 | |
| 581 | if old, ok := modules[key]; ok { |
| 582 | fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile) |
| 583 | duplicate = true |
| 584 | } |
| 585 | |
| 586 | poms = append(poms, pom) |
| 587 | modules[key] = pom |
| 588 | } |
| 589 | } |
| 590 | if duplicate { |
| 591 | os.Exit(1) |
| 592 | } |
| 593 | |
| 594 | for _, pom := range poms { |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 595 | if pom.IsAar() { |
| 596 | err := pom.ExtractMinSdkVersion() |
| 597 | if err != nil { |
Colin Cross | fe5a3b7 | 2018-07-13 21:25:15 -0700 | [diff] [blame] | 598 | 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] | 599 | os.Exit(1) |
| 600 | } |
| 601 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 602 | pom.FixDeps(modules) |
| 603 | } |
| 604 | |
| 605 | buf := &bytes.Buffer{} |
| 606 | |
| 607 | fmt.Fprintln(buf, "// Automatically generated with:") |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 608 | fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " ")) |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 609 | |
| 610 | for _, pom := range poms { |
| 611 | var err error |
| 612 | err = bpTemplate.Execute(buf, pom) |
| 613 | if err != nil { |
| 614 | fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err) |
| 615 | os.Exit(1) |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | out, err := bpfix.Reformat(buf.String()) |
| 620 | if err != nil { |
| 621 | fmt.Fprintln(os.Stderr, "Error formatting output", err) |
| 622 | os.Exit(1) |
| 623 | } |
| 624 | |
| 625 | os.Stdout.WriteString(out) |
| 626 | } |