lore-forge/scripts/fetch-lore.sh

164 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Fetches the pinned Lore release (`lore` and `loreserver`) into .lore/<version>/.
#
# scripts/fetch-lore.sh download if needed, print the directory holding the binaries
# scripts/fetch-lore.sh --liblore the same for liblore (the library and lore.h), which the
# desktop client's service loads
# scripts/fetch-lore.sh --liblore-to FILE
# fetch liblore, then copy the library to FILE (for zig build)
# LORE_TARGET=x86_64-pc-windows-msvc scripts/fetch-lore.sh --liblore
# liblore for another platform, into .lore/<version>/<target>/
# (for packaging the desktop client; lore and loreserver are
# always this machine's)
# scripts/fetch-lore.sh --pin vX.Y.Z re-pin lore.lock to another release (needs `gh`)
#
# The version and the SHA-256 of every archive are pinned in lore.lock, so a download that does
# not match is refused. Needs curl, tar and shasum or sha256sum. Progress goes to stderr; the
# only line on stdout is the directory, so other scripts can run
# `LORE_BIN_DIR="$(scripts/fetch-lore.sh)"`.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
LOCK="$ROOT/lore.lock"
REPO="EpicGames/lore"
say() { echo "$*" >&2; }
die() { say "fetch-lore: $*"; exit 1; }
sha256() {
if command -v sha256sum >/dev/null; then
sha256sum "$1" | cut -d' ' -f1
elif command -v shasum >/dev/null; then
shasum -a 256 "$1" | cut -d' ' -f1
else
die "need sha256sum or shasum to verify downloads"
fi
}
# The release's platform name for this machine.
target() {
case "$(uname -s)/$(uname -m)" in
Darwin/arm64) echo aarch64-apple-darwin ;;
Linux/x86_64) echo x86_64-unknown-linux-gnu ;;
Linux/aarch64 | Linux/arm64) echo aarch64-unknown-linux-gnu ;;
*) die "Lore publishes no release for $(uname -s)/$(uname -m). Build lore and loreserver from source and set LORE_BIN_DIR to their directory." ;;
esac
}
pin() {
local version=$1
command -v gh >/dev/null || die "--pin needs the GitHub CLI (gh)"
say "pinning $REPO $version"
{
echo "# The Lore release lore-forge is tested against, and the SHA-256 of each archive."
echo "# Written by: scripts/fetch-lore.sh --pin $version"
echo "# Re-run scripts/e2e-lore.sh after changing it."
echo "version $version"
gh release view "$version" --repo "$REPO" --json assets \
--jq '.assets[] | select(.name | test("^(lib)?lore(server)?-v.*\\.(tar\\.gz|zip)$")) | select(.name | test("-debug\\.") | not) | "sha256 \(.name) \(.digest | ltrimstr("sha256:"))"'
} >"$LOCK.tmp"
grep -q '^sha256 loreserver-' "$LOCK.tmp" || { rm -f "$LOCK.tmp"; die "$version has no loreserver archives"; }
mv "$LOCK.tmp" "$LOCK"
say "wrote $LOCK"
}
fetch() {
[ -f "$LOCK" ] || die "$LOCK is missing"
local version target dir
version=$(awk '$1 == "version" { print $2 }' "$LOCK")
[ -n "$version" ] || die "no version in $LOCK"
target=$(target)
dir="$ROOT/.lore/$version"
if [ -x "$dir/lore" ] && [ -x "$dir/loreserver" ]; then
echo "$dir"
return
fi
WORK_DIR=$(mktemp -d "${TMPDIR:-/tmp}/fetch-lore.XXXXXX")
trap 'rm -rf "$WORK_DIR"' EXIT
local work=$WORK_DIR
for name in lore loreserver; do
local archive="$name-$version-$target.tar.gz"
local expected
expected=$(awk -v a="$archive" '$1 == "sha256" && $2 == a { print $3 }' "$LOCK")
[ -n "$expected" ] || die "$LOCK pins no $archive"
say "downloading $archive"
curl -fsSL --retry 3 -o "$work/$archive" \
"https://github.com/$REPO/releases/download/$version/$archive"
local actual
actual=$(sha256 "$work/$archive")
[ "$actual" = "$expected" ] || die "$archive has SHA-256 $actual, but lore.lock pins $expected"
mkdir -p "$work/$name"
tar -xzf "$work/$archive" -C "$work/$name"
[ -x "$work/$name/$name" ] || die "$archive holds no $name binary"
done
mkdir -p "$dir"
cp "$work/lore/lore" "$work/loreserver/loreserver" "$dir/"
cp "$work/loreserver/LICENSE.txt" "$work/loreserver/THIRD-PARTY-NOTICES.txt" "$dir/" 2>/dev/null || true
# The release's sample server configuration, for reference.
[ -d "$work/loreserver/config" ] && cp -R "$work/loreserver/config" "$dir/config-examples"
say "Lore $version is in $dir"
echo "$dir"
}
# The library's file name in the release archive for a platform (default: this machine's).
library_file() {
case "${1:-$(target)}" in
*-windows-*) echo lore.dll ;;
*-apple-darwin) echo liblore.dylib ;;
*) echo liblore.so ;;
esac
}
fetch_liblore() {
[ -f "$LOCK" ] || die "$LOCK is missing"
local version target dir file ext
version=$(awk '$1 == "version" { print $2 }' "$LOCK")
target=${LORE_TARGET:-$(target)}
dir="$ROOT/.lore/$version"
# Another platform's library goes beside this machine's, not over it.
[ -z "${LORE_TARGET:-}" ] || [ "$LORE_TARGET" = "$(target)" ] || dir="$dir/$target"
file=$(library_file "$target")
if [ -f "$dir/$file" ] && [ -f "$dir/lore.h" ]; then
echo "$dir"
return
fi
case "$target" in *-windows-*) ext=zip ;; *) ext=tar.gz ;; esac
local archive="liblore-$version-$target.$ext" expected actual
expected=$(awk -v a="$archive" '$1 == "sha256" && $2 == a { print $3 }' "$LOCK")
[ -n "$expected" ] || die "$LOCK pins no $archive; re-pin with --pin $version"
WORK_DIR=$(mktemp -d "${TMPDIR:-/tmp}/fetch-lore.XXXXXX")
trap 'rm -rf "$WORK_DIR"' EXIT
say "downloading $archive"
curl -fsSL --retry 3 -o "$WORK_DIR/$archive" "https://github.com/$REPO/releases/download/$version/$archive"
actual=$(sha256 "$WORK_DIR/$archive")
[ "$actual" = "$expected" ] || die "$archive has SHA-256 $actual, but lore.lock pins $expected"
mkdir -p "$WORK_DIR/lib"
if [ "$ext" = zip ]; then
command -v unzip >/dev/null || die "unpacking $archive needs unzip"
unzip -q "$WORK_DIR/$archive" -d "$WORK_DIR/lib"
else
tar -xzf "$WORK_DIR/$archive" -C "$WORK_DIR/lib"
fi
[ -f "$WORK_DIR/lib/$file" ] && [ -f "$WORK_DIR/lib/lore.h" ] || die "$archive holds no $file and lore.h"
mkdir -p "$dir"
cp "$WORK_DIR/lib/$file" "$WORK_DIR/lib/lore.h" "$dir/"
say "liblore $version is in $dir"
echo "$dir"
}
case "${1:-}" in
--liblore) fetch_liblore ;;
--liblore-to)
[ -n "${2:-}" ] || die "usage: $0 --liblore-to FILE"
dir=$(fetch_liblore)
cp "$dir/$(library_file)" "$2"
;;
--pin) [ -n "${2:-}" ] || die "usage: $0 --pin vX.Y.Z"; pin "$2" ;;
"") fetch ;;
*) die "usage: $0 [--liblore | --liblore-to FILE | --pin vX.Y.Z]" ;;
esac