|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# scopelintup bootstrap installer |
| 5 | +# Usage: curl -L https://raw.githubusercontent.com/ScopeLift/scopelint/main/scopelintup/install | bash |
| 6 | + |
| 7 | +SCOPELINT_DIR="${SCOPELINT_DIR:-$HOME/.scopelint}" |
| 8 | +SCOPELINT_BIN_DIR="$SCOPELINT_DIR/bin" |
| 9 | +REPO="ScopeLift/scopelint" |
| 10 | +SCOPELINTUP_URL="https://raw.githubusercontent.com/$REPO/main/scopelintup/scopelintup" |
| 11 | + |
| 12 | +say() { printf "\033[1;32mscopelintup\033[0m: %s\n" "$*"; } |
| 13 | +err() { printf "\033[1;31mscopelintup\033[0m: error: %s\n" "$*" >&2; exit 1; } |
| 14 | + |
| 15 | +need_cmd() { |
| 16 | + command -v "$1" > /dev/null 2>&1 || err "need '$1' (command not found)" |
| 17 | +} |
| 18 | + |
| 19 | +need_cmd curl |
| 20 | + |
| 21 | +# 1. Create directory structure. |
| 22 | +say "creating $SCOPELINT_DIR ..." |
| 23 | +mkdir -p "$SCOPELINT_BIN_DIR" |
| 24 | +mkdir -p "$SCOPELINT_DIR/versions" |
| 25 | + |
| 26 | +# 2. Download scopelintup. |
| 27 | +say "downloading scopelintup..." |
| 28 | +curl -fsSL -o "$SCOPELINT_DIR/scopelintup" "$SCOPELINTUP_URL" |
| 29 | +chmod +x "$SCOPELINT_DIR/scopelintup" |
| 30 | + |
| 31 | +# 3. Symlink scopelintup into bin dir. |
| 32 | +ln -sf "$SCOPELINT_DIR/scopelintup" "$SCOPELINT_BIN_DIR/scopelintup" |
| 33 | + |
| 34 | +# 4. Write the shim by invoking scopelintup's write_shim (just generate inline). |
| 35 | +cat > "$SCOPELINT_BIN_DIR/scopelint" <<'SHIM' |
| 36 | +#!/usr/bin/env bash |
| 37 | +set -euo pipefail |
| 38 | +
|
| 39 | +SCOPELINT_DIR="${SCOPELINT_DIR:-$HOME/.scopelint}" |
| 40 | +
|
| 41 | +resolve_from_file() { |
| 42 | + local dir="$PWD" |
| 43 | + while [[ "$dir" != "/" ]]; do |
| 44 | + if [[ -f "$dir/.scopelint-version" ]]; then |
| 45 | + cat "$dir/.scopelint-version" |
| 46 | + return |
| 47 | + fi |
| 48 | + dir="$(dirname "$dir")" |
| 49 | + done |
| 50 | +} |
| 51 | +
|
| 52 | +version=$(resolve_from_file) |
| 53 | +
|
| 54 | +if [[ -z "${version:-}" ]]; then |
| 55 | + version="${SCOPELINT_VERSION:-}" |
| 56 | +fi |
| 57 | +
|
| 58 | +if [[ -z "${version:-}" && -f "$SCOPELINT_DIR/.current_version" ]]; then |
| 59 | + version=$(cat "$SCOPELINT_DIR/.current_version") |
| 60 | +fi |
| 61 | +
|
| 62 | +if [[ -z "${version:-}" ]]; then |
| 63 | + echo "scopelint: no version configured." >&2 |
| 64 | + echo " Run 'scopelintup' to install the latest version," >&2 |
| 65 | + echo " or create a .scopelint-version file." >&2 |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | +
|
| 69 | +[[ "$version" == v* ]] || version="v$version" |
| 70 | +
|
| 71 | +binary="$SCOPELINT_DIR/versions/$version/scopelint" |
| 72 | +if [[ ! -x "$binary" ]]; then |
| 73 | + echo "scopelint: version $version is not installed." >&2 |
| 74 | + echo " Run 'scopelintup --version $version' to install it." >&2 |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | +
|
| 78 | +exec "$binary" "$@" |
| 79 | +SHIM |
| 80 | +chmod +x "$SCOPELINT_BIN_DIR/scopelint" |
| 81 | + |
| 82 | +# 5. Add to PATH in shell profiles if not already present. |
| 83 | +add_to_path() { |
| 84 | + local profile="$1" |
| 85 | + if [[ -f "$profile" ]] && grep -q 'scopelint/bin' "$profile" 2>/dev/null; then |
| 86 | + return |
| 87 | + fi |
| 88 | + # Only touch files that already exist or the most common default. |
| 89 | + [[ -f "$profile" ]] || return |
| 90 | + echo >> "$profile" |
| 91 | + echo '# scopelint version manager' >> "$profile" |
| 92 | + echo 'export PATH="$HOME/.scopelint/bin:$PATH"' >> "$profile" |
| 93 | + say "added ~/.scopelint/bin to PATH in $profile" |
| 94 | +} |
| 95 | + |
| 96 | +case "$(basename "${SHELL:-bash}")" in |
| 97 | + zsh) add_to_path "$HOME/.zshrc" ;; |
| 98 | + bash) |
| 99 | + # Prefer .bashrc, fall back to .bash_profile on macOS. |
| 100 | + if [[ -f "$HOME/.bashrc" ]]; then |
| 101 | + add_to_path "$HOME/.bashrc" |
| 102 | + elif [[ -f "$HOME/.bash_profile" ]]; then |
| 103 | + add_to_path "$HOME/.bash_profile" |
| 104 | + fi |
| 105 | + ;; |
| 106 | + fish) |
| 107 | + # fish uses a different mechanism; just inform the user. |
| 108 | + say "add 'set -gx PATH \$HOME/.scopelint/bin \$PATH' to your fish config" |
| 109 | + ;; |
| 110 | + *) add_to_path "$HOME/.profile" ;; |
| 111 | +esac |
| 112 | + |
| 113 | +# 6. Optionally install the latest scopelint. |
| 114 | +say "" |
| 115 | +say "scopelintup installed successfully!" |
| 116 | +say "" |
| 117 | +say "To install the latest scopelint, run:" |
| 118 | +say " scopelintup" |
| 119 | +say "" |
| 120 | +say "If 'scopelintup' is not found, start a new terminal or run:" |
| 121 | +say " export PATH=\"\$HOME/.scopelint/bin:\$PATH\"" |
0 commit comments