publish.ts 7.0 KB

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