publish.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. const tags = [Script.channel]
  35. if (!Script.preview) {
  36. const major = Script.version.split(".")[0]
  37. tags[0] = `latest-${major}`
  38. }
  39. const tasks = Object.entries(binaries).map(async ([name]) => {
  40. if (process.platform !== "win32") {
  41. await $`chmod 755 -R .`.cwd(`./dist/${name}`)
  42. }
  43. await $`bun pm pack`.cwd(`./dist/${name}`)
  44. for (const tag of tags) {
  45. await $`npm publish *.tgz --access public --tag ${tag}`.cwd(`./dist/${name}`)
  46. }
  47. })
  48. await Promise.all(tasks)
  49. for (const tag of tags) {
  50. await $`cd ./dist/${pkg.name} && bun pm pack && npm publish *.tgz --access public --tag ${tag}`
  51. }
  52. if (!Script.preview) {
  53. for (const key of Object.keys(binaries)) {
  54. if (key.includes("linux")) {
  55. await $`cd dist/${key}/bin && tar -czf ../../${key}.tar.gz *`
  56. } else {
  57. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  58. }
  59. }
  60. // Calculate SHA values
  61. const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  62. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  63. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  64. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  65. const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
  66. // arch
  67. const binaryPkgbuild = [
  68. "# Maintainer: dax",
  69. "# Maintainer: adam",
  70. "",
  71. "pkgname='opencode-bin'",
  72. `pkgver=${pkgver}`,
  73. `_subver=${_subver}`,
  74. "options=('!debug' '!strip')",
  75. "pkgrel=1",
  76. "pkgdesc='The AI coding agent built for the terminal.'",
  77. "url='https://github.com/sst/opencode'",
  78. "arch=('aarch64' 'x86_64')",
  79. "license=('MIT')",
  80. "provides=('opencode')",
  81. "conflicts=('opencode')",
  82. "depends=('ripgrep')",
  83. "",
  84. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.tar.gz::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.tar.gz")`,
  85. `sha256sums_aarch64=('${arm64Sha}')`,
  86. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.tar.gz::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.tar.gz")`,
  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=('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/opencode",
  120. ` OPENCODE_CHANNEL=latest OPENCODE_VERSION=${pkgver} bun run ./script/build.ts --single`,
  121. "}",
  122. "",
  123. "package() {",
  124. ` cd "opencode-\${pkgver}/packages/opencode"`,
  125. ' mkdir -p "${pkgdir}/usr/bin"',
  126. ' target_arch="x64"',
  127. ' case "$CARCH" in',
  128. ' x86_64) target_arch="x64" ;;',
  129. ' aarch64) target_arch="arm64" ;;',
  130. ' *) printf "unsupported architecture: %s\\n" "$CARCH" >&2 ; return 1 ;;',
  131. " esac",
  132. ' libc=""',
  133. " if command -v ldd >/dev/null 2>&1; then",
  134. " if ldd --version 2>&1 | grep -qi musl; then",
  135. ' libc="-musl"',
  136. " fi",
  137. " fi",
  138. ' if [ -z "$libc" ] && ls /lib/ld-musl-* >/dev/null 2>&1; then',
  139. ' libc="-musl"',
  140. " fi",
  141. ' base=""',
  142. ' if [ "$target_arch" = "x64" ]; then',
  143. " if ! grep -qi avx2 /proc/cpuinfo 2>/dev/null; then",
  144. ' base="-baseline"',
  145. " fi",
  146. " fi",
  147. ' bin="dist/opencode-linux-${target_arch}${base}${libc}/bin/opencode"',
  148. ' if [ ! -f "$bin" ]; then',
  149. ' printf "unable to find binary for %s%s%s\\n" "$target_arch" "$base" "$libc" >&2',
  150. " return 1",
  151. " fi",
  152. ' install -Dm755 "$bin" "${pkgdir}/usr/bin/opencode"',
  153. "}",
  154. "",
  155. ].join("\n")
  156. for (const [pkg, pkgbuild] of [
  157. ["opencode-bin", binaryPkgbuild],
  158. ["opencode", sourcePkgbuild],
  159. ]) {
  160. for (let i = 0; i < 30; i++) {
  161. try {
  162. await $`rm -rf ./dist/aur-${pkg}`
  163. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  164. await $`cd ./dist/aur-${pkg} && git checkout master`
  165. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  166. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  167. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  168. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  169. await $`cd ./dist/aur-${pkg} && git push`
  170. break
  171. } catch (e) {
  172. continue
  173. }
  174. }
  175. }
  176. // Homebrew formula
  177. const homebrewFormula = [
  178. "# typed: false",
  179. "# frozen_string_literal: true",
  180. "",
  181. "# This file was generated by GoReleaser. DO NOT EDIT.",
  182. "class Opencode < Formula",
  183. ` desc "The AI coding agent built for the terminal."`,
  184. ` homepage "https://github.com/sst/opencode"`,
  185. ` version "${Script.version.split("-")[0]}"`,
  186. "",
  187. ` depends_on "ripgrep"`,
  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. }