94 lines
3.2 KiB
Bash
Executable File
94 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd -P)
|
|
# shellcheck source=release-lib.sh
|
|
. "$script_dir/release-lib.sh"
|
|
|
|
tool_name=build-release-assets
|
|
|
|
if [ "$#" -ne 2 ]; then
|
|
narratio_release_fail "$tool_name" 'usage: scripts/build-release-assets.sh VERSION OUTPUT_DIR'
|
|
fi
|
|
|
|
release_version=$1
|
|
output_dir=$2
|
|
if ! narratio_release_validate_version "$release_version"; then
|
|
narratio_release_fail "$tool_name" "invalid stable version: $release_version"
|
|
fi
|
|
|
|
case $output_dir in
|
|
/*) ;;
|
|
*) narratio_release_fail "$tool_name" "output directory must be absolute: $output_dir" ;;
|
|
esac
|
|
|
|
repo_root=$(narratio_release_repo_root "$0") || narratio_release_fail "$tool_name" 'cannot locate repository root'
|
|
if ! cd "$repo_root"; then
|
|
narratio_release_fail "$tool_name" "cannot enter repository root: $repo_root"
|
|
fi
|
|
if [ "$output_dir" = / ]; then
|
|
narratio_release_fail "$tool_name" 'output directory must not be /'
|
|
fi
|
|
if [ -L "$output_dir" ]; then
|
|
narratio_release_fail "$tool_name" "output directory must not be a symlink: $output_dir"
|
|
fi
|
|
if [ -e "$output_dir" ]; then
|
|
if [ ! -d "$output_dir" ]; then
|
|
narratio_release_fail "$tool_name" "output path must be a directory: $output_dir"
|
|
fi
|
|
resolved_output_dir=$(CDPATH= cd "$output_dir" && pwd -P) || narratio_release_fail "$tool_name" "cannot access output directory: $output_dir"
|
|
if [ "$resolved_output_dir" = "$repo_root" ]; then
|
|
narratio_release_fail "$tool_name" 'output directory must not be the repository root'
|
|
fi
|
|
for existing_entry in "$output_dir"/* "$output_dir"/.[!.]* "$output_dir"/..?*; do
|
|
if [ -e "$existing_entry" ] || [ -L "$existing_entry" ]; then
|
|
narratio_release_fail "$tool_name" "output directory must be empty: $output_dir"
|
|
fi
|
|
done
|
|
else
|
|
if ! mkdir "$output_dir"; then
|
|
narratio_release_fail "$tool_name" "cannot create output directory: $output_dir"
|
|
fi
|
|
fi
|
|
|
|
temporary_smoke_binary=
|
|
cleanup_temporary_smoke_binary() {
|
|
if [ -n "$temporary_smoke_binary" ]; then
|
|
rm -f "$temporary_smoke_binary"
|
|
fi
|
|
}
|
|
trap cleanup_temporary_smoke_binary 0 HUP INT TERM
|
|
|
|
build_flags="-s -w -X gitea.maximumdirect.net/eric/narratio/internal/buildinfo.Version=$release_version"
|
|
host_os=$(go env GOOS)
|
|
host_arch=$(go env GOARCH)
|
|
host_asset=
|
|
|
|
for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64; do
|
|
target_os=${target%%/*}
|
|
target_arch=${target#*/}
|
|
suffix=
|
|
if [ "$target_os" = windows ]; then
|
|
suffix=.exe
|
|
fi
|
|
asset_path="$output_dir/narratio-$release_version-$target_os-$target_arch$suffix"
|
|
CGO_ENABLED=0 GOOS="$target_os" GOARCH="$target_arch" go build -trimpath -ldflags "$build_flags" -o "$asset_path" ./cmd/narratio
|
|
if [ "$target_os" = "$host_os" ] && [ "$target_arch" = "$host_arch" ]; then
|
|
host_asset=$asset_path
|
|
fi
|
|
done
|
|
|
|
if [ -z "$host_asset" ]; then
|
|
temporary_smoke_binary="$output_dir/.narratio-release-version-smoke"
|
|
CGO_ENABLED=0 go build -trimpath -ldflags "$build_flags" -o "$temporary_smoke_binary" ./cmd/narratio
|
|
host_asset=$temporary_smoke_binary
|
|
fi
|
|
|
|
reported_version=$("$host_asset" version)
|
|
if [ "$reported_version" != "narratio $release_version" ]; then
|
|
narratio_release_fail "$tool_name" "release binary reported unexpected version: $reported_version"
|
|
fi
|
|
|
|
cleanup_temporary_smoke_binary
|
|
temporary_smoke_binary=
|