From fafc634d6cf4deee5fb6b1937ca2a6b74cc9bbdd Mon Sep 17 00:00:00 2001 From: Bertie690 Date: Fri, 24 Oct 2025 18:05:34 -0400 Subject: [PATCH] Update logging messages + add coloration yes i have bash ansi color coding functions up my back pocket don't ask --- build_and_release.sh | 61 ++++++++++++++++++++++++++++---------------- utils.sh | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 utils.sh diff --git a/build_and_release.sh b/build_and_release.sh index c1d390f..5355259 100755 --- a/build_and_release.sh +++ b/build_and_release.sh @@ -1,6 +1,12 @@ #!/bin/bash +# Exit early if a single command fails set -e +# Add color utilities. +# TODO: DO we need to check for color support if a self-hosted runner happens to do this? +source utils.sh + +# An array containing all goos platforms=( darwin-amd64 darwin-arm64 @@ -17,50 +23,53 @@ platforms=( ) if [[ "$RELEASE_ANDROID" == "true" ]]; then - platforms+=("android-amd64") - platforms+=("android-arm64") -fi - -# We must know the android sdk version to build for android. -if [[ "$RELEASE_ANDROID" == "true" && -z "$ANDROID_SDK_VERSION" ]]; then - echo "error: Cannot build for android without android_sdk_version." >&2 - exit 1 -fi + # We must have `ANDROID_SDK_VERSION` and `ANDROID_NDK_HOME` set to build for android. + # The latter is available by default on GitHub hosted runners, but not necessarily the former. + if [[ -z "$ANDROID_SDK_VERSION" ]]; then + fail "Cannot build for Android without ANDROID_SDK_VERSION environment variable!" + elif [[ ! -d "$ANDROID_NDK_HOME" ]]; then + fail "Cannot build for Android without ANDROID_NDK_HOME environment variable!" + fi -# We must have `ANDROID_NDK_HOME` set to build for android. -# This will be available by default on GitHub hosted runners. -if [[ "$RELEASE_ANDROID" == "true" && ! -d "$ANDROID_NDK_HOME" ]]; then - echo "error: Cannot build for android without android_ndk_home." >&2 - exit 1 + platforms+=("android-amd64" "android-arm64") fi prerelease="" +# TODO: Do we want to allow users to set `--prerelease` via a workflow flag? if [[ $GH_RELEASE_TAG = *-* ]]; then + info "Marking release as not production-ready..." prerelease="--prerelease" fi draft_release="" if [[ "$DRAFT_RELEASE" = "true" ]]; then + info "Marking release as draft..." draft_release="--draft" fi if [ -n "$GH_EXT_BUILD_SCRIPT" ]; then - echo "invoking build script override $GH_EXT_BUILD_SCRIPT" + info "Invoking build script override: $GH_EXT_BUILD_SCRIPT" ./"$GH_EXT_BUILD_SCRIPT" "$GH_RELEASE_TAG" else + # Create build for individual platforms, ensuring they are supported IFS=$'\n' read -d '' -r -a supported_platforms < <(go tool dist list) || true for p in "${platforms[@]}"; do goos="${p%-*}" goarch="${p#*-}" if [[ " ${supported_platforms[*]} " != *" ${goos}/${goarch} "* ]]; then - echo "warning: skipping unsupported platform $p" >&2 + warn "Skipping unsupported platform: $p" continue fi + + # Add .exe suffix on windows ext="" if [ "$goos" = "windows" ]; then ext=".exe" fi + + path="dist/${p}${ext}" + cc="" cgo_enabled="${CGO_ENABLED:-0}" if [ "$goos" = "android" ]; then @@ -72,10 +81,18 @@ else cgo_enabled="1" fi fi - GOOS="$goos" GOARCH="$goarch" CGO_ENABLED="$cgo_enabled" CC="$cc" go build -trimpath -ldflags="-s -w" -o "dist/${p}${ext}" "${GO_BUILD_OPTIONS}" + + if GOOS="$goos" GOARCH="$goarch" CGO_ENABLED="$cgo_enabled" CC="$cc" \ + go build -trimpath -ldflags="-s -w" -o "${path}" "${GO_BUILD_OPTIONS}"; then + success "Successfully created binary for ${goos}/${goarch} at ${path}!" + else + fail "Error creating binary for ${goos}/${goarch}!" $? + fi + done fi +# TODO: We can likely rework this to use `readarray` or just compacting the glob directly with `nullglob` assets=() for f in dist/*; do if [ -f "$f" ]; then @@ -84,20 +101,20 @@ for f in dist/*; do done if [ "${#assets[@]}" -eq 0 ]; then - echo "error: no files found in dist/*" >&2 - exit 1 + quit "No executable files found in dist/!" fi if [ -n "$GPG_FINGERPRINT" ]; then shasum -a 256 "${assets[@]}" > checksums.txt gpg --output checksums.txt.sig --detach-sign checksums.txt assets+=(checksums.txt checksums.txt.sig) + success "Successfully signed binaries!" fi if gh release view "$GH_RELEASE_TAG" >/dev/null; then - echo "uploading assets to an existing release..." gh release upload "$GH_RELEASE_TAG" --clobber -- "${assets[@]}" + success "Uploaded assets to existing release ${GH_RELEASE_TAG}!" else - echo "creating release and uploading assets..." gh release create "$GH_RELEASE_TAG" $prerelease $draft_release --title="${GH_RELEASE_TITLE_PREFIX} ${GH_RELEASE_TAG#v}" --generate-notes -- "${assets[@]}" -fi + success "Created release ${GH_RELEASE_TAG} and uploaded assets!" +fi \ No newline at end of file diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000..49628b6 --- /dev/null +++ b/utils.sh @@ -0,0 +1,49 @@ +#! /bin/bash +# Assorted color utilities for build_and_release.sh. + +# Make text orange. +orange () { + printf "\e[33m%s\e[0m" "$1" +} + +# Make text red. +red () { + printf "\e[31m%s\e[0m" "$1" +} + +# Make text green. +green () { + printf "\e[32m%s\e[0m" "$1" +} + +# Make text blue. +blue () { + printf "\e[34m%s\e[0m" "$1" +} + +# Annotate text with a blue [...]. +info () { + printf "[$(blue ...)] %s\n" "$1" +} + +# Annotate text with a green [✔]. +success () { + printf "[$(green '✔ ')] %s\n" "$1" +} + +# Annotate text with an orange [!!!] and send to stderr. +warn () { + printf "[$(orange !!!)] %s\n" "$1" >&2 +} + +# Annotate text with a red [✗] and send to stderr. +error () { + printf "[ $(red '✗') ] %s\n" "$1" >&2 +} + +# Annotate text with a red [✗], and then return the specified non-zero to kill the program. +# Defaults to 1 if not provided. +fail () { + error "$1" + return "${2:-1}" +} \ No newline at end of file