@@ -26,23 +26,39 @@ set -o pipefail
26
26
27
27
REPO_ROOT=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) /.." && pwd)
28
28
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
40
60
)
41
61
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
-
46
62
# list_deps lists dependencies of the supplied go.mod file (dependencies
47
63
# with version "v0.0.0" are skipped). The output is a json dictionary in the
48
64
# format `{"<Dependency>": "<Version>", ...}`.
@@ -123,7 +139,7 @@ function gomod_filepath_for {
123
139
k8s_gomod=" $( gomod_filepath_for k8s.io/kubernetes) "
124
140
k8s_deps=" $( list_deps ${k8s_gomod} ) "
125
141
126
- for dir in " ${DIRS [@]} " ; do
142
+ for dir in " ${MODULE_DIRS [@]} " ; do
127
143
(
128
144
cd " $dir "
129
145
echo " Verifying ${dir} "
0 commit comments