publish.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. async function published(name: string, version: string) {
  9. return (await $`npm view ${name}@${version} version`.nothrow()).exitCode === 0
  10. }
  11. async function publish(dir: string, name: string, version: string) {
  12. // GitHub artifact downloads can drop the executable bit, and Docker uses the
  13. // unpacked dist binaries directly rather than the published tarball.
  14. if (process.platform !== "win32") await $`chmod -R 755 .`.cwd(dir)
  15. if (await published(name, version)) {
  16. console.log(`already published ${name}@${version}`)
  17. return
  18. }
  19. await $`bun pm pack`.cwd(dir)
  20. await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(dir)
  21. }
  22. const binaries: Record<string, string> = {}
  23. for (const filepath of new Bun.Glob("*/package.json").scanSync({ cwd: "./dist" })) {
  24. const pkg = await Bun.file(`./dist/${filepath}`).json()
  25. binaries[pkg.name] = pkg.version
  26. }
  27. console.log("binaries", binaries)
  28. const version = Object.values(binaries)[0]
  29. await $`mkdir -p ./dist/${pkg.name}`
  30. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  31. await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
  32. await Bun.file(`./dist/${pkg.name}/LICENSE`).write(await Bun.file("../../LICENSE").text())
  33. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  34. JSON.stringify(
  35. {
  36. name: pkg.name + "-ai",
  37. bin: {
  38. [pkg.name]: `./bin/${pkg.name}`,
  39. },
  40. scripts: {
  41. postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs",
  42. },
  43. version: version,
  44. license: pkg.license,
  45. optionalDependencies: binaries,
  46. },
  47. null,
  48. 2,
  49. ),
  50. )
  51. const tasks = Object.entries(binaries).map(async ([name]) => {
  52. await publish(`./dist/${name}`, name, binaries[name])
  53. })
  54. await Promise.all(tasks)
  55. await publish(`./dist/${pkg.name}`, `${pkg.name}-ai`, version)
  56. const image = "ghcr.io/anomalyco/opencode"
  57. const platforms = "linux/amd64,linux/arm64"
  58. const tags = [`${image}:${version}`, `${image}:${Script.channel}`]
  59. const tagFlags = tags.flatMap((t) => ["-t", t])
  60. // registries
  61. if (!Script.preview) {
  62. await $`docker buildx build --platform ${platforms} ${tagFlags} --push .`
  63. // Calculate SHA values
  64. const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  65. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  66. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  67. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  68. const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
  69. // arch
  70. const binaryPkgbuild = [
  71. "# Maintainer: dax",
  72. "# Maintainer: adam",
  73. "",
  74. "pkgname='opencode-bin'",
  75. `pkgver=${pkgver}`,
  76. `_subver=${_subver}`,
  77. "options=('!debug' '!strip')",
  78. "pkgrel=1",
  79. "pkgdesc='The AI coding agent built for the terminal.'",
  80. "url='https://github.com/anomalyco/opencode'",
  81. "arch=('aarch64' 'x86_64')",
  82. "license=('MIT')",
  83. "provides=('opencode')",
  84. "conflicts=('opencode')",
  85. "depends=('ripgrep')",
  86. "",
  87. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.tar.gz::https://github.com/anomalyco/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.tar.gz")`,
  88. `sha256sums_aarch64=('${arm64Sha}')`,
  89. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.tar.gz::https://github.com/anomalyco/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.tar.gz")`,
  90. `sha256sums_x86_64=('${x64Sha}')`,
  91. "",
  92. "package() {",
  93. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  94. "}",
  95. "",
  96. ].join("\n")
  97. for (const [pkg, pkgbuild] of [["opencode-bin", binaryPkgbuild]]) {
  98. for (let i = 0; i < 30; i++) {
  99. try {
  100. await $`rm -rf ./dist/aur-${pkg}`
  101. await $`git clone ssh://aur@aur.archlinux.org/${pkg}.git ./dist/aur-${pkg}`
  102. await $`cd ./dist/aur-${pkg} && git checkout master`
  103. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  104. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  105. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  106. if ((await $`cd ./dist/aur-${pkg} && git diff --cached --quiet`.nothrow()).exitCode === 0) break
  107. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  108. await $`cd ./dist/aur-${pkg} && git push`
  109. break
  110. } catch {
  111. continue
  112. }
  113. }
  114. }
  115. // Homebrew formula
  116. const homebrewFormula = [
  117. "# typed: false",
  118. "# frozen_string_literal: true",
  119. "",
  120. "# This file was generated by GoReleaser. DO NOT EDIT.",
  121. "class Opencode < Formula",
  122. ` desc "The AI coding agent built for the terminal."`,
  123. ` homepage "https://github.com/anomalyco/opencode"`,
  124. ` version "${Script.version.split("-")[0]}"`,
  125. "",
  126. ` depends_on "ripgrep"`,
  127. "",
  128. " on_macos do",
  129. " if Hardware::CPU.intel?",
  130. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
  131. ` sha256 "${macX64Sha}"`,
  132. "",
  133. " def install",
  134. ' bin.install "opencode"',
  135. " end",
  136. " end",
  137. " if Hardware::CPU.arm?",
  138. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
  139. ` sha256 "${macArm64Sha}"`,
  140. "",
  141. " def install",
  142. ' bin.install "opencode"',
  143. " end",
  144. " end",
  145. " end",
  146. "",
  147. " on_linux do",
  148. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  149. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-linux-x64.tar.gz"`,
  150. ` sha256 "${x64Sha}"`,
  151. " def install",
  152. ' bin.install "opencode"',
  153. " end",
  154. " end",
  155. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  156. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-linux-arm64.tar.gz"`,
  157. ` sha256 "${arm64Sha}"`,
  158. " def install",
  159. ' bin.install "opencode"',
  160. " end",
  161. " end",
  162. " end",
  163. "end",
  164. "",
  165. "",
  166. ].join("\n")
  167. const token = process.env.GITHUB_TOKEN
  168. if (!token) {
  169. console.error("GITHUB_TOKEN is required to update homebrew tap")
  170. process.exit(1)
  171. }
  172. const tap = `https://x-access-token:${token}@github.com/anomalyco/homebrew-tap.git`
  173. await $`rm -rf ./dist/homebrew-tap`
  174. await $`git clone ${tap} ./dist/homebrew-tap`
  175. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  176. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  177. if ((await $`cd ./dist/homebrew-tap && git diff --cached --quiet`.nothrow()).exitCode !== 0) {
  178. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
  179. await $`cd ./dist/homebrew-tap && git push`
  180. }
  181. }