[mike@mwxm4]
This commit is contained in:
@@ -1,40 +1,70 @@
|
||||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
# Build goca for the usual platforms into ./bin, auto-incrementing the patch
|
||||
# version by 0.0.1 on every build.
|
||||
#
|
||||
# version.txt holds the currently built version. Each run increments the patch
|
||||
# component, then builds every platform with that one version injected via
|
||||
# -ldflags, and writes it back. So version.txt always reflects the version of
|
||||
# the binaries just built, and all of them carry the same one. The literal in
|
||||
# version.go is only ever what a bare `go build` picks up.
|
||||
#
|
||||
# The names in ./bin are what selfupdate.go looks for in a release:
|
||||
# "goca-<goos>-<goarch>", with .exe on Windows. Upload exactly these files to a
|
||||
# Gitea release whose tag is the bare version number, and --update finds them.
|
||||
#
|
||||
# Override the platform list to build just one:
|
||||
# PLATFORMS="linux/amd64" ./build.sh
|
||||
#
|
||||
# A plain run only ever steps the patch. A minor or major step is taken by
|
||||
# naming the version outright, which is then written back like any other:
|
||||
# VERSION=1.2.0 ./build.sh
|
||||
#
|
||||
# -s -w drops the symbol table and DWARF info, -trimpath keeps build paths out
|
||||
# of the binary; together they roughly halve it. Neither affects a panic trace.
|
||||
set -e
|
||||
|
||||
# Make sure we are in the script's directory
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
# Read version from version.go
|
||||
VERSION_LINE=$(grep "var Version =" version.go)
|
||||
CURRENT_VERSION=$(echo "$VERSION_LINE" | sed -E 's/.*"([^"]+)".*/\1/')
|
||||
PLATFORMS=${PLATFORMS:-"darwin/arm64 darwin/amd64 linux/amd64 linux/arm64 windows/amd64"}
|
||||
|
||||
if [ -z "$CURRENT_VERSION" ]; then
|
||||
echo "❌ Error: Could not parse version from version.go"
|
||||
exit 1
|
||||
if [ -n "$VERSION" ]; then
|
||||
NV="$VERSION"
|
||||
else
|
||||
V=$(cat version.txt 2>/dev/null || echo 1.1.2)
|
||||
|
||||
# split MAJOR.MINOR.PATCH and increment PATCH (no carry: 1.1.9 -> 1.1.10)
|
||||
MAJOR=${V%%.*}
|
||||
REST=${V#*.}
|
||||
MINOR=${REST%%.*}
|
||||
PATCH=${REST#*.}
|
||||
PATCH=$((PATCH + 1))
|
||||
NV="$MAJOR.$MINOR.$PATCH"
|
||||
fi
|
||||
|
||||
# Split version by dot
|
||||
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
|
||||
MAJOR="${VERSION_PARTS[0]}"
|
||||
MINOR="${VERSION_PARTS[1]}"
|
||||
PATCH="${VERSION_PARTS[2]}"
|
||||
mkdir -p bin
|
||||
HOST="$(go env GOOS)/$(go env GOARCH)"
|
||||
|
||||
# Increment patch version
|
||||
NEW_PATCH=$((PATCH + 1))
|
||||
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
|
||||
for p in $PLATFORMS; do
|
||||
os=${p%/*}
|
||||
arch=${p#*/}
|
||||
ext=""
|
||||
if [ "$os" = "windows" ]; then
|
||||
ext=".exe" # Windows runs nothing without it, and --update knows that
|
||||
fi
|
||||
out="bin/goca-$os-$arch$ext"
|
||||
|
||||
echo "🚀 Incrementing version: $CURRENT_VERSION ➡️ $NEW_VERSION"
|
||||
# CGO_ENABLED=0 throughout: it makes the cross builds work without a
|
||||
# toolchain per target and the binaries static. goca only needs the network
|
||||
# and the filesystem, so nothing is lost by dropping cgo.
|
||||
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" \
|
||||
go build -trimpath -ldflags "-s -w -X main.Version=$NV" -o "$out" .
|
||||
|
||||
# Write back to version.go using perl for portable in-place edit
|
||||
perl -pi -e "s/var Version = \"$CURRENT_VERSION\"/var Version = \"$NEW_VERSION\"/" version.go
|
||||
if [ "$p" = "$HOST" ]; then
|
||||
ln -sf "goca-$os-$arch$ext" bin/goca # the one for this machine
|
||||
echo " $out -> bin/goca"
|
||||
else
|
||||
echo " $out"
|
||||
fi
|
||||
done
|
||||
|
||||
# Update README.md version output
|
||||
if [ -f README.md ]; then
|
||||
perl -pi -e "s/goca v$CURRENT_VERSION/goca v$NEW_VERSION/g" README.md
|
||||
fi
|
||||
|
||||
# Build the project
|
||||
echo "🛠️ Building goca binary..."
|
||||
go build -o goca
|
||||
|
||||
echo "🎉 Build successful! version: v$NEW_VERSION"
|
||||
echo "$NV" > version.txt
|
||||
echo "built goca v$NV"
|
||||
|
||||
Reference in New Issue
Block a user