Skip to content

Commit 9082ae6

Browse files
committed
feat: faster list-all, move single-command functions out of lib/utils.bash
speedup from asdf-vm/asdf-plugin-template#58
1 parent e1a842c commit 9082ae6

File tree

4 files changed

+239
-200
lines changed

4 files changed

+239
-200
lines changed

bin/download

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,72 @@ current_script_path=${BASH_SOURCE[0]}
66
plugin_dir=$(dirname "$(dirname "${current_script_path}")")
77

88
# shellcheck source=../lib/utils.bash
9-
source "${plugin_dir}/lib/utils.bash"
9+
. "${plugin_dir}/lib/utils.bash"
10+
11+
TOOL_NAME="awscli"
12+
CURL_OPTS=(-fsSL)
1013

1114
mkdir -p "${ASDF_DOWNLOAD_PATH}"
1215

16+
download_source() {
17+
local version download_path major_version os_distribution
18+
version="$1"
19+
download_path="$2"
20+
major_version="${version:0:1}"
21+
22+
if [[ "${major_version}" = "2" ]]; then
23+
source_url="https://awscli.amazonaws.com/awscli-${version}.tar.gz"
24+
filename="awscli.tar.gz"
25+
source_file="${download_path}/${filename}"
26+
curl "${CURL_OPTS[@]}" -o "${source_file}" -C - "${source_url}" || fail "Could not download ${source_url}"
27+
tar -xzf "${source_file}" -C "${download_path}" --strip-components=1 || fail "Could not extract ${source_file}"
28+
rm "${source_file}"
29+
else
30+
fail "asdf-${TOOL_NAME} does not support downloading from source for major version v${major_version}"
31+
fi
32+
}
33+
34+
download_release() {
35+
local version download_path major_version os_distribution os_arch
36+
version="$1"
37+
download_path="$2"
38+
major_version="${version:0:1}"
39+
40+
if [[ "${major_version}" = "1" ]]; then
41+
release_url="https://s3.amazonaws.com/aws-cli/awscli-bundle-${version}.zip"
42+
filename="awscli-bundle.zip"
43+
elif [[ "${major_version}" = "2" ]]; then
44+
os_distribution="$(uname -s)"
45+
os_arch="$(uname -m)"
46+
47+
if [[ "${os_distribution}" = "Linux" ]]; then
48+
if [[ "${os_arch}" = "x86_64" || "${os_arch}" = "aarch64" ]]; then
49+
release_url="https://awscli.amazonaws.com/awscli-exe-linux-${os_arch}-${version}.zip"
50+
filename="awscliv2.zip"
51+
else
52+
fail "asdf-${TOOL_NAME} does not support ${os_arch} on ${os_distribution}"
53+
fi
54+
elif [[ "${os_distribution}" = "Darwin" ]]; then
55+
release_url="https://awscli.amazonaws.com/AWSCLIV2-${version}.pkg"
56+
filename="AWSCLIV2.pkg"
57+
# elif [[ "${os_distribution}" = "Windows_NT" ]]; then
58+
# release_url="https://awscli.amazonaws.com/AWSCLIV2-${version}.msi"
59+
# filename="AWSCLIV2.msi"
60+
else
61+
fail "asdf-${TOOL_NAME} does not support OS distribution ${os_distribution}"
62+
fi
63+
else
64+
fail "asdf-${TOOL_NAME} does not support major version v${version}"
65+
fi
66+
67+
release_file="${download_path}/${filename}"
68+
curl "${CURL_OPTS[@]}" -o "${release_file}" "${release_url}" || fail "Could not download ${release_url}"
69+
if [[ "${release_file: -3}" = "zip" ]]; then
70+
unzip -oq "${release_file}" -d "${download_path}"
71+
rm "${release_file}"
72+
fi
73+
}
74+
1375
if [ "${ASDF_INSTALL_TYPE}" = "version" ]; then
1476
download_release "${ASDF_INSTALL_VERSION}" "${ASDF_DOWNLOAD_PATH}"
1577
elif [ "${ASDF_INSTALL_TYPE}" = "ref" ]; then

bin/install

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,127 @@ current_script_path=${BASH_SOURCE[0]}
66
plugin_dir=$(dirname "$(dirname "${current_script_path}")")
77

