github.com/please-build/puku@v1.7.3-0.20240516143641-f7d7f4941f57/pleasew (about)

     1  #!/bin/sh
     2  
     3  set -e
     4  set -u
     5  
     6  ESC="$(printf '\033')"
     7  
     8  if [ "${NOCOLOR+x}" != 'x' ] || [ "${NO_COLOR+x}" != 'x' ]; then
     9      RED="${ESC}[31m"
    10      GREEN="${ESC}[32m"
    11      YELLOW="${ESC}[33m"
    12      RESET="${ESC}[0m"
    13  else
    14      RED=''
    15      GREEN=''
    16      YELLOW=''
    17      RESET=''
    18  fi
    19  
    20  
    21  DEFAULT_URL_BASE='https://get.please.build'
    22  
    23  OS="$(uname)"
    24  
    25  if [ "${OS}" = 'Darwin' ]; then
    26      # switch between mac amd64/arm64
    27      ARCH="$(uname -m)"
    28  else
    29      # default to amd64 on other operating systems
    30      # because we only build intel binaries
    31      ARCH='amd64'
    32  fi
    33  
    34  case "${ARCH}" in
    35      aarch64_be|aarch64|armv8b|armv8l) ARCH='arm64' ;;
    36      x86_64) ARCH='amd64' ;;
    37  esac
    38  
    39  has_command () {
    40      command -v "${1}" > /dev/null 2>&1
    41  }
    42  
    43  get_profile () {
    44      while [ "${#}" -gt 0 ]
    45      do
    46          case "${1}" in
    47              --profile=*) echo "${1#*=}"; return;;
    48              --profile) echo "${2}"; return;;
    49              *) shift;;
    50          esac
    51      done
    52  }
    53  
    54  # Check `PLZ_CONFIG_PROFILE` or fall back to arguments for a profile.
    55  PROFILE="${PLZ_CONFIG_PROFILE:-$(get_profile "${@}")}"
    56  
    57  # Config files on order of precedence high to low.
    58  CONFIGS="$(cat <<- EOS
    59  	.plzconfig.local
    60  	${PROFILE:+.plzconfig.${PROFILE}}
    61  	.plzconfig_${OS}_${ARCH}
    62  	.plzconfig
    63  	${HOME}/.config/please/plzconfig
    64  	/etc/please/plzconfig
    65  EOS
    66  )"
    67  
    68  read_config() {
    69      # Disable globbing to ensure word-splitting is safe.
    70      set -f
    71  
    72      old_ifs="${IFS}"
    73      search_term="${1}"
    74  
    75      IFS='
    76  '
    77  
    78      # This is intended, we *do* want word-splitting here.
    79      # shellcheck disable=2086
    80      set -- ${CONFIGS}
    81  
    82      grep -i "${search_term}" "${@}" 2> /dev/null | head -n 1
    83  
    84      IFS="${old_ifs}"
    85      set +f
    86  }
    87  
    88  # We might already have it downloaded...
    89  LOCATION="$(read_config '^\s*location' | cut -d '=' -f 2 | tr -d ' ')"
    90  
    91  if [ "${LOCATION:+x}" != 'x' ]; then
    92      if [ "${HOME:+x}" != 'x' ]; then
    93          # shellcheck disable=2016
    94          printf >&2 '%b$HOME not set, not sure where to look for Please.%b\n' "${RED}" "${RESET}"
    95          exit 1
    96      fi
    97  
    98      LOCATION="${HOME}/.please"
    99  else
   100      # It can contain a literal ~, need to explicitly handle that.
   101      LOCATION="$(echo "${LOCATION}" | sed "s|~|${HOME}|")"
   102  fi
   103  
   104  # If this exists at any version, let it handle any update.
   105  TARGET="${LOCATION}/please"
   106  
   107  if [ -f "${TARGET}" ]; then
   108      # shellcheck disable=2086
   109      exec "${TARGET}" ${PLZ_ARGS:-} "${@}"
   110  fi
   111  
   112  URL_BASE="$(read_config '^\s*downloadlocation' | cut -d '=' -f 2 | tr -d ' ')"
   113  
   114  if [ "${URL_BASE:+x}" != 'x' ]; then
   115      URL_BASE="${DEFAULT_URL_BASE}"
   116  fi
   117  
   118  URL_BASE="${URL_BASE%/}"
   119  
   120  VERSION="$(read_config '^\s*version[^a-z]')"
   121  VERSION="${VERSION#*=}"                    # Strip until after first =
   122  VERSION="$(echo "${VERSION}" | tr -d ' ')" # Remove all spaces
   123  VERSION="${VERSION#>=}"                    # Strip any initial >=
   124  
   125  if has_command curl; then
   126      TRANSFER_TOOL='curl'
   127      TRANSFER_SILENT_OPTS='-fsSL'
   128      TRANSFER_PROGRESS_OPTS='-fSL'
   129  elif has_command wget; then
   130      TRANSFER_TOOL='wget'
   131      TRANSFER_SILENT_OPTS='-qO-'
   132      TRANSFER_PROGRESS_OPTS='-O-'
   133  else
   134      printf >&2 '%bUnable to find a command for network operations%b\n' "${RED}" "${RESET}"
   135      printf >&2 'Please install either curl or wget\n'
   136      exit 1
   137  fi
   138  
   139  if [ "${VERSION:+x}" != 'x' ]; then
   140      printf >&2 "%bCan't determine version, will use latest.%b\n" "${YELLOW}" "${RESET}"
   141      VERSION=$(${TRANSFER_TOOL} ${TRANSFER_SILENT_OPTS} "${URL_BASE}"/latest_version)
   142  fi
   143  
   144  # Find the os / arch to download. You can do this quite nicely with go env
   145  # but we use this script on machines that don't necessarily have Go itself.
   146  if [ "${OS}" = 'Linux' ]; then
   147      GOOS='linux'
   148  elif [ "${OS}" = 'Darwin' ]; then
   149      GOOS='darwin'
   150  elif [ "${OS}" = 'FreeBSD' ]; then
   151      GOOS='freebsd'
   152  else
   153      printf >&2 '%bUnknown operating system %s%b\n' "${RED}" "${OS}" "${RESET}"
   154      exit 1
   155  fi
   156  
   157  PLEASE_URL="${URL_BASE}/${GOOS}_${ARCH}/${VERSION}/please_${VERSION}.tar.xz"
   158  DIR="${LOCATION}/${VERSION}"
   159  
   160  # Potentially we could reuse this but it's easier not to really.
   161  if [ ! -d "${DIR}" ]; then
   162      rm -Rf "${DIR}"
   163  fi
   164  
   165  printf >&2 '%bDownloading Please %s to %s...%b\n' "${GREEN}" "${VERSION}" "${DIR}" "${RESET}"
   166  mkdir -p "${DIR}"
   167  ${TRANSFER_TOOL} ${TRANSFER_PROGRESS_OPTS} "${PLEASE_URL}" | tar -xJpf- --strip-components=1 -C "${DIR}"
   168  
   169  # Link it all back up a dir
   170  for x in "${DIR}"/*; do
   171      ln -sf "${x}" "${LOCATION}"
   172  done
   173  
   174  printf >&2 '%bShould be good to go now, running plz...%b\n' "${GREEN}" "${RESET}"
   175  # shellcheck disable=2086
   176  exec "${TARGET}" ${PLZ_ARGS:-} "${@}"