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.

verify_examples.sh 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. # Copyright The OpenTelemetry 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. set -euo pipefail
  16. cd $(dirname $0)
  17. TOOLS_DIR=$(pwd)/.tools
  18. if [ -z "${GOPATH}" ] ; then
  19. printf "GOPATH is not defined.\n"
  20. exit -1
  21. fi
  22. if [ ! -d "${GOPATH}" ] ; then
  23. printf "GOPATH ${GOPATH} is invalid \n"
  24. exit -1
  25. fi
  26. # Pre-requisites
  27. if ! git diff --quiet; then \
  28. git status
  29. printf "\n\nError: working tree is not clean\n"
  30. exit -1
  31. fi
  32. if [ "$(git tag --contains $(git log -1 --pretty=format:"%H"))" = "" ] ; then
  33. printf "$(git log -1)"
  34. printf "\n\nError: HEAD is not pointing to a tagged version"
  35. fi
  36. make ${TOOLS_DIR}/gojq
  37. DIR_TMP="${GOPATH}/src/oteltmp/"
  38. rm -rf $DIR_TMP
  39. mkdir -p $DIR_TMP
  40. printf "Copy examples to ${DIR_TMP}\n"
  41. cp -a ./example ${DIR_TMP}
  42. # Update go.mod files
  43. printf "Update go.mod: rename module and remove replace\n"
  44. PACKAGE_DIRS=$(find . -mindepth 2 -type f -name 'go.mod' -exec dirname {} \; | egrep 'example' | sed 's/^\.\///' | sort)
  45. for dir in $PACKAGE_DIRS; do
  46. printf " Update go.mod for $dir\n"
  47. (cd "${DIR_TMP}/${dir}" && \
  48. # replaces is ("mod1" "mod2" …)
  49. replaces=($(go mod edit -json | ${TOOLS_DIR}/gojq '.Replace[].Old.Path')) && \
  50. # strip double quotes
  51. replaces=("${replaces[@]%\"}") && \
  52. replaces=("${replaces[@]#\"}") && \
  53. # make an array (-dropreplace=mod1 -dropreplace=mod2 …)
  54. dropreplaces=("${replaces[@]/#/-dropreplace=}") && \
  55. go mod edit -module "oteltmp/${dir}" "${dropreplaces[@]}" && \
  56. go mod tidy)
  57. done
  58. printf "Update done:\n\n"
  59. # Build directories that contain main package. These directories are different than
  60. # directories that contain go.mod files.
  61. printf "Build examples:\n"
  62. EXAMPLES=$(./get_main_pkgs.sh ./example)
  63. for ex in $EXAMPLES; do
  64. printf " Build $ex in ${DIR_TMP}/${ex}\n"
  65. (cd "${DIR_TMP}/${ex}" && \
  66. go build .)
  67. done
  68. # Cleanup
  69. printf "Remove copied files.\n"
  70. rm -rf $DIR_TMP