vhdl-style-guide/build-package.sh

136 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
show_help() {
cat <<'EOF'
Usage: ./build-package.sh [TOKEN_FILE] [--publish]
Build the VSG source distribution and wheel. With --publish, upload the newly
built artifacts to Gitea. TOKEN_FILE overrides all other token sources.
Optional publishing settings:
GITEA_TOKEN token value used when TOKEN_FILE is omitted
GITEA_TOKEN_FILE
token file used when TOKEN_FILE and GITEA_TOKEN are omitted
(default: ../gitea_token.txt)
GITEA_USERNAME Gitea login name (default: Dandaedre)
GITEA_OWNER package owner (default: MadGobblins)
GITEA_URL Gitea base URL (default: https://proxy.dandaedre.com/gitea)
EOF
}
fail_usage() {
echo "ERROR: $1" >&2
show_help >&2
exit 2
}
publish=false
token_file_argument=""
while [ "$#" -gt 0 ]; do
case "$1" in
--publish)
[ "${publish}" = false ] || fail_usage "--publish was specified more than once."
publish=true
;;
-h|--help)
show_help
exit 0
;;
-*)
fail_usage "unknown option: $1"
;;
*)
[ -z "${token_file_argument}" ] || fail_usage "more than one token file was specified."
token_file_argument="$1"
;;
esac
shift
done
if [ -n "${token_file_argument}" ] && [ "${publish}" = false ]; then
fail_usage "TOKEN_FILE requires --publish."
fi
repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || {
echo "ERROR: build-package.sh must run inside a Git worktree." >&2
exit 1
}
gitea_token="${GITEA_TOKEN:-}"
if [ -n "${token_file_argument}" ]; then
gitea_token=""
gitea_token_file="${token_file_argument}"
else
gitea_token_file="${GITEA_TOKEN_FILE:-${repo_root}/../gitea_token.txt}"
fi
if [ "${publish}" = true ] && [ -z "${gitea_token}" ]; then
if [ ! -f "${gitea_token_file}" ] || [ ! -r "${gitea_token_file}" ]; then
fail_usage "token file is not a readable regular file: ${gitea_token_file}"
fi
IFS= read -r gitea_token <"${gitea_token_file}" || :
fi
if [ "${publish}" = true ] && [ -z "${gitea_token}" ]; then
fail_usage "token file is empty: ${gitea_token_file}"
fi
cd "${repo_root}"
if ! command -v uv >/dev/null 2>&1; then
echo "ERROR: uv is required to build the package." >&2
exit 1
fi
if [ ! -x .venv/bin/python ]; then
echo "Creating the required local virtual environment..."
uv venv .venv
fi
readonly build_dir=".package-build"
if [ -e "${build_dir}" ]; then
rm -rf -- "${build_dir}"
fi
echo "Building the source distribution and wheel..."
uv build --no-sources --out-dir "${build_dir}"
shopt -s nullglob
artifacts=("${build_dir}"/vsg-*.tar.gz "${build_dir}"/vsg-*.whl)
if [ "${#artifacts[@]}" -lt 2 ]; then
echo "ERROR: uv build completed without producing both expected package formats." >&2
exit 1
fi
mkdir -p dist
built_artifacts=()
for artifact in "${artifacts[@]}"; do
destination="dist/${artifact##*/}"
cp -- "${artifact}" "${destination}"
built_artifacts+=("${destination}")
done
rm -rf -- "${build_dir}"
echo "Built package artifacts:"
printf ' %s\n' "${built_artifacts[@]}"
if [ "${publish}" = true ]; then
readonly gitea_username="${GITEA_USERNAME:-Dandaedre}"
readonly gitea_owner="${GITEA_OWNER:-MadGobblins}"
readonly gitea_url="${GITEA_URL:-https://proxy.dandaedre.com/gitea}"
echo
echo "Publishing package artifacts to Gitea..."
UV_PUBLISH_USERNAME="${gitea_username}" \
UV_PUBLISH_PASSWORD="${gitea_token}" \
uv publish \
--publish-url "${gitea_url}/api/packages/${gitea_owner}/pypi" \
--check-url "${gitea_url}/api/packages/${gitea_owner}/pypi/simple/" \
--no-attestations \
"${built_artifacts[@]}"
echo "Published package artifacts successfully."
fi
echo
echo "Install the wheel into a uv environment with:"
printf ' uv pip install %q\n' "${built_artifacts[-1]}"