Skip to content

Commit

Permalink
Function-ize the check api script
Browse files Browse the repository at this point in the history
Add functions, making the script uniform with others currently being
introduced in other crates around the place.
  • Loading branch information
tcharding committed Jan 25, 2024
1 parent c973a04 commit 0bd55be
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions contrib/check-for-api-changes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,55 @@

set -e

export RUSTDOCFLAGS='-A rustdoc::broken-intra-doc-links'
REPO_DIR=$(git rev-parse --show-toplevel)
API_DIR="$REPO_DIR/api"
CMD="cargo +nightly public-api --simplified"
# `sort -n -u` doesn't work for some reason.
SORT="sort --numeric-sort"

# cargo public-api uses nightly so the toolchain must be available.
if ! cargo +nightly --version > /dev/null; then
echo "script requires a nightly toolchain to be installed (possibly >= nightly-2023-05-24)" >&2
exit 1
fi

pushd "$REPO_DIR" > /dev/null
$CMD --no-default-features | $SORT | uniq > "$API_DIR/no-default-features.txt"
$CMD | $SORT | uniq > "$API_DIR/default-features.txt"
$CMD --no-default-features --features=alloc | $SORT | uniq > "$API_DIR/alloc.txt"
$CMD --no-default-features --features=core2 | $SORT | uniq > "$API_DIR/core2.txt"
$CMD --all-features | $SORT | uniq > "$API_DIR/all-features.txt"

if [[ $(git status --porcelain api) ]]; then
echo "You have introduced changes to the public API, commit the changes to api/ currently in your working directory" >&2
main() {
# cargo public-api uses nightly so the toolchain must be available.
if ! cargo +nightly --version > /dev/null; then
echo "script requires a nightly toolchain to be installed (possibly >= nightly-2023-05-24)" >&2
exit 1
fi

generate_api_files
check_for_changes
}

generate_api_files() {
pushd "$REPO_DIR" > /dev/null

$CMD --no-default-features | $SORT | uniq > "$API_DIR/no-default-features.txt"
$CMD | $SORT | uniq > "$API_DIR/default-features.txt"
$CMD --no-default-features --features=alloc | $SORT | uniq > "$API_DIR/alloc.txt"
$CMD --no-default-features --features=core2 | $SORT | uniq > "$API_DIR/core2.txt"
$CMD --all-features | $SORT | uniq > "$API_DIR/all-features.txt"

popd > /dev/null
exit 1
else
echo "No changes to the current public API"
}

# Check if there are changes (dirty git index) to the `api/` directory.
check_for_changes() {
pushd "$REPO_DIR" > /dev/null

if [[ $(git status --porcelain api) ]]; then
git diff --color=always

echo ""
echo "You have introduced changes to the public API, commit the changes to api/ currently in your working directory" >&2
exit 1

else
echo "No changes to the current public API"
fi

popd > /dev/null
fi
}

#
# Main script
#
main "$@"
exit 0

0 comments on commit 0bd55be

Please sign in to comment.