publish.ts 7.5 KB

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