publish.ts 6.9 KB

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