|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { $ } from "bun" |
| 3 | +import { Script } from "@opencode-ai/script" |
| 4 | + |
| 5 | +if (!Script.preview) { |
| 6 | + // Calculate SHA values |
| 7 | + const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim()) |
| 8 | + const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim()) |
| 9 | + const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim()) |
| 10 | + const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim()) |
| 11 | + |
| 12 | + const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2) |
| 13 | + |
| 14 | + // arch |
| 15 | + const binaryPkgbuild = [ |
| 16 | + "# Maintainer: dax", |
| 17 | + "# Maintainer: adam", |
| 18 | + "", |
| 19 | + "pkgname='opencode-bin'", |
| 20 | + `pkgver=${pkgver}`, |
| 21 | + `_subver=${_subver}`, |
| 22 | + "options=('!debug' '!strip')", |
| 23 | + "pkgrel=1", |
| 24 | + "pkgdesc='The AI coding agent built for the terminal.'", |
| 25 | + "url='https://github.com/sst/opencode'", |
| 26 | + "arch=('aarch64' 'x86_64')", |
| 27 | + "license=('MIT')", |
| 28 | + "provides=('opencode')", |
| 29 | + "conflicts=('opencode')", |
| 30 | + "depends=('ripgrep')", |
| 31 | + "", |
| 32 | + `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.tar.gz::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.tar.gz")`, |
| 33 | + `sha256sums_aarch64=('${arm64Sha}')`, |
| 34 | + |
| 35 | + `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.tar.gz::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.tar.gz")`, |
| 36 | + `sha256sums_x86_64=('${x64Sha}')`, |
| 37 | + "", |
| 38 | + "package() {", |
| 39 | + ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"', |
| 40 | + "}", |
| 41 | + "", |
| 42 | + ].join("\n") |
| 43 | + |
| 44 | + // Source-based PKGBUILD for opencode |
| 45 | + const sourcePkgbuild = [ |
| 46 | + "# Maintainer: dax", |
| 47 | + "# Maintainer: adam", |
| 48 | + "", |
| 49 | + "pkgname='opencode'", |
| 50 | + `pkgver=${pkgver}`, |
| 51 | + `_subver=${_subver}`, |
| 52 | + "options=('!debug' '!strip')", |
| 53 | + "pkgrel=1", |
| 54 | + "pkgdesc='The AI coding agent built for the terminal.'", |
| 55 | + "url='https://github.com/sst/opencode'", |
| 56 | + "arch=('aarch64' 'x86_64')", |
| 57 | + "license=('MIT')", |
| 58 | + "provides=('opencode')", |
| 59 | + "conflicts=('opencode-bin')", |
| 60 | + "depends=('ripgrep')", |
| 61 | + "makedepends=('git' 'bun-bin' 'go')", |
| 62 | + "", |
| 63 | + `source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v\${pkgver}\${_subver}.tar.gz")`, |
| 64 | + `sha256sums=('SKIP')`, |
| 65 | + "", |
| 66 | + "build() {", |
| 67 | + ` cd "opencode-\${pkgver}"`, |
| 68 | + ` bun install`, |
| 69 | + " cd ./packages/opencode", |
| 70 | + ` OPENCODE_CHANNEL=latest OPENCODE_VERSION=${pkgver} bun run ./script/build.ts --single`, |
| 71 | + "}", |
| 72 | + "", |
| 73 | + "package() {", |
| 74 | + ` cd "opencode-\${pkgver}/packages/opencode"`, |
| 75 | + ' mkdir -p "${pkgdir}/usr/bin"', |
| 76 | + ' target_arch="x64"', |
| 77 | + ' case "$CARCH" in', |
| 78 | + ' x86_64) target_arch="x64" ;;', |
| 79 | + ' aarch64) target_arch="arm64" ;;', |
| 80 | + ' *) printf "unsupported architecture: %s\\n" "$CARCH" >&2 ; return 1 ;;', |
| 81 | + " esac", |
| 82 | + ' libc=""', |
| 83 | + " if command -v ldd >/dev/null 2>&1; then", |
| 84 | + " if ldd --version 2>&1 | grep -qi musl; then", |
| 85 | + ' libc="-musl"', |
| 86 | + " fi", |
| 87 | + " fi", |
| 88 | + ' if [ -z "$libc" ] && ls /lib/ld-musl-* >/dev/null 2>&1; then', |
| 89 | + ' libc="-musl"', |
| 90 | + " fi", |
| 91 | + ' base=""', |
| 92 | + ' if [ "$target_arch" = "x64" ]; then', |
| 93 | + " if ! grep -qi avx2 /proc/cpuinfo 2>/dev/null; then", |
| 94 | + ' base="-baseline"', |
| 95 | + " fi", |
| 96 | + " fi", |
| 97 | + ' bin="dist/opencode-linux-${target_arch}${base}${libc}/bin/opencode"', |
| 98 | + ' if [ ! -f "$bin" ]; then', |
| 99 | + ' printf "unable to find binary for %s%s%s\\n" "$target_arch" "$base" "$libc" >&2', |
| 100 | + " return 1", |
| 101 | + " fi", |
| 102 | + ' install -Dm755 "$bin" "${pkgdir}/usr/bin/opencode"', |
| 103 | + "}", |
| 104 | + "", |
| 105 | + ].join("\n") |
| 106 | + |
| 107 | + for (const [pkg, pkgbuild] of [ |
| 108 | + ["opencode-bin", binaryPkgbuild], |
| 109 | + ["opencode", sourcePkgbuild], |
| 110 | + ]) { |
| 111 | + for (let i = 0; i < 30; i++) { |
| 112 | + try { |
| 113 | + await $`rm -rf ./dist/aur-${pkg}` |
| 114 | + await $`git clone ssh://aur@aur.archlinux.org/${pkg}.git ./dist/aur-${pkg}` |
| 115 | + await $`cd ./dist/aur-${pkg} && git checkout master` |
| 116 | + await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild) |
| 117 | + await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO` |
| 118 | + await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO` |
| 119 | + await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"` |
| 120 | + await $`cd ./dist/aur-${pkg} && git push` |
| 121 | + break |
| 122 | + } catch (e) { |
| 123 | + continue |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Homebrew formula |
| 129 | + const homebrewFormula = [ |
| 130 | + "# typed: false", |
| 131 | + "# frozen_string_literal: true", |
| 132 | + "", |
| 133 | + "# This file was generated by GoReleaser. DO NOT EDIT.", |
| 134 | + "class Opencode < Formula", |
| 135 | + ` desc "The AI coding agent built for the terminal."`, |
| 136 | + ` homepage "https://github.com/sst/opencode"`, |
| 137 | + ` version "${Script.version.split("-")[0]}"`, |
| 138 | + "", |
| 139 | + ` depends_on "ripgrep"`, |
| 140 | + "", |
| 141 | + " on_macos do", |
| 142 | + " if Hardware::CPU.intel?", |
| 143 | + ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`, |
| 144 | + ` sha256 "${macX64Sha}"`, |
| 145 | + "", |
| 146 | + " def install", |
| 147 | + ' bin.install "opencode"', |
| 148 | + " end", |
| 149 | + " end", |
| 150 | + " if Hardware::CPU.arm?", |
| 151 | + ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`, |
| 152 | + ` sha256 "${macArm64Sha}"`, |
| 153 | + "", |
| 154 | + " def install", |
| 155 | + ' bin.install "opencode"', |
| 156 | + " end", |
| 157 | + " end", |
| 158 | + " end", |
| 159 | + "", |
| 160 | + " on_linux do", |
| 161 | + " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?", |
| 162 | + ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-x64.tar.gz"`, |
| 163 | + ` sha256 "${x64Sha}"`, |
| 164 | + " def install", |
| 165 | + ' bin.install "opencode"', |
| 166 | + " end", |
| 167 | + " end", |
| 168 | + " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?", |
| 169 | + ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-arm64.tar.gz"`, |
| 170 | + ` sha256 "${arm64Sha}"`, |
| 171 | + " def install", |
| 172 | + ' bin.install "opencode"', |
| 173 | + " end", |
| 174 | + " end", |
| 175 | + " end", |
| 176 | + "end", |
| 177 | + "", |
| 178 | + "", |
| 179 | + ].join("\n") |
| 180 | + |
| 181 | + await $`rm -rf ./dist/homebrew-tap` |
| 182 | + await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap` |
| 183 | + await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula) |
| 184 | + await $`cd ./dist/homebrew-tap && git add opencode.rb` |
| 185 | + await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"` |
| 186 | + await $`cd ./dist/homebrew-tap && git push` |
| 187 | +} |
0 commit comments