51 lines
1.6 KiB
Bash
51 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
readonly module_path='repos.gfranz.ch/Gabriel20xx/shared/backend'
|
||
|
|
readonly module_directory='backend'
|
||
|
|
|
||
|
|
release_tag="${1:?usage: package-go-module.sh backend/vX.Y.Z [output-directory]}"
|
||
|
|
output_directory="${2:-dist}"
|
||
|
|
version="${release_tag#backend/}"
|
||
|
|
|
||
|
|
if [[ "$release_tag" != backend/v* ]] || [[ ! "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]]; then
|
||
|
|
echo "Expected a release tag such as backend/v0.1.0; got: $release_tag" >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
repository_root="$(git rev-parse --show-toplevel)"
|
||
|
|
git rev-parse --verify --quiet "${release_tag}^{commit}" >/dev/null || {
|
||
|
|
echo "Release tag does not resolve to a commit: $release_tag" >&2
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
|
||
|
|
stage_directory="$(mktemp -d)"
|
||
|
|
trap 'rm -rf "$stage_directory"' EXIT
|
||
|
|
|
||
|
|
module_root="$stage_directory/${module_path}@${version}"
|
||
|
|
mkdir -p "$module_root"
|
||
|
|
|
||
|
|
# Go module ZIP files must contain <module>@<version>/ at their root. The
|
||
|
|
# module is stored in the backend/ subdirectory of the shared repository,
|
||
|
|
# so strip that repository directory while extracting the tagged source.
|
||
|
|
git archive --format=tar "$release_tag" "$module_directory" \
|
||
|
|
| tar -x --strip-components=1 -C "$module_root"
|
||
|
|
|
||
|
|
test -f "$module_root/go.mod"
|
||
|
|
grep -Fxq "module $module_path" "$module_root/go.mod"
|
||
|
|
if [[ "$output_directory" = /* ]]; then
|
||
|
|
output_path="$output_directory"
|
||
|
|
else
|
||
|
|
output_path="$repository_root/$output_directory"
|
||
|
|
fi
|
||
|
|
mkdir -p "$output_path"
|
||
|
|
zip_file="$output_path/${module_path//\//_}@${version}.zip"
|
||
|
|
rm -f "$zip_file"
|
||
|
|
|
||
|
|
(
|
||
|
|
cd "$stage_directory"
|
||
|
|
zip -qr "$zip_file" "${module_path}@${version}"
|
||
|
|
)
|
||
|
|
|
||
|
|
unzip -tq "$zip_file" >/dev/null
|
||
|
|
echo "Created $zip_file"
|