publish.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import pkg from "../package.json"
  4. const dry = process.argv.includes("--dry")
  5. const snapshot = process.argv.includes("--snapshot")
  6. const version = snapshot
  7. ? `0.0.0-${new Date().toISOString().slice(0, 16).replace(/[-:T]/g, "")}`
  8. : await $`git describe --tags --exact-match HEAD`
  9. .text()
  10. .then((x) => x.substring(1).trim())
  11. .catch(() => {
  12. console.error("tag not found")
  13. process.exit(1)
  14. })
  15. console.log(`publishing ${version}`)
  16. const GOARCH: Record<string, string> = {
  17. arm64: "arm64",
  18. x64: "amd64",
  19. }
  20. const targets = [
  21. ["linux", "arm64"],
  22. ["linux", "x64"],
  23. ["darwin", "x64"],
  24. ["darwin", "arm64"],
  25. ["windows", "x64"],
  26. ]
  27. await $`rm -rf dist`
  28. const optionalDependencies: Record<string, string> = {}
  29. const npmTag = snapshot ? "snapshot" : "latest"
  30. for (const [os, arch] of targets) {
  31. console.log(`building ${os}-${arch}`)
  32. const name = `${pkg.name}-${os}-${arch}`
  33. await $`mkdir -p dist/${name}/bin`
  34. await $`GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X main.Version=${version}" -o ../opencode/dist/${name}/bin/tui ../tui/cmd/opencode/main.go`.cwd(
  35. "../tui",
  36. )
  37. await $`bun build --define OPENCODE_VERSION="'${version}'" --compile --minify --target=bun-${os}-${arch} --outfile=dist/${name}/bin/opencode ./src/index.ts ./dist/${name}/bin/tui`
  38. await $`rm -rf ./dist/${name}/bin/tui`
  39. await Bun.file(`dist/${name}/package.json`).write(
  40. JSON.stringify(
  41. {
  42. name,
  43. version,
  44. os: [os === "windows" ? "win32" : os],
  45. cpu: [arch],
  46. },
  47. null,
  48. 2,
  49. ),
  50. )
  51. if (!dry)
  52. await $`cd dist/${name} && bun publish --access public --tag ${npmTag}`
  53. optionalDependencies[name] = version
  54. }
  55. await $`mkdir -p ./dist/${pkg.name}`
  56. await $`cp -r ./bin ./dist/${pkg.name}/bin`
  57. await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
  58. await Bun.file(`./dist/${pkg.name}/package.json`).write(
  59. JSON.stringify(
  60. {
  61. name: pkg.name + "-ai",
  62. bin: {
  63. [pkg.name]: `./bin/${pkg.name}`,
  64. },
  65. scripts: {
  66. postinstall: "node ./postinstall.mjs",
  67. },
  68. version,
  69. optionalDependencies,
  70. },
  71. null,
  72. 2,
  73. ),
  74. )
  75. if (!dry)
  76. await $`cd ./dist/${pkg.name} && bun publish --access public --tag ${npmTag}`
  77. if (!snapshot) {
  78. // Github Release
  79. for (const key of Object.keys(optionalDependencies)) {
  80. await $`cd dist/${key}/bin && zip -r ../../${key}.zip *`
  81. }
  82. const previous = await fetch(
  83. "https://api.github.com/repos/sst/opencode/releases/latest",
  84. )
  85. .then((res) => res.json())
  86. .then((data) => data.tag_name)
  87. const commits = await fetch(
  88. `https://api.github.com/repos/sst/opencode/compare/${previous}...HEAD`,
  89. )
  90. .then((res) => res.json())
  91. .then((data) => data.commits || [])
  92. const notes = commits
  93. .map((commit: any) => `- ${commit.commit.message.split("\n")[0]}`)
  94. .filter((x: string) => {
  95. const lower = x.toLowerCase()
  96. return (
  97. !lower.includes("ignore:") &&
  98. !lower.includes("ci:") &&
  99. !lower.includes("docs:") &&
  100. !lower.includes("doc:")
  101. )
  102. })
  103. .join("\n")
  104. if (!dry)
  105. await $`gh release create v${version} --title "v${version}" --notes ${notes} ./dist/*.zip`
  106. // Calculate SHA values
  107. const arm64Sha =
  108. await $`sha256sum ./dist/opencode-linux-arm64.zip | cut -d' ' -f1`
  109. .text()
  110. .then((x) => x.trim())
  111. const x64Sha =
  112. await $`sha256sum ./dist/opencode-linux-x64.zip | cut -d' ' -f1`
  113. .text()
  114. .then((x) => x.trim())
  115. const macX64Sha =
  116. await $`sha256sum ./dist/opencode-darwin-x64.zip | cut -d' ' -f1`
  117. .text()
  118. .then((x) => x.trim())
  119. const macArm64Sha =
  120. await $`sha256sum ./dist/opencode-darwin-arm64.zip | cut -d' ' -f1`
  121. .text()
  122. .then((x) => x.trim())
  123. // AUR package
  124. const pkgbuild = [
  125. "# Maintainer: dax",
  126. "# Maintainer: adam",
  127. "",
  128. "pkgname='opencode-bin'",
  129. `pkgver=${version.split("-")[0]}`,
  130. "options=('!debug' '!strip')",
  131. "pkgrel=1",
  132. "pkgdesc='The AI coding agent built for the terminal.'",
  133. "url='https://github.com/sst/opencode'",
  134. "arch=('aarch64' 'x86_64')",
  135. "license=('MIT')",
  136. "provides=('opencode')",
  137. "conflicts=('opencode')",
  138. "depends=('fzf' 'ripgrep')",
  139. "",
  140. `source_aarch64=("\${pkgname}_\${pkgver}_aarch64.zip::https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip")`,
  141. `sha256sums_aarch64=('${arm64Sha}')`,
  142. "",
  143. `source_x86_64=("\${pkgname}_\${pkgver}_x86_64.zip::https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip")`,
  144. `sha256sums_x86_64=('${x64Sha}')`,
  145. "",
  146. "package() {",
  147. ' install -Dm755 ./opencode "${pkgdir}/usr/bin/opencode"',
  148. "}",
  149. "",
  150. ].join("\n")
  151. await $`rm -rf ./dist/aur-opencode-bin`
  152. await $`git clone ssh://[email protected]/opencode-bin.git ./dist/aur-opencode-bin`
  153. await Bun.file("./dist/aur-opencode-bin/PKGBUILD").write(pkgbuild)
  154. await $`cd ./dist/aur-opencode-bin && makepkg --printsrcinfo > .SRCINFO`
  155. await $`cd ./dist/aur-opencode-bin && git add PKGBUILD .SRCINFO`
  156. await $`cd ./dist/aur-opencode-bin && git commit -m "Update to v${version}"`
  157. if (!dry) await $`cd ./dist/aur-opencode-bin && git push`
  158. // Homebrew formula
  159. const homebrewFormula = [
  160. "# typed: false",
  161. "# frozen_string_literal: true",
  162. "",
  163. "# This file was generated by GoReleaser. DO NOT EDIT.",
  164. "class Opencode < Formula",
  165. ` desc "The AI coding agent built for the terminal."`,
  166. ` homepage "https://github.com/sst/opencode"`,
  167. ` version "${version.split("-")[0]}"`,
  168. "",
  169. " on_macos do",
  170. " if Hardware::CPU.intel?",
  171. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-darwin-x64.zip"`,
  172. ` sha256 "${macX64Sha}"`,
  173. "",
  174. " def install",
  175. ' bin.install "opencode"',
  176. " end",
  177. " end",
  178. " if Hardware::CPU.arm?",
  179. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-darwin-arm64.zip"`,
  180. ` sha256 "${macArm64Sha}"`,
  181. "",
  182. " def install",
  183. ' bin.install "opencode"',
  184. " end",
  185. " end",
  186. " end",
  187. "",
  188. " on_linux do",
  189. " if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
  190. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-x64.zip"`,
  191. ` sha256 "${x64Sha}"`,
  192. " def install",
  193. ' bin.install "opencode"',
  194. " end",
  195. " end",
  196. " if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
  197. ` url "https://github.com/sst/opencode/releases/download/v${version}/opencode-linux-arm64.zip"`,
  198. ` sha256 "${arm64Sha}"`,
  199. " def install",
  200. ' bin.install "opencode"',
  201. " end",
  202. " end",
  203. " end",
  204. "end",
  205. "",
  206. "",
  207. ].join("\n")
  208. await $`rm -rf ./dist/homebrew-tap`
  209. await $`git clone https://${process.env["GITHUB_TOKEN"]}@github.com/sst/homebrew-tap.git ./dist/homebrew-tap`
  210. await Bun.file("./dist/homebrew-tap/opencode.rb").write(homebrewFormula)
  211. await $`cd ./dist/homebrew-tap && git add opencode.rb`
  212. await $`cd ./dist/homebrew-tap && git commit -m "Update to v${version}"`
  213. if (!dry) await $`cd ./dist/homebrew-tap && git push`
  214. }