Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add example for os & arch detection #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions template/lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,39 @@ list_all_versions() {
}

download_release() {
local version filename url
version="$1"
filename="$2"
local version="$1"
local filename="$2"

# TODO: Adapt the OS & Architecture naming convention for <YOUR TOOL>
# See the release flavours in the /releases page of <YOUR TOOL>
#local uname_s="$(uname -s)"
#local uname_m="$(uname -m)"
#local os arch

#case "$uname_s" in
#FreeBSD) os="freebsd" ;;
#Darwin) os="darwin" ;;
#Linux) os="linux" ;;
#*) fail "OS not supported: $uname_s" ;;
#esac

#case "$uname_m" in
#i?86) arch="386" ;;
#x86_64) arch="amd64" ;;
#aarch64) arch="arm64" ;;
#armv8l) arch="arm64" ;;
#arm64) arch="arm64" ;;
#armv7l) arch="arm" ;;
#mips) arch="mips" ;;
#mipsel) arch="mipsle" ;;
#mips64) arch="mips64" ;;
#mips64el) arch="mips64le" ;;
#*) fail "Architecture not supported: $uname_m" ;;
#esac
Comment on lines +43 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created a plugin using this template. This PR comment line would indeed be helpful 👍

However, I believe it is more appropriate to have it in template/bin/download. This is because many plugins detected the OS and architecture before calling download_release(). Required to extract the command file itself.

e.g.)

# File generated based on template/bin/download

# Download tar.gz file to the download directory
release_file="${TOOL_NAME}-${ASDF_INSTALL_VERSION}-${arch}-${os}.tar.gz"

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

#  Extract contents of tar.gz file into the download directory
tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" --strip-components=1 || fail "Could not extract $release_file"

# Remove the tar.gz file since we don't need to keep it
rm "$release_file"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a separate function in the lib/utils.bash file that has a call commented out in the bin/download file. Can someone please update this to that?

Copy link
Contributor

@airtonix airtonix Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this :

get_machine_os() {
  local OS
  OS=$(uname -s | tr '[:upper:]' '[:lower:]')

  case "${OS}" in
    darwin*) echo "darwin" ;;
     linux*) echo "linux" ;;
          *) fail "OS not supported: ${OS}" ;;
  esac
}

get_machine_arch() {
  local ARCH
  ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')

  case "${ARCH}" in
       i?86) echo "386" ;;
     x86_64) echo "amd64" ;;
    aarch64) echo "arm64" ;;
     armv8l) echo "arm64" ;;
      arm64) echo "arm64" ;;
          *) fail "Architecture not supported: $ARCH" ;;
  esac
}

then later on :

download_release() {
  local version filename url os arch
  version="$1"
  filename="$2"

  // Optional, if your TOOL requires os and arch
  // os=$(get_machine_os)
  // arch=$(get_machine_arch)

  [ -f "${filename}" ] || {
    // TODO: If your tool requires OS and ARCH
    // url="${GH_REPO}/releases/download/v${version}/${TOOL_NAME}_${version}_${os}_${arch}.tar.gz"
    url="${GH_REPO}/releases/download/v${version}/${TOOL_NAME}_${version}.tar.gz"

    echo "* Downloading ${TOOL_NAME} release ${version}"

    curl "${curl_opts[@]}" -o "${filename}" -C - "${url}" || fail "Could not download ${url}"
  }
}
  • You can add more cases in os and machine, but people will remove or add other cases in order to guard against the supported aspects of their particular release policy.
  • the ${release_file} is not describing the source fileurl to download, but the filepath where the download should be saved.


# TODO: Adapt the release URL convention for <YOUR TOOL>
url="$GH_REPO/archive/v${version}.tar.gz"
# Example: local url="$GH_REPO/archive/v${version}-${os}-${arch}.tar.gz"
local url="$GH_REPO/archive/v${version}.tar.gz"

echo "* Downloading $TOOL_NAME release $version..."
curl "${curl_opts[@]}" -o "$filename" -C - "$url" || fail "Could not download $url"
Expand Down