|
| 1 | +#! /usr/bin/env bash |
| 2 | +# Copyright 2025 Raphael Thomazella. All rights reserved. |
| 3 | +# Use of this source code is governed by the BSD-3-Clause |
| 4 | +# license that can be found in the LICENSE file and online |
| 5 | +# at https://opensource.org/license/BSD-3-clause. |
| 6 | +# |
| 7 | +# Installs development tools required by this repo. |
| 8 | +# Self-contained: does not require $BASH_ENV. |
| 9 | +# Interactive shell: prints install instructions and exits. |
| 10 | +# Non-interactive: downloads and installs binaries from GitHub releases. |
| 11 | + |
| 12 | +set -euo pipefail |
| 13 | +shopt -s globstar |
| 14 | + |
| 15 | +### vars and functions ### |
| 16 | + |
| 17 | +msgln() { echo -e "$*"; } |
| 18 | +log() { echo -e "INFO ($0:${1}) ${*:2}" >&2; } |
| 19 | +err() { echo -e "ERROR ($0:${1})" >&2; } |
| 20 | +fatal() { err "$1" "${*:2}"; exit 1; } |
| 21 | + |
| 22 | +trap 'err $LINENO' ERR |
| 23 | + |
| 24 | +readonly install_dir="/usr/local/bin" |
| 25 | + |
| 26 | +print_manual_instructions() { |
| 27 | + msgln "Some tools are missing. Please install them manually:" |
| 28 | + msgln |
| 29 | + msgln " shfmt: https://github.com/mvdan/sh/releases/latest" |
| 30 | + msgln " shellcheck: https://github.com/koalaman/shellcheck/releases/latest" |
| 31 | + msgln |
| 32 | + msgln "On Arch: sudo pacman -S shfmt shellcheck" |
| 33 | + msgln "On macOS: brew install shfmt shellcheck" |
| 34 | +} |
| 35 | + |
| 36 | +latest_version() { |
| 37 | + local repo=$1 |
| 38 | + |
| 39 | + curl -sf "https://api.github.com/repos/$repo/releases/latest" \ |
| 40 | + | grep '"tag_name"' \ |
| 41 | + | sed -E 's/.*"tag_name": "(.*)".*/\1/' |
| 42 | +} |
| 43 | + |
| 44 | +install_shfmt() { |
| 45 | + local version os arch url tmp |
| 46 | + |
| 47 | + log $LINENO "fetching latest shfmt version" |
| 48 | + version=$(latest_version mvdan/sh) |
| 49 | + |
| 50 | + os=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 51 | + arch=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') |
| 52 | + url="https://github.com/mvdan/sh/releases/download/${version}/shfmt_${version}_${os}_${arch}" |
| 53 | + tmp=$(mktemp) |
| 54 | + |
| 55 | + log $LINENO "downloading shfmt $version" |
| 56 | + curl -sfL "$url" -o "$tmp" |
| 57 | + chmod +x "$tmp" |
| 58 | + sudo mv "$tmp" "$install_dir/shfmt" |
| 59 | + log $LINENO "shfmt installed to $install_dir/shfmt" |
| 60 | +} |
| 61 | + |
| 62 | +install_shellcheck() { |
| 63 | + local version os arch tarball url tmp_dir |
| 64 | + |
| 65 | + log $LINENO "fetching latest shellcheck version" |
| 66 | + version=$(latest_version koalaman/shellcheck) |
| 67 | + |
| 68 | + os=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 69 | + arch=$(uname -m) |
| 70 | + tarball="shellcheck-${version}.${os}.${arch}.tar.xz" |
| 71 | + url="https://github.com/koalaman/shellcheck/releases/download/${version}/${tarball}" |
| 72 | + tmp_dir=$(mktemp -d) |
| 73 | + |
| 74 | + log $LINENO "downloading shellcheck $version" |
| 75 | + curl -sfL "$url" | tar -xJ -C "$tmp_dir" |
| 76 | + sudo mv "$tmp_dir/shellcheck-${version}/shellcheck" "$install_dir/shellcheck" |
| 77 | + rm -rf "$tmp_dir" |
| 78 | + log $LINENO "shellcheck installed to $install_dir/shellcheck" |
| 79 | +} |
| 80 | + |
| 81 | +install_tool() { |
| 82 | + local tool=$1 install_fn=$2 |
| 83 | + |
| 84 | + if command -v "$tool" >/dev/null 2>&1; then |
| 85 | + log $LINENO "$tool already installed" |
| 86 | + return |
| 87 | + fi |
| 88 | + |
| 89 | + "$install_fn" |
| 90 | +} |
| 91 | + |
| 92 | +### script ### |
| 93 | + |
| 94 | +missing=false |
| 95 | + |
| 96 | +command -v shfmt >/dev/null 2>&1 || missing=true |
| 97 | +command -v shellcheck >/dev/null 2>&1 || missing=true |
| 98 | + |
| 99 | +if ! $missing; then |
| 100 | + log $LINENO "all tools already installed" |
| 101 | + exit 0 |
| 102 | +fi |
| 103 | + |
| 104 | +# interactive shell: just tell the user |
| 105 | +if [[ $- == *i* ]]; then |
| 106 | + print_manual_instructions |
| 107 | + exit 0 |
| 108 | +fi |
| 109 | + |
| 110 | +# non-interactive: download and install |
| 111 | +install_tool shfmt install_shfmt |
| 112 | +install_tool shellcheck install_shellcheck |
| 113 | + |
| 114 | +msgln "setup complete" |
0 commit comments