publish.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if (key.includes("linux")) {
  57. await $`cd dist/${key}/bin && tar -czf ../../${key}.tar.gz *`
  58. } else {
  59. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  60. }
  61. }
  62. // Calculate SHA values
  63. const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  64. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  65. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  66. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  67. const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
  68. // arch
  69. const binaryPkgbuild = [
  70. "# Maintainer: dax",
  71. "# Maintainer: adam",
  72. "",
  73. "pkgname='opencode-bin'",
  74. `pkgver=${pkgver}`,
  75. `_subver=${_subver}`,
  76. "options=('!debug' '!strip')",
  77. "pkgrel=1",
  78. "pkgdesc='The AI coding agent built for the terminal.'",
  79. "url='https://github.com/sst/opencode'",
  80. "arch=('aarch64' 'x86_64')",
  81. "license=('MIT')",
  82. "provides=('opencode')",
  83. "conflicts=('opencode')",
  84. "depends=('fzf' 'ripgrep')",
  85. "",
  86. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.tar.gz::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.tar.gz")`,
  87. `sha256sums_aarch64=('${arm64Sha}')`,
  88. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.tar.gz::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.tar.gz")`,
  89. `sha256sums_x86_64=('${x64Sha}')`,
  90. "",
  91. "package() {",
  92. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  93. "}",
  94. "",
  95. ].join("\n")
  96. // Source-based PKGBUILD for opencode
  97. const sourcePkgbuild = [
  98. "# Maintainer: dax",
  99. "# Maintainer: adam",
  100. "",
  101. "pkgname='opencode'",
  102. `pkgver=${pkgver}`,
  103. `_subver=${_subver}`,
  104. "options=('!debug' '!strip')",
  105. "pkgrel=1",
  106. "pkgdesc='The AI coding agent built for the terminal.'",
  107. "url='https://github.com/sst/opencode'",
  108. "arch=('aarch64' 'x86_64')",
  109. "license=('MIT')",
  110. "provides=('opencode')",
  111. "conflicts=('opencode-bin')",
  112. "depends=('fzf' 'ripgrep')",
  113. "makedepends=('git' 'bun-bin' 'go')",
  114. "",
  115. `source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v\${pkgver}\${_subver}.tar.gz")`,
  116. `sha256sums=('SKIP')`,
  117. "",
  118. "build() {",
  119. ` cd "opencode-\${pkgver}"`,
  120. ` bun install`,
  121. " cd ./packages/opencode",
  122. ` OPENCODE_CHANNEL=latest OPENCODE_VERSION=${pkgver} bun run ./script/build.ts --single`,
  123. "}",
  124. "",
  125. "package() {",
  126. ` cd "opencode-\${pkgver}/packages/opencode"`,
  127. ' mkdir -p "${pkgdir}/usr/bin"',
  128. ' target_arch="x64"',
  129. ' case "$CARCH" in',
  130. ' x86_64) target_arch="x64" ;;',
  131. ' aarch64) target_arch="arm64" ;;',
  132. ' *) printf "unsupported architecture: %s\\n" "$CARCH" >&2 ; return 1 ;;',
  133. " esac",
  134. ' libc=""',
  135. " if command -v ldd >/dev/null 2>&1; then",
  136. " if ldd --version 2>&1 | grep -qi musl; then",
  137. ' libc="-musl"',
  138. " fi",
  139. " fi",
  140. ' if [ -z "$libc" ] && ls /lib/ld-musl-* >/dev/null 2>&1; then',
  141. ' libc="-musl"',
  142. " fi",
  143. ' base=""',
  144. ' if [ "$target_arch" = "x64" ]; then',
  145. " if ! grep -qi avx2 /proc/cpuinfo 2>/dev/null; then",
  146. ' base="-baseline"',
  147. " fi",
  148. " fi",
  149. ' bin="dist/opencode-linux-${target_arch}${base}${libc}/bin/opencode"',
  150. ' if [ ! -f "$bin" ]; then',
  151. ' printf "unable to find binary for %s%s%s\\n" "$target_arch" "$base" "$libc" >&2',
  152. " return 1",
  153. " fi",
  154. ' install -Dm755 "$bin" "${pkgdir}/usr/bin/opencode"',
  155. "}",
  156. "",
  157. ].join("\n")
  158. for (const [pkg, pkgbuild] of [
  159. ["opencode-bin", binaryPkgbuild],
  160. ["opencode", sourcePkgbuild],
  161. ]) {
  162. for (let i = 0; i < 30; i++) {
  163. try {
  164. await $`rm -rf ./dist/aur-${pkg}`
  165. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  166. await $`cd ./dist/aur-${pkg} && git checkout master`
  167. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  168. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  169. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  170. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  171. await $`cd ./dist/aur-${pkg} && git push`
  172. break
  173. } catch (e) {
  174. continue
  175. }
  176. }
  177. }
  178. // Homebrew formula
  179. const homebrewFormula = [
  180. "# typed: false",
  181. "# frozen_string_literal: true",
  182. "",
  183. "# This file was generated by GoReleaser. DO NOT EDIT.",
  184. "class Opencode < Formula",
  185. ` desc "The AI coding agent built for the terminal."`,
  186. ` homepage "https://github.com/sst/opencode"`,
  187. ` version "${Script.version.split("-")[0]}"`,
  188. "",
  189. " on_macos do",
  190. " if Hardware::CPU.intel?",
  191. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
  192. ` sha256 "${macX64Sha}"`,
  193. "",
  194. " def install",
  195. ' bin.install "opencode"',
  196. " end",
  197. " end",
  198. " if Hardware::CPU.arm?",
  199. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
  200. ` sha256 "${macArm64Sha}"`,
  201. "",
  202. " def install",
  203. ' bin.install "opencode"',
  204. " end",
  205. " end",
  206. " end",
  207. "",
  208. " on_linux do",
  209. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  210. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-x64.tar.gz"`,
  211. ` sha256 "${x64Sha}"`,
  212. " def install",
  213. ' bin.install "opencode"',
  214. " end",
  215. " end",
  216. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  217. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-arm64.tar.gz"`,
  218. ` sha256 "${arm64Sha}"`,
  219. " def install",
  220. ' bin.install "opencode"',
  221. " end",
  222. " end",
  223. " end",
  224. "end",
  225. "",
  226. "",
  227. ].join("\n")
  228. await $`rm -rf ./dist/homebrew-tap`
  229. await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap`
  230. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  231. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  232. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
  233. await $`cd ./dist/homebrew-tap && git push`
  234. const image = "ghcr.io/sst/opencode"
  235. await $`docker build -t ${image}:${Script.version} .`
  236. await $`docker push ${image}:${Script.version}`
  237. await $`docker tag ${image}:${Script.version} ${image}:latest`
  238. await $`docker push ${image}:latest`
  239. }