dep.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 golang
  13. import (
  14. "io/ioutil"
  15. "sort"
  16. "github.com/bazelbuild/bazel-gazelle/label"
  17. "github.com/bazelbuild/bazel-gazelle/language"
  18. "github.com/bazelbuild/bazel-gazelle/rule"
  19. toml "github.com/pelletier/go-toml"
  20. )
  21. type depLockFile struct {
  22. Projects []depProject `toml:"projects"`
  23. }
  24. type depProject struct {
  25. Name string `toml:"name"`
  26. Revision string `toml:"revision"`
  27. Source string `toml:"source"`
  28. }
  29. func importReposFromDep(args language.ImportReposArgs) language.ImportReposResult {
  30. data, err := ioutil.ReadFile(args.Path)
  31. if err != nil {
  32. return language.ImportReposResult{Error: err}
  33. }
  34. var file depLockFile
  35. if err := toml.Unmarshal(data, &file); err != nil {
  36. return language.ImportReposResult{Error: err}
  37. }
  38. gen := make([]*rule.Rule, len(file.Projects))
  39. for i, p := range file.Projects {
  40. gen[i] = rule.NewRule("go_repository", label.ImportPathToBazelRepoName(p.Name))
  41. gen[i].SetAttr("importpath", p.Name)
  42. gen[i].SetAttr("commit", p.Revision)
  43. if p.Source != "" {
  44. // TODO(#411): Handle source directives correctly. It may be an import
  45. // path, or a URL. In the case of an import path, we should resolve it
  46. // to the correct remote and vcs. In the case of a URL, we should
  47. // correctly determine what VCS to use (the URL will usually start
  48. // with "https://", which is used by multiple VCSs).
  49. gen[i].SetAttr("remote", p.Source)
  50. gen[i].SetAttr("vcs", "git")
  51. }
  52. }
  53. sort.SliceStable(gen, func(i, j int) bool {
  54. return gen[i].Name() < gen[j].Name()
  55. })
  56. return language.ImportReposResult{Gen: gen}
  57. }