build.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import fs from "fs"
  4. import path from "path"
  5. import { fileURLToPath } from "url"
  6. import { createSolidTransformPlugin } from "@opentui/solid/bun-plugin"
  7. const __filename = fileURLToPath(import.meta.url)
  8. const __dirname = path.dirname(__filename)
  9. const dir = path.resolve(__dirname, "..")
  10. process.chdir(dir)
  11. import { Script } from "@opencode-ai/script"
  12. import pkg from "../package.json"
  13. const modelsUrl = process.env.OPENCODE_MODELS_URL || "https://models.dev"
  14. // Fetch and generate models.dev snapshot
  15. const modelsData = process.env.MODELS_DEV_API_JSON
  16. ? await Bun.file(process.env.MODELS_DEV_API_JSON).text()
  17. : await fetch(`${modelsUrl}/api.json`).then((x) => x.text())
  18. await Bun.write(
  19. path.join(dir, "src/provider/models-snapshot.js"),
  20. `// @ts-nocheck\n// Auto-generated by build.ts - do not edit\nexport const snapshot = ${modelsData}\n`,
  21. )
  22. await Bun.write(
  23. path.join(dir, "src/provider/models-snapshot.d.ts"),
  24. `// Auto-generated by build.ts - do not edit\nexport declare const snapshot: Record<string, unknown>\n`,
  25. )
  26. console.log("Generated models-snapshot.js")
  27. // Load migrations from migration directories
  28. const migrationDirs = (
  29. await fs.promises.readdir(path.join(dir, "migration"), {
  30. withFileTypes: true,
  31. })
  32. )
  33. .filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
  34. .map((entry) => entry.name)
  35. .sort()
  36. const migrations = await Promise.all(
  37. migrationDirs.map(async (name) => {
  38. const file = path.join(dir, "migration", name, "migration.sql")
  39. const sql = await Bun.file(file).text()
  40. const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
  41. const timestamp = match
  42. ? Date.UTC(
  43. Number(match[1]),
  44. Number(match[2]) - 1,
  45. Number(match[3]),
  46. Number(match[4]),
  47. Number(match[5]),
  48. Number(match[6]),
  49. )
  50. : 0
  51. return { sql, timestamp, name }
  52. }),
  53. )
  54. console.log(`Loaded ${migrations.length} migrations`)
  55. const singleFlag = process.argv.includes("--single")
  56. const baselineFlag = process.argv.includes("--baseline")
  57. const skipInstall = process.argv.includes("--skip-install")
  58. const plugin = createSolidTransformPlugin()
  59. const skipEmbedWebUi = process.argv.includes("--skip-embed-web-ui")
  60. const createEmbeddedWebUIBundle = async () => {
  61. console.log(`Building Web UI to embed in the binary`)
  62. const appDir = path.join(import.meta.dirname, "../../app")
  63. const dist = path.join(appDir, "dist")
  64. await $`bun run --cwd ${appDir} build`
  65. const files = (await Array.fromAsync(new Bun.Glob("**/*").scan({ cwd: dist })))
  66. .map((file) => file.replaceAll("\\", "/"))
  67. .sort()
  68. const imports = files.map((file, i) => {
  69. const spec = path.relative(dir, path.join(dist, file)).replaceAll("\\", "/")
  70. return `import file_${i} from ${JSON.stringify(spec.startsWith(".") ? spec : `./${spec}`)} with { type: "file" };`
  71. })
  72. const entries = files.map((file, i) => ` ${JSON.stringify(file)}: file_${i},`)
  73. return [
  74. `// Import all files as file_$i with type: "file"`,
  75. ...imports,
  76. `// Export with original mappings`,
  77. `export default {`,
  78. ...entries,
  79. `}`,
  80. ].join("\n")
  81. }
  82. const embeddedFileMap = skipEmbedWebUi ? null : await createEmbeddedWebUIBundle()
  83. const allTargets: {
  84. os: string
  85. arch: "arm64" | "x64"
  86. abi?: "musl"
  87. avx2?: false
  88. }[] = [
  89. {
  90. os: "linux",
  91. arch: "arm64",
  92. },
  93. {
  94. os: "linux",
  95. arch: "x64",
  96. },
  97. {
  98. os: "linux",
  99. arch: "x64",
  100. avx2: false,
  101. },
  102. {
  103. os: "linux",
  104. arch: "arm64",
  105. abi: "musl",
  106. },
  107. {
  108. os: "linux",
  109. arch: "x64",
  110. abi: "musl",
  111. },
  112. {
  113. os: "linux",
  114. arch: "x64",
  115. abi: "musl",
  116. avx2: false,
  117. },
  118. {
  119. os: "darwin",
  120. arch: "arm64",
  121. },
  122. {
  123. os: "darwin",
  124. arch: "x64",
  125. },
  126. {
  127. os: "darwin",
  128. arch: "x64",
  129. avx2: false,
  130. },
  131. {
  132. os: "win32",
  133. arch: "arm64",
  134. },
  135. {
  136. os: "win32",
  137. arch: "x64",
  138. },
  139. {
  140. os: "win32",
  141. arch: "x64",
  142. avx2: false,
  143. },
  144. ]
  145. const targets = singleFlag
  146. ? allTargets.filter((item) => {
  147. if (item.os !== process.platform || item.arch !== process.arch) {
  148. return false
  149. }
  150. // When building for the current platform, prefer a single native binary by default.
  151. // Baseline binaries require additional Bun artifacts and can be flaky to download.
  152. if (item.avx2 === false) {
  153. return baselineFlag
  154. }
  155. // also skip abi-specific builds for the same reason
  156. if (item.abi !== undefined) {
  157. return false
  158. }
  159. return true
  160. })
  161. : allTargets
  162. await $`rm -rf dist`
  163. const binaries: Record<string, string> = {}
  164. if (!skipInstall) {
  165. await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
  166. await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
  167. }
  168. for (const item of targets) {
  169. const name = [
  170. pkg.name,
  171. // changing to win32 flags npm for some reason
  172. item.os === "win32" ? "windows" : item.os,
  173. item.arch,
  174. item.avx2 === false ? "baseline" : undefined,
  175. item.abi === undefined ? undefined : item.abi,
  176. ]
  177. .filter(Boolean)
  178. .join("-")
  179. console.log(`building ${name}`)
  180. await $`mkdir -p dist/${name}/bin`
  181. const localPath = path.resolve(dir, "node_modules/@opentui/core/parser.worker.js")
  182. const rootPath = path.resolve(dir, "../../node_modules/@opentui/core/parser.worker.js")
  183. const parserWorker = fs.realpathSync(fs.existsSync(localPath) ? localPath : rootPath)
  184. const workerPath = "./src/cli/cmd/tui/worker.ts"
  185. // Use platform-specific bunfs root path based on target OS
  186. const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
  187. const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
  188. await Bun.build({
  189. conditions: ["browser"],
  190. tsconfig: "./tsconfig.json",
  191. plugins: [plugin],
  192. compile: {
  193. autoloadBunfig: false,
  194. autoloadDotenv: false,
  195. autoloadTsconfig: true,
  196. autoloadPackageJson: true,
  197. target: name.replace(pkg.name, "bun") as any,
  198. outfile: `dist/${name}/bin/opencode`,
  199. execArgv: [`--user-agent=opencode/${Script.version}`, "--use-system-ca", "--"],
  200. windows: {},
  201. },
  202. files: {
  203. ...(embeddedFileMap ? { "opencode-web-ui.gen.ts": embeddedFileMap } : {}),
  204. },
  205. entrypoints: ["./src/index.ts", parserWorker, workerPath, ...(embeddedFileMap ? ["opencode-web-ui.gen.ts"] : [])],
  206. define: {
  207. OPENCODE_VERSION: `'${Script.version}'`,
  208. OPENCODE_MIGRATIONS: JSON.stringify(migrations),
  209. OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
  210. OPENCODE_WORKER_PATH: workerPath,
  211. OPENCODE_CHANNEL: `'${Script.channel}'`,
  212. OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
  213. },
  214. })
  215. // Smoke test: only run if binary is for current platform
  216. if (item.os === process.platform && item.arch === process.arch && !item.abi) {
  217. const binaryPath = `dist/${name}/bin/opencode`
  218. console.log(`Running smoke test: ${binaryPath} --version`)
  219. try {
  220. const versionOutput = await $`${binaryPath} --version`.text()
  221. console.log(`Smoke test passed: ${versionOutput.trim()}`)
  222. } catch (e) {
  223. console.error(`Smoke test failed for ${name}:`, e)
  224. process.exit(1)
  225. }
  226. }
  227. await $`rm -rf ./dist/${name}/bin/tui`
  228. await Bun.file(`dist/${name}/package.json`).write(
  229. JSON.stringify(
  230. {
  231. name,
  232. version: Script.version,
  233. os: [item.os],
  234. cpu: [item.arch],
  235. },
  236. null,
  237. 2,
  238. ),
  239. )
  240. binaries[name] = Script.version
  241. }
  242. if (Script.release) {
  243. for (const key of Object.keys(binaries)) {
  244. if (key.includes("linux")) {
  245. await $`tar -czf ../../${key}.tar.gz *`.cwd(`dist/${key}/bin`)
  246. } else {
  247. await $`zip -r ../../${key}.zip *`.cwd(`dist/${key}/bin`)
  248. }
  249. }
  250. await $`gh release upload v${Script.version} ./dist/*.zip ./dist/*.tar.gz --clobber --repo ${process.env.GH_REPO}`
  251. }
  252. export { binaries }