Skip to content

Commit

Permalink
Merge pull request #49 from XaF/patch-1
Browse files Browse the repository at this point in the history
Add support for arm64 on top of x86_64
  • Loading branch information
spencergilbert committed Jul 23, 2024
2 parents a47c152 + 643e3eb commit 98c93b0
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- name: Install missing dependencies
if: matrix.os == 'macos-latest'
run: brew install coreutils

- name: asdf_plugin_test
uses: asdf-vm/actions/plugin-test@v3
with:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- [Dependencies](#dependencies)
- [Install](#install)
- [Environment Variables](#environment-variables)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -47,6 +48,10 @@ vector --help
Check [asdf](https://github.com/asdf-vm/asdf) readme for more instructions on how to
install & manage versions.

# Environment Variables

- `ASDF_VECTOR_DISABLE_ROSETTA`: Set to any non-empty value to disable defaulting back to using Rosetta 2 on Apple Silicon Macs if a native version is not available. This is useful if you do not want to install binaries that require Rosetta 2 to run.

# Contributing

Contributions of any kind welcome! See the [contributing guide](contributing.md).
Expand Down
8 changes: 3 additions & 5 deletions bin/download
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ source "${plugin_dir}/lib/utils.bash"

mkdir -p "$ASDF_DOWNLOAD_PATH"

platform=$(get_platform)

# TODO: Ensure this covers all variants Vector builds for
release_file="$ASDF_DOWNLOAD_PATH/$TOOL_NAME-$ASDF_INSTALL_VERSION.tar.gz"
asset=$(get_compatible_asset "$ASDF_INSTALL_VERSION")
release_file="$ASDF_DOWNLOAD_PATH/${asset}"

# Download tar.gz file to the download directory
download_release "$ASDF_INSTALL_VERSION" "$release_file" "$platform"
download_release "$ASDF_INSTALL_VERSION" "$release_file" "$asset"

# Extract contents of tar.gz file into the download directory
tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=2 || fail "Could not extract $release_file"
Expand Down
147 changes: 133 additions & 14 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TOOL_NAME="vector"
TOOL_TEST="vector --version"

fail() {
echo -e "asdf-$TOOL_NAME: $*"
echo >&2 -e "asdf-$TOOL_NAME: $*"
exit 1
}

Expand All @@ -32,27 +32,146 @@ list_all_versions() {
list_github_tags
}

get_platform() {
if [[ $(uname -s) == "Darwin" ]]; then
echo "apple-$(uname | tr '[:upper:]' '[:lower:]')"
elif [[ $(uname -s) == "Linux" ]]; then
echo "unknown-$(uname | tr '[:upper:]' '[:lower:]')-gnu"
else
echo >&2 'Platform not supported' && exit 1
github_api_curl() {
local gh_opts
local gh_url="${1}"

# Prepare options to do GH api requests
gh_opts=("--location")
gh_opts+=("--header" "Accept: application/vnd.github+json")
gh_opts+=("--header" "X-GitHub-Api-Version: 2022-11-28")

# Make the call
curl "${curl_opts[@]}" "${gh_opts[@]}" "$gh_url"
}

check_rosetta_installed() {
if [[ -n "${ASDF_VECTOR_DISABLE_ROSETTA:-}" ]] ||
! [[ -x /usr/bin/pgrep ]] ||
! [[ -x /usr/bin/arch ]] ||
! [[ -x /usr/bin/uname ]]; then
return 1
fi

local platform=$(uname -s | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
if [[ "$platform" != "darwin" ]]; then
return 1
fi

local oahd_pid=$(/usr/bin/pgrep oahd || true)
if [[ -z "$oahd_pid" ]]; then
return 1
fi

local arch=$(/usr/bin/arch -x86_64 /usr/bin/uname -m 2>/dev/null || true)
if [[ "$arch" != "x86_64" ]]; then
return 1
fi

return 0
}

get_compatible_asset() {
if [[ $# -eq 0 ]]; then
echo >&2 "usage: get_compatible_asset <version>"
exit 1
fi

local version="${1}"

# Prepare a grep depending on the current platform
local platform_grep
local platform=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$platform" in
darwin)
platform_grep='\b(apple|darwin)\b'
;;
linux)
platform_grep='\b(linux)\b'
;;
*)
echo >&2 "Platform '$platform' not supported"
exit 1
;;
esac

# Prepare a grep depending on the current architecture
local arch_grep
local arch_grep_rosetta=""
local arch=$(uname -m | tr '[:upper:]' '[:lower:]')
case "$arch" in
aarch64 | arm64)
arch_grep='\b(aarch64|arm64)\b'
if [[ "$platform" == "darwin" ]] && check_rosetta_installed; then
arch_grep_rosetta='\b(x64|x86_64|x86-64)\b'
fi
;;
x86_64 | x64 | x86-64)
arch_grep='\b(x64|x86_64|x86-64)\b'
;;
*)
echo >&2 "Architecture '${arch}' not supported"
exit 1
;;
esac

# Get the assets from the release version
set +e
local release_json
release_json=$(github_api_curl "https://api.github.com/repos/vectordotdev/vector/releases/tags/v${version}")
[[ $? == 0 ]] || fail "Unable to get release data from GitHub"
set -e

# List the asset names, and filter only the .tar.gz files
local assets
assets=$(echo "$release_json" | jq --raw-output '.assets | map(.name) | .[] | select(endswith(".tar.gz"))' || true)
[[ -n "$assets" ]] || fail "No asset found for release $version"

# Go over the assets and find the ones that match both the architecture and the platform
local matching_assets
matching_assets=$(echo "$assets" | grep -E "$platform_grep" | grep -E "$arch_grep" || true)
if [[ -z "$matching_assets" ]] && [[ -n "$arch_grep_rosetta" ]]; then
echo >&2 "No asset found for release $version for $(uname -sm), trying with Rosetta"
matching_assets=$(echo "$assets" | grep -E "$platform_grep" | grep -E "$arch_grep_rosetta" || true)
fi
[[ -n "$matching_assets" ]] || fail "No asset found for release $version for $(uname -sm)"

# If more than one file, take only the first one
echo "$matching_assets" | head -n1
}

download_release() {
local version filename url
local version filename asset url checksums_url checksums_dir
version="$1"
filename="$2"
platform="$3"
asset="$3"

url="$GH_REPO/releases/download/v${version}/${asset}"

# TODO: Add support for all the platforms and arches Vector builds for
url="$GH_REPO/releases/download/v${version}/vector-${version}-x86_64-${platform}.tar.gz"
checksums_url="$GH_REPO/releases/download/v${version}/vector-${version}-SHA256SUMS"
checksums_dir="$(dirname "$filename")"

# Download the asset
echo "* Downloading $TOOL_NAME release $version..."
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"
set +e
status_code=$(curl "${curl_opts[@]}" -w "%{http_code}" -o "$filename" -C - "$url" 2>/dev/null)
if [[ $? -ne 0 ]] && [[ "${status_code}" != "416" ]]; then
fail "Could not download $url: status $status_code"
fi
set -e

# Download the SHA256
if command -v sha256sum >/dev/null || command -v shasum >/dev/null; then
echo "* Verifying checksums..."
curl "${curl_opts[@]}" -o "${checksums_dir}/all_files.sha256" "$checksums_url" || fail "Could not download checksums"
cat "${checksums_dir}/all_files.sha256" | grep "${asset}" >"${checksums_dir}/${asset}.sha256"
[[ -n "$(cat "${checksums_dir}/${asset}.sha256")" ]] || fail "Could not find checksum for asset ${asset}"
sha256sum=$(command -v sha256sum || echo "shasum --algorithm 256") # from https://github.com/XaF/omni
(cd "${checksums_dir}/" && "$sha256sum" --check "${asset}.sha256") || fail "Checksum for asset ${asset} failed to match"
rm -f "${checksums_dir}/"*.sha256
else
echo "* Could not find binary to check SHA-256 sums; skipping verification"
fi
}

install_version() {
Expand All @@ -66,7 +185,7 @@ install_version() {

(
mkdir -p "$install_path"
cp -r "$ASDF_DOWNLOAD_PATH"/* "$install_path"
cp -r "${ASDF_DOWNLOAD_PATH}"/* "$install_path"

local tool_cmd
tool_cmd="$(echo "$TOOL_TEST" | cut -d' ' -f1)"
Expand Down

0 comments on commit 98c93b0

Please sign in to comment.