Vectrum
Sign In
202 lines · 8.8 KB · Shell
#!/usr/bin/env bash
# Builds Vectrum Code in release mode and writes two artifacts to ./build:
#   build/vectrum-code                       plain binary
#   build/Vectrum_Code-<version>-<arch>.AppImage
#
# appimagetool is used from $PATH if present, otherwise downloaded once into
# build/.tools. Set APPIMAGETOOL=/path/to/appimagetool to use a specific one.
#
# Options:
#   -v, --version X     set the version to X (0.1.2) first: updates Cargo.toml
#                       and Cargo.lock and commits them as "Version X"
#   -r, --release       then publish the build as a release on Vectrum: tag the
#                       commit with the version, push the tag, and upload the
#                       AppImage, the binary and SHA256SUMS (the first upload
#                       publishes the release). Each release needs a new version.
#   -h, --help          show this
#
# So ./build.sh -v 0.1.2 -r makes and publishes version 0.1.2. Releasing reads:
#   VECTRUM_TOKEN       a read & write API token (vec_…); otherwise read from
#                       ~/.config/vectrum/token (or $VECTRUM_TOKEN_FILE)
#   VECTRUM_URL         the server, https://vectrum.cc by default
#   VECTRUM_REPO        owner/repo, taken from the origin remote by default
set -euo pipefail

log() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
die() { printf '\033[1;31mxx\033[0m %s\n' "$*" >&2; exit 1; }

RELEASE=0
NEW_VERSION=""
while (( $# )); do
    case "$1" in
        -r|--release) RELEASE=1 ;;
        -v|--version)
            [[ $# -ge 2 ]] || die "$1 needs a version, like $1 0.1.2"
            NEW_VERSION="$2"
            shift
            ;;
        --version=*) NEW_VERSION="${1#*=}" ;;
        -h|--help) sed -n '2,/^set -euo/p' "$0" | sed '$d; s/^# \{0,1\}//'; exit 0 ;;
        *) printf 'Unknown option: %s (try --help)\n' "$1" >&2; exit 2 ;;
    esac
    shift
done

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD="$ROOT/build"
TOOLS="$BUILD/.tools"
APPDIR="$BUILD/AppDir"

NAME="vectrum-code"
PRETTY="Vectrum Code"
VERSION="$(sed -n 's/^version *= *"\(.*\)"/\1/p' "$ROOT/Cargo.toml" | head -n1)"
ARCH="$(uname -m)"

dirty() { [[ -n "$(git -C "$ROOT" status --porcelain)" ]]; }

# Stops if tag $1 already names a commit other than $2 (any commit, if $2 is
# empty), here or on origin. Sets REMOTE_TAG and LOCAL_TAG.
check_tag() {
    REMOTE_TAG="$(git -C "$ROOT" ls-remote origin "refs/tags/$1^{}" "refs/tags/$1" | awk 'NR==1 || /\^\{\}$/ {sha=$1} END {print sha}')"
    LOCAL_TAG="$(git -C "$ROOT" rev-parse -q --verify "refs/tags/$1^{commit}" || true)"
    for tagged in "$REMOTE_TAG" "$LOCAL_TAG"; do
        [[ -z "$tagged" || "$tagged" == "$2" ]] ||
            die "$1 is already tagged at ${tagged:0:7}; pick a new version with -v"
    done
}

