release-fork.fish

  1#!/usr/bin/env fish
  2
  3# Release script for fork tags. Computes the next -fork.N tag from the
  4# latest upstream version, has Crush update the README version string
  5# (amending HEAD), pushes the branch, tags, pushes the tag, then
  6# cross-compiles, packs with UPX, and uploads to the releases server.
  7
  8set -l targets \
  9    linux/amd64 \
 10    linux/arm64 \
 11    darwin/amd64 \
 12    darwin/arm64 \
 13    windows/amd64 \
 14    freebsd/amd64
 15
 16# UPX 5.x dropped macOS support; windows/amd64 is PE32 only and
 17# freebsd/amd64 is ELF32 only, so only Linux targets are packed.
 18set -l pack_targets \
 19    linux-amd64 \
 20    linux-arm64
 21
 22# --- Guards ---
 23
 24set -l branch (git symbolic-ref --short HEAD)
 25if test "$branch" != dev
 26    echo "Error: not on dev branch (on $branch)" >&2
 27    exit 1
 28end
 29
 30if test (git status --porcelain=2 | wc -l) -ne 0
 31    echo "Error: git working tree is dirty" >&2
 32    exit 1
 33end
 34
 35# --- Fetch tags ---
 36
 37git tag -d nightly 2>/dev/null; or true
 38git fetch upstream --tags
 39
 40# --- Compute next fork tag ---
 41
 42set -l upstream_version (git tag -l "v*" | grep -v -- '-fork\.' | sort -V | tail -1; or echo v0.0.0)
 43set -l existing (git tag -l "$upstream_version-fork.*" | wc -l | string trim)
 44set -l next_num (math $existing + 1)
 45set -l tag "$upstream_version-fork.$next_num"
 46
 47# --- Confirm creation ---
 48
 49read -P "Create fork release $tag? [y/N] " confirm
 50if test "$confirm" != y -a "$confirm" != Y
 51    echo Aborted.
 52    exit 0
 53end
 54
 55# --- Push branch (force because we amended) ---
 56
 57git push --force-with-lease; or begin
 58    echo "Error: branch push failed" >&2
 59    exit 1
 60end
 61
 62# --- Tag and push ---
 63
 64git tag -a $tag; or begin
 65    echo "Error: tagging failed" >&2
 66    exit 1
 67end
 68
 69git push soft $tag; or begin
 70    echo "Error: tag push failed — deleting local tag" >&2
 71    git tag -d $tag 2>/dev/null
 72    exit 1
 73end
 74
 75echo "Released $tag"
 76
 77# --- Cross-compile ---
 78
 79set -l ldflags "-s -w -X git.secluded.site/crush/internal/version.Version=$tag"
 80
 81rm -rf dist
 82mkdir -p dist
 83
 84for target in $targets
 85    set -l os (echo $target | cut -d/ -f1)
 86    set -l arch (echo $target | cut -d/ -f2)
 87    set -l ext ""
 88    if test "$os" = windows
 89        set ext .exe
 90    end
 91
 92    echo "Building $os/$arch..."
 93    env CGO_ENABLED=0 GOEXPERIMENT=greenteagc GOOS=$os GOARCH=$arch \
 94        go build -o "dist/crush-$tag-$os-$arch$ext" -ldflags "$ldflags"; or begin
 95        echo "Error: build failed for $os/$arch" >&2
 96        exit 1
 97    end
 98end
 99
100# --- Pack with UPX ---
101
102for suffix in $pack_targets
103    set -l bin "dist/crush-$tag-$suffix"
104    if test -f "$bin"
105        echo "Packing $suffix..."
106        upx -q "$bin"
107    end
108end
109
110# --- Upload ---
111
112fish -c "release upload crush $tag --latest dist/*"
113
114echo "Done — $tag built, packed, and uploaded."