2
0

publish.ts 7.6 KB

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