publish.ts 7.2 KB

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