publish.ts 7.5 KB

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