publish.ts 8.4 KB

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