publish.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. import { Script } from "@opencode-ai/script"
  5. const dir = new URL("..", import.meta.url).pathname
  6. process.chdir(dir)
  7. const { binaries } = await import("./build.ts")
  8. {
  9. const name = `${pkg.name}-${process.platform}-${process.arch}`
  10. console.log(`smoke test: running dist/${name}/bin/opencode --version`)
  11. await $`./dist/${name}/bin/opencode --version`
  12. }
  13. await $`mkdir -p ./dist/${pkg.name}`
  14. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  15. await $`cp ./script/preinstall.mjs ./dist/${pkg.name}/preinstall.mjs`
  16. await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
  17. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  18. JSON.stringify(
  19. {
  20. name: pkg.name + "-ai",
  21. bin: {
  22. [pkg.name]: `./bin/${pkg.name}`,
  23. },
  24. scripts: {
  25. preinstall: "node ./preinstall.mjs",
  26. postinstall: "node ./postinstall.mjs",
  27. },
  28. version: Script.version,
  29. optionalDependencies: binaries,
  30. },
  31. null,
  32. 2,
  33. ),
  34. )
  35. for (const [name] of Object.entries(binaries)) {
  36. await $`cd dist/${name} && chmod 777 -R . && bun publish --access public --tag ${Script.channel}`
  37. }
  38. await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${Script.channel}`
  39. if (!Script.preview) {
  40. for (const key of Object.keys(binaries)) {
  41. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  42. }
  43. // Calculate SHA values
  44. const arm64Sha = await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  45. const x64Sha = await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  46. const macX64Sha = await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  47. const macArm64Sha = await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
  48. const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
  49. // arch
  50. const binaryPkgbuild = [
  51. "# Maintainer: dax",
  52. "# Maintainer: adam",
  53. "",
  54. "pkgname='opencode-bin'",
  55. `pkgver=${pkgver}`,
  56. `_subver=${_subver}`,
  57. "options=('!debug' '!strip')",
  58. "pkgrel=1",
  59. "pkgdesc='The AI coding agent built for the terminal.'",
  60. "url='https://github.com/sst/opencode'",
  61. "arch=('aarch64' 'x86_64')",
  62. "license=('MIT')",
  63. "provides=('opencode')",
  64. "conflicts=('opencode')",
  65. "depends=('fzf' 'ripgrep')",
  66. "",
  67. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.zip::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-arm64.zip")`,
  68. `sha256sums_aarch64=('${arm64Sha}')`,
  69. "",
  70. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.zip::https://github.com/sst/opencode/releases/download/v\${pkgver}\${_subver}/opencode-linux-x64.zip")`,
  71. `sha256sums_x86_64=('${x64Sha}')`,
  72. "",
  73. "package() {",
  74. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  75. "}",
  76. "",
  77. ].join("\n")
  78. // Source-based PKGBUILD for opencode
  79. const sourcePkgbuild = [
  80. "# Maintainer: dax",
  81. "# Maintainer: adam",
  82. "",
  83. "pkgname='opencode'",
  84. `pkgver=${pkgver}`,
  85. `_subver=${_subver}`,
  86. "options=('!debug' '!strip')",
  87. "pkgrel=1",
  88. "pkgdesc='The AI coding agent built for the terminal.'",
  89. "url='https://github.com/sst/opencode'",
  90. "arch=('aarch64' 'x86_64')",
  91. "license=('MIT')",
  92. "provides=('opencode')",
  93. "conflicts=('opencode-bin')",
  94. "depends=('fzf' 'ripgrep')",
  95. "makedepends=('git' 'bun-bin' 'go')",
  96. "",
  97. `source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v\${pkgver}\${_subver}.tar.gz")`,
  98. `sha256sums=('SKIP')`,
  99. "",
  100. "build() {",
  101. ` cd "opencode-\${pkgver}"`,
  102. ` bun install`,
  103. " cd packages/tui",
  104. ` CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=\${pkgver}" -o tui cmd/opencode/main.go`,
  105. " cd ../opencode",
  106. ` bun build --define OPENCODE_TUI_PATH="'$(realpath ../tui/tui)'" --define OPENCODE_VERSION="'\${pkgver}'" --compile --target=bun-linux-x64 --outfile=opencode ./src/index.ts`,
  107. "}",
  108. "",
  109. "package() {",
  110. ` cd "opencode-\${pkgver}/packages/opencode"`,
  111. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  112. "}",
  113. "",
  114. ].join("\n")
  115. for (const [pkg, pkgbuild] of [
  116. ["opencode-bin", binaryPkgbuild],
  117. ["opencode", sourcePkgbuild],
  118. ]) {
  119. for (let i = 0; i < 30; i++) {
  120. try {
  121. await $`rm -rf ./dist/aur-${pkg}`
  122. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  123. await $`cd ./dist/aur-${pkg} && git checkout master`
  124. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  125. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  126. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  127. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${Script.version}"`
  128. await $`cd ./dist/aur-${pkg} && git push`
  129. break
  130. } catch (e) {
  131. continue
  132. }
  133. }
  134. }
  135. // Homebrew formula
  136. const homebrewFormula = [
  137. "# typed: false",
  138. "# frozen_string_literal: true",
  139. "",
  140. "# This file was generated by GoReleaser. DO NOT EDIT.",
  141. "class Opencode < Formula",
  142. ` desc "The AI coding agent built for the terminal."`,
  143. ` homepage "https://github.com/sst/opencode"`,
  144. ` version "${Script.version.split("-")[0]}"`,
  145. "",
  146. " on_macos do",
  147. " if Hardware::CPU.intel?",
  148. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-x64.zip"`,
  149. ` sha256 "${macX64Sha}"`,
  150. "",
  151. " def install",
  152. ' bin.install "opencode"',
  153. " end",
  154. " end",
  155. " if Hardware::CPU.arm?",
  156. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-darwin-arm64.zip"`,
  157. ` sha256 "${macArm64Sha}"`,
  158. "",
  159. " def install",
  160. ' bin.install "opencode"',
  161. " end",
  162. " end",
  163. " end",
  164. "",
  165. " on_linux do",
  166. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  167. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-x64.zip"`,
  168. ` sha256 "${x64Sha}"`,
  169. " def install",
  170. ' bin.install "opencode"',
  171. " end",
  172. " end",
  173. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  174. ` url "https://github.com/sst/opencode/releases/download/v${Script.version}/opencode-linux-arm64.zip"`,
  175. ` sha256 "${arm64Sha}"`,
  176. " def install",
  177. ' bin.install "opencode"',
  178. " end",
  179. " end",
  180. " end",
  181. "end",
  182. "",
  183. "",
  184. ].join("\n")
  185. await $`rm -rf ./dist/homebrew-tap`
  186. await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap`
  187. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  188. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  189. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
  190. await $`cd ./dist/homebrew-tap && git push`
  191. }