|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +usage() { |
| 5 | + echo "Usage: $0 [--version <version>] [--assignee <github handle>]" |
| 6 | + echo " --version <version> Set the VERSION variable to fetch and generate the cask file for" |
| 7 | + echo " --assignee <github handle> Set the ASSIGNE variable to assign the PR to (optional)" |
| 8 | + echo " -h, --help Display this help message" |
| 9 | +} |
| 10 | + |
| 11 | +VERSION="" |
| 12 | +ASSIGNE="" |
| 13 | + |
| 14 | +# Parse command line arguments |
| 15 | +while [[ "$#" -gt 0 ]]; do |
| 16 | + case $1 in |
| 17 | + --version) |
| 18 | + VERSION="$2" |
| 19 | + shift 2 |
| 20 | + ;; |
| 21 | + --assignee) |
| 22 | + ASSIGNE="$2" |
| 23 | + shift 2 |
| 24 | + ;; |
| 25 | + -h | --help) |
| 26 | + usage |
| 27 | + exit 0 |
| 28 | + ;; |
| 29 | + *) |
| 30 | + echo "Unknown parameter passed: $1" |
| 31 | + usage |
| 32 | + exit 1 |
| 33 | + ;; |
| 34 | + esac |
| 35 | +done |
| 36 | + |
| 37 | +# Assert version is not empty and starts with v |
| 38 | +[ -z "$VERSION" ] && { |
| 39 | + echo "Error: VERSION cannot be empty" |
| 40 | + exit 1 |
| 41 | +} |
| 42 | +[[ "$VERSION" =~ ^v || "$VERSION" == "preview" ]] || { |
| 43 | + echo "Error: VERSION must start with a 'v'" |
| 44 | + exit 1 |
| 45 | +} |
| 46 | + |
| 47 | +# Download the Coder Desktop dmg |
| 48 | +GH_RELEASE_FOLDER=$(mktemp -d) |
| 49 | + |
| 50 | +gh release download "$VERSION" \ |
| 51 | + --repo coder/coder-desktop-macos \ |
| 52 | + --dir "$GH_RELEASE_FOLDER" \ |
| 53 | + --pattern 'Coder.Desktop.dmg' |
| 54 | + |
| 55 | +HASH=$(shasum -a 256 "$GH_RELEASE_FOLDER"/Coder.Desktop.dmg | awk '{print $1}' | tr -d '\n') |
| 56 | + |
| 57 | +IS_PREVIEW=false |
| 58 | +if [[ "$VERSION" == "preview" ]]; then |
| 59 | + IS_PREVIEW=true |
| 60 | + VERSION=$(make 'print-CURRENT_PROJECT_VERSION' | sed 's/CURRENT_PROJECT_VERSION=//g') |
| 61 | +fi |
| 62 | + |
| 63 | +# Check out the homebrew tap repo |
| 64 | +TAP_CHECHOUT_FOLDER=$(mktemp -d) |
| 65 | + |
| 66 | +gh repo clone "coder/homebrew-coder" "$TAP_CHECHOUT_FOLDER" |
| 67 | + |
| 68 | +cd "$TAP_CHECHOUT_FOLDER" |
| 69 | + |
| 70 | +BREW_BRANCH="auto-release/desktop-$VERSION" |
| 71 | + |
| 72 | +# Check if a PR already exists. |
| 73 | +# Continue on a main branch release, as the sha256 will change. |
| 74 | +pr_count="$(gh pr list --search "head:$BREW_BRANCH" --json id,closed | jq -r ".[] | select(.closed == false) | .id" | wc -l)" |
| 75 | +if [[ "$pr_count" -gt 0 && "$IS_PREVIEW" == false ]]; then |
| 76 | + echo "Bailing out as PR already exists" 2>&1 |
| 77 | + exit 0 |
| 78 | +fi |
| 79 | + |
| 80 | +git checkout -b "$BREW_BRANCH" |
| 81 | + |
| 82 | +# If this is a main branch build, append a preview suffix to the cask. |
| 83 | +SUFFIX="" |
| 84 | +CONFLICTS_WITH="coder-desktop-preview" |
| 85 | +TAG=$VERSION |
| 86 | +if [[ "$IS_PREVIEW" == true ]]; then |
| 87 | + SUFFIX="-preview" |
| 88 | + CONFLICTS_WITH="coder-desktop" |
| 89 | + TAG="preview" |
| 90 | +fi |
| 91 | + |
| 92 | +mkdir -p "$TAP_CHECHOUT_FOLDER"/Casks |
| 93 | + |
| 94 | +# Overwrite the cask file |
| 95 | +cat >"$TAP_CHECHOUT_FOLDER"/Casks/coder-desktop${SUFFIX}.rb <<EOF |
| 96 | +cask "coder-desktop${SUFFIX}" do |
| 97 | + version "${VERSION#v}" |
| 98 | + sha256 "${HASH}" |
| 99 | +
|
| 100 | + url "https://github.com/coder/coder-desktop-macos/releases/download/${TAG}/Coder.Desktop.dmg" |
| 101 | + name "Coder Desktop" |
| 102 | + desc "Coder Desktop client" |
| 103 | + homepage "https://github.com/coder/coder-desktop-macos" |
| 104 | +
|
| 105 | + depends_on macos: ">= :sonoma" |
| 106 | +
|
| 107 | + app "Coder Desktop.app" |
| 108 | + conflicts_with cask: "coder/coder/${CONFLICTS_WITH}" |
| 109 | +end |
| 110 | +EOF |
| 111 | + |
| 112 | +git add . |
| 113 | +git commit -m "Coder Desktop $VERSION" |
| 114 | +git push -u origin -f "$BREW_BRANCH" |
| 115 | + |
| 116 | +# Create a PR only if none exists |
| 117 | +if [[ "$pr_count" -eq 0 ]]; then |
| 118 | + gh pr create \ |
| 119 | + --base master --head "$BREW_BRANCH" \ |
| 120 | + --title "Coder Desktop $VERSION" \ |
| 121 | + --body "This automatic PR was triggered by the release of Coder Desktop $VERSION" \ |
| 122 | + ${ASSIGNE:+ --assignee "$ASSIGNE" --reviewer "$ASSIGNE"} |
| 123 | +fi |
0 commit comments