|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Based on install.sh from https://github.com/japaric/trust |
| 4 | +# License: MIT/Apache. |
| 5 | +# See: |
| 6 | +# * https://github.com/japaric/trust/blob/master/LICENSE-MIT |
| 7 | +# * https://github.com/japaric/trust/blob/master/LICENSE-APACHE |
| 8 | + |
| 9 | +set -e |
| 10 | + |
| 11 | +help() { |
| 12 | + cat <<'EOF' |
| 13 | +Install a binary release of a Rust crate hosted on github.com |
| 14 | +
|
| 15 | +Usage: |
| 16 | + crate-ci.sh [options] |
| 17 | +
|
| 18 | +Options: |
| 19 | + -h, --help Display this message |
| 20 | + --git SLUG Get the crate from "https://github.com/$SLUG" |
| 21 | + -f, --force Force overwriting an existing binary |
| 22 | + --crate NAME Name of the crate to install (default <repository name>) |
| 23 | + --tag TAG Tag (version) of the crate to install (default <latest release>) |
| 24 | + --target TARGET Install the release compiled for $TARGET (default <`rustc` host>) |
| 25 | + --to LOCATION Where to install the binary (default ~/.cargo/bin) |
| 26 | +EOF |
| 27 | +} |
| 28 | + |
| 29 | +say() { |
| 30 | + echo "install.sh: $1" |
| 31 | +} |
| 32 | + |
| 33 | +say_err() { |
| 34 | + say "$1" >&2 |
| 35 | +} |
| 36 | + |
| 37 | +err() { |
| 38 | + if [ -n "$td" ]; then |
| 39 | + rm -rf "$td" |
| 40 | + fi |
| 41 | + |
| 42 | + say_err "ERROR $1" |
| 43 | + exit 1 |
| 44 | +} |
| 45 | + |
| 46 | +need() { |
| 47 | + if ! command -v "$1" > /dev/null 2>&1; then |
| 48 | + err "need $1 (command not found)" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +force=false |
| 53 | +while test $# -gt 0; do |
| 54 | + case $1 in |
| 55 | + --crate) |
| 56 | + crate=$2 |
| 57 | + shift |
| 58 | + ;; |
| 59 | + --force | -f) |
| 60 | + force=true |
| 61 | + ;; |
| 62 | + --git) |
| 63 | + git=$2 |
| 64 | + shift |
| 65 | + ;; |
| 66 | + --help | -h) |
| 67 | + help |
| 68 | + exit 0 |
| 69 | + ;; |
| 70 | + --tag) |
| 71 | + tag=$2 |
| 72 | + shift |
| 73 | + ;; |
| 74 | + --target) |
| 75 | + target=$2 |
| 76 | + shift |
| 77 | + ;; |
| 78 | + --to) |
| 79 | + dest=$2 |
| 80 | + shift |
| 81 | + ;; |
| 82 | + *) |
| 83 | + ;; |
| 84 | + esac |
| 85 | + shift |
| 86 | +done |
| 87 | + |
| 88 | +# Dependencies |
| 89 | +need basename |
| 90 | +need curl |
| 91 | +need install |
| 92 | +need mkdir |
| 93 | +need mktemp |
| 94 | +need tar |
| 95 | + |
| 96 | +# Optional dependencies |
| 97 | +if [ -z "$crate" ] || [ -z "$tag" ] || [ -z "$target" ]; then |
| 98 | + need cut |
| 99 | +fi |
| 100 | + |
| 101 | +if [ -z "$tag" ]; then |
| 102 | + need rev |
| 103 | +fi |
| 104 | + |
| 105 | +if [ -z "$target" ]; then |
| 106 | + need grep |
| 107 | + need rustc |
| 108 | +fi |
| 109 | + |
| 110 | +if [ -z "$git" ]; then |
| 111 | + # Markdown-style backticks |
| 112 | + # shellcheck disable=SC2016 |
| 113 | + err 'must specify a git repository using `--git`. Example: `install.sh --git japaric/cross`' |
| 114 | +fi |
| 115 | + |
| 116 | +url="https://github.com/$git" |
| 117 | +say_err "Git repository: $url" |
| 118 | + |
| 119 | +if [ -z "$crate" ]; then |
| 120 | + crate=$(echo "$git" | cut -d'/' -f2) |
| 121 | +fi |
| 122 | + |
| 123 | +say_err "Crate: $crate" |
| 124 | + |
| 125 | +url="$url/releases" |
| 126 | + |
| 127 | +if [ -z "$tag" ]; then |
| 128 | + latest_url=$url/latest |
| 129 | + tag_url=$(curl -Ls -o /dev/null -w "%{url_effective}" "$latest_url") |
| 130 | + tag=$(echo "$tag_url" | rev | cut -d'/' -f1 | rev) |
| 131 | + if [ -z "$tag" ]; then |
| 132 | + err "Failed to get tag from $latest_url" |
| 133 | + fi |
| 134 | + say_err "Tag: latest ($tag)" |
| 135 | +else |
| 136 | + say_err "Tag: $tag" |
| 137 | +fi |
| 138 | + |
| 139 | +if [ -z "$target" ]; then |
| 140 | + target=$(rustc -Vv | grep host | cut -d' ' -f2) |
| 141 | +fi |
| 142 | + |
| 143 | +say_err "Target: $target" |
| 144 | + |
| 145 | +if [ -z "$dest" ]; then |
| 146 | + dest="$HOME/.cargo/bin" |
| 147 | +fi |
| 148 | + |
| 149 | +say_err "Installing to: $dest" |
| 150 | + |
| 151 | +url="$url/download/$tag/$crate-$tag-$target.tar.gz" |
| 152 | + |
| 153 | +td=$(mktemp -d || mktemp -d -t tmp) |
| 154 | +curl -sL "$url" | tar xz -f - -C "$td" |
| 155 | + |
| 156 | +for f in "$td"/*; do |
| 157 | + test -x "$f" || continue |
| 158 | + test -f "$f" || continue |
| 159 | + |
| 160 | + if [ -e "$dest/$(basename "$f")" ] && [ "$force" = false ]; then |
| 161 | + err "$f already exists in $dest" |
| 162 | + else |
| 163 | + mkdir -p "$dest" |
| 164 | + install -m 755 "$f" "$dest" |
| 165 | + fi |
| 166 | +done |
| 167 | + |
| 168 | +rm -rf "$td" |
0 commit comments