diff --git a/.github/workflows/release-plz-pr.yml b/.github/workflows/release-plz-pr.yml index d348af6..d2f6e22 100644 --- a/.github/workflows/release-plz-pr.yml +++ b/.github/workflows/release-plz-pr.yml @@ -24,6 +24,8 @@ jobs: fetch-depth: 0 - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable + - name: Update package.json and latest.txt + run: ./script/update-latest-txt.sh - name: Run release-plz uses: release-plz/action@v0.5 with: diff --git a/frontend/latest.txt b/frontend/latest.txt new file mode 100644 index 0000000..abd4105 --- /dev/null +++ b/frontend/latest.txt @@ -0,0 +1 @@ +0.2.4 diff --git a/frontend/src/pages/latest.txt.ts b/frontend/src/pages/latest.txt.ts index 005eb76..c7382ba 100644 --- a/frontend/src/pages/latest.txt.ts +++ b/frontend/src/pages/latest.txt.ts @@ -2,14 +2,6 @@ import type { APIRoute } from "astro"; import { readFileSync } from "fs"; export const GET: APIRoute = (_context): Response => { - const file = readFileSync("../Cargo.toml", "utf-8"); - const versionMatch = file.match(/^\[workspace\.package\][\s\S]*?version\s*=\s*["'](.+?)["']/m); - - const version = versionMatch?.[1]; - - if (!version) { - throw new Error("Could not find version in Cargo.toml"); - } - - return new Response(version); + const content = readFileSync("./latest.txt", "utf-8"); + return new Response(content); }; diff --git a/frontend/src/templates/install.sh.ejs b/frontend/src/templates/install.sh.ejs index be0098d..d2ec0f6 100644 --- a/frontend/src/templates/install.sh.ejs +++ b/frontend/src/templates/install.sh.ejs @@ -8,81 +8,127 @@ set -euo pipefail LATEST_VERSION_URL="https://webterm.run/latest.txt" REPO_BASE_URL="https://github.com/nasa42/webterm/releases/download" BINARY_NAME="<%= binary_name %>" - -# By default, install to /usr/bin unless the user provides INSTALL_DIR DEFAULT_INSTALL_DIR="/usr/bin" INSTALL_DIR="${INSTALL_DIR:-$DEFAULT_INSTALL_DIR}" -echo "Checking latest ${BINARY_NAME} version..." - -# Fetch the latest version from a remote text file -LATEST_VERSION="$(curl -sSfL "${LATEST_VERSION_URL}" | tr -d '\r\n')" -if [ -z "${LATEST_VERSION}" ]; then - echo "Unable to fetch the latest version from ${LATEST_VERSION_URL}. Please check your internet connection or try again." - exit 1 -fi - -echo "Latest version: ${LATEST_VERSION}" - -# Check if ${BINARY_NAME} is already installed and get installed version -INSTALLED_VERSION="" -if [ -x "${INSTALL_DIR}/${BINARY_NAME}" ]; then - INSTALLED_VERSION="$("${INSTALL_DIR}/${BINARY_NAME}" --version 2>/dev/null | awk '{print $2}')" - echo "Installed version: ${INSTALLED_VERSION}" -fi - -# If installed version matches latest version, skip installation -if [ "${INSTALLED_VERSION}" = "${LATEST_VERSION}" ]; then - echo "${BINARY_NAME} is already up to date (version ${INSTALLED_VERSION}). No action needed." - exit 0 -fi - -# Determine architecture -ARCH="$(uname -m)" -case "${ARCH}" in - x86_64) - ARCHIVE="${BINARY_NAME}-x86_64-unknown-linux-gnu.tar.gz" - ;; - aarch64|arm64) - ARCHIVE="${BINARY_NAME}-aarch64-unknown-linux-gnu.tar.gz" - ;; - *) - echo "Unsupported architecture: ${ARCH}" +get_architecture() { + local arch="$(uname -m)" + case "${arch}" in + x86_64) + echo "${BINARY_NAME}-x86_64-unknown-linux-gnu.tar.gz" + ;; + aarch64|arm64) + echo "${BINARY_NAME}-aarch64-unknown-linux-gnu.tar.gz" + ;; + *) + echo "" + return 1 + ;; + esac +} + +get_installed_version() { + local install_dir="$1" + if [ -x "${install_dir}/${BINARY_NAME}" ]; then + "${install_dir}/${BINARY_NAME}" --version 2>/dev/null | awk '{print $2}' + fi +} + +try_version() { + local version="$1" + local archive="$2" + local download_url="${REPO_BASE_URL}/${BINARY_NAME}-v${version}/${archive}" + + echo "Trying to download ${BINARY_NAME} ${version}..." + if curl -sSfL "${download_url}" -o "${archive}" 2>/dev/null; then + echo "Download successful for version ${version}. Extracting..." + if tar -xzf "${archive}"; then + rm -f "${archive}" + return 0 + fi + rm -f "${archive}" + fi + return 1 +} + +install_binary() { + local install_dir="$1" + local version="$2" + + if [ ! -w "${install_dir}" ]; then + echo "You do not have write permissions to '${install_dir}'." + echo "To perform a system-wide install, re-run with sudo:" + echo " sudo curl -sSfL https://webterm.run/get | bash" + echo + echo "Or specify a custom installation directory via the INSTALL_DIR environment variable, for example:" + echo " curl -sSfL https://webterm.run/get | INSTALL_DIR=\"\$HOME/bin\" bash" + rm -f "${BINARY_NAME}" + return 1 + fi + + echo "Installing ${BINARY_NAME} version ${version} to '${install_dir}'..." + mv "${BINARY_NAME}" "${install_dir}/${BINARY_NAME}" + chmod +x "${install_dir}/${BINARY_NAME}" + return 0 +} + +print_success() { + local install_dir="$1" + echo "Installation successful!" + echo "To verify, run:" + echo " ${install_dir}/${BINARY_NAME} --version" + echo + echo "To uninstall, just remove the binary (no additional files or configurations are left behind)" + echo " rm ${install_dir}/${BINARY_NAME}" + echo + echo "Learn more at https://webterm.run" +} + +main() { + echo "Checking available ${BINARY_NAME} versions..." + + local versions=($(curl -sSfL "${LATEST_VERSION_URL}" | tr -d '\r')) + if [ ${#versions[@]} -eq 0 ]; then + echo "Unable to fetch versions from ${LATEST_VERSION_URL}. Please check your internet connection or try again." + exit 1 + fi + echo "Available versions: ${versions[*]}" + + local installed_version="$(get_installed_version "${INSTALL_DIR}")" + if [ -n "${installed_version}" ]; then + echo "Installed version: ${installed_version}" + fi + + local archive + archive="$(get_architecture)" || { + echo "Unsupported architecture: $(uname -m)" echo "Please download and install ${BINARY_NAME} manually for your system." exit 1 - ;; -esac - -DOWNLOAD_URL="${REPO_BASE_URL}/${BINARY_NAME}-v${LATEST_VERSION}/${ARCHIVE}" - -echo "Downloading ${BINARY_NAME} ${LATEST_VERSION} for architecture ${ARCH}..." -curl -sSfL "${DOWNLOAD_URL}" -o "${ARCHIVE}" - -echo "Download complete. Extracting..." -tar -xzf "${ARCHIVE}" -rm -f "${ARCHIVE}" - -# Check if we can write to the chosen directory -if [ ! -w "${INSTALL_DIR}" ]; then - echo "You do not have write permissions to '${INSTALL_DIR}'." - echo "To perform a system-wide install, re-run with sudo:" - echo " sudo curl -sSfL https://webterm.run/get | bash" - echo - echo "Or specify a custom installation directory via the INSTALL_DIR environment variable, for example:" - echo " curl -sSfL https://webterm.run/get | INSTALL_DIR=\"\$HOME/bin\" bash" - rm -f "${BINARY_NAME}" - exit 1 -fi - -echo "Installing ${BINARY_NAME} to '${INSTALL_DIR}'..." -mv "${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}" -chmod +x "${INSTALL_DIR}/${BINARY_NAME}" - -echo "Installation successful!" -echo "To verify, run:" -echo " ${INSTALL_DIR}/${BINARY_NAME} --version" -echo -echo "To uninstall, just remove the binary (no additional files or configurations are left behind)" -echo " rm ${INSTALL_DIR}/${BINARY_NAME}" -echo -echo "Learn more at https://webterm.run" + } + + local success=0 + local version + for version in "${versions[@]}"; do + if [ "${installed_version}" = "${version}" ]; then + echo "${BINARY_NAME} version ${version} is already installed. No action needed." + exit 0 + fi + + if try_version "${version}" "${archive}"; then + success=1 + break + else + echo "Version ${version} not available, trying next version..." + fi + done + + if [ ${success} -eq 0 ]; then + echo "Failed to download any version. Please check your internet connection or try again later." + exit 1 + fi + + install_binary "${INSTALL_DIR}" "${version}" || exit 1 + print_success "${INSTALL_DIR}" +} + +main diff --git a/scripts/update-latest-txt.sh b/scripts/update-latest-txt.sh new file mode 100755 index 0000000..0cb7cb8 --- /dev/null +++ b/scripts/update-latest-txt.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +set -e + +VERSION=$(grep '^version = ' Cargo.toml | head -n1 | cut -d'"' -f2) + +if [ -z "$VERSION" ]; then + echo "Error: Could not extract version from Cargo.toml" + exit 1 +fi + +echo "Current version is $VERSION" + +jq ".version = \"$VERSION\"" frontend/package.json > frontend/package.json.tmp && \ + mv frontend/package.json.tmp frontend/package.json + +LATEST_TXT="frontend/latest.txt" + +if [ ! -s "$LATEST_TXT" ]; then + echo "Initialising $LATEST_TXT" + echo "$VERSION" > "$LATEST_TXT" +elif ! grep -Fxq "$VERSION" "$LATEST_TXT"; then + echo "Updating $LATEST_TXT" + sed -i "1i$VERSION" "$LATEST_TXT" + sed -i '3,$d' "$LATEST_TXT" +else + echo "latest.txt is already up to date" +fi + + +echo "cat latest.txt" +cat $LATEST_TXT + +echo "Done."