bender 0.32.0

A dependency management tool for hardware projects.
#!/usr/bin/env sh

# Copyright (c) 2019 ETH Zurich
# SPDX-License-Identifier: Apache-2.0 OR MIT

# Installer entry point for Bender.
# Routes to the cargo-dist installer (v0.32.0+) or the legacy installer for
# older versions. Pass a version to pin to a specific release, or omit it for
# the latest.
#
# Install location:
#   v0.32.0+ default: $CARGO_HOME/bin (PATH updated by cargo-dist).
#   pre-v0.32.0 default: current directory (legacy behavior).
#   --local  forces current-directory install (no-op for pre-v0.32.0).
#   --global forces $CARGO_HOME/bin install (no-op for v0.32.0+).
#
# Usage:
#   curl --proto '=https' --tlsv1.2 https://pulp-platform.github.io/bender/init -sSf | sh
#   curl --proto '=https' --tlsv1.2 https://pulp-platform.github.io/bender/init -sSf | sh -s -- 0.31.0
#   curl --proto '=https' --tlsv1.2 https://pulp-platform.github.io/bender/init -sSf | sh -s -- --local
#   curl --proto '=https' --tlsv1.2 https://pulp-platform.github.io/bender/init -sSf | sh -s -- --global 0.31.0

set -eu

REPO="pulp-platform/bender"
# Derive the GitHub Pages base from $REPO so a fork only needs to edit one
# variable to retarget both release artifacts and the legacy installer.
PAGES_URL="https://${REPO%%/*}.github.io/${REPO##*/}"
# v0.32.0 is the first release with cargo-dist installers
CARGO_DIST_MIN_MINOR=32

LOCAL=0
GLOBAL=0
VERSION=latest
for arg in "$@"; do
    case "$arg" in
        --local) LOCAL=1 ;;
        --global) GLOBAL=1 ;;
        *) VERSION="$arg" ;;
    esac
done
if [ "$LOCAL" -eq 1 ] && [ "$GLOBAL" -eq 1 ]; then
    echo "init: --local and --global are mutually exclusive" >&2
    exit 1
fi

# Returns 0 (true) if VERSION should use the cargo-dist installer.
# Uses cargo-dist for latest, any v1.x.y+, or v0.32.0+.
# Only falls back to legacy for v0.x.y where x < 32.
uses_cargo_dist() {
    [ "$VERSION" = "latest" ] && return 0
    major=$(printf '%s' "$VERSION" | cut -d. -f1)
    [ "$major" -ge 1 ] && return 0
    minor=$(printf '%s' "$VERSION" | cut -d. -f2)
    [ "$minor" -ge "$CARGO_DIST_MIN_MINOR" ]
}

if uses_cargo_dist; then
    if [ "$VERSION" = "latest" ]; then
        URL="https://github.com/$REPO/releases/latest/download/bender-installer.sh"
    else
        URL="https://github.com/$REPO/releases/download/v$VERSION/bender-installer.sh"
    fi
    if [ "$LOCAL" -eq 1 ]; then
        # --local: have cargo-dist install into a temp dir without touching PATH,
        # then move the binary into the current directory.
        _tmpdir=$(mktemp -d)
        trap 'rm -rf "$_tmpdir"' EXIT
        curl --proto '=https' --tlsv1.2 -LsSf "$URL" | \
            CARGO_DIST_FORCE_INSTALL_DIR="$_tmpdir" INSTALLER_NO_MODIFY_PATH=1 sh
        mv "$_tmpdir/bin/bender" .
    else
        # Default (or --global): cargo-dist installs into $CARGO_HOME/bin and updates PATH.
        curl --proto '=https' --tlsv1.2 -LsSf "$URL" | sh
    fi
elif [ "$GLOBAL" -eq 1 ]; then
    # --global on pre-v0.32.0: legacy installer drops the binary in CWD, so run
    # it inside a temp dir and relocate to $CARGO_HOME/bin to match the v0.32+
    # global install layout. PATH is not modified.
    _dest="${CARGO_HOME:-$HOME/.cargo}/bin"
    mkdir -p "$_dest"
    _tmpdir=$(mktemp -d)
    trap 'rm -rf "$_tmpdir"' EXIT
    ( cd "$_tmpdir" && curl --proto '=https' --tlsv1.2 -LsSf \
        "$PAGES_URL/init-legacy" | sh -s -- "$VERSION" )
    mv "$_tmpdir/bender" "$_dest/"
    echo "Installed bender to $_dest/bender"
    case ":${PATH:-}:" in
        *":$_dest:"*) ;;
        *) echo "Note: '$_dest' is not on PATH; add it to use 'bender' from anywhere." >&2 ;;
    esac
else
    # Default (or --local) on pre-v0.32.0: legacy installer drops binary in CWD.
    curl --proto '=https' --tlsv1.2 -LsSf \
        "$PAGES_URL/init-legacy" | sh -s -- "$VERSION"
fi