publish.ts 8.5 KB

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