publish.ts 7.2 KB

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