Skip to content

Commit 9a6bd5b

Browse files
committed
Functionize check API script
Add functions to the `check-for-api-changes.sh` script. Refactor only, no logic changes.
1 parent 4fda7f1 commit 9a6bd5b

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

contrib/check-for-api-changes.sh

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,55 @@ set -e
99
export RUSTDOCFLAGS='-A rustdoc::broken-intra-doc-links'
1010
REPO_DIR=$(git rev-parse --show-toplevel)
1111
API_DIR="$REPO_DIR/api"
12-
CMD="cargo +nightly public-api --simplified"
12+
CARGO="cargo +nightly public-api --simplified"
1313
# `sort -n -u` doesn't work for some reason.
1414
SORT="sort --numeric-sort"
1515

16-
# cargo public-api uses nightly so the toolchain must be available.
17-
if ! cargo +nightly --version > /dev/null; then
18-
echo "script requires a nightly toolchain to be installed (possibly >= nightly-2023-05-24)" >&2
19-
exit 1
20-
fi
21-
22-
pushd "$REPO_DIR" > /dev/null
23-
$CMD --no-default-features | $SORT | uniq > "$API_DIR/no-default-features.txt"
24-
$CMD | $SORT | uniq > "$API_DIR/default-features.txt"
25-
$CMD --no-default-features --features=alloc | $SORT | uniq > "$API_DIR/alloc.txt"
26-
$CMD --no-default-features --features=core2 | $SORT | uniq > "$API_DIR/core2.txt"
27-
$CMD --all-features | $SORT | uniq > "$API_DIR/all-features.txt"
28-
29-
if [[ $(git status --porcelain api) ]]; then
30-
echo "You have introduced changes to the public API, commit the changes to api/ currently in your working directory" >&2
16+
main() {
17+
# cargo public-api uses nightly so the toolchain must be available.
18+
if ! cargo +nightly --version > /dev/null; then
19+
echo "script requires a nightly toolchain to be installed (possibly >= nightly-2023-05-24)" >&2
20+
exit 1
21+
fi
22+
23+
generate_api_files
24+
check_for_changes
25+
}
26+
27+
generate_api_files() {
28+
pushd "$REPO_DIR" > /dev/null
29+
30+
$CARGO | $SORT | uniq > "$API_DIR/default-features.txt"
31+
32+
$CARGO --no-default-features | $SORT | uniq > "$API_DIR/no-default-features.txt"
33+
$CARGO --no-default-features --features=alloc | $SORT | uniq > "$API_DIR/alloc.txt"
34+
$CARGO --no-default-features --features=core2 | $SORT | uniq > "$API_DIR/core2.txt"
35+
36+
$CARGO --all-features | $SORT | uniq > "$API_DIR/all-features.txt"
37+
3138
popd > /dev/null
32-
exit 1
33-
else
34-
echo "No changes to the current public API"
39+
}
40+
41+
# Check if there are changes (dirty git index) to the `api/` directory.
42+
check_for_changes() {
43+
pushd "$REPO_DIR" > /dev/null
44+
45+
if [[ $(git status --porcelain api) ]]; then
46+
git diff --color=always
47+
48+
echo
49+
echo "You have introduced changes to the public API, commit the changes to api/ currently in your working directory" >&2
50+
exit 1
51+
52+
else
53+
echo "No changes to the current public API"
54+
fi
55+
3556
popd > /dev/null
36-
fi
57+
}
3758

59+
#
60+
# Main script
61+
#
62+
main "$@"
3863
exit 0
39-

0 commit comments

Comments
 (0)