Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .quarkus/cli/plugins/quarkus-cli-catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version" : "v1",
"lastUpdate" : "30/07/2025 08:57:50",
"plugins" : { }
}
303 changes: 303 additions & 0 deletions amper
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
#!/bin/sh

#
# Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
#

# Possible environment variables:
# AMPER_DOWNLOAD_ROOT Maven repository to download Amper dist from.
# default: https://packages.jetbrains.team/maven/p/amper/amper
# AMPER_JRE_DOWNLOAD_ROOT Url prefix to download Amper JRE from.
# default: https:/
# AMPER_BOOTSTRAP_CACHE_DIR Cache directory to store extracted JRE and Amper distribution
# AMPER_JAVA_HOME JRE to run Amper itself (optional, does not affect compilation)
# AMPER_JAVA_OPTIONS JVM options to pass to the JVM running Amper (does not affect the user's application)

set -e -u

# The version of the Amper distribution to provision and use
amper_version=0.7.0
# Establish chain of trust from here by specifying exact checksum of Amper distribution to be run
amper_sha256=c98afab6c152937dacd27d47575dd521eec95848504d0a140e496d8e2840f184

AMPER_DOWNLOAD_ROOT="${AMPER_DOWNLOAD_ROOT:-https://packages.jetbrains.team/maven/p/amper/amper}"
AMPER_JRE_DOWNLOAD_ROOT="${AMPER_JRE_DOWNLOAD_ROOT:-https:/}"

die() {
echo >&2
echo "$@" >&2
echo >&2
exit 1
}

download_and_extract() {
moniker="$1"
file_url="$2"
file_sha="$3"
sha_size="$4"
cache_dir="$5"
extract_dir="$6"

if [ -e "$extract_dir/.flag" ] && [ "$(cat "$extract_dir/.flag")" = "${file_sha}" ]; then
# Everything is up-to-date in $extract_dir, do nothing
return 0;
fi

mkdir -p "$cache_dir"

# Take a lock for the download of this file
short_sha=$(echo "$file_sha" | cut -c1-32) # cannot use the ${short_sha:0:32} syntax in regular /bin/sh
download_lock_file="$cache_dir/download-${short_sha}.lock"
process_lock_file="$cache_dir/download-${short_sha}.$$.lock"
echo $$ >"$process_lock_file"
while ! ln "$process_lock_file" "$download_lock_file" 2>/dev/null; do
lock_owner=$(cat "$download_lock_file" 2>/dev/null || true)
if [ -n "$lock_owner" ] && ps -p "$lock_owner" >/dev/null; then
echo "Another Amper instance (pid $lock_owner) is downloading $moniker. Awaiting the result..."
sleep 1
elif [ -n "$lock_owner" ] && [ "$(cat "$download_lock_file" 2>/dev/null)" = "$lock_owner" ]; then
rm -f "$download_lock_file"
# We don't want to simply loop again here, because multiple concurrent processes may face this at the same time,
# which means the 'rm' command above from another script could delete our new valid lock file. Instead, we just
# ask the user to try again. This doesn't 100% eliminate the race, but the probability of issues is drastically
# reduced because it would involve 4 processes with perfect timing. We can revisit this later.
die "Another Amper instance (pid $lock_owner) locked the download of $moniker, but is no longer running. The lock file is now removed, please try again."
fi
done

# shellcheck disable=SC2064
trap "rm -f \"$download_lock_file\"" EXIT
rm -f "$process_lock_file"

unlock_and_cleanup() {
rm -f "$download_lock_file"
trap - EXIT
return 0
}

if [ -e "$extract_dir/.flag" ] && [ "$(cat "$extract_dir/.flag")" = "${file_sha}" ]; then
# Everything is up-to-date in $extract_dir, just release the lock
unlock_and_cleanup
return 0;
fi

temp_file="$cache_dir/download-file-$$.bin"

echo "Downloading $moniker... (only happens on the first run of this version)"

rm -f "$temp_file"
if command -v curl >/dev/null 2>&1; then
if [ -t 1 ]; then CURL_PROGRESS="--progress-bar"; else CURL_PROGRESS="--silent --show-error"; fi
# shellcheck disable=SC2086
curl $CURL_PROGRESS -L --fail --retry 5 --connect-timeout 30 --output "${temp_file}" "$file_url" 2>&1
elif command -v wget >/dev/null 2>&1; then
if [ -t 1 ]; then WGET_PROGRESS=""; else WGET_PROGRESS="-nv"; fi
wget $WGET_PROGRESS --tries=5 --connect-timeout=30 --read-timeout=120 -O "${temp_file}" "$file_url" 2>&1
else
die "ERROR: Please install 'wget' or 'curl', as Amper needs one of them to download $moniker"
fi

check_sha "$file_url" "$temp_file" "$file_sha" "$sha_size"

rm -rf "$extract_dir"
mkdir -p "$extract_dir"

case "$file_url" in
*".zip")
if command -v unzip >/dev/null 2>&1; then
unzip -q "$temp_file" -d "$extract_dir"
else
die "ERROR: Please install 'unzip', as Amper needs it to extract $moniker"
fi ;;
*)
if command -v tar >/dev/null 2>&1; then
tar -x -f "$temp_file" -C "$extract_dir"
else
die "ERROR: Please install 'tar', as Amper needs it to extract $moniker"
fi ;;
esac

