cache_go_dirs.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script finds, caches, and prints a list of all directories that hold
  16. # *.go files. If any directory is newer than the cache, re-find everything and
  17. # update the cache. Otherwise use the cached file.
  18. set -o errexit
  19. set -o nounset
  20. set -o pipefail
  21. if [[ -z "${1:-}" ]]; then
  22. echo "usage: $0 <cache-file>"
  23. exit 1
  24. fi
  25. CACHE="$1"; shift
  26. trap 'rm -f "${CACHE}"' HUP INT TERM ERR
  27. # This is a partial 'find' command. The caller is expected to pass the
  28. # remaining arguments.
  29. #
  30. # Example:
  31. # kfind -type f -name foobar.go
  32. function kfind() {
  33. # We want to include the "special" vendor directories which are actually
  34. # part of the Kubernetes source tree (./staging/*) but we need them to be
  35. # named as their ./vendor/* equivalents. Also, we do not want all of
  36. # ./vendor or even all of ./vendor/k8s.io.
  37. find -H . \
  38. \( \
  39. -not \( \
  40. \( \
  41. -path ./vendor -o \
  42. -path ./_\* -o \
  43. -path ./.\* -o \
  44. -path ./docs \
  45. \) -prune \
  46. \) \
  47. \) \
  48. "$@" \
  49. | sed 's|^./staging/src|vendor|'
  50. }
  51. NEED_FIND=true
  52. # It's *significantly* faster to check whether any directories are newer than
  53. # the cache than to blindly rebuild it.
  54. if [[ -f "${CACHE}" ]]; then
  55. N=$(kfind -type d -newer "${CACHE}" -print -quit | wc -l)
  56. [[ "${N}" == 0 ]] && NEED_FIND=false
  57. fi
  58. mkdir -p "$(dirname "${CACHE}")"
  59. if ${NEED_FIND}; then
  60. kfind -type f -name \*.go \
  61. | sed 's|/[^/]*$||' \
  62. | sed 's|^./||' \
  63. | LC_ALL=C sort -u \
  64. > "${CACHE}"
  65. fi
  66. cat "${CACHE}"