You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.2 KiB

  1. package buildutil
  2. import (
  3. "fmt"
  4. "go/build"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "sort"
  11. "strings"
  12. "time"
  13. )
  14. // FakeContext returns a build.Context for the fake file tree specified
  15. // by pkgs, which maps package import paths to a mapping from file base
  16. // names to contents.
  17. //
  18. // The fake Context has a GOROOT of "/go" and no GOPATH, and overrides
  19. // the necessary file access methods to read from memory instead of the
  20. // real file system.
  21. //
  22. // Unlike a real file tree, the fake one has only two levels---packages
  23. // and files---so ReadDir("/go/src/") returns all packages under
  24. // /go/src/ including, for instance, "math" and "math/big".
  25. // ReadDir("/go/src/math/big") would return all the files in the
  26. // "math/big" package.
  27. //
  28. func FakeContext(pkgs map[string]map[string]string) *build.Context {
  29. clean := func(filename string) string {
  30. f := path.Clean(filepath.ToSlash(filename))
  31. // Removing "/go/src" while respecting segment
  32. // boundaries has this unfortunate corner case:
  33. if f == "/go/src" {
  34. return ""
  35. }
  36. return strings.TrimPrefix(f, "/go/src/")
  37. }
  38. ctxt := build.Default // copy
  39. ctxt.GOROOT = "/go"
  40. ctxt.GOPATH = ""
  41. ctxt.IsDir = func(dir string) bool {
  42. dir = clean(dir)
  43. if dir == "" {
  44. return true // needed by (*build.Context).SrcDirs
  45. }
  46. return pkgs[dir] != nil
  47. }
  48. ctxt.ReadDir = func(dir string) ([]os.FileInfo, error) {
  49. dir = clean(dir)
  50. var fis []os.FileInfo
  51. if dir == "" {
  52. // enumerate packages
  53. for importPath := range pkgs {
  54. fis = append(fis, fakeDirInfo(importPath))
  55. }
  56. } else {
  57. // enumerate files of package
  58. for basename := range pkgs[dir] {
  59. fis = append(fis, fakeFileInfo(basename))
  60. }
  61. }
  62. sort.Sort(byName(fis))
  63. return fis, nil
  64. }
  65. ctxt.OpenFile = func(filename string) (io.ReadCloser, error) {
  66. filename = clean(filename)
  67. dir, base := path.Split(filename)
  68. content, ok := pkgs[path.Clean(dir)][base]
  69. if !ok {
  70. return nil, fmt.Errorf("file not found: %s", filename)
  71. }
  72. return ioutil.NopCloser(strings.NewReader(content)), nil
  73. }
  74. ctxt.IsAbsPath = func(path string) bool {
  75. path = filepath.ToSlash(path)
  76. // Don't rely on the default (filepath.Path) since on
  77. // Windows, it reports virtual paths as non-absolute.
  78. return strings.HasPrefix(path, "/")
  79. }
  80. return &ctxt
  81. }
  82. type byName []os.FileInfo
  83. func (s byName) Len() int { return len(s) }
  84. func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  85. func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
  86. type fakeFileInfo string
  87. func (fi fakeFileInfo) Name() string { return string(fi) }
  88. func (fakeFileInfo) Sys() interface{} { return nil }
  89. func (fakeFileInfo) ModTime() time.Time { return time.Time{} }
  90. func (fakeFileInfo) IsDir() bool { return false }
  91. func (fakeFileInfo) Size() int64 { return 0 }
  92. func (fakeFileInfo) Mode() os.FileMode { return 0644 }
  93. type fakeDirInfo string
  94. func (fd fakeDirInfo) Name() string { return string(fd) }
  95. func (fakeDirInfo) Sys() interface{} { return nil }
  96. func (fakeDirInfo) ModTime() time.Time { return time.Time{} }
  97. func (fakeDirInfo) IsDir() bool { return true }
  98. func (fakeDirInfo) Size() int64 { return 0 }
  99. func (fakeDirInfo) Mode() os.FileMode { return 0755 }