Skip to content

Commit

Permalink
allow passing version argument
Browse files Browse the repository at this point in the history
  • Loading branch information
war-in committed Jan 12, 2024
1 parent 75ef6be commit 70fed08
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#!/bin/bash

REPO="https://github.com/software-mansion/universal-sierra-compiler"
BINARY_NAME="universal-sierra-compiler"
LOCAL_BIN="${HOME}/.local/bin"

main () {
check_cmd curl
check_cmd tar

download_and_extract_binary
if [ $# -eq 0 ]; then
# If there is no arguments, fetch the latest release tag from GitHub API
release_tag=$(curl -# -Ls -H 'Accept: application/json' "${REPO}/releases/latest" | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
else
release_tag=$1
verify_release_tag "$release_tag"

if [[ ! "$release_tag" =~ ^v ]]; then
release_tag="v$release_tag"
fi
fi

download_and_extract_binary "$release_tag"

add_binary_to_path

Expand All @@ -22,10 +35,30 @@ check_cmd() {
fi
}

verify_release_tag() {
local version=$1
IFS='.' read -ra parts <<< "${version#v}"

# Ensure there are three parts in the version (major.minor.patch)
if [ "${#parts[@]}" -ne 3 ]; then
echo "Please pass correct version (e.g. \"v1.0.0\")"
exit 1
fi

for part in "${parts[@]}"; do
if ! is_integer "$part"; then
echo "One of the major, minor, patch is not an integer"
exit 1
fi
done
}

is_integer() {
[[ "$1" =~ ^[0-9]+$ ]]
}

download_and_extract_binary() {
repo="https://github.com/software-mansion/universal-sierra-compiler"
# Fetch the latest release tag from GitHub API
release_tag=$(curl -# -Ls -H 'Accept: application/json' "${repo}/releases/latest" | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
release_tag=$1

# Define the operating system and architecture
get_architecture
Expand All @@ -38,7 +71,7 @@ download_and_extract_binary() {
temp_dir=$(mktemp -d)

# Download and extract the archive
curl -L "${repo}/releases/download/${release_tag}/${artifact_name}.tar.gz" | tar -xz -C "${temp_dir}"
curl -L "${REPO}/releases/download/${release_tag}/${artifact_name}.tar.gz" | tar -xz -C "${temp_dir}"

# Move the binary to a LOCAL_BIN directory
mkdir -p "${LOCAL_BIN}"
Expand Down Expand Up @@ -125,4 +158,5 @@ add_binary_to_path() {
esac
}

main
set -e
main "$@"

0 comments on commit 70fed08

Please sign in to comment.