88
# shellcheck source=../lib/utils.bash
9-
source "${plugin_dir}/lib/utils.bash"
9+
. "${plugin_dir}/lib/utils.bash"
10+
11+
TOOL_NAME="awscli"
12+
TOOL_TEST="aws --version"
13+
14+
install_source() {
15+
local version download_path install_path major_version os_distribution make_concurrency tool_cmd
16+
version="$1"
17+
download_path="$2"
18+
install_path="$3"
19+
make_concurrency="$4"
20+
major_version="${version:0:1}"
21+
tool_cmd="$(echo "${TOOL_TEST}" | cut -d' ' -f1)"
22+
23+
(
24+
if [[ "${major_version}" = "2" ]]; then
25+
os_distribution="$(uname -s)"
26+
27+
if [[ "${os_distribution}" = "Linux" || "${os_distribution}" = "Darwin" ]]; then
28+
pushd "${download_path}"
29+
./configure --prefix="${install_path}" --with-download-deps --with-install-type=portable-exe
30+
make --jobs "${make_concurrency}"
31+
make install
32+
popd
33+
else
34+
fail "asdf-${TOOL_NAME} does not support installing from source for OS distribution ${os_distribution}"
35+
fi
36+
else
37+
fail "asdf-${TOOL_NAME} does not support installing from source for major version v${major_version}"
38+
fi
39+
40+
test -x "${install_path}/bin/${tool_cmd}" || fail "Expected ${install_path}/bin/${tool_cmd} to be executable."
41+
echo "asdf-${TOOL_NAME} ${version} installation was successful!"
42+
) || (
43+
rm -rf "${install_path}"
44+
fail "An error ocurred while installing awscli ${version}."
45+
)
46+
}
47+
48+
install_release() {
49+
local version download_path install_path major_version os_distribution os_arch
50+
version="$1"
51+
download_path="$2"
52+
install_path="$3"
53+
major_version="${version:0:1}"
54+
55+
(
56+
if [[ "${major_version}" = "1" ]]; then
57+
install_v1_bundled_installer "${download_path}" "${install_path}"
58+
elif [[ "${major_version}" = "2" ]]; then
59+
os_distribution="$(uname -s)"
60+
os_arch="$(uname -m)"
61+
62+
if [[ "${os_distribution}" = "Linux" ]]; then
63+
if [[ "${os_arch}" = "x86_64" || "${os_arch}" = "aarch64" ]]; then
64+
install_v2_linux_bundled_installer "${download_path}" "${install_path}"
65+
else
66+
fail "asdf-${TOOL_NAME} does not support ${os_arch} on ${os_distribution}"
67+
fi
68+
elif [[ "${os_distribution}" = "Darwin" ]]; then
69+
install_v2_macos_bundled_installer "${download_path}" "${install_path}"
70+
elif [[ "${os_distribution}" = "Windows_NT" ]]; then
71+
install_v2_windows_bundled_installer "${download_path}" "${install_path}"
72+
else
73+
fail "asdf-${TOOL_NAME} does not support OS distribution ${os_distribution}"
74+
fi
75+
else
76+
fail "asdf-${TOOL_NAME} does not support major version v${major_version}"
77+
fi
78+
79+
local tool_cmd
80+
tool_cmd="$(echo "${TOOL_TEST}" | cut -d' ' -f1)"
81+
test -x "${install_path}/bin/${tool_cmd}" || fail "Expected ${install_path}/bin/${tool_cmd} to be executable."
82+
echo "asdf-${TOOL_NAME} ${version} installation was successful!"
83+
) || (
84+
rm -rf "${install_path}"
85+
fail "An error ocurred while installing awscli ${version}."
86+
)
87+
}
88+
89+
install_v1_bundled_installer() {
90+
local download_path install_path
91+
download_path="$1"
92+
install_path="$2"
93+
# requires python 3.7+ https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-install.html#cli-chap-install-python
94+
"${download_path}"/awscli-bundle/install --install-dir "${install_path}"
95+
}
96+
97+
install_v2_linux_bundled_installer() {
98+
local download_path install_path
99+
download_path="$1"
100+
install_path="$2"
101+
# requires glibc, groff, less
102+
"${download_path}"/aws/install --install-dir "${install_path}" --bin-dir "${install_path}/bin"
103+
}
104+
105+
# The official AWS CLI directions suggest using installer and a choices.xml
106+
# but I was unable to find a deterministic way to make that work
107+
# so copypasta
108+
install_v2_macos_bundled_installer() {
109+
local download_path install_path
110+
download_path="$1"
111+
install_path="$2"
112+
# requires rosetta on M1 macs
113+
114+
mkdir -p "${install_path}/bin"
115+
pkgutil --expand-full "${download_path}/AWSCLIV2.pkg" "${download_path}/tmp-awscliv2"
116+
cp -a "${download_path}/tmp-awscliv2/aws-cli.pkg/Payload/aws-cli/" "${install_path}"
117+
ln -snf "${install_path}/aws" "${install_path}/bin/aws"
118+
ln -snf "${install_path}/aws_completer" "${install_path}/bin/aws_completer"
119+
rm -rf "${download_path}/tmp-awscliv2"
120+
}
121+
122+
install_v2_windows_bundled_installer() {
123+
local download_path install_path
124+
download_path="$1"
125+
install_path="$2"
126+
127+
# requires curl, msiexec
128+
msiexec.exe /i "${download_path}/AWSCLIV2.msi" "INSTALLDIR=${ASDF_INSTALL_PATH}" MSIINSTALLPERUSER=1
129+
}
10130

