publish.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. for (const [pkg, pkgbuild] of [["opencode-bin", binaryPkgbuild]]) {
  88. for (let i = 0; i < 30; i++) {
  89. try {
  90. await $`rm -rf ./dist/aur-${pkg}`
  91. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  92. await $`cd ./dist/aur-${pkg} && git checkout master`
  93. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  94. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  95. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  96. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  97. await $`cd ./dist/aur-${pkg} && git push`
  98. break
  99. } catch (e) {
  100. continue
  101. }
  102. }
  103. }
  104. // Homebrew formula
  105. const homebrewFormula = [
  106. "# typed: false",
  107. "# frozen_string_literal: true",
  108. "",
  109. "# This file was generated by GoReleaser. DO NOT EDIT.",
  110. "class Opencode < Formula",
  111. ` desc "The AI coding agent built for the terminal."`,
  112. ` homepage "https://github.com/anomalyco/opencode"`,
  113. ` version "${Script.version.split("-")[0]}"`,
  114. "",
  115. ` depends_on "ripgrep"`,
  116. "",
  117. " on_macos do",
  118. " if Hardware::CPU.intel?",
  119. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
  120. ` sha256 "${macX64Sha}"`,
  121. "",
  122. " def install",
  123. ' bin.install "opencode"',
  124. " end",
  125. " end",
  126. " if Hardware::CPU.arm?",
  127. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
  128. ` sha256 "${macArm64Sha}"`,
  129. "",
  130. " def install",
  131. ' bin.install "opencode"',
  132. " end",
  133. " end",
  134. " end",
  135. "",
  136. " on_linux do",
  137. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  138. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-linux-x64.tar.gz"`,
  139. ` sha256 "${x64Sha}"`,
  140. " def install",
  141. ' bin.install "opencode"',
  142. " end",
  143. " end",
  144. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  145. ` url "https://github.com/anomalyco/opencode/releases/download/v${Script.version}/opencode-linux-arm64.tar.gz"`,
  146. ` sha256 "${arm64Sha}"`,
  147. " def install",
  148. ' bin.install "opencode"',
  149. " end",
  150. " end",
  151. " end",
  152. "end",
  153. "",
  154. "",
  155. ].join("\n")
  156. const token = process.env.GITHUB_TOKEN
  157. if (!token) {
  158. console.error("GITHUB_TOKEN is required to update homebrew tap")
  159. process.exit(1)
  160. }
  161. const tap = `https://x-access-token:${token}@github.com/anomalyco/homebrew-tap.git`
  162. await $`rm -rf ./dist/homebrew-tap`
  163. await $`git clone ${tap} ./dist/homebrew-tap`
  164. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  165. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  166. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
  167. await $`cd ./dist/homebrew-tap && git push`
  168. }