2
0

publish.ts 8.1 KB

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