|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md). |
| 3 | +# Licensed under the Apache License, Version 2.0 (see LICENSE). |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +COLOR_RED="\x1b[31m" |
| 8 | +COLOR_GREEN="\x1b[32m" |
| 9 | +COLOR_YELLOW="\x1b[33m" |
| 10 | +COLOR_RESET="\x1b[0m" |
| 11 | + |
| 12 | +function log() { |
| 13 | + echo -e "$@" 1>&2 |
| 14 | +} |
| 15 | + |
| 16 | +function die() { |
| 17 | + (($# > 0)) && log "${COLOR_RED}$*${COLOR_RESET}" |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +function green() { |
| 22 | + (($# > 0)) && log "${COLOR_GREEN}$*${COLOR_RESET}" |
| 23 | +} |
| 24 | + |
| 25 | +function warn() { |
| 26 | + (($# > 0)) && log "${COLOR_YELLOW}$*${COLOR_RESET}" |
| 27 | +} |
| 28 | + |
| 29 | +function check_cmd() { |
| 30 | + local cmd="$1" |
| 31 | + command -v "$cmd" > /dev/null || die "This script requires the ${cmd} binary to be on the PATH." |
| 32 | +} |
| 33 | + |
| 34 | +help_url="https://www.pantsbuild.org/docs/getting-help" |
| 35 | + |
| 36 | +_GC=() |
| 37 | + |
| 38 | +function gc() { |
| 39 | + if (($# > 0)); then |
| 40 | + check_cmd rm |
| 41 | + _GC+=("$@") |
| 42 | + else |
| 43 | + rm -rf "${_GC[@]}" |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +trap gc EXIT |
| 48 | + |
| 49 | +check_cmd uname |
| 50 | + |
| 51 | +function calculate_os() { |
| 52 | + local os |
| 53 | + |
| 54 | + os="$(uname -s)" |
| 55 | + if [[ "${os}" =~ [Ll]inux ]]; then |
| 56 | + echo linux |
| 57 | + elif [[ "${os}" =~ [Dd]arwin ]]; then |
| 58 | + echo macos |
| 59 | + elif [[ "${os}" =~ [Ww]in|[Mm][Ii][Nn][Gg] ]]; then |
| 60 | + # Powershell reports something like: Windows_NT |
| 61 | + # Git bash reports something like: MINGW64_NT-10.0-22621 |
| 62 | + echo windows |
| 63 | + else |
| 64 | + die "Pants is not supported on this operating system (${os}). Please reach out to us at ${help_url} for help." |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +OS="$(calculate_os)" |
| 69 | + |
| 70 | +check_cmd basename |
| 71 | +if [[ "${OS}" == "windows" ]]; then |
| 72 | + check_cmd pwsh |
| 73 | +else |
| 74 | + check_cmd curl |
| 75 | +fi |
| 76 | + |
| 77 | +function fetch() { |
| 78 | + local url="$1" |
| 79 | + local dest_dir="$2" |
| 80 | + |
| 81 | + local dest |
| 82 | + dest="${dest_dir}/$(basename "${url}")" |
| 83 | + |
| 84 | + if [[ "${OS}" == "windows" ]]; then |
| 85 | + pwsh -c "Invoke-WebRequest -OutFile $dest -Uri $url" |
| 86 | + else |
| 87 | + curl --proto '=https' --tlsv1.2 -sSfL -o "${dest}" "${url}" |
| 88 | + fi |
| 89 | +} |
| 90 | + |
| 91 | +if [[ "${OS}" == "macos" ]]; then |
| 92 | + check_cmd shasum |
| 93 | +else |
| 94 | + check_cmd sha256sum |
| 95 | +fi |
| 96 | + |
| 97 | +function sha256() { |
| 98 | + if [[ "${OS}" == "macos" ]]; then |
| 99 | + shasum --algorithm 256 "$@" |
| 100 | + else |
| 101 | + sha256sum "$@" |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +check_cmd mktemp |
| 106 | + |
| 107 | +function install_from_url() { |
| 108 | + local url="$1" |
| 109 | + local dest="$2" |
| 110 | + |
| 111 | + local workdir |
| 112 | + workdir="$(mktemp -d)" |
| 113 | + gc "${workdir}" |
| 114 | + |
| 115 | + fetch "${url}.sha256" "${workdir}" |
| 116 | + fetch "${url}" "${workdir}" |
| 117 | + ( |
| 118 | + cd "${workdir}" |
| 119 | + sha256 -c --status ./*.sha256 || |
| 120 | + die "Download from ${url} did not match the fingerprint at ${url}.sha256" |
| 121 | + ) |
| 122 | + rm "${workdir}/"*.sha256 |
| 123 | + if [[ "${OS}" == "macos" ]]; then |
| 124 | + mkdir -p "$(dirname "${dest}")" |
| 125 | + install -m 755 "${workdir}/"* "${dest}" |
| 126 | + else |
| 127 | + install -D -m 755 "${workdir}/"* "${dest}" |
| 128 | + fi |
| 129 | +} |
| 130 | + |
| 131 | +function calculate_arch() { |
| 132 | + local arch |
| 133 | + |
| 134 | + arch="$(uname -m)" |
| 135 | + if [[ "${arch}" =~ x86[_-]64 ]]; then |
| 136 | + echo x86_64 |
| 137 | + elif [[ "${arch}" =~ arm64|aarch64 ]]; then |
| 138 | + echo aarch64 |
| 139 | + else |
| 140 | + die "Pants is not supported for this chip architecture (${arch}). Please reach out to us at ${help_url} for help." |
| 141 | + fi |
| 142 | +} |
| 143 | + |
| 144 | +check_cmd cat |
| 145 | + |
| 146 | +function usage() { |
| 147 | + cat << EOF |
| 148 | +Usage: $0 |
| 149 | +
|
| 150 | +Installs the pants launcher binary. |
| 151 | +
|
| 152 | +You only need to run this once on a machine when you do not have "pants" |
| 153 | +available to run yet. |
| 154 | +
|
| 155 | +The pants binary takes care of managing and running the underlying |
| 156 | +Pants version configured in "pants.toml" in the surrounding Pants-using |
| 157 | +project. |
| 158 | +
|
| 159 | +Once installed, if you want to update your "pants" launcher binary, use |
| 160 | +"SCIE_BOOT=update pants" to get the latest release or |
| 161 | +"SCIE_BOOT=update pants --help" to learn more options. |
| 162 | +
|
| 163 | +-h | --help: Print this help message. |
| 164 | +
|
| 165 | +-d | --bin-dir: |
| 166 | + The directory to install the scie-pants binary in, "~/bin" by default. |
| 167 | +
|
| 168 | +-b | --base-name: |
| 169 | + The name to use for the scie-pants binary, "pants" by default. |
| 170 | +
|
| 171 | +-V | --version: |
| 172 | + The version of the scie-pants binary to install, the latest version by default. |
| 173 | + The available versions can be seen at: |
| 174 | + https://github.com/pantsbuild/scie-pants/releases |
| 175 | +
|
| 176 | +EOF |
| 177 | +} |
| 178 | + |
| 179 | +bin_dir="${HOME}/bin" |
| 180 | +base_name="pants" |
| 181 | +version="latest/download" |
| 182 | +while (($# > 0)); do |
| 183 | + case "$1" in |
| 184 | + --help | -h) |
| 185 | + usage |
| 186 | + exit 0 |
| 187 | + ;; |
| 188 | + --bin-dir | -d) |
| 189 | + bin_dir="$2" |
| 190 | + shift |
| 191 | + ;; |
| 192 | + --base-name | -b) |
| 193 | + base_name="$2" |
| 194 | + shift |
| 195 | + ;; |
| 196 | + --version | -V) |
| 197 | + version="download/v$2" |
| 198 | + shift |
| 199 | + ;; |
| 200 | + *) |
| 201 | + usage |
| 202 | + die "Unexpected argument $1\n" |
| 203 | + ;; |
| 204 | + esac |
| 205 | + shift |
| 206 | +done |
| 207 | + |
| 208 | +ARCH="$(calculate_arch)" |
| 209 | +URL="https://github.com/pantsbuild/scie-pants/releases/${version}/scie-pants-${OS}-${ARCH}" |
| 210 | +dest="${bin_dir}/${base_name}" |
| 211 | + |
| 212 | +log "Downloading and installing the pants launcher ..." |
| 213 | +install_from_url "${URL}" "${dest}" |
| 214 | +green "Installed the pants launcher from ${URL} to ${dest}" |
| 215 | +if ! command -v "${base_name}" > /dev/null; then |
| 216 | + warn "${dest} is not on the PATH." |
| 217 | + log "You'll either need to invoke ${dest} explicitly or else add ${bin_dir} to your shell's PATH." |
| 218 | +fi |
| 219 | + |
| 220 | +green "\nRunning \`pants\` in a Pants-enabled repo will use the version of Pants configured for that repo." |
| 221 | +green "In a repo not yet Pants-enabled, it will prompt you to set up Pants for that repo." |
0 commit comments