|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 3 | +# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 4 | +# asdf-ephapax utility functions |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +readonly GITHUB_REPO="hyperpolymath/ephapax" |
| 9 | +readonly GITHUB_API="https://api.github.com/repos/${GITHUB_REPO}" |
| 10 | + |
| 11 | +export TOOL_NAME="ephapax" |
| 12 | +export TOOL_CMD="ephapax" |
| 13 | + |
| 14 | +# Colors for output |
| 15 | +if [[ -t 2 ]]; then |
| 16 | + readonly RED='\033[0;31m' |
| 17 | + readonly GREEN='\033[0;32m' |
| 18 | + readonly YELLOW='\033[0;33m' |
| 19 | + readonly BLUE='\033[0;34m' |
| 20 | + readonly NC='\033[0m' |
| 21 | +else |
| 22 | + readonly RED='' |
| 23 | + readonly GREEN='' |
| 24 | + readonly YELLOW='' |
| 25 | + readonly BLUE='' |
| 26 | + readonly NC='' |
| 27 | +fi |
| 28 | + |
| 29 | +log_info() { |
| 30 | + echo -e "${BLUE}[asdf-ephapax]${NC} $*" >&2 |
| 31 | +} |
| 32 | + |
| 33 | +log_success() { |
| 34 | + echo -e "${GREEN}[asdf-ephapax]${NC} $*" >&2 |
| 35 | +} |
| 36 | + |
| 37 | +log_warn() { |
| 38 | + echo -e "${YELLOW}[asdf-ephapax]${NC} WARNING: $*" >&2 |
| 39 | +} |
| 40 | + |
| 41 | +log_error() { |
| 42 | + echo -e "${RED}[asdf-ephapax]${NC} ERROR: $*" >&2 |
| 43 | +} |
| 44 | + |
| 45 | +fail() { |
| 46 | + log_error "$*" |
| 47 | + exit 1 |
| 48 | +} |
| 49 | + |
| 50 | +get_platform() { |
| 51 | + local os |
| 52 | + os="$(uname -s)" |
| 53 | + case "${os}" in |
| 54 | + Linux*) echo "linux" ;; |
| 55 | + Darwin*) echo "macos" ;; |
| 56 | + MINGW*|MSYS*|CYGWIN*) echo "windows" ;; |
| 57 | + *) fail "Unsupported operating system: ${os}" ;; |
| 58 | + esac |
| 59 | +} |
| 60 | + |
| 61 | +get_arch() { |
| 62 | + local arch |
| 63 | + arch="$(uname -m)" |
| 64 | + case "${arch}" in |
| 65 | + x86_64|amd64) echo "x86_64" ;; |
| 66 | + aarch64|arm64) echo "aarch64" ;; |
| 67 | + armv7*) echo "armv7a" ;; |
| 68 | + i686|i386) echo "x86" ;; |
| 69 | + *) fail "Unsupported architecture: ${arch}" ;; |
| 70 | + esac |
| 71 | +} |
| 72 | + |
| 73 | +curl_wrapper() { |
| 74 | + local -a curl_args=("-fsSL" "--retry" "3" "--retry-delay" "2") |
| 75 | + |
| 76 | + # Use GITHUB_TOKEN if available for higher rate limits |
| 77 | + if [[ -n "${GITHUB_TOKEN:-}" ]] || [[ -n "${GITHUB_API_TOKEN:-}" ]]; then |
| 78 | + local token="${GITHUB_TOKEN:-${GITHUB_API_TOKEN}}" |
| 79 | + curl_args+=("-H" "Authorization: token ${token}") |
| 80 | + fi |
| 81 | + |
| 82 | + curl "${curl_args[@]}" "$@" |
| 83 | +} |
| 84 | + |
| 85 | +download_file() { |
| 86 | + local url="$1" |
| 87 | + local output="$2" |
| 88 | + |
| 89 | + log_info "Downloading ${url}..." |
| 90 | + |
| 91 | + if ! curl_wrapper -o "${output}" "${url}"; then |
| 92 | + fail "Failed to download: ${url}" |
| 93 | + fi |
| 94 | +} |
| 95 | + |
| 96 | +list_all_versions() { |
| 97 | + # Query GitHub releases API for all Ephapax versions |
| 98 | + local releases |
| 99 | + releases=$(curl_wrapper "${GITHUB_API}/releases" 2>/dev/null || echo "[]") |
| 100 | + |
| 101 | + echo "${releases}" \ |
| 102 | + | grep '"tag_name"' \ |
| 103 | + | sed 's/.*"tag_name": *"v\{0,1\}\([^"]*\)".*/\1/' \ |
| 104 | + | sort -V |
| 105 | +} |
| 106 | + |
| 107 | +get_latest_stable() { |
| 108 | + # Get the latest non-prerelease version |
| 109 | + local releases |
| 110 | + releases=$(curl_wrapper "${GITHUB_API}/releases/latest" 2>/dev/null || echo "{}") |
| 111 | + |
| 112 | + echo "${releases}" \ |
| 113 | + | grep '"tag_name"' \ |
| 114 | + | head -1 \ |
| 115 | + | sed 's/.*"tag_name": *"v\{0,1\}\([^"]*\)".*/\1/' |
| 116 | +} |
| 117 | + |
| 118 | +get_download_url() { |
| 119 | + local version="$1" |
| 120 | + local platform |
| 121 | + local arch |
| 122 | + platform="$(get_platform)" |
| 123 | + arch="$(get_arch)" |
| 124 | + |
| 125 | + # Ephapax release artifact naming convention: |
| 126 | + # ephapax-{version}-{platform}-{arch}.tar.gz |
| 127 | + echo "https://github.com/${GITHUB_REPO}/releases/download/v${version}/ephapax-${version}-${platform}-${arch}.tar.gz" |
| 128 | +} |
| 129 | + |
| 130 | +command_exists() { |
| 131 | + command -v "$1" >/dev/null 2>&1 |
| 132 | +} |
| 133 | + |
| 134 | +check_dependencies() { |
| 135 | + local -a required=(curl tar grep sed sort cut mkdir rm) |
| 136 | + local missing=() |
| 137 | + |
| 138 | + for cmd in "${required[@]}"; do |
| 139 | + if ! command_exists "${cmd}"; then |
| 140 | + missing+=("${cmd}") |
| 141 | + fi |
| 142 | + done |
| 143 | + |
| 144 | + if [[ ${#missing[@]} -gt 0 ]]; then |
| 145 | + fail "Missing required dependencies: ${missing[*]}" |
| 146 | + fi |
| 147 | +} |
0 commit comments