opencode_offline/packages/opencode/script/publish.ts

218 lines
7.2 KiB
TypeScript
Raw Normal View History

2025-05-31 08:47:56 +08:00
#!/usr/bin/env bun
import { $ } from "bun"
import pkg from "../package.json"
2025-10-15 02:34:19 +08:00
import { Script } from "@opencode-ai/script"
import { fileURLToPath } from "url"
2025-05-31 08:47:56 +08:00
const dir = fileURLToPath(new URL("..", import.meta.url))
2025-10-15 02:34:19 +08:00
process.chdir(dir)
2025-05-31 08:47:56 +08:00
2025-09-19 18:10:27 +08:00
const { binaries } = await import("./build.ts")
{
const name = `${pkg.name}-${process.platform}-${process.arch}`
console.log(`smoke test: running dist/${name}/bin/opencode --version`)
await $`./dist/${name}/bin/opencode --version`
2025-05-31 08:47:56 +08:00
}
await $`mkdir -p ./dist/${pkg.name}`
await $`cp -r ./bin ./dist/${pkg.name}/bin`
2025-06-16 01:10:58 +08:00
await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
2025-05-31 08:47:56 +08:00
await Bun.file(`./dist/${pkg.name}/package.json`).write(
JSON.stringify(
{
name: pkg.name + "-ai",
bin: {
[pkg.name]: `./bin/${pkg.name}`,
2025-05-31 08:47:56 +08:00
},
scripts: {
2025-11-01 03:07:36 +08:00
postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs",
},
2025-10-15 02:34:19 +08:00
version: Script.version,
2025-09-19 18:10:27 +08:00
optionalDependencies: binaries,
2025-05-31 08:47:56 +08:00
},
null,
2,
),
)
2025-09-22 13:45:23 +08:00
for (const [name] of Object.entries(binaries)) {
try {
process.chdir(`./dist/${name}`)
if (process.platform !== "win32") {
await $`chmod 755 -R .`
}
await $`bun publish --access public --tag ${Script.channel}`
} finally {
process.chdir(dir)
}
2025-09-19 18:10:27 +08:00
}
2025-10-15 02:51:35 +08:00
await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${Script.channel}`
2025-06-12 23:00:37 +08:00
2025-10-29 08:28:30 +08:00
if (!Script.preview) {
const major = Script.version.split(".")[0]
2025-10-29 08:35:18 +08:00
const majorTag = `latest-${major}`
2025-10-29 08:28:30 +08:00
for (const [name] of Object.entries(binaries)) {
await $`cd dist/${name} && npm dist-tag add ${name}@${Script.version} ${majorTag}`
}
await $`cd ./dist/${pkg.name} && npm dist-tag add ${pkg.name}-ai@${Script.version} ${majorTag}`
}
2025-10-15 02:34:19 +08:00
if (!Script.preview) {
2025-09-19 18:10:27 +08:00
for (const key of Object.keys(binaries)) {
2025-06-12 23:02:51 +08:00
await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
}
2025-06-12 23:00:37 +08:00
2025-06-14 04:17:27 +08:00
// Calculate SHA values
2025-11-08 09:59:02 +08:00
const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
2025-06-14 04:17:27 +08:00
const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
2025-10-05 19:11:43 +08:00
// arch
2025-08-25 06:31:44 +08:00
const binaryPkgbuild = [
2025-08-14 03:44:11 +08:00
"# Maintainer: dax",
"# Maintainer: adam",
"",
2025-08-25 06:31:44 +08:00
"pkgname='opencode-bin'",
`pkgver=${pkgver}`,
`_subver=${_subver}`,
2025-08-14 03:44:11 +08:00
"options=('!debug' '!strip')",
"pkgrel=1",
"pkgdesc='The AI coding agent built for the terminal.'",
"url='https://github.com/sst/opencode'",
"arch=('aarch64' 'x86_64')",
"license=('MIT')",
"provides=('opencode')",
"conflicts=('opencode')",
"depends=('fzf' 'ripgrep')",
"",
`source_aarch64=("\${pkgname}_\${pkgver}_aarch64.zip::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.zip")`,
2025-08-14 03:44:11 +08:00
`sha256sums_aarch64=('${arm64Sha}')`,
"",
`source_x86_64=("\${pkgname}_\${pkgver}_x86_64.zip::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.zip")`,
2025-08-14 03:44:11 +08:00
`sha256sums_x86_64=('${x64Sha}')`,
"",
"package() {",
' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
"}",
"",
].join("\n")
2025-06-12 23:55:07 +08:00
2025-08-25 06:31:44 +08:00
// Source-based PKGBUILD for opencode
const sourcePkgbuild = [
"# Maintainer: dax",
"# Maintainer: adam",
"",
"pkgname='opencode'",
`pkgver=${pkgver}`,
`_subver=${_subver}`,
2025-08-25 06:31:44 +08:00
"options=('!debug' '!strip')",
"pkgrel=1",
"pkgdesc='The AI coding agent built for the terminal.'",
"url='https://github.com/sst/opencode'",
"arch=('aarch64' 'x86_64')",
"license=('MIT')",
"provides=('opencode')",
"conflicts=('opencode-bin')",
"depends=('fzf' 'ripgrep')",
"makedepends=('git' 'bun-bin' 'go')",
"",
`source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v\${pkgver}\${_subver}.tar.gz")`,
2025-08-25 06:31:44 +08:00
`sha256sums=('SKIP')`,
"",
"build() {",
` cd "opencode-\${pkgver}"`,
2025-08-25 06:55:32 +08:00
` bun install`,
" cd ./packages/opencode",
` OPENCODE_CHANNEL=latest OPENCODE_VERSION=${pkgver} bun run ./script/build.ts --single`,
2025-08-25 06:31:44 +08:00
"}",
"",
"package() {",
` cd "opencode-\${pkgver}/packages/opencode"`,
2025-11-06 02:03:24 +08:00
' install -Dm755 $(find dist/*/bin/opencode) "${pkgdir}/usr/bin/opencode"',
2025-08-25 06:31:44 +08:00
"}",
"",
].join("\n")
for (const [pkg, pkgbuild] of [
["opencode-bin", binaryPkgbuild],
["opencode", sourcePkgbuild],
]) {
2025-10-12 12:22:16 +08:00
for (let i = 0; i < 30; i++) {
2025-08-25 06:48:03 +08:00
try {
2025-10-12 12:22:16 +08:00
await $`rm -rf ./dist/aur-${pkg}`
2025-08-25 06:48:03 +08:00
await $`git clone ssh://aur@aur.archlinux.org/${pkg}.git ./dist/aur-${pkg}`
2025-10-12 12:22:16 +08:00
await $`cd ./dist/aur-${pkg} && git checkout master`
await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
2025-10-15 02:34:19 +08:00
await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
2025-10-12 12:22:16 +08:00
await $`cd ./dist/aur-${pkg} && git push`
2025-08-25 06:48:03 +08:00
break
} catch (e) {
continue
}
}
2025-08-14 03:44:11 +08:00
}
2025-06-14 04:17:27 +08:00
// Homebrew formula
const homebrewFormula = [
"# typed: false",
"# frozen_string_literal: true",
"",
"# This file was generated by GoReleaser. DO NOT EDIT.",
"class Opencode < Formula",
` desc "The AI coding agent built for the terminal."`,
` homepage "https://github.com/sst/opencode"`,
2025-10-15 02:34:19 +08:00
` version "${Script.version.split("-")[0]}"`,
2025-06-14 04:17:27 +08:00
"",
" on_macos do",
" if Hardware::CPU.intel?",
2025-10-15 02:34:19 +08:00
` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
2025-06-14 04:17:27 +08:00
` sha256 "${macX64Sha}"`,
"",
" def install",
' bin.install "opencode"',
" end",
" end",
" if Hardware::CPU.arm?",
2025-10-15 02:34:19 +08:00
` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
2025-06-14 04:17:27 +08:00
` sha256 "${macArm64Sha}"`,
"",
" def install",
' bin.install "opencode"',
" end",
" end",
" end",
"",
" on_linux do",
" if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
2025-10-15 02:34:19 +08:00
` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-x64.zip"`,
2025-06-14 04:17:27 +08:00
` sha256 "${x64Sha}"`,
" def install",
' bin.install "opencode"',
" end",
" end",
" if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
2025-10-15 02:34:19 +08:00
` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-arm64.zip"`,
2025-06-14 04:17:27 +08:00
` sha256 "${arm64Sha}"`,
" def install",
' bin.install "opencode"',
" end",
" end",
" end",
"end",
"",
"",
].join("\n")
await $`rm -rf ./dist/homebrew-tap`
2025-06-14 04:29:57 +08:00
await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap`
2025-06-14 04:17:27 +08:00
await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
await $`cd ./dist/homebrew-tap && git add opencode.rb`
2025-10-15 02:34:19 +08:00
await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
2025-09-22 13:45:23 +08:00
await $`cd ./dist/homebrew-tap && git push`
2025-06-12 23:02:51 +08:00
}