Skip to content

Commit

Permalink
feat: add installer script
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed May 11, 2024
1 parent 122bda0 commit c360fbe
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ $ nix profile install 'github:ryanccn/nrr#nrr'

nrr is also available in [Nixpkgs](https://github.com/NixOS/nixpkgs) as `nixpkgs#nrr`.

### Installer

This installer script works on Linux and macOS and downloads binaries from GitHub Releases, falling back to `cargo install` if a prebuilt binary cannot be found.

```console
$ curl --proto '=https' --tlsv1.2 -fsSL https://nrr.ryanccn.dev | sh
```

### Cargo

nrr supports [cargo-binstall](https://github.com/cargo-bins/cargo-binstall), which downloads a binary if possible from GitHub Releases instead of compiling the crate from source.
Expand Down
79 changes: 79 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/sh
set -euf

ansi_red="\033[31m"
ansi_green="\033[32m"
ansi_blue="\033[34m"
ansi_yellow="\033[33m"
ansi_bold="\033[1m"
ansi_reset="\033[0m"

success() {
printf "${ansi_green}success${ansi_reset} %s\n" "$*"
}

info() {
printf "${ansi_blue}info${ansi_reset} %s\n" "$*"
}

err() {
printf "${ansi_red}error${ansi_reset} %s\n" "$*"
}

warn() {
printf "${ansi_yellow}warn${ansi_reset} %s\n" "$*"
}

bold() {
printf "${ansi_bold}%s${ansi_reset}" "$*"
}

main() {
platform="$(uname -s)"
arch="$(uname -m)"

if [ "$platform" = "Darwin" ]; then
if [ "$arch" = "arm64" ]; then target="aarch64-apple-darwin";
elif [ "$arch" = "x86_64" ]; then target="x86_64-apple-darwin";
fi
elif [ "$platform" = "Linux" ]; then
if [ "$arch" = "arm64" ]; then target="aarch64-unknown-linux-musl";
elif [ "$arch" = "x86_64" ]; then target="x86_64-unknown-linux-musl";
fi
fi

if [ -z "$target" ]; then
if command -v cargo > /dev/null 2>&1; then
warn "GitHub Releases binary not found, falling back to cargo install"
cargo install nrr
else
err "Unsupported platform! Could not find binary to download from GitHub Releases"
exit 1
fi
fi

info "Downloading binary for target $(bold "$target")..."

asset_temp="$(mktemp)"
curl -fSL "https://github.com/ryanccn/nrr/releases/latest/download/nrr-$target.zip" > "$asset_temp"

unpack_temp="$(mktemp -d)"
unzip "$asset_temp" -d "$unpack_temp" > /dev/null 2>&1

cargo_home="${CARGO_HOME:-"$HOME/.cargo"}"
mkdir -p "$cargo_home/bin"

cp "$unpack_temp/nrr" "$cargo_home/bin"
chmod +x "$cargo_home/bin/nrr"

version="$(bold "$("$cargo_home/bin/nrr" --version)")"

success "Installed $version to $cargo_home/bin ^^"

case :$PATH:
in *:"$cargo_home/bin":*) ;;
*) warn "The installation directory is not in your PATH. You might want to add it in order to use nrr :p" >&2;;
esac
}

main || exit 1

0 comments on commit c360fbe

Please sign in to comment.