Skip to content

Commit cb5de41

Browse files
Scotttekton-robot
authored andcommitted
Update hack scripts to shim temp GOPATH as needed
When we run our codegen and openapigen scripts from a directory that is not under GOPATH the generated output uses relative prefixes instead of absolute prefixes. The new hack/setup-temporary-gopath.sh script detects if the repo is not in GOPATH and creates a temporary one under .gopath if so. codegen and openapigen scripts now depend on setup-temporary-gopath.sh before generating anything.
1 parent 863ae57 commit cb5de41

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ cmd/*/kodata/source.tar.gz
5353
test/pullrequest/pullrequest-init
5454
/.bin/
5555
/bin/
56+
57+
# Temporary GOPATH used during code gen if user's Tekton checkout is
58+
# not already in GOPATH.
59+
.gopath

hack/setup-temporary-gopath.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
# Conditionally create a temporary GOPATH for codegen
7+
# and openapigen to execute in. This is only done if
8+
# the current repo directory is not within GOPATH.
9+
function shim_gopath() {
10+
local REPO_DIR=$(git rev-parse --show-toplevel)
11+
local TEMP_GOPATH="${REPO_DIR}/.gopath"
12+
local TEMP_TEKTONCD="${TEMP_GOPATH}/src/github.com/tektoncd"
13+
local TEMP_PIPELINE="${TEMP_TEKTONCD}/pipeline"
14+
local NEEDS_MOVE=1
15+
16+
# Check if repo is in GOPATH already and return early if so.
17+
# Unfortunately this doesn't respect a repo that's symlinked into
18+
# GOPATH and will create a temporary anyway. I couldn't figure out
19+
# a way to get the absolute path to the symlinked repo root.
20+
if [ ! -z $GOPATH ] ; then
21+
case $REPO_DIR/ in
22+
$GOPATH/*) NEEDS_MOVE=0;;
23+
*) NEEDS_MOVE=1;;
24+
esac
25+
fi
26+
27+
if [ $NEEDS_MOVE -eq 0 ]; then
28+
return
29+
fi
30+
31+
echo "You appear to be running from outside of GOPATH."
32+
echo "This script will create a temporary GOPATH at $TEMP_GOPATH for code generation."
33+
34+
# Ensure that the temporary pipelines symlink doesn't exist
35+
# before proceeding.
36+
delete_pipeline_repo_symlink
37+
38+
mkdir -p "$TEMP_TEKTONCD"
39+
# This will create a symlink from
40+
# (repo-root)/.gopath/src/github.com/tektoncd/pipeline
41+
# to the user's pipeline checkout.
42+
ln -s "$REPO_DIR" "$TEMP_TEKTONCD"
43+
echo "Moving to $TEMP_PIPELINE"
44+
cd "$TEMP_PIPELINE"
45+
export GOPATH="$TEMP_GOPATH"
46+
}
47+
48+
# Helper that wraps deleting the temp pipelines repo symlink
49+
# and prints a message about deleting the temp GOPATH.
50+
#
51+
# Why doesn't this func just delete the temp GOPATH outright?
52+
# Because it might be reused across multiple hack scripts and many
53+
# packages seem to be installed into GOPATH with read-only
54+
# permissions, requiring sudo to delete the directory. Rather
55+
# than surprise the dev with a password entry at the end of the
56+
# script's execution we just print a message to let them know.
57+
function shim_gopath_clean() {
58+
local REPO_DIR=$(git rev-parse --show-toplevel)
59+
local TEMP_GOPATH="${REPO_DIR}/.gopath"
60+
if [ -d "$TEMP_GOPATH" ] ; then
61+
# Put the user back at the root of the pipelines repo
62+
# after all the symlink shenanigans.
63+
echo "Moving to $REPO_DIR"
64+
cd "$REPO_DIR"
65+
delete_pipeline_repo_symlink
66+
echo "When you are finished with codegen you can safely run the following:"
67+
echo "sudo rm -rf \".gopath\""
68+
fi
69+
}
70+
71+
# Delete the temp symlink to pipelines repo from the temp GOPATH dir.
72+
function delete_pipeline_repo_symlink() {
73+
local REPO_DIR=$(git rev-parse --show-toplevel)
74+
local TEMP_GOPATH="${REPO_DIR}/.gopath"
75+
if [ -d "$TEMP_GOPATH" ] ; then
76+
local REPO_SYMLINK="${TEMP_GOPATH}/src/github.com/tektoncd/pipeline"
77+
if [ -L $REPO_SYMLINK ] ; then
78+
echo "Deleting symlink to pipelines repo $REPO_SYMLINK"
79+
rm -f "${REPO_SYMLINK}"
80+
fi
81+
fi
82+
}

hack/update-codegen.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ set -o errexit
1818
set -o nounset
1919
set -o pipefail
2020

21+
source $(git rev-parse --show-toplevel)/hack/setup-temporary-gopath.sh
22+
shim_gopath
23+
trap shim_gopath_clean EXIT
24+
2125
source $(git rev-parse --show-toplevel)/vendor/github.com/tektoncd/plumbing/scripts/library.sh
2226

2327
PREFIX=${GOBIN:-${GOPATH}/bin}

hack/update-openapigen.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
set -o errexit
1818
set -o nounset
1919

20+
source $(git rev-parse --show-toplevel)/hack/setup-temporary-gopath.sh
21+
shim_gopath
22+
trap shim_gopath_clean EXIT
23+
2024
echo "Generating OpenAPI specification ..."
2125
go run k8s.io/kube-openapi/cmd/openapi-gen \
2226
--input-dirs ./pkg/apis/pipeline/v1beta1,./pkg/apis/pipeline/pod,./pkg/apis/resource/v1alpha1,knative.dev/pkg/apis,knative.dev/pkg/apis/duck/v1beta1 \
@@ -25,4 +29,3 @@ go run k8s.io/kube-openapi/cmd/openapi-gen \
2529

2630
echo "Generating swagger file ..."
2731
go run hack/spec-gen/main.go v0.17.2 > pkg/apis/pipeline/v1beta1/swagger.json
28-

0 commit comments

Comments
 (0)