-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`cargo.sh` is a small utility that supports running Cargo with human-readable toolchain names that are mapped to the equivalent versions pinned in CI. It supports "msrv", "stable", and "nightly". It also supports a meta-toolchain, "all", which instructs it to run a command once with each of the three pinned toolchains. In this commit, we replace the toolchain name-to-version resolution logic in CI with this command. This simplifies our CI configuration, and maintains the property that the code run in CI is the same code a developer would run on their local machine.
- Loading branch information
Showing
2 changed files
with
112 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright 2023 The Fuchsia Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
# This script is a thin wrapper around Cargo that provides human-friendly | ||
# toolchain names which are automatically translated to the toolchain versions | ||
# we have pinned in CI. | ||
# | ||
# cargo.sh --version <toolchain-name> # looks up the version for the named toolchain | ||
# cargo.sh +<toolchain-name> [...] # runs cargo commands with the named toolchain | ||
# cargo.sh +all [...] # runs cargo commands with each toolchain | ||
# | ||
# The meta-toolchain "all" instructs this script to run the provided command | ||
# once for each toolchain (msrv, stable, nightly). | ||
# | ||
# A common task that is especially annoying to perform by hand is to update | ||
# trybuild's stderr files. Using this script: | ||
# | ||
# TRYBUILD=overwrite ./cargo.sh +all test --workspace | ||
|
||
set -eo pipefail | ||
|
||
function print-usage-and-exit { | ||
echo "Usage:" >&2 | ||
echo " $0 --version <toolchain-name>" >&2 | ||
echo " $0 +<toolchain-name> [...]" >&2 | ||
echo " $0 +all [...]" >&2 | ||
exit 1 | ||
} | ||
|
||
[[ $# -gt 0 ]] || print-usage-and-exit | ||
|
||
function pkg-meta { | ||
cargo metadata --format-version 1 | jq -r ".packages[] | select(.name == \"zerocopy\").$1" | ||
} | ||
|
||
function lookup-version { | ||
VERSION="$1" | ||
case "$VERSION" in | ||
msrv) | ||
pkg-meta rust_version | ||
;; | ||
stable) | ||
pkg-meta 'metadata.ci."pinned-stable"' | ||
;; | ||
nightly) | ||
pkg-meta 'metadata.ci."pinned-nightly"' | ||
;; | ||
*) | ||
echo "Unrecognized toolchain name: '$VERSION' (options are 'msrv', 'stable', 'nightly')" >&2 | ||
return 1 | ||
;; | ||
esac | ||
} | ||
|
||
case "$1" in | ||
# cargo.sh --version <toolchain-name> | ||
--version) | ||
[[ $# -eq 2 ]] || print-usage-and-exit | ||
lookup-version "$2" | ||
;; | ||
# cargo.sh +all [...] | ||
+all) | ||
echo "[cargo.sh] warning: running the same command for each toolchain (msrv, stable, nightly)" >&2 | ||
for toolchain in msrv stable nightly; do | ||
echo "[cargo.sh] running with toolchain: $toolchain" >&2 | ||
TOOLCHAIN="$(lookup-version $toolchain)" | ||
cargo "+$TOOLCHAIN" ${@:2} | ||
done | ||
exit 0 | ||
;; | ||
# cargo.sh +<toolchain-name> [...] | ||
+*) | ||
TOOLCHAIN="$(lookup-version ${1:1})" | ||
cargo "+$TOOLCHAIN" ${@:2} | ||
;; | ||
*) | ||
print-usage-and-exit | ||
;; | ||
esac |