hate-no-d/.forgejo/scripts/make-archive.sh

66 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
# Stage one release asset into a directory and pack it, so every download is a
# self-contained "folder" instead of a bare binary.
#
# make-archive.sh <asset-name> <tar.gz|zip> <payload>...
#
# asset-name e.g. hate-no-d-linux-x86_64; also the directory inside the
# archive, so unpacking never scatters files
# format tar.gz for Unix payloads (keeps the executable bit), zip for
# Windows ones
# payload files to pack, each keeping its basename
#
# LICENSE and README.md are added from the working directory when present.
# Writes ./<asset-name>.<format> and leaves the staging directory behind.
set -eu
if [ $# -lt 3 ]; then
echo "usage: $0 <asset-name> <tar.gz|zip> <payload>..." >&2
exit 2
fi
name=$1
format=$2
shift 2
# Guard the rm -rf below.
case "$name" in
'' | */*) echo "make-archive: bad asset name: '$name'" >&2; exit 2 ;;
esac
case "$format" in
tar.gz | zip) ;;
*) echo "make-archive: unknown format: '$format'" >&2; exit 2 ;;
esac
for f in "$@"; do
[ -f "$f" ] || { echo "make-archive: no such file: $f" >&2; exit 1; }
done
rm -rf "$name"
mkdir -p "$name"
# The first payload is the binary: make its mode deterministic instead of
# trusting whatever the build/copy path produced.
first=1
for f in "$@"; do
cp "$f" "$name/$(basename "$f")"
if [ "$first" = 1 ]; then
chmod 755 "$name/$(basename "$f")"
first=0
fi
done
for f in LICENSE README.md; do
if [ -f "$f" ]; then
cp "$f" "$name/"
fi
done
case "$format" in
tar.gz) tar -czf "$name.tar.gz" "$name" ;;
zip) zip -qr -X "$name.zip" "$name" >/dev/null ;;
esac
echo "make-archive: wrote $name.$format"
ls -l "$name" | sed 's/^/make-archive: /'