verify_examples.sh (2381B)
1 #!/bin/bash 2 3 # Copyright The OpenTelemetry Authors 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 set -euo pipefail 18 19 cd $(dirname $0) 20 TOOLS_DIR=$(pwd)/.tools 21 22 if [ -z "${GOPATH}" ] ; then 23 printf "GOPATH is not defined.\n" 24 exit -1 25 fi 26 27 if [ ! -d "${GOPATH}" ] ; then 28 printf "GOPATH ${GOPATH} is invalid \n" 29 exit -1 30 fi 31 32 # Pre-requisites 33 if ! git diff --quiet; then \ 34 git status 35 printf "\n\nError: working tree is not clean\n" 36 exit -1 37 fi 38 39 if [ "$(git tag --contains $(git log -1 --pretty=format:"%H"))" = "" ] ; then 40 printf "$(git log -1)" 41 printf "\n\nError: HEAD is not pointing to a tagged version" 42 fi 43 44 make ${TOOLS_DIR}/gojq 45 46 DIR_TMP="${GOPATH}/src/oteltmp/" 47 rm -rf $DIR_TMP 48 mkdir -p $DIR_TMP 49 50 printf "Copy examples to ${DIR_TMP}\n" 51 cp -a ./example ${DIR_TMP} 52 53 # Update go.mod files 54 printf "Update go.mod: rename module and remove replace\n" 55 56 PACKAGE_DIRS=$(find . -mindepth 2 -type f -name 'go.mod' -exec dirname {} \; | egrep 'example' | sed 's/^\.\///' | sort) 57 58 for dir in $PACKAGE_DIRS; do 59 printf " Update go.mod for $dir\n" 60 (cd "${DIR_TMP}/${dir}" && \ 61 # replaces is ("mod1" "mod2" …) 62 replaces=($(go mod edit -json | ${TOOLS_DIR}/gojq '.Replace[].Old.Path')) && \ 63 # strip double quotes 64 replaces=("${replaces[@]%\"}") && \ 65 replaces=("${replaces[@]#\"}") && \ 66 # make an array (-dropreplace=mod1 -dropreplace=mod2 …) 67 dropreplaces=("${replaces[@]/#/-dropreplace=}") && \ 68 go mod edit -module "oteltmp/${dir}" "${dropreplaces[@]}" && \ 69 go mod tidy) 70 done 71 printf "Update done:\n\n" 72 73 # Build directories that contain main package. These directories are different than 74 # directories that contain go.mod files. 75 printf "Build examples:\n" 76 EXAMPLES=$(./get_main_pkgs.sh ./example) 77 for ex in $EXAMPLES; do 78 printf " Build $ex in ${DIR_TMP}/${ex}\n" 79 (cd "${DIR_TMP}/${ex}" && \ 80 go build .) 81 done 82 83 # Cleanup 84 printf "Remove copied files.\n" 85 rm -rf $DIR_TMP