publish.ts 8.2 KB

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