| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #!/usr/bin/env bun
- import { $ } from "bun"
- import fs from "fs"
- import path from "path"
- import { fileURLToPath } from "url"
- import { createSolidTransformPlugin } from "@opentui/solid/bun-plugin"
- const __filename = fileURLToPath(import.meta.url)
- const __dirname = path.dirname(__filename)
- const dir = path.resolve(__dirname, "..")
- process.chdir(dir)
- await import("./generate.ts")
- import { Script } from "@opencode-ai/script"
- import pkg from "../package.json"
- // Load migrations from migration directories
- const migrationDirs = (
- await fs.promises.readdir(path.join(dir, "migration"), {
- withFileTypes: true,
- })
- )
- .filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
- .map((entry) => entry.name)
- .sort()
- const migrations = await Promise.all(
- migrationDirs.map(async (name) => {
- const file = path.join(dir, "migration", name, "migration.sql")
- const sql = await Bun.file(file).text()
- const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
- const timestamp = match
- ? Date.UTC(
- Number(match[1]),
- Number(match[2]) - 1,
- Number(match[3]),
- Number(match[4]),
- Number(match[5]),
- Number(match[6]),
- )
- : 0
- return { sql, timestamp, name }
- }),
- )
- console.log(`Loaded ${migrations.length} migrations`)
- const singleFlag = process.argv.includes("--single")
- const baselineFlag = process.argv.includes("--baseline")
- const skipInstall = process.argv.includes("--skip-install")
- const plugin = createSolidTransformPlugin()
- const skipEmbedWebUi = process.argv.includes("--skip-embed-web-ui")
- const createEmbeddedWebUIBundle = async () => {
- console.log(`Building Web UI to embed in the binary`)
- const appDir = path.join(import.meta.dirname, "../../app")
- const dist = path.join(appDir, "dist")
- await $`bun run --cwd ${appDir} build`
- const files = (await Array.fromAsync(new Bun.Glob("**/*").scan({ cwd: dist })))
- .map((file) => file.replaceAll("\\", "/"))
- .sort()
- const imports = files.map((file, i) => {
- const spec = path.relative(dir, path.join(dist, file)).replaceAll("\\", "/")
- return `import file_${i} from ${JSON.stringify(spec.startsWith(".") ? spec : `./${spec}`)} with { type: "file" };`
- })
- const entries = files.map((file, i) => ` ${JSON.stringify(file)}: file_${i},`)
- return [
- `// Import all files as file_$i with type: "file"`,
- ...imports,
- `// Export with original mappings`,
- `export default {`,
- ...entries,
- `}`,
- ].join("\n")
- }
- const embeddedFileMap = skipEmbedWebUi ? null : await createEmbeddedWebUIBundle()
- const allTargets: {
- os: string
- arch: "arm64" | "x64"
- abi?: "musl"
- avx2?: false
- }[] = [
- {
- os: "linux",
- arch: "arm64",
- },
- {
- os: "linux",
- arch: "x64",
- },
- {
- os: "linux",
- arch: "x64",
- avx2: false,
- },
- {
- os: "linux",
- arch: "arm64",
- abi: "musl",
- },
- {
- os: "linux",
- arch: "x64",
- abi: "musl",
- },
- {
- os: "linux",
- arch: "x64",
- abi: "musl",
- avx2: false,
- },
- {
- os: "darwin",
- arch: "arm64",
- },
- {
- os: "darwin",
- arch: "x64",
- },
- {
- os: "darwin",
- arch: "x64",
- avx2: false,
- },
- // kilocode_change start - Windows ARM64 target
- {
- os: "win32",
- arch: "arm64",
- },
- // kilocode_change end
- {
- os: "win32",
- arch: "x64",
- },
- {
- os: "win32",
- arch: "x64",
- avx2: false,
- },
- // kilocode_change start - added Windows ARM64 target
- {
- os: "win32",
- arch: "arm64",
- },
- // kilocode_change end
- ]
- const targets = singleFlag
- ? allTargets.filter((item) => {
- if (item.os !== process.platform || item.arch !== process.arch) {
- return false
- }
- // When building for the current platform, prefer a single native binary by default.
- // Baseline binaries require additional Bun artifacts and can be flaky to download.
- if (item.avx2 === false) {
- return baselineFlag
- }
- // also skip abi-specific builds for the same reason
- if (item.abi !== undefined) {
- return false
- }
- return true
- })
- : allTargets
- await $`rm -rf dist`
- const binaries: Record<string, string> = {}
- if (!skipInstall) {
- await $`bun install --os="*" --cpu="*" @opentui/core@${pkg.dependencies["@opentui/core"]}`
- await $`bun install --os="*" --cpu="*" @parcel/watcher@${pkg.dependencies["@parcel/watcher"]}`
- }
- for (const item of targets) {
- const name = [
- pkg.name,
- // changing to win32 flags npm for some reason
- item.os === "win32" ? "windows" : item.os,
- item.arch,
- item.avx2 === false ? "baseline" : undefined,
- item.abi === undefined ? undefined : item.abi,
- ]
- .filter(Boolean)
- .join("-")
- console.log(`building ${name}`)
- await $`mkdir -p dist/${name}/bin`
- const localPath = path.resolve(dir, "node_modules/@opentui/core/parser.worker.js")
- const rootPath = path.resolve(dir, "../../node_modules/@opentui/core/parser.worker.js")
- const parserWorker = fs.realpathSync(fs.existsSync(localPath) ? localPath : rootPath)
- const workerPath = "./src/cli/cmd/tui/worker.ts"
- // Use platform-specific bunfs root path based on target OS
- const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
- const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
- await Bun.build({
- conditions: ["browser"],
- tsconfig: "./tsconfig.json",
- plugins: [plugin],
- external: ["node-gyp"],
- compile: {
- autoloadBunfig: false,
- autoloadDotenv: false,
- autoloadTsconfig: true,
- autoloadPackageJson: true,
- target: name.replace(pkg.name, "bun") as any,
- outfile: `dist/${name}/bin/kilo`, // kilocode_change
- execArgv: [`--user-agent=kilo/${Script.version}`, "--use-system-ca", "--"], // kilocode_change
- windows: {},
- },
- files: {
- ...(embeddedFileMap ? { "opencode-web-ui.gen.ts": embeddedFileMap } : {}),
- },
- entrypoints: ["./src/index.ts", parserWorker, workerPath, ...(embeddedFileMap ? ["opencode-web-ui.gen.ts"] : [])],
- define: {
- KILO_VERSION: `'${Script.version}'`,
- KILO_MIGRATIONS: JSON.stringify(migrations),
- OTUI_TREE_SITTER_WORKER_PATH: bunfsRoot + workerRelativePath,
- KILO_WORKER_PATH: workerPath,
- KILO_CHANNEL: `'${Script.channel}'`,
- KILO_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
- },
- })
- // kilocode_change start - fix Nix-specific ELF interpreter paths for Linux binaries
- if (item.os === "linux") {
- const interpreters: Record<string, string> = {
- x64: "/lib64/ld-linux-x86-64.so.2",
- arm64: "/lib/ld-linux-aarch64.so.1",
- "x64-musl": "/lib/ld-musl-x86_64.so.1",
- "arm64-musl": "/lib/ld-musl-aarch64.so.1",
- }
- const key = item.abi === "musl" ? `${item.arch}-musl` : item.arch
- const interpreter = interpreters[key]
- if (interpreter) {
- try {
- await $`patchelf --set-interpreter ${interpreter} dist/${name}/bin/kilo`
- console.log(`patched interpreter for ${name} -> ${interpreter}`)
- } catch {
- console.warn(`patchelf not available, skipping interpreter fix for ${name}`)
- }
- }
- }
- // kilocode_change end
- // Smoke test: only run if binary is for current platform
- if (item.os === process.platform && item.arch === process.arch && !item.abi) {
- const binaryPath = `dist/${name}/bin/kilo` // kilocode_change
- console.log(`Running smoke test: ${binaryPath} --version`)
- try {
- const versionOutput = await $`${binaryPath} --version`.text()
- console.log(`Smoke test passed: ${versionOutput.trim()}`)
- } catch (e) {
- console.error(`Smoke test failed for ${name}:`, e)
- process.exit(1)
- }
- }
- await $`rm -rf ./dist/${name}/bin/tui`
- await Bun.file(`dist/${name}/package.json`).write(
- JSON.stringify(
- {
- name,
- version: Script.version,
- os: [item.os],
- cpu: [item.arch],
- // kilocode_change start
- repository: {
- type: "git",
- url: "https://github.com/Kilo-Org/kilocode",
- },
- // kilocode_change end
- },
- null,
- 2,
- ),
- )
- binaries[name] = Script.version
- }
- if (Script.release) {
- const archives: string[] = [] // kilocode_change
- for (const key of Object.keys(binaries)) {
- const archive = key.replace(pkg.name, "kilo") // kilocode_change
- if (key.includes("linux")) {
- const out = path.resolve("dist", `${archive}.tar.gz`) // kilocode_change
- await $`tar -czf ${out} *`.cwd(`dist/${key}/bin`) // kilocode_change
- archives.push(out) // kilocode_change
- } else {
- const out = path.resolve("dist", `${archive}.zip`) // kilocode_change
- await $`zip -r ${out} *`.cwd(`dist/${key}/bin`) // kilocode_change
- archives.push(out) // kilocode_change
- }
- }
- await $`gh release upload v${Script.version} ${archives} --clobber` // kilocode_change
- }
- export { binaries }
|