repo.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright 2017 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package repos
  13. import (
  14. "fmt"
  15. "os"
  16. "path/filepath"
  17. "sort"
  18. "strings"
  19. "github.com/bazelbuild/bazel-gazelle/internal/rule"
  20. )
  21. // Repo describes an external repository rule declared in a Bazel
  22. // WORKSPACE file.
  23. type Repo struct {
  24. // Name is the value of the "name" attribute of the repository rule.
  25. Name string
  26. // GoPrefix is the portion of the Go import path for the root of this
  27. // repository. Usually the same as Remote.
  28. GoPrefix string
  29. // Commit is the revision at which a repository is checked out (for example,
  30. // a Git commit id).
  31. Commit string
  32. // Tag is the name of the version at which a repository is checked out.
  33. Tag string
  34. // Remote is the URL the repository can be cloned or checked out from.
  35. Remote string
  36. // VCS is the version control system used to check out the repository.
  37. // May also be "http" for HTTP archives.
  38. VCS string
  39. }
  40. type byName []Repo
  41. func (s byName) Len() int { return len(s) }
  42. func (s byName) Less(i, j int) bool { return s[i].Name < s[j].Name }
  43. func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  44. type lockFileFormat int
  45. const (
  46. unknownFormat lockFileFormat = iota
  47. depFormat
  48. moduleFormat
  49. )
  50. var lockFileParsers = map[lockFileFormat]func(string) ([]Repo, error){
  51. depFormat: importRepoRulesDep,
  52. moduleFormat: importRepoRulesModules,
  53. }
  54. // ImportRepoRules reads the lock file of a vendoring tool and returns
  55. // a list of equivalent repository rules that can be merged into a WORKSPACE
  56. // file. The format of the file is inferred from its basename. Currently,
  57. // only Gopkg.lock is supported.
  58. func ImportRepoRules(filename string) ([]*rule.Rule, error) {
  59. format := getLockFileFormat(filename)
  60. if format == unknownFormat {
  61. return nil, fmt.Errorf(`%s: unrecognized lock file format. Expected "Gopkg.lock"`, filename)
  62. }
  63. parser := lockFileParsers[format]
  64. repos, err := parser(filename)
  65. if err != nil {
  66. return nil, fmt.Errorf("error parsing %q: %v", filename, err)
  67. }
  68. sort.Stable(byName(repos))
  69. rules := make([]*rule.Rule, 0, len(repos))
  70. for _, repo := range repos {
  71. rules = append(rules, GenerateRule(repo))
  72. }
  73. return rules, nil
  74. }
  75. func getLockFileFormat(filename string) lockFileFormat {
  76. switch filepath.Base(filename) {
  77. case "Gopkg.lock":
  78. return depFormat
  79. case "go.mod":
  80. return moduleFormat
  81. default:
  82. return unknownFormat
  83. }
  84. }
  85. // GenerateRule returns a repository rule for the given repository that can
  86. // be written in a WORKSPACE file.
  87. func GenerateRule(repo Repo) *rule.Rule {
  88. r := rule.NewRule("go_repository", repo.Name)
  89. if repo.Commit != "" {
  90. r.SetAttr("commit", repo.Commit)
  91. }
  92. if repo.Tag != "" {
  93. r.SetAttr("tag", repo.Tag)
  94. }
  95. r.SetAttr("importpath", repo.GoPrefix)
  96. if repo.Remote != "" {
  97. r.SetAttr("remote", repo.Remote)
  98. }
  99. if repo.VCS != "" {
  100. r.SetAttr("vcs", repo.VCS)
  101. }
  102. return r
  103. }
  104. // FindExternalRepo attempts to locate the directory where Bazel has fetched
  105. // the external repository with the given name. An error is returned if the
  106. // repository directory cannot be located.
  107. func FindExternalRepo(repoRoot, name string) (string, error) {
  108. // See https://docs.bazel.build/versions/master/output_directories.html
  109. // for documentation on Bazel directory layout.
  110. // We expect the bazel-out symlink in the workspace root directory to point to
  111. // <output-base>/execroot/<workspace-name>/bazel-out
  112. // We expect the external repository to be checked out at
  113. // <output-base>/external/<name>
  114. // Note that users can change the prefix for most of the Bazel symlinks with
  115. // --symlink_prefix, but this does not include bazel-out.
  116. externalPath := strings.Join([]string{repoRoot, "bazel-out", "..", "..", "..", "external", name}, string(os.PathSeparator))
  117. cleanPath, err := filepath.EvalSymlinks(externalPath)
  118. if err != nil {
  119. return "", err
  120. }
  121. st, err := os.Stat(cleanPath)
  122. if err != nil {
  123. return "", err
  124. }
  125. if !st.IsDir() {
  126. return "", fmt.Errorf("%s: not a directory", externalPath)
  127. }
  128. return cleanPath, nil
  129. }
  130. // ListRepositories extracts metadata about repositories declared in a
  131. // WORKSPACE file.
  132. //
  133. // The set of repositories returned is necessarily incomplete, since we don't
  134. // evaluate the file, and repositories may be declared in macros in other files.
  135. func ListRepositories(workspace *rule.File) []Repo {
  136. var repos []Repo
  137. for _, r := range workspace.Rules {
  138. name := r.Name()
  139. if name == "" {
  140. continue
  141. }
  142. var repo Repo
  143. switch r.Kind() {
  144. case "go_repository":
  145. // TODO(jayconrod): extract other fields needed by go_repository.
  146. // Currently, we don't use the result of this function to produce new
  147. // go_repository rules, so it doesn't matter.
  148. goPrefix := r.AttrString("importpath")
  149. revision := r.AttrString("commit")
  150. remote := r.AttrString("remote")
  151. vcs := r.AttrString("vcs")
  152. if goPrefix == "" {
  153. continue
  154. }
  155. repo = Repo{
  156. Name: name,
  157. GoPrefix: goPrefix,
  158. Commit: revision,
  159. Remote: remote,
  160. VCS: vcs,
  161. }
  162. // TODO(jayconrod): infer from {new_,}git_repository, {new_,}http_archive,
  163. // local_repository.
  164. default:
  165. continue
  166. }
  167. repos = append(repos, repo)
  168. }
  169. // TODO(jayconrod): look for directives that describe repositories that
  170. // aren't declared in the top-level of WORKSPACE (e.g., behind a macro).
  171. return repos
  172. }