Skip to content

Commit bc0c23b

Browse files
committed
Add two scripts to check and get the current version of a crate
1 parent 5a1508f commit bc0c23b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

scripts/get_current_version.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# Script requirements:
4+
# - curl
5+
# - jq
6+
7+
# Fail on first error, on undefined variables, and on failures in pipelines.
8+
set -euo pipefail
9+
10+
# Go to the repo root directory.
11+
cd "$(git rev-parse --show-toplevel)"
12+
13+
# The first argument should be the name of a crate.
14+
CRATE_NAME="$1"
15+
16+
cargo metadata --format-version 1 | \
17+
jq --arg crate_name "$CRATE_NAME" --exit-status -r \
18+
'.packages[] | select(.name == $crate_name) | .version'
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
# Script requirements:
4+
# - curl
5+
# - jq
6+
# - sort with `-V` flag, available in `coreutils-7`
7+
# On macOS this may require `brew install coreutils`.
8+
9+
# Fail on first error, on undefined variables, and on failures in pipelines.
10+
set -euo pipefail
11+
12+
# Go to the repo root directory.
13+
cd "$(git rev-parse --show-toplevel)"
14+
15+
# The first argument should be the name of a crate.
16+
CRATE_NAME="$1"
17+
18+
CURRENT_VERSION="$(./scripts/get_current_version.sh "$CRATE_NAME")" || \
19+
(echo >&2 "No crate named $CRATE_NAME found in workspace."; exit 1)
20+
echo >&2 "Crate $CRATE_NAME current version: $CURRENT_VERSION"
21+
22+
# The leading whitespace is important! With it, we know that every version is both
23+
# preceded by and followed by whitespace. We use this fact to avoid matching
24+
# on substrings of versions.
25+
EXISTING_VERSIONS="
26+
$( \
27+
curl 2>/dev/null "https://crates.io/api/v1/crates/$CRATE_NAME" | \
28+
jq --exit-status -r .versions[].num \
29+
)"
30+
echo >&2 -e "Versions on crates.io:$EXISTING_VERSIONS\n"
31+
32+
# Use version sort (sort -V) to get all versions in ascending order, then use grep to:
33+
# - grab the first line that matches the current version (--max-count=1)
34+
# - only match full lines (--line-regexp)
35+
OUTPUT="$( \
36+
echo -e "$EXISTING_VERSIONS" | \
37+
sort -V | \
38+
grep --line-regexp --max-count=1 "$CURRENT_VERSION" || true
39+
)"
40+
41+
if [[ "$OUTPUT" == "$CURRENT_VERSION" ]]; then
42+
echo >&2 "The current version $CURRENT_VERSION is already on crates.io"
43+
exit 7
44+
fi

0 commit comments

Comments
 (0)