aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/pom2mk
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2018-04-19 16:52:21 -0700
committerJeff Gaston <jeffrygaston@google.com>2018-04-26 13:24:32 -0400
commitf84c8636d38582896e577c530a491c15e7fe1626 (patch)
treeb097a8b77a1681b4af30fb1136dba3b926617b6c /cmd/pom2mk
parente6c053388b5d7f84da09557bf6529b839eda0c77 (diff)
downloadbuild_soong-f84c8636d38582896e577c530a491c15e7fe1626.tar.gz
build_soong-f84c8636d38582896e577c530a491c15e7fe1626.tar.bz2
build_soong-f84c8636d38582896e577c530a491c15e7fe1626.zip
Add pom2mk -regen
pom2mk -regen Android.mk will read the previous arguments out of Android.mk, rerun pom2mk with those arguments, and write the result to Android.mk. Bug: 78300023 Test: cd prebuilts/sdk/current/support && pom2mk -regen Android.mk Change-Id: Ie8e0f97ae5b87c8078cf8e753489326d80da1896 Merged-In: Ie8e0f97ae5b87c8078cf8e753489326d80da1896 (manually cherry-picked from 6b359adc1e2bf1726567000d97e412fea6e88b95)
Diffstat (limited to 'cmd/pom2mk')
-rw-r--r--cmd/pom2mk/pom2mk.go81
1 files changed, 76 insertions, 5 deletions
diff --git a/cmd/pom2mk/pom2mk.go b/cmd/pom2mk/pom2mk.go
index a609860a..d6a50210 100644
--- a/cmd/pom2mk/pom2mk.go
+++ b/cmd/pom2mk/pom2mk.go
@@ -15,11 +15,14 @@
package main
import (
+ "bufio"
+ "bytes"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"os"
+ "os/exec"
"path/filepath"
"regexp"
"sort"
@@ -271,6 +274,58 @@ func parse(filename string) (*Pom, error) {
return &pom, nil
}
+func rerunForRegen(filename string) error {
+ buf, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+
+ scanner := bufio.NewScanner(bytes.NewBuffer(buf))
+
+ // Skip the first line in the file
+ for i := 0; i < 2; i++ {
+ if !scanner.Scan() {
+ if scanner.Err() != nil {
+ return scanner.Err()
+ } else {
+ return fmt.Errorf("unexpected EOF")
+ }
+ }
+ }
+
+ // Extract the old args from the file
+ line := scanner.Text()
+ if strings.HasPrefix(line, "# pom2mk ") {
+ line = strings.TrimPrefix(line, "# pom2mk ")
+ } else {
+ return fmt.Errorf("unexpected second line: %q", line)
+ }
+ args := strings.Split(line, " ")
+ lastArg := args[len(args)-1]
+ args = args[:len(args)-1]
+
+ // Append all current command line args except -regen <file> to the ones from the file
+ for i := 1; i < len(os.Args); i++ {
+ if os.Args[i] == "-regen" {
+ i++
+ } else {
+ args = append(args, os.Args[i])
+ }
+ }
+ args = append(args, lastArg)
+
+ cmd := os.Args[0] + " " + strings.Join(args, " ")
+ // Re-exec pom2mk with the new arguments
+ output, err := exec.Command("/bin/sh", "-c", cmd).Output()
+ if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
+ return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
+ } else if err != nil {
+ return err
+ }
+
+ return ioutil.WriteFile(filename, output, 0666)
+}
+
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos
@@ -278,7 +333,7 @@ func main() {
The tool will extract the necessary information from *.pom files to create an Android.mk whose
aar libraries can be linked against when using AAPT2.
-Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module>]] <dir>
+Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>]
-rewrite <regex>=<replace>
rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite
@@ -299,20 +354,36 @@ Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module
Whether to statically include direct dependencies.
<dir>
The directory to search for *.pom files under.
-
-The makefile is written to stdout, to be put in the current directory (often as Android.mk)
+ The makefile is written to stdout, to be put in the current directory (often as Android.mk)
+ -regen <file>
+ Read arguments from <file> and overwrite it.
`, os.Args[0])
}
+ var regen string
+
flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
+ flag.StringVar(&regen, "regen", "", "Rewrite specified file")
flag.Parse()
- if flag.NArg() != 1 {
- flag.Usage()
+ if regen != "" {
+ err := rerunForRegen(regen)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ os.Exit(0)
+ }
+
+ if flag.NArg() == 0 {
+ fmt.Fprintln(os.Stderr, "Directory argument is required")
+ os.Exit(1)
+ } else if flag.NArg() > 1 {
+ fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
os.Exit(1)
}