# ── 0. Version and release checks, before spending time on a build ───────────
if (( RELEASE )); then
    SERVER="${VECTRUM_URL:-https://vectrum.cc}"
    SERVER="${SERVER%/}"
    TOKEN_FILE="${VECTRUM_TOKEN_FILE:-$HOME/.config/vectrum/token}"
    TOKEN="${VECTRUM_TOKEN:-}"
    if [[ -z "$TOKEN" && -r "$TOKEN_FILE" ]]; then
        TOKEN="$(tr -d '[:space:]' < "$TOKEN_FILE")"
    fi
    [[ -n "$TOKEN" ]] || die "No API token: set VECTRUM_TOKEN or save a read & write token to $TOKEN_FILE"
    # ssh://git@git.vectrum.cc:2222/vectrum/code.git, https://…/vectrum/code → vectrum/code
    REPO="${VECTRUM_REPO:-$(git -C "$ROOT" remote get-url origin | sed -E 's#\.git$##; s#^.*[:/]([^/:]+/[^/:]+)$#\1#')}"
    [[ "$REPO" == */* ]] || die "Couldn't tell the repository from the origin remote; set VECTRUM_REPO=owner/repo"
fi

if [[ -n "$NEW_VERSION" && "$NEW_VERSION" != "$VERSION" ]]; then
    [[ "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?$ ]] ||
        die "$NEW_VERSION isn't a version like 0.1.2"
    ! dirty || die "Commit or stash your changes first: the new version is committed on its own"
    if (( RELEASE )); then
        check_tag "$NEW_VERSION" ""
    fi
    log "Version $VERSION → $NEW_VERSION"
    sed -i "0,/^version *= *\".*\"/s//version = \"$NEW_VERSION\"/" "$ROOT/Cargo.toml"
    cargo update --workspace --offline --quiet --manifest-path "$ROOT/Cargo.toml"
    git -C "$ROOT" commit -q -m "Version $NEW_VERSION" -- Cargo.toml Cargo.lock
    VERSION="$NEW_VERSION"
fi

if (( RELEASE )); then
    TAG="$VERSION"
    ! dirty || die "Commit or stash your changes first: a release is built from a commit"
    HEAD="$(git -C "$ROOT" rev-parse HEAD)"
    check_tag "$TAG" "$HEAD"
    log "Releasing $TAG of $REPO on $SERVER from ${HEAD:0:7}"
fi

# ── 1. Binary ────────────────────────────────────────────────────────────────
log "Building $NAME $VERSION (release, $ARCH)"
cargo build --release --manifest-path "$ROOT/Cargo.toml"

TARGET_DIR="$(cargo metadata --format-version 1 --no-deps --manifest-path "$ROOT/Cargo.toml" |
    sed -n 's/.*"target_directory":"\([^"]*\)".*/\1/p')"
mkdir -p "$BUILD"
install -m755 "$TARGET_DIR/release/$NAME" "$BUILD/$NAME"
log "Binary: build/$NAME"

# ── 2. AppDir ────────────────────────────────────────────────────────────────
rm -rf "$APPDIR"
mkdir -p "$APPDIR/usr/bin" \
         "$APPDIR/usr/share/applications" \
         "$APPDIR/usr/share/icons/hicolor/scalable/apps"

install -m755 "$BUILD/$NAME" "$APPDIR/usr/bin/$NAME"

cat > "$APPDIR/usr/share/applications/$NAME.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=$PRETTY
Comment=A T3 Code-style GUI for Claude Code
Exec=$NAME
Icon=$NAME
Terminal=false
Categories=Development;IDE;
StartupWMClass=$NAME
EOF

install -m644 "$ROOT/assets/icons/$NAME.svg" "$APPDIR/usr/share/icons/hicolor/scalable/apps/$NAME.svg"

cat > "$APPDIR/AppRun" <<'EOF'
#!/bin/sh
HERE="$(dirname "$(readlink -f "$0")")"
exec "$HERE/usr/bin/vectrum-code" "$@"
EOF
chmod +x "$APPDIR/AppRun"

ln -sf "usr/share/applications/$NAME.desktop" "$APPDIR/$NAME.desktop"
ln -sf "usr/share/icons/hicolor/scalable/apps/$NAME.svg" "$APPDIR/$NAME.svg"
ln -sf "$NAME.svg" "$APPDIR/.DirIcon"

# ── 3. appimagetool ──────────────────────────────────────────────────────────
APPIMAGETOOL="${APPIMAGETOOL:-$(command -v appimagetool || true)}"
if [[ -z "$APPIMAGETOOL" ]]; then
    APPIMAGETOOL="$TOOLS/appimagetool-$ARCH.AppImage"
    if [[ ! -x "$APPIMAGETOOL" ]]; then
        log "Downloading appimagetool"
        mkdir -p "$TOOLS"
        curl -fL --progress-bar -o "$APPIMAGETOOL.part" \
            "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-$ARCH.AppImage"
        chmod +x "$APPIMAGETOOL.part"
        mv "$APPIMAGETOOL.part" "$APPIMAGETOOL"
    fi
fi

# ── 4. AppImage ──────────────────────────────────────────────────────────────
OUT="$BUILD/${PRETTY// /_}-$VERSION-$ARCH.AppImage"
log "Packaging AppImage"
# Extract-and-run so this works on systems without FUSE (containers, CI).
APPIMAGE_EXTRACT_AND_RUN=1 ARCH="$ARCH" "$APPIMAGETOOL" --no-appstream "$APPDIR" "$OUT"
rm -rf "$APPDIR"

log "AppImage: ${OUT#"$ROOT"/}"

# ── 5. Release ───────────────────────────────────────────────────────────────
if (( RELEASE )); then
    STAGE="$BUILD/release-$TAG"
    rm -rf "$STAGE"
    mkdir -p "$STAGE"
    cp "$OUT" "$STAGE/"
    cp "$BUILD/$NAME" "$STAGE/$NAME-$VERSION-$ARCH"
    (cd "$STAGE" && sha256sum -- * > SHA256SUMS)

    if [[ -z "$LOCAL_TAG" ]]; then
        git -C "$ROOT" tag -a "$TAG" -m "$PRETTY $VERSION" "$HEAD"
    fi
    if [[ -z "$REMOTE_TAG" ]]; then
        log "Pushing tag $TAG"
        git -C "$ROOT" push -q origin "refs/tags/$TAG"
    fi

    # The checksums go last, so the release never shows them alone.
    for name in "$(basename "$OUT")" "$NAME-$VERSION-$ARCH" SHA256SUMS; do
        file="$STAGE/$name"
        log "Uploading $name"
        # The token goes in through a file descriptor, so it never shows in ps.
        if ! reply="$(curl -sS --fail-with-body -T "$file" -H @<(printf 'Authorization: Bearer %s\n' "$TOKEN") \
            "$SERVER/$REPO/releases/download/$TAG/$name" 2>&1)"; then
            die "Uploading $name failed: $reply (run the same command again to retry)"
        fi
    done
    log "Released: $SERVER/$REPO/releases/tag/$TAG"
fi
if [[ -n "$NEW_VERSION" ]] && [[ -n "$(git -C "$ROOT" log --oneline '@{upstream}..HEAD' 2>/dev/null || echo unpushed)" ]]; then
    log "The version commit is only on your $(git -C "$ROOT" branch --show-current) branch here: git push to share it"
fi