blob: dbb61a42279596084e861431c619496717143dbd [file] [log] [blame]
Joey Armstrong5f51f2e2023-01-17 17:06:26 -05001#!/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
17set -euo pipefail
18
19cd $(dirname $0)
20TOOLS_DIR=$(pwd)/.tools
21
22if [ -z "${GOPATH}" ] ; then
23 printf "GOPATH is not defined.\n"
24 exit -1
25fi
26
27if [ ! -d "${GOPATH}" ] ; then
28 printf "GOPATH ${GOPATH} is invalid \n"
29 exit -1
30fi
31
32# Pre-requisites
33if ! git diff --quiet; then \
34 git status
35 printf "\n\nError: working tree is not clean\n"
36 exit -1
37fi
38
39if [ "$(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"
42fi
43
44make ${TOOLS_DIR}/gojq
45
46DIR_TMP="${GOPATH}/src/oteltmp/"
47rm -rf $DIR_TMP
48mkdir -p $DIR_TMP
49
50printf "Copy examples to ${DIR_TMP}\n"
51cp -a ./example ${DIR_TMP}
52
53# Update go.mod files
54printf "Update go.mod: rename module and remove replace\n"
55
56PACKAGE_DIRS=$(find . -mindepth 2 -type f -name 'go.mod' -exec dirname {} \; | egrep 'example' | sed 's/^\.\///' | sort)
57
58for 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)
70done
71printf "Update done:\n\n"
72
73# Build directories that contain main package. These directories are different than
74# directories that contain go.mod files.
75printf "Build examples:\n"
76EXAMPLES=$(./get_main_pkgs.sh ./example)
77for ex in $EXAMPLES; do
78 printf " Build $ex in ${DIR_TMP}/${ex}\n"
79 (cd "${DIR_TMP}/${ex}" && \
80 go build .)
81done
82
83# Cleanup
84printf "Remove copied files.\n"
85rm -rf $DIR_TMP