|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Pants wrapper script that auto-installs pants if not already available |
| 3 | +# |
| 4 | +# This wrapper is installed to /usr/local/bin/pants during Copilot setup. |
| 5 | +# It allows users to just run "pants" without worrying about installation. |
| 6 | +# |
| 7 | +# How it works: |
| 8 | +# 1. Check if pants is already installed in ~/.local/bin (default install location) |
| 9 | +# 2. If not, automatically run get-pants.sh to install it |
| 10 | +# 3. Then execute pants with all arguments passed through |
| 11 | +# |
| 12 | +# This means users can always run "pants <args>" and it will "just work" |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +GET_PANTS="${GET_PANTS:-}" |
| 17 | + |
| 18 | + |
| 19 | +run_pants () { |
| 20 | + if [[ -x "$HOME/.local/bin/pants" ]]; then |
| 21 | + exec "$HOME/.local/bin/pants" "$@" |
| 22 | + exit 0 |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +find_get_pants () { |
| 27 | + if [[ -z "$GET_PANTS" ]]; then |
| 28 | + # Try to find get-pants.sh |
| 29 | + if [[ -f "get-pants.sh" ]]; then |
| 30 | + GET_PANTS="$(realpath ./get-pants.sh)" |
| 31 | + elif [[ -f "../get-pants.sh" ]]; then |
| 32 | + GET_PANTS="$(realpath ../get-pants.sh)" |
| 33 | + elif [[ -f "/home/runner/work/toolshed/toolshed/get-pants.sh" ]]; then |
| 34 | + GET_PANTS="/home/runner/work/toolshed/toolshed/get-pants.sh" |
| 35 | + else |
| 36 | + echo "Error: Cannot find get-pants.sh. Please set GET_PANTS or run from repository directory." >&2 |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + fi |
| 40 | + if [[ ! -f "$GET_PANTS" ]]; then |
| 41 | + echo "Error: Cannot find get-pants.sh (${GET_PANTS})." >&2 |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +install_pants () { |
| 46 | + echo "Pants not found. Installing pants..." >&2 |
| 47 | + find_get_pants |
| 48 | + echo "Running get-pants.sh from $GET_PANTS..." >&2 |
| 49 | + "${GET_PANTS}" |
| 50 | +} |
| 51 | + |
| 52 | +run_pants "${@}" |
| 53 | +install_pants |
| 54 | +run_pants "${@}" |
| 55 | +echo "Error: pants installation failed" >&2 |
| 56 | +exit 1 |
0 commit comments