build.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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 util
  14. import (
  15. gobuild "go/build"
  16. "os"
  17. "path/filepath"
  18. "reflect"
  19. "strings"
  20. "golang.org/x/tools/go/packages"
  21. )
  22. type empty struct{}
  23. // CurrentPackage returns the go package of the current directory, or "" if it cannot
  24. // be derived from the GOPATH.
  25. func CurrentPackage() string {
  26. for _, root := range gobuild.Default.SrcDirs() {
  27. if pkg, ok := hasSubdir(root, "."); ok {
  28. return pkg
  29. }
  30. }
  31. return ""
  32. }
  33. func hasSubdir(root, dir string) (rel string, ok bool) {
  34. // ensure a tailing separator to properly compare on word-boundaries
  35. const sep = string(filepath.Separator)
  36. root = filepath.Clean(root)
  37. if !strings.HasSuffix(root, sep) {
  38. root += sep
  39. }
  40. // check whether root dir starts with root
  41. dir = filepath.Clean(dir)
  42. if !strings.HasPrefix(dir, root) {
  43. return "", false
  44. }
  45. // cut off root
  46. return filepath.ToSlash(dir[len(root):]), true
  47. }
  48. // BoilerplatePath returns the path to the boilerplate file in code-generator,
  49. // or "" if the default boilerplate.go.txt file cannot be located.
  50. func BoilerplatePath() string {
  51. // set up paths to check
  52. paths := []string{
  53. // works when run from root of $GOPATH containing k8s.io/code-generator
  54. filepath.Join(reflect.TypeOf(empty{}).PkgPath(), "/../../hack/boilerplate.go.txt"),
  55. // works when run from root of module vendoring k8s.io/code-generator
  56. "vendor/k8s.io/code-generator/hack/boilerplate.go.txt",
  57. // works when run from root of $GOPATH containing k8s.io/kubernetes
  58. "k8s.io/kubernetes/vendor/k8s.io/code-generator/hack/boilerplate.go.txt",
  59. }
  60. // see if we can locate the module directory and add that to the list
  61. config := packages.Config{Mode: packages.NeedModule}
  62. if loadedPackages, err := packages.Load(&config, "k8s.io/code-generator/pkg/util"); err == nil {
  63. for _, loadedPackage := range loadedPackages {
  64. if loadedPackage.Module != nil && loadedPackage.Module.Dir != "" {
  65. paths = append(paths, filepath.Join(loadedPackage.Module.Dir, "hack/boilerplate.go.txt"))
  66. }
  67. }
  68. }
  69. // try all paths and return the first that exists
  70. for _, path := range paths {
  71. if _, err := os.Stat(path); err == nil {
  72. return path
  73. }
  74. }
  75. // cannot be located, invoker will have to explicitly specify boilerplate file
  76. return ""
  77. }
  78. // Vendorless trims vendor prefix from a package path to make it canonical
  79. func Vendorless(p string) string {
  80. if pos := strings.LastIndex(p, "/vendor/"); pos != -1 {
  81. return p[pos+len("/vendor/"):]
  82. }
  83. return p
  84. }