workspace.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright 2016 Google Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package wspace provides a method to find the root of the bazel tree.
  14. package wspace
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "github.com/bazelbuild/buildtools/build"
  21. )
  22. const workspaceFile = "WORKSPACE"
  23. func alwaysTrue(fi os.FileInfo) bool {
  24. return true
  25. }
  26. var repoRootFiles = map[string]func(os.FileInfo) bool{
  27. workspaceFile: alwaysTrue,
  28. ".buckconfig": alwaysTrue,
  29. "pants": func(fi os.FileInfo) bool {
  30. return fi.Mode()&os.ModeType == 0 && fi.Mode()&0100 == 0100
  31. },
  32. }
  33. // findContextPath finds the context path inside of a WORKSPACE-rooted source tree.
  34. func findContextPath(rootDir string) (string, error) {
  35. if rootDir == "" {
  36. return os.Getwd()
  37. }
  38. return rootDir, nil
  39. }
  40. // FindWorkspaceRoot splits the current code context (the rootDir if present,
  41. // the working directory if not.) It returns the path of the directory
  42. // containing the WORKSPACE file, and the rest.
  43. func FindWorkspaceRoot(rootDir string) (root string, rest string) {
  44. wd, err := findContextPath(rootDir)
  45. if err != nil {
  46. return "", ""
  47. }
  48. if root, err = Find(wd); err != nil {
  49. return "", ""
  50. }
  51. if len(wd) == len(root) {
  52. return root, ""
  53. }
  54. return root, wd[len(root)+1:]
  55. }
  56. // Find searches from the given dir and up for the WORKSPACE file
  57. // returning the directory containing it, or an error if none found in the tree.
  58. func Find(dir string) (string, error) {
  59. if dir == "" || dir == "/" || dir == "." {
  60. return "", os.ErrNotExist
  61. }
  62. for repoRootFile, fiFunc := range repoRootFiles {
  63. if fi, err := os.Stat(filepath.Join(dir, repoRootFile)); err == nil && fiFunc(fi) {
  64. return dir, nil
  65. } else if !os.IsNotExist(err) {
  66. return "", err
  67. }
  68. }
  69. return Find(filepath.Dir(dir))
  70. }
  71. // FindRepoBuildFiles parses the WORKSPACE to find BUILD files for non-Bazel
  72. // external repositories, specifically those defined by one of these rules:
  73. // new_local_repository(), new_git_repository(), new_http_archive()
  74. func FindRepoBuildFiles(root string) (map[string]string, error) {
  75. ws := filepath.Join(root, workspaceFile)
  76. kinds := []string{
  77. "new_local_repository",
  78. "new_git_repository",
  79. "new_http_archive",
  80. }
  81. data, err := ioutil.ReadFile(ws)
  82. if err != nil {
  83. return nil, err
  84. }
  85. ast, err := build.Parse(ws, data)
  86. if err != nil {
  87. return nil, err
  88. }
  89. files := make(map[string]string)
  90. for _, kind := range kinds {
  91. for _, r := range ast.Rules(kind) {
  92. buildFile := r.AttrString("build_file")
  93. if buildFile == "" {
  94. continue
  95. }
  96. buildFile = strings.Replace(buildFile, ":", "/", -1)
  97. files[r.Name()] = filepath.Join(root, buildFile)
  98. }
  99. }
  100. return files, nil
  101. }