|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to display help message |
| 4 | +show_help() { |
| 5 | + echo "Usage: $0 [stable|nightly] [command]" |
| 6 | + echo |
| 7 | + echo "This script is used to run a specified cargo command with either the stable or nightly Rust toolchain." |
| 8 | + echo |
| 9 | + echo "Arguments:" |
| 10 | + echo " stable - Use the stable Rust toolchain." |
| 11 | + echo " nightly - Use the nightly Rust toolchain." |
| 12 | + echo " command - The cargo command to run (e.g., clippy, build)." |
| 13 | + echo |
| 14 | + echo "Examples:" |
| 15 | + echo " $0 stable clippy - Run clippy with the stable Rust version." |
| 16 | + echo " $0 nightly build - Build with the nightly Rust version." |
| 17 | +} |
| 18 | + |
| 19 | +# Function to run a specified cargo command with a specified toolchain |
| 20 | +run_cargo_command() { |
| 21 | + local toolchain=$1 |
| 22 | + local command=$2 |
| 23 | + echo "Running cargo $command with Rust $toolchain version..." |
| 24 | + cargo +"$toolchain" "$command" |
| 25 | +} |
| 26 | + |
| 27 | +# Check if the first argument is -h or --help |
| 28 | +if [[ $1 == "-h" || $1 == "--help" ]]; then |
| 29 | + show_help |
| 30 | + exit 0 |
| 31 | +fi |
| 32 | + |
| 33 | +# Check if the second argument is not provided |
| 34 | +if [[ -z $2 ]]; then |
| 35 | + echo "Error: No command specified." |
| 36 | + show_help |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Path to the rust-version.sh script |
| 41 | +rust_version_script="./ci/rust-version.sh" |
| 42 | + |
| 43 | +# Check if rust-version.sh exists and is executable |
| 44 | +if [[ -f "$rust_version_script" && -x "$rust_version_script" ]]; then |
| 45 | + # Source the rust-version.sh script |
| 46 | + source "$rust_version_script" |
| 47 | +else |
| 48 | + echo "Error: rust-version.sh not found or not executable." |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | + |
| 53 | +# Main execution logic based on passed arguments |
| 54 | +case $1 in |
| 55 | + stable) |
| 56 | + # Check if Rust stable version is set |
| 57 | + if [[ -z $rust_stable ]]; then |
| 58 | + echo "Error: Rust stable version is not set." |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + run_cargo_command "$rust_stable" "$2" |
| 62 | + ;; |
| 63 | + nightly) |
| 64 | + # Check if Rust nightly version is set |
| 65 | + if [[ -z $rust_nightly ]]; then |
| 66 | + echo "Error: Rust nightly version is not set." |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + run_cargo_command "$rust_nightly" "$2" |
| 70 | + ;; |
| 71 | + *) |
| 72 | + echo "Usage: $0 [stable|nightly] [command]" |
| 73 | + exit 1 |
| 74 | + ;; |
| 75 | +esac |
0 commit comments