Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018nX3d3VJCpEXS76x2tZod6
118 lines
5.3 KiB
Shell
Executable file
118 lines
5.3 KiB
Shell
Executable file
#!/usr/bin/env bash
|
|
# jumphp: JumPHP's command for the whole system, installed by `bin/jumphp cli:install` (in ~/.local/bin).
|
|
# jumphp <command> in a JumPHP project (this folder or one above it): its bin/jumphp <command>
|
|
# jumphp new <folder> a new project (composer create-project jumphp/jumphp, then the set-up wizard)
|
|
# jumphp cli:version this command's version, the project's JumPHP version, the newest on Packagist
|
|
# Once a day it asks Packagist whether a newer JumPHP is out and, in a terminal, offers to update the project
|
|
# (bin/jumphp update; nothing without a yes). JUMPHP_NO_UPDATE_CHECK=1: never. The request carries no personal
|
|
# data (User-Agent: jumphp-cli). It offers to update itself too when the project has a newer copy of it.
|
|
set -euo pipefail
|
|
|
|
CLI_VERSION=1
|
|
PACKAGE=jumphp/jumphp
|
|
PACKAGIST="https://repo.packagist.org/p2/$PACKAGE.json"
|
|
REPO="${JUMPHP_REPO:-https://git.jumphp.net/jumphp/jumPhp.git}"
|
|
COMPOSER_IMAGE="${JUMPHP_COMPOSER_IMAGE:-composer:2.10.2}"
|
|
STATE="${XDG_CACHE_HOME:-$HOME/.cache}/jumphp"
|
|
SELF="$(readlink -f "${BASH_SOURCE[0]}")"
|
|
|
|
say() { printf '\033[1;36m==> %s\033[0m\n' "$*" >&2; }
|
|
die() { printf '\033[1;31m[x] %s\033[0m\n' "$*" >&2; exit 1; }
|
|
interactive() { [ -t 0 ] && [ -t 2 ]; }
|
|
|
|
# the JumPHP project this folder belongs to
|
|
project_root() {
|
|
local dir="$PWD"
|
|
while [ "$dir" != / ]; do
|
|
if [ -x "$dir/bin/jumphp" ] && [ -f "$dir/VERSION" ] && [ -d "$dir/engine" ]; then echo "$dir"; return 0; fi
|
|
dir="$(dirname "$dir")"
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# the newest JumPHP on Packagist (asked at most once a day; empty when offline)
|
|
latest() {
|
|
local file="$STATE/latest" now; now="$(date +%s)"
|
|
mkdir -p "$STATE"
|
|
if [ ! -f "$file" ] || [ $((now - $(stat -c %Y "$file" 2>/dev/null || echo 0))) -gt 86400 ]; then
|
|
local version
|
|
version="$(curl -fsS --max-time 4 -A jumphp-cli "$PACKAGIST" 2>/dev/null \
|
|
| grep -oE '"version":"v?[0-9]+\.[0-9]+\.[0-9]+"' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -1)" || true
|
|
if [ -n "$version" ]; then echo "$version" > "$file"; else touch "$file"; fi
|
|
fi
|
|
cat "$file" 2>/dev/null || true
|
|
}
|
|
|
|
newer() { [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -1)" = "$1" ]; }
|
|
|
|
# a newer JumPHP: asked once per version (y: update now, n: later, s: not this version)
|
|
offer_update() {
|
|
local root="$1" current newest answer
|
|
[ -z "${JUMPHP_NO_UPDATE_CHECK:-}" ] && interactive || return 0
|
|
current="$(tr -d '[:space:]' < "$root/VERSION")"
|
|
newest="$(latest)"
|
|
[ -n "$newest" ] && newer "$newest" "$current" || return 0
|
|
grep -qxF "$newest" "$STATE/skipped" 2>/dev/null && return 0
|
|
printf '\033[1;33mJumPHP %s is out (this project: %s).\033[0m Update the project now (bin/jumphp update)? [y/N/s=skip this version] ' "$newest" "$current" >&2
|
|
read -r answer </dev/tty || answer=n
|
|
case "$answer" in
|
|
y|Y|o|O) "$root/bin/jumphp" update || say "not updated: the command goes on with JumPHP $current" ;;
|
|
s|S) echo "$newest" >> "$STATE/skipped" ;;
|
|
esac
|
|
}
|
|
|
|
# the project has a newer copy of this command: install it?
|
|
offer_self_update() {
|
|
local copy="$1/tools/jumphp-cli" theirs answer
|
|
[ -f "$copy" ] && interactive || return 0
|
|
theirs="$(sed -nE 's/^CLI_VERSION=([0-9]+).*/\1/p' "$copy")"
|
|
[ -n "$theirs" ] && [ "$theirs" -gt "$CLI_VERSION" ] || return 0
|
|
printf 'The jumphp command has a new version (%s, you have %s). Install it? [Y/n] ' "$theirs" "$CLI_VERSION" >&2
|
|
read -r answer </dev/tty || answer=n
|
|
case "$answer" in n|N) return 0 ;; esac
|
|
install -m 755 "$copy" "$SELF" && say "jumphp $theirs installed"
|
|
}
|
|
|
|
cmd_new() {
|
|
local dir="${1:-}"
|
|
[ -n "$dir" ] || die "jumphp new <folder>"
|
|
[ -e "$dir" ] && die "$dir already exists"
|
|
if command -v composer >/dev/null 2>&1; then
|
|
composer create-project "$PACKAGE" "$dir"
|
|
elif ! command -v docker >/dev/null 2>&1; then
|
|
# a new computer: the newest release with git; the wizard then offers to install Docker
|
|
command -v git >/dev/null 2>&1 || die "git is needed (or composer, or docker): install it, then jumphp new $dir"
|
|
local tag; tag="$(latest)"
|
|
say "git clone JumPHP ${tag:+v$tag }into $dir"
|
|
git -c advice.detachedHead=false clone -q ${tag:+--branch "v$tag"} "$REPO" "$dir" && git -C "$dir" remote rename origin jumphp
|
|
git -C "$dir" checkout -q -B main
|
|
(cd "$dir" && bash tools/post-create.sh)
|
|
else
|
|
say "composer create-project $PACKAGE $dir (in Docker: $COMPOSER_IMAGE)"
|
|
docker run --rm $([ -t 0 ] && echo -it) --user "$(id -u):$(id -g)" -v "$PWD:/app" -w /app \
|
|
-e HOME=/tmp -e COMPOSER_HOME=/tmp/composer "$COMPOSER_IMAGE" create-project --no-scripts "$PACKAGE" "$dir"
|
|
# the project's own set-up, on this computer (git, then the wizard)
|
|
(cd "$dir" && bash tools/post-create.sh)
|
|
fi
|
|
}
|
|
|
|
case "${1:-}" in
|
|
new) shift; cmd_new "$@"; exit ;;
|
|
cli:version)
|
|
echo "jumphp command $CLI_VERSION ($SELF)"
|
|
if root="$(project_root)"; then echo "project: $root, JumPHP $(cat "$root/VERSION")"; fi
|
|
echo "newest on Packagist: $(latest || true)"
|
|
exit ;;
|
|
esac
|
|
|
|
if ! root="$(project_root)"; then
|
|
cat >&2 <<EOF
|
|
Not in a JumPHP project (no bin/jumphp in this folder or above it).
|
|
jumphp new <folder> a new project (a game in PHP: desktop + browser)
|
|
cd <project> && jumphp its commands (jumphp help)
|
|
EOF
|
|
exit 1
|
|
fi
|
|
offer_self_update "$root"
|
|
offer_update "$root"
|
|
exec "$root/bin/jumphp" "$@"
|