publish.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/env bun
  2. const dir = new URL("..", import.meta.url).pathname
  3. process.chdir(dir)
  4. import { $ } from "bun"
  5. import pkg from "../package.json"
  6. const dry = process.env["OPENCODE_DRY"] === "true"
  7. const version = process.env["OPENCODE_VERSION"]!
  8. const snapshot = process.env["OPENCODE_SNAPSHOT"] === "true"
  9. console.log(`publishing ${version}`)
  10. const GOARCH: Record<string, string> = {
  11. arm64: "arm64",
  12. x64: "amd64",
  13. "x64-baseline": "amd64",
  14. }
  15. const targets = [
  16. ["windows", "x64"],
  17. ["linux", "arm64"],
  18. ["linux", "x64"],
  19. ["linux", "x64-baseline"],
  20. ["darwin", "x64"],
  21. ["darwin", "x64-baseline"],
  22. ["darwin", "arm64"],
  23. ]
  24. await $`rm -rf dist`
  25. const optionalDependencies: Record<string, string> = {}
  26. const npmTag = snapshot ? "snapshot" : "latest"
  27. for (const [os, arch] of targets) {
  28. console.log(`building ${os}-${arch}`)
  29. const name = `${pkg.name}-${os}-${arch}`
  30. await $`mkdir -p dist/${name}/bin`
  31. await $`CGO_ENABLED=0 GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X main.Version=${version}" -o ../opencode/dist/${name}/bin/tui ../tui/cmd/opencode/main.go`.cwd(
  32. "../tui",
  33. )
  34. await Bun.build({
  35. compile: {
  36. target: `bun-${os}-${arch}` as any,
  37. outfile: `dist/${name}/bin/opencode`,
  38. execArgv: [`--user-agent=opencode/${version}`],
  39. windows: {},
  40. },
  41. entrypoints: ["./src/index.ts"],
  42. define: {
  43. OPENCODE_VERSION: `'${version}'`,
  44. OPENCODE_TUI_PATH: `'../../../dist/${name}/bin/tui'`,
  45. },
  46. })
  47. // await $`bun build --define OPENCODE_TUI_PATH="'../../../dist/${name}/bin/tui'" --define OPENCODE_VERSION="'${version}'" --compile --target=bun-${os}-${arch} --outfile=dist/${name}/bin/opencode ./src/index.ts`
  48. // Run the binary only if it matches current OS/arch
  49. if (
  50. process.platform === (os === "windows" ? "win32" : os) &&
  51. (process.arch === arch || (process.arch === "x64" && arch === "x64-baseline"))
  52. ) {
  53. console.log(`smoke test: running dist/${name}/bin/opencode --version`)
  54. await $`./dist/${name}/bin/opencode --version`
  55. }
  56. await $`rm -rf ./dist/${name}/bin/tui`
  57. await Bun.file(`dist/${name}/package.json`).write(
  58. JSON.stringify(
  59. {
  60. name,
  61. version,
  62. os: [os === "windows" ? "win32" : os],
  63. cpu: [arch],
  64. },
  65. null,
  66. 2,
  67. ),
  68. )
  69. if (!dry) await $`cd dist/${name} && chmod 777 -R . && bun publish --access public --tag ${npmTag}`
  70. optionalDependencies[name] = version
  71. }
  72. await $`mkdir -p ./dist/${pkg.name}`
  73. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  74. await $`cp ./script/preinstall.mjs ./dist/${pkg.name}/preinstall.mjs`
  75. await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
  76. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  77. JSON.stringify(
  78. {
  79. name: pkg.name + "-ai",
  80. bin: {
  81. [pkg.name]: `./bin/${pkg.name}`,
  82. },
  83. scripts: {
  84. preinstall: "node ./preinstall.mjs",
  85. postinstall: "node ./postinstall.mjs",
  86. },
  87. version,
  88. optionalDependencies,
  89. },
  90. null,
  91. 2,
  92. ),
  93. )
  94. if (!dry) await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${npmTag}`
  95. if (!snapshot) {
  96. for (const key of Object.keys(optionalDependencies)) {
  97. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  98. }
  99. // Calculate SHA values
  100. const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  101. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  102. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  103. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  104. const binaryPkgbuild = [
  105. "# Maintainer: dax",
  106. "# Maintainer: adam",
  107. "",
  108. "pkgname='opencode-bin'",
  109. `pkgver=${version.split("-")[0]}`,
  110. "options=('!debug' '!strip')",
  111. "pkgrel=1",
  112. "pkgdesc='The AI coding agent built for the terminal.'",
  113. "url='https://github.com/sst/opencode'",
  114. "arch=('aarch64' 'x86_64')",
  115. "license=('MIT')",
  116. "provides=('opencode')",
  117. "conflicts=('opencode')",
  118. "depends=('fzf' 'ripgrep')",
  119. "",
  120. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.zip::https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip")`,
  121. `sha256sums_aarch64=('${arm64Sha}')`,
  122. "",
  123. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.zip::https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip")`,
  124. `sha256sums_x86_64=('${x64Sha}')`,
  125. "",
  126. "package() {",
  127. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  128. "}",
  129. "",
  130. ].join("\n")
  131. // Source-based PKGBUILD for opencode
  132. const sourcePkgbuild = [
  133. "# Maintainer: dax",
  134. "# Maintainer: adam",
  135. "",
  136. "pkgname='opencode'",
  137. `pkgver=${version.split("-")[0]}`,
  138. "options=('!debug' '!strip')",
  139. "pkgrel=1",
  140. "pkgdesc='The AI coding agent built for the terminal.'",
  141. "url='https://github.com/sst/opencode'",
  142. "arch=('aarch64' 'x86_64')",
  143. "license=('MIT')",
  144. "provides=('opencode')",
  145. "conflicts=('opencode-bin')",
  146. "depends=('fzf' 'ripgrep')",
  147. "makedepends=('git' 'bun-bin' 'go')",
  148. "",
  149. `source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v${version}.tar.gz")`,
  150. `sha256sums=('SKIP')`,
  151. "",
  152. "build() {",
  153. ` cd "opencode-\${pkgver}"`,
  154. ` bun install`,
  155. " cd packages/tui",
  156. ` CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=\${pkgver}" -o tui cmd/opencode/main.go`,
  157. " cd ../opencode",
  158. ` bun build --define OPENCODE_TUI_PATH="'$(realpath ../tui/tui)'" --define OPENCODE_VERSION="'\${pkgver}'" --compile --target=bun-linux-x64 --outfile=opencode ./src/index.ts`,
  159. "}",
  160. "",
  161. "package() {",
  162. ` cd "opencode-\${pkgver}/packages/opencode"`,
  163. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  164. "}",
  165. "",
  166. ].join("\n")
  167. for (const [pkg, pkgbuild] of [
  168. ["opencode-bin", binaryPkgbuild],
  169. ["opencode", sourcePkgbuild],
  170. ]) {
  171. await $`rm -rf ./dist/aur-${pkg}`
  172. while (true) {
  173. try {
  174. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  175. break
  176. } catch (e) {
  177. continue
  178. }
  179. }
  180. await $`cd ./dist/aur-${pkg} && git checkout master`
  181. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  182. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  183. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  184. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${version}"`
  185. if (!dry) await $`cd ./dist/aur-${pkg} && git push`
  186. }
  187. // Homebrew formula
  188. const homebrewFormula = [
  189. "# typed: false",
  190. "# frozen_string_literal: true",
  191. "",
  192. "# This file was generated by GoReleaser. DO NOT EDIT.",
  193. "class Opencode < Formula",
  194. ` desc "The AI coding agent built for the terminal."`,
  195. ` homepage "https://github.com/sst/opencode"`,
  196. ` version "${version.split("-")[0]}"`,
  197. "",
  198. " on_macos do",
  199. " if Hardware::CPU.intel?",
  200. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-darwin-x64.zip"`,
  201. ` sha256 "${macX64Sha}"`,
  202. "",
  203. " def install",
  204. ' bin.install "opencode"',
  205. " end",
  206. " end",
  207. " if Hardware::CPU.arm?",
  208. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-darwin-arm64.zip"`,
  209. ` sha256 "${macArm64Sha}"`,
  210. "",
  211. " def install",
  212. ' bin.install "opencode"',
  213. " end",
  214. " end",
  215. " end",
  216. "",
  217. " on_linux do",
  218. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  219. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip"`,
  220. ` sha256 "${x64Sha}"`,
  221. " def install",
  222. ' bin.install "opencode"',
  223. " end",
  224. " end",
  225. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  226. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip"`,
  227. ` sha256 "${arm64Sha}"`,
  228. " def install",
  229. ' bin.install "opencode"',
  230. " end",
  231. " end",
  232. " end",
  233. "end",
  234. "",
  235. "",
  236. ].join("\n")
  237. await $`rm -rf ./dist/homebrew-tap`
  238. await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap`
  239. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  240. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  241. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${version}"`
  242. if (!dry) await $`cd ./dist/homebrew-tap && git push`
  243. }