publish.ts 8.6 KB

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