publish.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. import { Script } from "@kirincode-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 $`mkdir -p ./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}/bin/${pkg.name}.exe`).write(
  34. [
  35. `echo "Error: ${pkg.name}-ai's postinstall script was not run." >&2`,
  36. 'echo "" >&2',
  37. 'echo "This occurs when using --ignore-scripts during installation, or when using a" >&2',
  38. 'echo "package manager like pnpm that does not run postinstall scripts by default." >&2',
  39. 'echo "" >&2',
  40. 'echo "To fix this, run the postinstall script manually:" >&2',
  41. `echo " cd node_modules/${pkg.name}-ai && node postinstall.mjs" >&2`,
  42. 'echo "" >&2',
  43. `echo "Or reinstall ${pkg.name}-ai without the --ignore-scripts flag." >&2`,
  44. "exit 1",
  45. "",
  46. ].join("\n"),
  47. )
  48. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  49. JSON.stringify(
  50. {
  51. name: pkg.name + "-ai",
  52. bin: {
  53. [pkg.name]: `./bin/${pkg.name}.exe`,
  54. },
  55. scripts: {
  56. postinstall: "node ./postinstall.mjs",
  57. },
  58. version: version,
  59. license: pkg.license,
  60. os: ["darwin", "linux", "win32"],
  61. cpu: ["arm64", "x64"],
  62. optionalDependencies: binaries,
  63. },
  64. null,
  65. 2,
  66. ),
  67. )
  68. const tasks = Object.entries(binaries).map(async ([name]) => {
  69. await publish(`./dist/${name}`, name, binaries[name])
  70. })
  71. await Promise.all(tasks)
  72. await publish(`./dist/${pkg.name}`, `${pkg.name}-ai`, version)
  73. const image = "ghcr.io/kirincode/kirincode"
  74. const platforms = "linux/amd64,linux/arm64"
  75. const tags = [`${image}:${version}`, `${image}:${Script.channel}`]
  76. const tagFlags = tags.flatMap((t) => ["-t", t])
  77. // registries
  78. if (!Script.preview) {
  79. await $`docker buildx build --platform ${platforms} ${tagFlags} --push .`
  80. // Calculate SHA values
  81. const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  82. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
  83. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  84. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  85. const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
  86. // arch
  87. const binaryPkgbuild = [
  88. "# Maintainer: dax",
  89. "# Maintainer: adam",
  90. "",
  91. "pkgname='opencode-bin'",
  92. `pkgver=${pkgver}`,
  93. `_subver=${_subver}`,
  94. "options=('!debug' '!strip')",
  95. "pkgrel=1",
  96. "pkgdesc='The AI coding agent built for the terminal.'",
  97. "url='https://github.com/kirincode/kirincode'",
  98. "arch=('aarch64' 'x86_64')",
  99. "license=('MIT')",
  100. "provides=('kirincode')",
  101. "conflicts=('kirincode')",
  102. "depends=('ripgrep')",
  103. "",
  104. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.tar.gz::https://github.com/anomalyco/kirincode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.tar.gz")`,
  105. `sha256sums_aarch64=('${arm64Sha}')`,
  106. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.tar.gz::https://github.com/anomalyco/kirincode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.tar.gz")`,
  107. `sha256sums_x86_64=('${x64Sha}')`,
  108. "",
  109. "package() {",
  110. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  111. "}",
  112. "",
  113. ].join("\n")
  114. for (const [pkg, pkgbuild] of [["opencode-bin", binaryPkgbuild]]) {
  115. for (let i = 0; i < 30; i++) {
  116. try {
  117. await $`rm -rf ./dist/aur-${pkg}`
  118. await $`git clone ssh://aur@aur.archlinux.org/${pkg}.git ./dist/aur-${pkg}`
  119. await $`cd ./dist/aur-${pkg} && git checkout master`
  120. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  121. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  122. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  123. if ((await $`cd ./dist/aur-${pkg} && git diff --cached --quiet`.nothrow()).exitCode === 0) break
  124. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  125. await $`cd ./dist/aur-${pkg} && git push`
  126. break
  127. } catch {
  128. continue
  129. }
  130. }
  131. }
  132. // Homebrew formula
  133. const homebrewFormula = [
  134. "# typed: false",
  135. "# frozen_string_literal: true",
  136. "",
  137. "# This file was generated by GoReleaser. DO NOT EDIT.",
  138. "class Opencode < Formula",
  139. ` desc "The AI coding agent built for the terminal."`,
  140. ` homepage "https://github.com/kirincode/kirincode"`,
  141. ` version "${Script.version.split("-")[0]}"`,
  142. "",
  143. ` depends_on "ripgrep"`,
  144. "",
  145. " on_macos do",
  146. " if Hardware::CPU.intel?",
  147. ` url "https://github.com/anomalyco/kirincode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
  148. ` sha256 "${macX64Sha}"`,
  149. "",
  150. " def install",
  151. ' bin.install "kirincode"',
  152. " end",
  153. " end",
  154. " if Hardware::CPU.arm?",
  155. ` url "https://github.com/anomalyco/kirincode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
  156. ` sha256 "${macArm64Sha}"`,
  157. "",
  158. " def install",
  159. ' bin.install "kirincode"',
  160. " end",
  161. " end",
  162. " end",
  163. "",
  164. " on_linux do",
  165. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  166. ` url "https://github.com/anomalyco/kirincode/releases/download/v${Script.version}/opencode-linux-x64.tar.gz"`,
  167. ` sha256 "${x64Sha}"`,
  168. " def install",
  169. ' bin.install "kirincode"',
  170. " end",
  171. " end",
  172. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  173. ` url "https://github.com/anomalyco/kirincode/releases/download/v${Script.version}/opencode-linux-arm64.tar.gz"`,
  174. ` sha256 "${arm64Sha}"`,
  175. " def install",
  176. ' bin.install "kirincode"',
  177. " end",
  178. " end",
  179. " end",
  180. "end",
  181. "",
  182. "",
  183. ].join("\n")
  184. const token = process.env.GITHUB_TOKEN
  185. if (!token) {
  186. console.error("GITHUB_TOKEN is required to update homebrew tap")
  187. process.exit(1)
  188. }
  189. const tap = `https://x-access-token:${token}@github.com/anomalyco/homebrew-tap.git`
  190. await $`rm -rf ./dist/homebrew-tap`
  191. await $`git clone ${tap} ./dist/homebrew-tap`
  192. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  193. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  194. if ((await $`cd ./dist/homebrew-tap && git diff --cached --quiet`.nothrow()).exitCode !== 0) {
  195. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
  196. await $`cd ./dist/homebrew-tap && git push`
  197. }
  198. }