publish.ts 7.4 KB

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