From 1576db8e94bf653133e915247b2591da21bcdcb3 Mon Sep 17 00:00:00 2001 From: rosica Date: Tue, 5 Feb 2019 06:09:12 -0800 Subject: [PATCH] Fix paths resolving for --crosstool and --output_location. This is to make the script usable from bazel run PiperOrigin-RevId: 232472300 --- .../convert_crosstool_to_starlark.go | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/tools/migration/convert_crosstool_to_starlark.go b/tools/migration/convert_crosstool_to_starlark.go index 799ef32..ccbbec3 100644 --- a/tools/migration/convert_crosstool_to_starlark.go +++ b/tools/migration/convert_crosstool_to_starlark.go @@ -17,6 +17,9 @@ import ( "fmt" "io/ioutil" "os" + "os/user" + "path" + "strings" "log" "github.com/golang/protobuf/proto" @@ -32,17 +35,45 @@ var ( "output_location", "", "Location of the output .bzl file") ) +func toAbsolutePath(pathString string) (string, error) { + usr, err := user.Current() + if err != nil { + return "", err + } + homeDir := usr.HomeDir + + if strings.HasPrefix(pathString, "~") { + return path.Join(homeDir, pathString[1:]), nil + } + + if path.IsAbs(pathString) { + return pathString, nil + } + + workingDirectory := os.Getenv("BUILD_WORKING_DIRECTORY") + return path.Join(workingDirectory, pathString), nil +} + func main() { flag.Parse() if *crosstoolLocation == "" { log.Fatalf("Missing mandatory argument 'crosstool'") } + crosstoolPath, err := toAbsolutePath(*crosstoolLocation) + if err != nil { + log.Fatalf("Error while resolving CROSSTOOL location:", err) + } + if *outputLocation == "" { log.Fatalf("Missing mandatory argument 'output_location'") } + outputPath, err := toAbsolutePath(*outputLocation) + if err != nil { + log.Fatalf("Error resolving output location:", err) + } - in, err := ioutil.ReadFile(*crosstoolLocation) + in, err := ioutil.ReadFile(crosstoolPath) if err != nil { log.Fatalf("Error reading CROSSTOOL file:", err) } @@ -51,7 +82,7 @@ func main() { log.Fatalf("Failed to parse CROSSTOOL:", err) } - file, err := os.Create(*outputLocation) + file, err := os.Create(outputPath) if err != nil { log.Fatalf("Error creating output file:", err) }