publish.ts 7.1 KB

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