publish.ts 7.1 KB

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