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: List multiple versions in latest.txt. #123

Merged
merged 3 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/workflows/release-plz-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
Expand Down
1 change: 1 addition & 0 deletions frontend/latest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.4
12 changes: 2 additions & 10 deletions frontend/src/pages/latest.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
192 changes: 119 additions & 73 deletions frontend/src/templates/install.sh.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions scripts/update-latest-txt.sh
Original file line number Diff line number Diff line change
@@ -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."