Skip to content

Commit 4a0a660

Browse files
committed
hack/verify-go-modules.sh: generate list of nested modules
The list is no longer constructed manually. On-behalf-of: SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent 9298a44 commit 4a0a660

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

hack/verify-go-modules.sh

+32-16
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,39 @@ set -o pipefail
2626

2727
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
2828

29-
# This is the list of directories that host the individual kcp go modules.
30-
#
31-
# KEEP IT UP TO DATE and SORTED!
32-
#
33-
# The items must be sorted such that the module that is being dependent on
34-
# comes before the one that depends on it (i.e. topological order).
35-
DIRS=(
36-
"${REPO_ROOT}/sdk"
37-
"${REPO_ROOT}/cli"
38-
"${REPO_ROOT}"
39-
"${REPO_ROOT}/docs/generators/cli-doc"
29+
if [[ ! -f "${REPO_ROOT}/go.work" ]]; then
30+
(
31+
cd "${REPO_ROOT}"
32+
go work init
33+
go work use -r .
34+
)
35+
fi
36+
37+
# Array of all modules inside the repository, sorted in
38+
# descending topological order (dependant comes before its dependee).
39+
mapfile -t MODULE_DIRS < <(
40+
for mod in $(tsort <(
41+
# Lines in format "<Dependant> <Dependee>" are fed into `tsort` to perform the topological sort.
42+
for dir in $(go list -m -json | jq -r '.Dir'); do
43+
(
44+
cd "${dir}"
45+
# Prints the dependency graph where we extract only the direct
46+
# github.com/kcp-dev/kcp* dependencies of the currently examined module,
47+
# and we discard the dependency version (the line is in format
48+
# "<Module> <Dependency>@<Version>", so we split by '@' and get the
49+
# first part). We skip when no such deps are found.
50+
go mod graph \
51+
| grep -E "^$(go mod edit -json | jq -r '.Module.Path') github.com/kcp-dev/kcp" \
52+
| cut -d'@' -f1 \
53+
|| true
54+
)
55+
done
56+
) | tac); do # We need to reverse the lines (with `tac`) to have a descending order.
57+
# We have sorted module paths. We need to convert them into their respective directory locations.
58+
go list -m -json "$mod" | jq -r '.Dir'
59+
done
4060
)
4161

42-
# Check that DIRS is up to date.
43-
[[ "$(printf '%s\n' "${DIRS[@]}" | sort)" == "$(find "${REPO_ROOT}" -name go.mod -print0 | xargs -0 dirname | sort)" ]] \
44-
|| { echo "Error: DIRS list in ${BASH_SOURCE[0]} is out-of-date, and needs to be manually updated" 1>&2 ; exit 1 ; }
45-
4662
# list_deps lists dependencies of the supplied go.mod file (dependencies
4763
# with version "v0.0.0" are skipped). The output is a json dictionary in the
4864
# format `{"<Dependency>": "<Version>", ...}`.
@@ -123,7 +139,7 @@ function gomod_filepath_for {
123139
k8s_gomod="$(gomod_filepath_for k8s.io/kubernetes)"
124140
k8s_deps="$(list_deps ${k8s_gomod})"
125141

126-
for dir in "${DIRS[@]}"; do
142+
for dir in "${MODULE_DIRS[@]}"; do
127143
(
128144
cd "$dir"
129145
echo "Verifying ${dir}"

0 commit comments

Comments
 (0)