#!/usr/bin/env bash #!/bin/bash set -e BIFROST_PATH=${BIFROST_PATH:-"$HOME/.bifrost"} BIFROST_BIN_DIR="$BIFROST_PATH/bin" main() { # ensuring git, curl, and cargo are installed requires_cmd git requires_cmd curl requires_cmd cargo ensure_apt_package "libssl-dev" ensure_apt_package "build-essential" # parsing parameters while [[ $1 ]]; do case $1 in --) shift; break ;; -u|--upgrade|--update) shift; echo "bifrost: removing old binaries" rm -rf "$BIFROST_PATH" ensure curl -L https://raw.githubusercontent.com/Jon-Becker/heimdall-rs/main/bifrost/install | bash exit 0 ;; -v|--version) shift; TARGET_VERSION=$1 shift ;; -B|--binary|--bin) shift; USE_BINARY=true ;; +nightly) shift; NIGHTLY_CHANNEL=true ;; -h|--help) usage exit 0 ;; -l|--list|--versions) shift; versions exit 0 ;; *) echo "bifrost: option '$1' not recognized" exit 1 ;; esac; done # print channel if [ -n "$NIGHTLY_CHANNEL" ]; then echo "bifrost: using nightly channel" else echo "bifrost: using stable channel" fi # remove the current heimdall installation if it exists ensure rm -f "$BIFROST_BIN_DIR/heimdall" # make the build path if it doesn't exist BUILD_PATH="${BIFROST_PATH}/build" if [ ! -d $BUILD_PATH ]; then ensure mkdir -p $BUILD_PATH fi # remove the source directory if it exists ensure rm -rf "$BUILD_PATH/heimdall-rs" # clone heimdall-rs and cd into it cd $BUILD_PATH echo "bifrost: cloning 'Jon-Becker/heimdall-rs'." # git clone, but only include the "Resolving deltas: ..." and "Receiving objects: ..." lines ensure git clone "https://github.com/Jon-Becker/heimdall-rs" --progress 2>&1 | grep -E "Resolving deltas:|Receiving objects:" cd "heimdall-rs" ensure git fetch origin # if we are nightly, use `main` branch if [ -n "$NIGHTLY_CHANNEL" ]; then ensure git checkout main > /dev/null 2>&1 # get the latest short commit hash TARGET_VERSION=$(git rev-parse --short HEAD) # get the latest tag tag=$(git describe --tags `git rev-list --tags --max-count=1`) # build nightly version nightly_version="$tag+nightly.$TARGET_VERSION" echo "bifrost: installing version $nightly_version." # if they specified a version, checkout that tag or branch elif [ -n "$TARGET_VERSION" ]; then echo "bifrost: installing version $TARGET_VERSION." ensure git checkout $TARGET_VERSION > /dev/null 2>&1 else # checkout the latest tag tag=$(git describe --tags `git rev-list --tags --max-count=1`) echo "bifrost: installing version $tag." TARGET_VERSION=$tag ensure git checkout $tag -b latest > /dev/null 2>&1 fi # if the user wants to use the precompiled binary, download it if [ -n "$USE_BINARY" ]; then # nightly binaries are not available if [ -n "$NIGHTLY_CHANNEL" ]; then echo "bifrost: nightly binaries are not available." exit 1 fi # cd into the binary directory ensure cd $BIFROST_BIN_DIR echo "bifrost: fetching binary." # download the binary if [[ "$OSTYPE" == "linux-gnu"* ]]; then ensure curl -k -L -s --compressed "https://github.com/Jon-Becker/heimdall-rs/releases/download/$TARGET_VERSION/heimdall-linux-amd64" -o heimdall elif [[ "$OSTYPE" == "darwin"* ]]; then ensure curl -k -L -s --compressed "https://github.com/Jon-Becker/heimdall-rs/releases/download/$TARGET_VERSION/heimdall-macos-amd64" -o heimdall else echo "bifrost: unsupported operating system: $OSTYPE" exit 1 fi echo "bifrost: installing binary." # make the binary executable ensure chmod +x heimdall else # if nightly, we need to update cargo.toml versions (hacky lol) if [ -n "$NIGHTLY_CHANNEL" ]; then find . -name 'Cargo.toml' -type f | while read -r file; do set_version "$file" "$nightly_version" done fi # try-catch build system { { RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true cargo install --path ./crates/cli --bins --locked --force --root $BIFROST_PATH } || { echo "bifrost: falling back to 0.6.0 >= version < 0.8.0 build system." RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true cargo install --path ./cli --bins --locked --force --root $BIFROST_PATH } } || { echo "bifrost: falling back to old legacy system." RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true ensure cargo install --path ./heimdall --locked --force --root $BIFROST_PATH } fi echo "bifrost: installation complete." } # list all available versions of heimdall versions() { if [ "$NIGHTLY_CHANNEL" = true ]; then msg="Available versions of Heimdall (including nightly builds):" tag_filter="cat" # Do not filter any tags else msg="Available versions of Heimdall:" tag_filter="grep -v '+nightly'" # Exclude nightly builds fi cat 1>&2 <0; i--) print line[i]}' } # usage prints the usage message usage() { cat 1>&2 < OPTIONS: -h, --help Print help information -u, --update Update bifrost to the latest version -B, --binary Install a precompiled binary instead of building from source -v, --version Install a specific version -l, --list List all available versions FLAGS: +nightly Install the latest nightly build EOF } # ensure runs a command and exits if it fails ensure() { if ! "$@"; then echo "bifrost: required command '$*' failed."; exit 1; fi } # ensure an apt package is installed ensure_apt_package() { # if we are not on a linux system, return success if [[ "$OSTYPE" != "linux-gnu"* ]]; then return fi # if the package is not installed, install it if ! dpkg -l | grep -q $1; then echo "bifrost: installing $1." ensure sudo apt-get install -y $1 else echo "bifrost: $1 is already installed." fi } # command_exists checks if a command exists command_exists() { command -v "$1" > /dev/null 2>&1 } # requires_cmd checks if a command exists and exits if it doesn't requires_cmd() { if ! command_exists "$1"; then echo "bifrost: '$1' is required but not installed on this system" exit 1 fi } # set the version of $1 to $2 set_version() { local file=$1 local version=$2 sed -i "" "s/^version.*/version = \"${version}\"/" $file } # run main main "$@" || exit 1