publish.ts 8.4 KB

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