rm -f "$temp_file"

echo "$file_sha" >"$extract_dir/.flag"

# Unlock and cleanup the lock file
unlock_and_cleanup

echo "Download complete."
echo
}

# usage: check_sha SOURCE_MONIKER FILE SHA_CHECKSUM SHA_SIZE
# $1 SOURCE_MONIKER (e.g. url)
# $2 FILE
# $3 SHA hex string
# $4 SHA size in bits (256, 512, ...)
check_sha() {
sha_size=$4
if command -v shasum >/dev/null 2>&1; then
echo "$3 *$2" | shasum -a "$sha_size" --status -c || {
die "ERROR: Checksum mismatch for $2 (downloaded from $1): expected checksum $3 but got $(shasum --binary -a "$sha_size" "$2" | awk '{print $1}')"
}
return 0
fi

shaNsumCommand="sha${sha_size}sum"
if command -v "$shaNsumCommand" >/dev/null 2>&1; then
echo "$3 *$2" | $shaNsumCommand -w -c || {
die "ERROR: Checksum mismatch for $2 (downloaded from $1): expected checksum $3 but got $($shaNsumCommand "$2" | awk '{print $1}')"
}
return 0
fi

echo "Both 'shasum' and 'sha${sha_size}sum' utilities are missing. Please install one of them"
return 1
}

# ********** System detection **********

kernelName=$(uname -s)
arch=$(uname -m)
case "$kernelName" in
Darwin* )
simpleOs="macos"
jbr_os="osx"
default_amper_cache_dir="$HOME/Library/Caches/Amper"
;;
Linux* )
simpleOs="linux"
jbr_os="linux"
default_amper_cache_dir="$HOME/.cache/Amper"
# If linux runs in 32-bit mode, we want the "fake" 32-bit architecture, not the real hardware,
# because in this mode linux cannot run 64-bit binaries.
# shellcheck disable=SC2046
arch=$(linux$(getconf LONG_BIT) uname -m)
;;
CYGWIN* | MSYS* | MINGW* )
simpleOs="windows"
jbr_os="windows"
if command -v cygpath >/dev/null 2>&1; then
default_amper_cache_dir=$(cygpath -u "$LOCALAPPDATA\Amper")
else
die "The 'cypath' command is not available, but Amper needs it. Use amper.bat instead, or try a Cygwin or MSYS environment."
fi
;;
*)
die "Unsupported platform $kernelName"
;;
esac

amper_cache_dir="${AMPER_BOOTSTRAP_CACHE_DIR:-$default_amper_cache_dir}"

# ********** Provision Amper distribution **********

amper_url="$AMPER_DOWNLOAD_ROOT/org/jetbrains/amper/amper-cli/$amper_version/amper-cli-$amper_version-dist.tgz"
amper_target_dir="$amper_cache_dir/amper-cli-$amper_version"
download_and_extract "Amper distribution v$amper_version" "$amper_url" "$amper_sha256" 256 "$amper_cache_dir" "$amper_target_dir"

# ********** Provision JRE for Amper **********

if [ "x${AMPER_JAVA_HOME:-}" = "x" ]; then
case $arch in
x86_64 | x64) jbr_arch="x64" ;;
aarch64 | arm64) jbr_arch="aarch64" ;;
*) die "Unsupported architecture $arch" ;;
esac

# Auto-updated from syncVersions.main.kts, do not modify directly here
jbr_version=21.0.6
jbr_build=b895.97

