-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
145 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["configs.go"], | ||
importpath = "github.com/bazelbuild/bazelisk/core/configs", | ||
visibility = ["//visibility:public"], | ||
deps = ["//core/path:go_default_library"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Package core.configs contains the core.configs Bazelisk logic, as well as abstractions for Bazel repositories. | ||
package configs | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/bazelbuild/bazelisk/core/path" | ||
) | ||
|
||
var ( | ||
fileConfig map[string]string | ||
fileConfigOnce sync.Once | ||
) | ||
|
||
// GetEnvOrConfig reads a configuration value from the environment, but fall back to reading it from .bazeliskrc in the workspace root. | ||
func GetEnvOrConfig(name string) string { | ||
if val := os.Getenv(name); val != "" { | ||
return val | ||
} | ||
|
||
// Parse .bazeliskrc in the workspace root, once, if it can be found. | ||
fileConfigOnce.Do(func() { | ||
workingDirectory, err := os.Getwd() | ||
if err != nil { | ||
return | ||
} | ||
workspaceRoot := path.FindWorkspaceRoot(workingDirectory) | ||
if workspaceRoot == "" { | ||
return | ||
} | ||
rcFilePath := filepath.Join(workspaceRoot, ".bazeliskrc") | ||
contents, err := ioutil.ReadFile(rcFilePath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return | ||
} | ||
log.Fatal(err) | ||
} | ||
fileConfig = make(map[string]string) | ||
for _, line := range strings.Split(string(contents), "\n") { | ||
if strings.HasPrefix(line, "#") { | ||
// comments | ||
continue | ||
} | ||
parts := strings.SplitN(line, "=", 2) | ||
if len(parts) < 2 { | ||
continue | ||
} | ||
key := strings.TrimSpace(parts[0]) | ||
fileConfig[key] = strings.TrimSpace(parts[1]) | ||
} | ||
}) | ||
|
||
return fileConfig[name] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["path.go"], | ||
importpath = "github.com/bazelbuild/bazelisk/core/path", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Package core.configs contains the core.configs Bazelisk logic, as well as abstractions for Bazel repositories. | ||
package path | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// isValidWorkspace returns true iff the supplied path is the workspace root, defined by the presence of | ||
// a file named WORKSPACE or WORKSPACE.bazel | ||
// see https://github.com/bazelbuild/bazel/blob/8346ea4cfdd9fbd170d51a528fee26f912dad2d5/src/main/cpp/workspace_layout.cc#L37 | ||
func isValidWorkspace(path string) bool { | ||
info, err := os.Stat(path) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return !info.IsDir() | ||
} | ||
|
||
// FindWorkspaceRoot find the WORKSPACE root path. | ||
func FindWorkspaceRoot(root string) string { | ||
if isValidWorkspace(filepath.Join(root, "WORKSPACE")) { | ||
return root | ||
} | ||
|
||
if isValidWorkspace(filepath.Join(root, "WORKSPACE.bazel")) { | ||
return root | ||
} | ||
|
||
parentDirectory := filepath.Dir(root) | ||
if parentDirectory == root { | ||
return "" | ||
} | ||
|
||
return FindWorkspaceRoot(parentDirectory) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters