publish.ts 8.4 KB

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