# URL for JBR (vanilla) - see https://github.com/JetBrains/JetBrainsRuntime/releases
jbr_url="$AMPER_JRE_DOWNLOAD_ROOT/cache-redirector.jetbrains.com/intellij-jbr/jbr-$jbr_version-$jbr_os-$jbr_arch-$jbr_build.tar.gz"
jbr_target_dir="$amper_cache_dir/jbr-$jbr_version-$jbr_os-$jbr_arch-$jbr_build"

platform="$jbr_os $jbr_arch"
case $platform in
"osx x64") jbr_sha512=eeef2a2f82c16d7a0bebb1771b0e122aeb78c2bb1414d48767ea4b2d52086152f149bb5a076c9f98ef1f56dee7b40da1118fa01ba32a53576fffc4d1145044fd ;;
"osx aarch64") jbr_sha512=00afc547ff9e5e408446ece3c8eabef2d0b29909a8be4774c8bd3e296f823e1a902717f5f44313f1a634e22b093c80de8d8ac760869d61fb4647a73711793e0f ;;
"linux x64") jbr_sha512=7f0693316c8ac9a6c323d19682effeb33d5a69da8b79c86253d02e206dd0d2afe2ca49be86c58cc8115ad8626e70ec11ee9008282886d7102aa328722e8f032e ;;
"linux aarch64") jbr_sha512=62c2c1a241278951523bace44742a23bb4b48c713e53c1aaad4204af1f4057cca186a17a840a523b440c739b44af4815e7f388c4a6c167a4e7023da4295f6207 ;;
"windows x64") jbr_sha512=7e71a463327a92e6974b3d1013efde00f9d852660d5a18eae5765534b6d3cf0de471f72fd30d3caae910253b8b0df7202e2a76f0435e84ad80d13fb298a84c48 ;;
"windows aarch64") jbr_sha512=188bb92c35bc31b8ec9596701b498797c6578fb8513f1a854a2c8501ff3d2883a1fc74d24c45322526cdaaeb86940fffaf9729f39ba8dd52dd0f2b6f63da35fe ;;
*) die "Unsupported platform $platform" ;;
esac

download_and_extract "JetBrains Runtime v$jbr_version$jbr_build" "$jbr_url" "$jbr_sha512" 512 "$amper_cache_dir" "$jbr_target_dir"

AMPER_JAVA_HOME=
for d in "$jbr_target_dir" "$jbr_target_dir"/* "$jbr_target_dir"/Contents/Home "$jbr_target_dir"/*/Contents/Home; do
if [ -e "$d/bin/java" ]; then
AMPER_JAVA_HOME="$d"
fi
done

if [ "x${AMPER_JAVA_HOME:-}" = "x" ]; then
die "Unable to find bin/java under $jbr_target_dir"
fi
fi

java_exe="$AMPER_JAVA_HOME/bin/java"
if [ '!' -x "$java_exe" ]; then
die "Unable to find bin/java executable at $java_exe"
fi

# ********** Script path detection **********

# We might need to resolve symbolic links here
wrapper_path=$(realpath "$0")

# ********** Launch Amper **********

# In this section we construct the command line by prepending arguments from biggest to lowest precedence:
# 1. basic main class and classpath
# 2. user JVM args (AMPER_JAVA_OPTIONS)
# 3. default JVM args (prepended last, which means they appear first, so they are overridden by user args)

# 1. Prepend basic launch arguments
if [ "$simpleOs" = "windows" ]; then
# Can't cygpath the '*' so it has to be outside
classpath="$(cygpath -w "$amper_target_dir")\lib\*"
else
classpath="$amper_target_dir/lib/*"
fi

set -- -cp "$classpath" org.jetbrains.amper.cli.MainKt "$@"

# 2. Prepend user JVM args from AMPER_JAVA_OPTS
#
# We use "xargs" to parse quoted JVM args from inside AMPER_JAVA_OPTS.
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
if [ -n "${AMPER_JAVA_OPTIONS:-}" ]; then
eval "set -- $(
printf '%s\n' "$AMPER_JAVA_OPTIONS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
fi

# 3. Prepend default JVM args
set -- \
"-ea" \
"-XX:+EnableDynamicAgentLoading" \
"-Damper.wrapper.dist.sha256=$amper_sha256" \
"-Damper.wrapper.path=$wrapper_path" \
"$@"

# Then we can launch with the overridden $@ arguments
exec "$java_exe" "$@"
Loading