-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathset-version.sh
More file actions
executable file
·107 lines (93 loc) · 4 KB
/
Copy pathset-version.sh
File metadata and controls
executable file
·107 lines (93 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
#
# set-version.sh — set the SP1 workspace version.
#
# Updates, in the root Cargo.toml:
# 1. [workspace.package] version (inherited by every crate that uses
# `version.workspace = true`, so all member crates pick it up).
# 2. The `version = "..."` field of every workspace dependency that is a
# local member (i.e. has a `path = ...`). External deps such as p3-* and
# crates.io packages have no `path` and are left untouched.
#
# Usage:
# ./set-version.sh <new-version> [path/to/Cargo.toml]
#
# Example:
# ./set-version.sh 6.3.0
set -euo pipefail
NEW_VERSION="${1:-}"
if [[ -z "$NEW_VERSION" ]]; then
echo "usage: $0 <new-version> [path/to/Cargo.toml]" >&2
exit 1
fi
# Loose semver sanity check: MAJOR.MINOR.PATCH with an optional -pre/+build suffix.
if [[ ! "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?$ ]]; then
echo "error: '$NEW_VERSION' does not look like a semver version (e.g. 6.3.0)" >&2
exit 1
fi
# Default to the Cargo.toml next to this script (the workspace root).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MANIFEST="${2:-$SCRIPT_DIR/Cargo.toml}"
if [[ ! -f "$MANIFEST" ]]; then
echo "error: manifest not found: $MANIFEST" >&2
exit 1
fi
# Read the current workspace version from [workspace.package]. It is the only
# line that starts with `version = "` (member-dep versions are nested inside
# `{ version = ... }`, and `rust-version` starts with a different token).
OLD_VERSION="$(sed -nE 's/^version = "([^"]+)"/\1/p' "$MANIFEST" | head -n1)"
if [[ -z "$OLD_VERSION" ]]; then
echo "error: could not find [workspace.package] version in $MANIFEST" >&2
exit 1
fi
if [[ "$OLD_VERSION" == "$NEW_VERSION" ]]; then
echo "version is already $NEW_VERSION; nothing to do."
exit 0
fi
# Escape regex metacharacters in the old version (it contains dots) so it is
# matched literally, and escape replacement metacharacters in the new version.
escape_re() { printf '%s' "$1" | sed -e 's/[.[\*^$()+?{}|\/]/\\&/g'; }
escape_repl() { printf '%s' "$1" | sed -e 's/[\/&]/\\&/g'; }
OLD_RE="$(escape_re "$OLD_VERSION")"
NEW_REPL="$(escape_repl "$NEW_VERSION")"
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
sed -E \
-e "s/^version = \"${OLD_RE}\"/version = \"${NEW_REPL}\"/" \
-e "/path *=/ s/version = \"${OLD_RE}\"/version = \"${NEW_REPL}\"/" \
"$MANIFEST" > "$tmp"
# Report how many member-dependency lines changed (excludes the package line).
DEP_COUNT="$(grep -cE "path *=.*version = \"${NEW_REPL}\"|version = \"${NEW_REPL}\".*path *=" "$tmp" || true)"
mv "$tmp" "$MANIFEST"
trap - EXIT
# Update Cargo.lock files. The workspace version is recorded in each lockfile,
# so bumping it changes the lock entries for the local member crates. Sweep the
# tree rooted at the manifest's directory, refresh every Cargo.lock, and record
# which ones actually changed (checksum before vs. after).
ROOT="$(cd "$(dirname "$MANIFEST")" && pwd)"
UPDATED_LOCKS=()
while IFS= read -r lockfile; do
lockdir="$(dirname "$lockfile")"
before="$(cksum < "$lockfile")"
if ( cd "$lockdir" && cargo update --workspace --offline ); then
if [[ "$(cksum < "$lockfile")" != "$before" ]]; then
# Tag locks that git won't track (e.g. patch-testing/**/program/Cargo.lock
# via patch-testing/.gitignore) — those are rewritten on disk but never
# appear in `git status`, so flag them to avoid a confusing mismatch.
if git -C "$lockdir" check-ignore -q "$lockfile" 2>/dev/null; then
UPDATED_LOCKS+=("$lockfile (gitignored)")
else
UPDATED_LOCKS+=("$lockfile")
fi
fi
else
echo " warning: 'cargo update' failed in $lockdir" >&2
fi
done < <(find "$ROOT" -path '*/target' -prune -o -name Cargo.lock -type f -print)
echo "Updated $MANIFEST: $OLD_VERSION -> $NEW_VERSION"
echo " [workspace.package] version bumped (inherited by version.workspace = true crates)"
echo " $DEP_COUNT workspace member dependency entries updated"
echo " ${#UPDATED_LOCKS[@]} Cargo.lock file(s) updated:"
for lock in "${UPDATED_LOCKS[@]}"; do
echo " $lock"
done