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