publish.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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`
  53. .text()
  54. .then((x) => x.trim())
  55. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`
  56. .text()
  57. .then((x) => x.trim())
  58. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`
  59. .text()
  60. .then((x) => x.trim())
  61. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`
  62. .text()
  63. .then((x) => x.trim())
  64. const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
  65. // arch
  66. const binaryPkgbuild = [
  67. "# Maintainer: dax",
  68. "# Maintainer: adam",
  69. "",
  70. "pkgname='opencode-bin'",
  71. `pkgver=${pkgver}`,
  72. `_subver=${_subver}`,
  73. "options=('!debug' '!strip')",
  74. "pkgrel=1",
  75. "pkgdesc='The AI coding agent built for the terminal.'",
  76. "url='https://github.com/sst/opencode'",
  77. "arch=('aarch64' 'x86_64')",
  78. "license=('MIT')",
  79. "provides=('opencode')",
  80. "conflicts=('opencode')",
  81. "depends=('fzf' 'ripgrep')",
  82. "",
  83. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.zip::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.zip")`,
  84. `sha256sums_aarch64=('${arm64Sha}')`,
  85. "",
  86. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.zip::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.zip")`,
  87. `sha256sums_x86_64=('${x64Sha}')`,
  88. "",
  89. "package() {",
  90. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  91. "}",
  92. "",
  93. ].join("\n")
  94. // Source-based PKGBUILD for opencode
  95. const sourcePkgbuild = [
  96. "# Maintainer: dax",
  97. "# Maintainer: adam",
  98. "",
  99. "pkgname='opencode'",
  100. `pkgver=${pkgver}`,
  101. `_subver=${_subver}`,
  102. "options=('!debug' '!strip')",
  103. "pkgrel=1",
  104. "pkgdesc='The AI coding agent built for the terminal.'",
  105. "url='https://github.com/sst/opencode'",
  106. "arch=('aarch64' 'x86_64')",
  107. "license=('MIT')",
  108. "provides=('opencode')",
  109. "conflicts=('opencode-bin')",
  110. "depends=('fzf' 'ripgrep')",
  111. "makedepends=('git' 'bun-bin' 'go')",
  112. "",
  113. `source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v\${pkgver}\${_subver}.tar.gz")`,
  114. `sha256sums=('SKIP')`,
  115. "",
  116. "build() {",
  117. ` cd "opencode-\${pkgver}"`,
  118. ` bun install`,
  119. " cd packages/tui",
  120. ` CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=\${pkgver}" -o tui cmd/opencode/main.go`,
  121. " cd ../opencode",
  122. ` bun build --define OPENCODE_TUI_PATH="'$(realpath ../tui/tui)'" --define OPENCODE_VERSION="'\${pkgver}'" --compile --target=bun-linux-x64 --outfile=opencode ./src/index.ts`,
  123. "}",
  124. "",
  125. "package() {",
  126. ` cd "opencode-\${pkgver}/packages/opencode"`,
  127. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  128. "}",
  129. "",
  130. ].join("\n")
  131. for (const [pkg, pkgbuild] of [
  132. ["opencode-bin", binaryPkgbuild],
  133. ["opencode", sourcePkgbuild],
  134. ]) {
  135. for (let i = 0; i < 30; i++) {
  136. try {
  137. await $`rm -rf ./dist/aur-${pkg}`
  138. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  139. await $`cd ./dist/aur-${pkg} && git checkout master`
  140. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  141. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  142. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  143. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  144. await $`cd ./dist/aur-${pkg} && git push`
  145. break
  146. } catch (e) {
  147. continue
  148. }
  149. }
  150. }
  151. // Homebrew formula
  152. const homebrewFormula = [
  153. "# typed: false",
  154. "# frozen_string_literal: true",
  155. "",
  156. "# This file was generated by GoReleaser. DO NOT EDIT.",
  157. "class Opencode < Formula",
  158. ` desc "The AI coding agent built for the terminal."`,
  159. ` homepage "https://github.com/sst/opencode"`,
  160. ` version "${Script.version.split("-")[0]}"`,
  161. "",
  162. " on_macos do",
  163. " if Hardware::CPU.intel?",
  164. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
  165. ` sha256 "${macX64Sha}"`,
  166. "",
  167. " def install",
  168. ' bin.install "opencode"',
  169. " end",
  170. " end",
  171. " if Hardware::CPU.arm?",
  172. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
  173. ` sha256 "${macArm64Sha}"`,
  174. "",
  175. " def install",
  176. ' bin.install "opencode"',
  177. " end",
  178. " end",
  179. " end",
  180. "",
  181. " on_linux do",
  182. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  183. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-x64.zip"`,
  184. ` sha256 "${x64Sha}"`,
  185. " def install",
  186. ' bin.install "opencode"',
  187. " end",
  188. " end",
  189. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  190. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-arm64.zip"`,
  191. ` sha256 "${arm64Sha}"`,
  192. " def install",
  193. ' bin.install "opencode"',
  194. " end",
  195. " end",
  196. " end",
  197. "end",
  198. "",
  199. "",
  200. ].join("\n")
  201. await $`rm -rf ./dist/homebrew-tap`
  202. await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap`
  203. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  204. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  205. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
  206. await $`cd ./dist/homebrew-tap && git push`
  207. }