11131
# Preserve compatibilty with older ASDF versions
12132
# https://github.com/asdf-vm/asdf/pull/669#issuecomment-600330467

bin/list-all

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,59 @@ current_script_path=${BASH_SOURCE[0]}
66
plugin_dir=$(dirname "$(dirname "${current_script_path}")")
77

88
# shellcheck source=../lib/utils.bash
9-
source "${plugin_dir}/lib/utils.bash"
9+
. "${plugin_dir}/lib/utils.bash"
1010

11-
list_all_versions | sort_versions | xargs echo
11+
GIT_REMINDER_FILE="${ASDF_INSTALL_PATH}/.upgradegit"
12+
GH_REPO="https://github.com/aws/aws-cli"
13+
GIT_VERSION=$(git --version)
14+
GIT_VERSION="${GIT_VERSION##* }"
15+
16+
git_supports_sort() {
17+
awk '{ split($0,a,"."); if ((a[1] < 2) || (a[2] < 18)) { print "1" } else { print "0" } }' <<<"$1"
18+
}
19+
20+
if [ "$(git_supports_sort "${GIT_VERSION}")" -eq 0 ]; then
21+
rm -f "${GIT_REMINDER_FILE}"
22+
GIT_SUPPORTS_SORT=0
23+
else
24+
GIT_SUPPORTS_SORT=1
25+
if [ ! -f "${GIT_REMINDER_FILE}" ]; then
26+
printf "consider upgrading git to a version >= 2.18.0 for faster asdf - you have v%s\n" "${GIT_VERSION}"
27+
touch "${GIT_REMINDER_FILE}"
28+
fi
29+
fi
30+
31+
# NOTE: This is a fallback for if the user's installed version of git doesn't support sorting.
32+
sort_versions() {
33+
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' |
34+
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
35+
}
36+
37+
list_github_tags() {
38+
git ls-remote --tags --refs "${GH_REPO}" |
39+
grep -o 'refs/tags/.*' | cut -d/ -f3- |
40+
sed 's/^v//'
41+
}
42+
43+
list_remote_tags() {
44+
if [ "${GIT_SUPPORTS_SORT}" -eq 0 ]; then
45+
git -c 'versionsort.suffix=a' -c 'versionsort.suffix=b' \
46+
-c 'versionsort.suffix=r' -c 'versionsort.suffix=p' \
47+
-c 'versionsort.suffix=-' -c 'versionsort.suffix=_' \
48+
ls-remote --exit-code --tags --refs --sort="version:refname" "${REPO}" |
49+
awk -F'[/v]' '$NF ~ /^[0-9]+.*/ { print $NF }' || fail "no releases found"
50+
else
51+
git ls-remote --exit-code --tags --refs "${REPO}" |
52+
awk -F'[/v]' '$NF ~ /^[0-9]+.*/ { print $NF }' || fail "no releases found"
53+
fi
54+
}
55+
56+
list_all_versions() {
57+
if [ "${GIT_SUPPORTS_SORT}" -eq 0 ]; then
58+
list_remote_tags
59+
else
60+
list_remote_tags | sort_versions
61+
fi
62+
}
63+
64+
list_all_versions | xargs echo

0 commit comments

Comments
 (0)