publish.ts 7.2 KB

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