publish.ts 7.8 KB

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