|
| 1 | +#!/usr/bin/env bash |
| 2 | +#------------------------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. |
| 5 | +#------------------------------------------------------------------------------------------------------------- |
| 6 | +# |
| 7 | +# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/github.md |
| 8 | +# Maintainer: The VS Code and Codespaces Teams |
| 9 | + |
| 10 | +CLI_VERSION=${VERSION:-"latest"} |
| 11 | + |
| 12 | +set -e |
| 13 | + |
| 14 | +if [ "$(id -u)" -ne 0 ]; then |
| 15 | + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +# Figure out correct version of a three part version number is not passed |
| 20 | +find_version_from_git_tags() { |
| 21 | + local variable_name=$1 |
| 22 | + local requested_version=${!variable_name} |
| 23 | + if [ "${requested_version}" = "none" ]; then return; fi |
| 24 | + local repository=$2 |
| 25 | + local prefix=${3:-"tags/v"} |
| 26 | + local separator=${4:-"."} |
| 27 | + local last_part_optional=${5:-"false"} |
| 28 | + if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then |
| 29 | + local escaped_separator=${separator//./\\.} |
| 30 | + local last_part |
| 31 | + if [ "${last_part_optional}" = "true" ]; then |
| 32 | + last_part="(${escaped_separator}[0-9]+)?" |
| 33 | + else |
| 34 | + last_part="${escaped_separator}[0-9]+" |
| 35 | + fi |
| 36 | + local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$" |
| 37 | + local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)" |
| 38 | + if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then |
| 39 | + declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)" |
| 40 | + else |
| 41 | + set +e |
| 42 | + declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")" |
| 43 | + set -e |
| 44 | + fi |
| 45 | + fi |
| 46 | + if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then |
| 47 | + echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2 |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + echo "${variable_name}=${!variable_name}" |
| 51 | +} |
| 52 | + |
| 53 | +# Use semver logic to decrement a version number then look for the closest match |
| 54 | +find_prev_version_from_git_tags() { |
| 55 | + local variable_name=$1 |
| 56 | + local current_version=${!variable_name} |
| 57 | + local repository=$2 |
| 58 | + # Normally a "v" is used before the version number, but support alternate cases |
| 59 | + local prefix=${3:-"tags/v"} |
| 60 | + # Some repositories use "_" instead of "." for version number part separation, support that |
| 61 | + local separator=${4:-"."} |
| 62 | + # Some tools release versions that omit the last digit (e.g. go) |
| 63 | + local last_part_optional=${5:-"false"} |
| 64 | + # Some repositories may have tags that include a suffix (e.g. actions/node-versions) |
| 65 | + local version_suffix_regex=$6 |
| 66 | + # Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios. |
| 67 | + set +e |
| 68 | + major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')" |
| 69 | + minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')" |
| 70 | + breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')" |
| 71 | + |
| 72 | + if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then |
| 73 | + ((major=major-1)) |
| 74 | + declare -g ${variable_name}="${major}" |
| 75 | + # Look for latest version from previous major release |
| 76 | + find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}" |
| 77 | + # Handle situations like Go's odd version pattern where "0" releases omit the last part |
| 78 | + elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then |
| 79 | + ((minor=minor-1)) |
| 80 | + declare -g ${variable_name}="${major}.${minor}" |
| 81 | + # Look for latest version from previous minor release |
| 82 | + find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}" |
| 83 | + else |
| 84 | + ((breakfix=breakfix-1)) |
| 85 | + if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then |
| 86 | + declare -g ${variable_name}="${major}.${minor}" |
| 87 | + else |
| 88 | + declare -g ${variable_name}="${major}.${minor}.${breakfix}" |
| 89 | + fi |
| 90 | + fi |
| 91 | + set -e |
| 92 | +} |
| 93 | + |
| 94 | +install_using_github() { |
| 95 | + check_packages wget |
| 96 | + arch=$(dpkg --print-architecture) |
| 97 | + |
| 98 | + find_version_from_git_tags CLI_VERSION https://github.com/github/copilot-cli |
| 99 | + cli_filename="copilot_linux_${arch}.tar.gz" |
| 100 | + |
| 101 | + mkdir -p /tmp/copilotcli |
| 102 | + pushd /tmp/copilotcli |
| 103 | + wget -q --show-progress --progress=dot:giga https://github.com/github/copilot-cli/releases/download/v${CLI_VERSION}/${cli_filename} |
| 104 | + exit_code=$? |
| 105 | + set -e |
| 106 | + if [ "$exit_code" != "0" ]; then |
| 107 | + # Handle situation where git tags are ahead of what was is available to actually download |
| 108 | + echo "(!) copilot-cli version ${CLI_VERSION} failed to download. Attempting to fall back one version to retry..." |
| 109 | + find_prev_version_from_git_tags CLI_VERSION https://github.com/github/copilot-cli |
| 110 | + wget -q --show-progress --progress=dot:giga https://github.com/github/copilot-cli/releases/download/v${CLI_VERSION}/${cli_filename} |
| 111 | + fi |
| 112 | + |
| 113 | + tar -xzf /tmp/copilotcli/${cli_filename} |
| 114 | + mv copilot /usr/local/bin/copilot |
| 115 | + popd |
| 116 | + rm -rf /tmp/copilotcli |
| 117 | +} |
| 118 | + |
| 119 | +# Install the Copilot CLI |
| 120 | +echo "Downloading Copilot CLI..." |
| 121 | + |
| 122 | +install_using_github |
| 123 | + |
0 commit comments