publish.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. const binaryPkgbuild = [
  57. "# Maintainer: dax",
  58. "# Maintainer: adam",
  59. "",
  60. "pkgname='opencode-bin'",
  61. `pkgver=${version.split("-")[0]}`,
  62. "options=('!debug' '!strip')",
  63. "pkgrel=1",
  64. "pkgdesc='The AI coding agent built for the terminal.'",
  65. "url='https://github.com/sst/opencode'",
  66. "arch=('aarch64' 'x86_64')",
  67. "license=('MIT')",
  68. "provides=('opencode')",
  69. "conflicts=('opencode')",
  70. "depends=('fzf' 'ripgrep')",
  71. "",
  72. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.zip::https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip")`,
  73. `sha256sums_aarch64=('${arm64Sha}')`,
  74. "",
  75. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.zip::https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip")`,
  76. `sha256sums_x86_64=('${x64Sha}')`,
  77. "",
  78. "package() {",
  79. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  80. "}",
  81. "",
  82. ].join("\n")
  83. // Source-based PKGBUILD for opencode
  84. const sourcePkgbuild = [
  85. "# Maintainer: dax",
  86. "# Maintainer: adam",
  87. "",
  88. "pkgname='opencode'",
  89. `pkgver=${version.split("-")[0]}`,
  90. "options=('!debug' '!strip')",
  91. "pkgrel=1",
  92. "pkgdesc='The AI coding agent built for the terminal.'",
  93. "url='https://github.com/sst/opencode'",
  94. "arch=('aarch64' 'x86_64')",
  95. "license=('MIT')",
  96. "provides=('opencode')",
  97. "conflicts=('opencode-bin')",
  98. "depends=('fzf' 'ripgrep')",
  99. "makedepends=('git' 'bun-bin' 'go')",
  100. "",
  101. `source=("opencode-\${pkgver}.tar.gz::https://github.com/sst/opencode/archive/v${version}.tar.gz")`,
  102. `sha256sums=('SKIP')`,
  103. "",
  104. "build() {",
  105. ` cd "opencode-\${pkgver}"`,
  106. ` bun install`,
  107. " cd packages/tui",
  108. ` CGO_ENABLED=0 go build -ldflags="-s -w -X main.Version=\${pkgver}" -o tui cmd/opencode/main.go`,
  109. " cd ../opencode",
  110. ` bun build --define OPENCODE_TUI_PATH="'$(realpath ../tui/tui)'" --define OPENCODE_VERSION="'\${pkgver}'" --compile --target=bun-linux-x64 --outfile=opencode ./src/index.ts`,
  111. "}",
  112. "",
  113. "package() {",
  114. ` cd "opencode-\${pkgver}/packages/opencode"`,
  115. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  116. "}",
  117. "",
  118. ].join("\n")
  119. for (const [pkg, pkgbuild] of [
  120. ["opencode-bin", binaryPkgbuild],
  121. ["opencode", sourcePkgbuild],
  122. ]) {
  123. await $`rm -rf ./dist/aur-${pkg}`
  124. while (true) {
  125. try {
  126. await $`git clone ssh://[email protected]/${pkg}.git ./dist/aur-${pkg}`
  127. break
  128. } catch (e) {
  129. continue
  130. }
  131. }
  132. await $`cd ./dist/aur-${pkg} && git checkout master`
  133. await Bun.file(`./dist/aur-${pkg}/PKGBUILD`).write(pkgbuild)
  134. await $`cd ./dist/aur-${pkg} && makepkg --printsrcinfo > .SRCINFO`
  135. await $`cd ./dist/aur-${pkg} && git add PKGBUILD .SRCINFO`
  136. await $`cd ./dist/aur-${pkg} && git commit -m "Update to v${version}"`
  137. await $`cd ./dist/aur-${pkg} && git push`
  138. }
  139. /*
  140. // Homebrew formula
  141. const homebrewFormula = [
  142. "# typed: false",
  143. "# frozen_string_literal: true",
  144. "",
  145. "# This file was generated by GoReleaser. DO NOT EDIT.",
  146. "class Opencode < Formula",
  147. ` desc "The AI coding agent built for the terminal."`,
  148. ` homepage "https://github.com/sst/opencode"`,
  149. ` version "${version.split("-")[0]}"`,
  150. "",
  151. " on_macos do",
  152. " if Hardware::CPU.intel?",
  153. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-darwin-x64.zip"`,
  154. ` sha256 "${macX64Sha}"`,
  155. "",
  156. " def install",
  157. ' bin.install "opencode"',
  158. " end",
  159. " end",
  160. " if Hardware::CPU.arm?",
  161. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-darwin-arm64.zip"`,
  162. ` sha256 "${macArm64Sha}"`,
  163. "",
  164. " def install",
  165. ' bin.install "opencode"',
  166. " end",
  167. " end",
  168. " end",
  169. "",
  170. " on_linux do",
  171. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  172. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip"`,
  173. ` sha256 "${x64Sha}"`,
  174. " def install",
  175. ' bin.install "opencode"',
  176. " end",
  177. " end",
  178. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  179. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip"`,
  180. ` sha256 "${arm64Sha}"`,
  181. " def install",
  182. ' bin.install "opencode"',
  183. " end",
  184. " end",
  185. " end",
  186. "end",
  187. "",
  188. "",
  189. ].join("\n")
  190. await $`rm -rf ./dist/homebrew-tap`
  191. await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./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. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${version}"`
  195. await $`cd ./dist/homebrew-tap && git push`
  196. */
  197. }