|
| 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