publish.ts 7.5 KB

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