diff --git a/AGENTS.md b/AGENTS.md index ef5e2fa4d..2aa63ab8c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -79,6 +79,7 @@ Shared agent skills live in `.agents/skills/` (`.claude/skills/` links to them): `pnpm dev:app` also enables the global Tauri API. `pnpm dev:status` reports this worktree's running MCP bridges and actual ports. - `pnpm build` — typecheck + bundle frontend. +- `pnpm dogfood:build` / `pnpm dogfood:install` (or `mise run dogfood:build` / `mise run dogfood:install`) — build and locally install the grey-icon dogfood desktop app sharing release settings and data. - `cargo nextest run --workspace` (or `cargo test`), `cargo clippy --workspace --all-targets -- -D warnings`, `cargo fmt --all`. - `cargo test -p ide-host --test language_smoke -- --ignored --exact ` — code intelligence on `fixtures/` with that language's real server installed (CI: `Language smoke`, pinned versions). diff --git a/docs/en/contribute/setup.mdx b/docs/en/contribute/setup.mdx index faa8bd89e..899349675 100644 --- a/docs/en/contribute/setup.mdx +++ b/docs/en/contribute/setup.mdx @@ -67,7 +67,7 @@ mise run dev `setup` runs `pnpm install` in this worktree. `dev` and `dev:app` use the same worktree-aware launchers as the direct commands. No shell activation is needed for `mise run`. -Run `mise tasks` to list all six tasks. `mise run build` builds the frontend (not a desktop installer). `mise run test` runs Vitest and `cargo test --workspace`; `mise run check` runs the format, lint, type and unused-code checks, then both test suites. Neither requires nextest or the plugin WASM target. +Run `mise tasks` to list all tasks. `mise run build` builds the frontend (not a desktop installer); `mise run dogfood:build` and `mise run dogfood:install` build and locally install a grey-icon dogfood desktop app that shares settings and data with the release app. Only one of the two apps runs at a time, since they share one instance; settings written by the newer dogfood build can make an older release ignore the settings file. The dogfood app never updates itself: run `dogfood:install` again to pick up new changes. `mise run test` runs Vitest and `cargo test --workspace`; `mise run check` runs the format, lint, type and unused-code checks, then both test suites. Neither requires nextest or the plugin WASM target. ## Worktrees diff --git a/docs/zh/contribute/setup.mdx b/docs/zh/contribute/setup.mdx index d2f0071d8..87950897d 100644 --- a/docs/zh/contribute/setup.mdx +++ b/docs/zh/contribute/setup.mdx @@ -67,7 +67,7 @@ mise run dev `setup` 在当前 worktree 中执行 `pnpm install`。`dev` 和 `dev:app` 都沿用原来的启动脚本,保留 worktree 隔离。使用 `mise run` 不需要在 shell 中激活 mise。 -运行 `mise tasks` 可以查看全部六个任务。`mise run build` 构建前端,不生成桌面安装包。`mise run test` 运行 Vitest 和 `cargo test --workspace`;`mise run check` 先检查格式、代码规范、类型和未使用的代码,再运行这两套测试。两者都不需要 nextest 或插件的 WASM 目标。 +运行 `mise tasks` 可以查看全部任务。`mise run build` 构建前端,不生成桌面安装包;`mise run dogfood:build` 和 `mise run dogfood:install` 用于构建并在本地安装灰色图标的 dogfood 桌面版(与正式版共享设置和数据)。两者共用同一个实例,同一时间只能运行一个;较新的 dogfood 版写入的设置可能让较旧的正式版忽略整个设置文件。dogfood 版不会自动更新,需要更新时重新运行 `dogfood:install`。`mise run test` 运行 Vitest 和 `cargo test --workspace`;`mise run check` 先检查格式、代码规范、类型和未使用的代码,再运行这两套测试。两者都不需要 nextest 或插件的 WASM 目标。 ## Worktree diff --git a/mise.toml b/mise.toml index 4e0146049..93b95b73f 100644 --- a/mise.toml +++ b/mise.toml @@ -20,6 +20,15 @@ run = "pnpm dev:app" description = "Typecheck, bundle and verify the frontend" run = "pnpm build" +[tasks."dogfood:build"] +description = "Build a local dogfood desktop app bundle" +run = "pnpm dogfood:build" + +[tasks."dogfood:install"] +description = "Build and install the local dogfood desktop app" +depends = ["dogfood:build"] +run = "pnpm dogfood:install --no-build" + [tasks.test] description = "Run the frontend and Rust workspace tests" run = ["pnpm test", "cargo test --workspace"] diff --git a/package.json b/package.json index 0834d5f72..a018510f8 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,9 @@ "perf": "node perf/run.ts", "perf:rust": "node perf/rust.ts", "eval": "node evals/run.ts", - "release:prepare": "node scripts/release-prepare.ts" + "release:prepare": "node scripts/release-prepare.ts", + "dogfood:build": "node scripts/dogfood.ts build", + "dogfood:install": "node scripts/dogfood.ts install" }, "simple-git-hooks": { "pre-commit": "pnpm lint-staged" diff --git a/scripts/dogfood.test.ts b/scripts/dogfood.test.ts new file mode 100644 index 000000000..ba4989cb2 --- /dev/null +++ b/scripts/dogfood.test.ts @@ -0,0 +1,135 @@ +// @vitest-environment node +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; +import { + DEFAULT_DOGFOOD_APP_NAME, + dogfoodBuildEnv, + dogfoodCliArgs, + dogfoodPaths, + dogfoodTauriConfig, + generateDogfoodIcons, + installDogfood, + parseDogfoodArgs, +} from "./dogfood.ts"; + +const root = fileURLToPath(new URL("..", import.meta.url)); + +describe("dogfood build and install", () => { + it("parses build and install arguments, including pnpm separator flags", () => { + expect(parseDogfoodArgs(["build"])).toEqual({ action: "build", skipBuild: false }); + expect(parseDogfoodArgs(["install"])).toEqual({ action: "install", skipBuild: false }); + expect(parseDogfoodArgs(["install", "--no-build"])).toEqual({ action: "install", skipBuild: true }); + expect(parseDogfoodArgs(["install", "--", "--no-build"])).toEqual({ action: "install", skipBuild: true }); + expect(() => parseDogfoodArgs([])).toThrow(/Expected 'build' or 'install'/); + expect(() => parseDogfoodArgs(["build", "--no-build"])).toThrow(/Unexpected option/); + }); + + it("keeps the vanilla app identifier so settings, MCP, Skills and agent sessions are shared, without updates", () => { + const baseConfig = JSON.parse(readFileSync(join(root, "src-tauri", "tauri.conf.json"), "utf8")) as { + identifier: string; + }; + const override = dogfoodTauriConfig(DEFAULT_DOGFOOD_APP_NAME, ["../target/dogfood-icons/dogfood.icns"]); + expect(baseConfig.identifier).toBe("ai.l0ng.openidea"); + expect("identifier" in override).toBe(false); + expect(override).toEqual({ + productName: "openidea_dogfood", + bundle: { + createUpdaterArtifacts: false, + icon: ["../target/dogfood-icons/dogfood.icns"], + }, + plugins: { + updater: { endpoints: [] }, + }, + }); + }); + + it("uses fast release compilation defaults while respecting caller overrides", () => { + expect(dogfoodBuildEnv({})).toEqual({ + CARGO_PROFILE_RELEASE_LTO: "off", + CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16", + }); + expect(dogfoodBuildEnv({ CARGO_PROFILE_RELEASE_LTO: "thin", CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "4" })).toEqual({ + CARGO_PROFILE_RELEASE_LTO: "thin", + CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "4", + }); + }); + + it("resolves platform bundle arguments and install destinations", () => { + expect(dogfoodCliArgs("openidea_dogfood", ["icon.icns"], "darwin").slice(0, 3)).toEqual([ + "build", + "--bundles", + "app", + ]); + expect(dogfoodCliArgs("openidea_dogfood", ["icon.ico"], "win32").slice(0, 2)).toEqual(["build", "--no-bundle"]); + expect(dogfoodPaths(join("/repo", "target"), "openidea_dogfood", "darwin", {})).toEqual({ + bundlePath: join("/repo", "target", "release", "bundle", "macos", "openidea_dogfood.app"), + installPath: join("/Applications", "openidea_dogfood.app"), + }); + expect( + dogfoodPaths(join("/repo", "target"), "openidea_dogfood", "darwin", { + DOGFOOD_INSTALL_DIR: "/Users/me/Applications", + }), + ).toEqual({ + bundlePath: join("/repo", "target", "release", "bundle", "macos", "openidea_dogfood.app"), + installPath: join("/Users/me/Applications", "openidea_dogfood.app"), + }); + }); + + it("keeps the dogfood source SVGs geometrically identical to the vanilla icons in grey", () => { + const sourceDir = join(root, "src-tauri", "icons", "source"); + for (const [vanillaName, dogfoodName] of [ + ["openidea.svg", "openidea-dogfood.svg"], + ["openidea-windows.svg", "openidea-dogfood-windows.svg"], + ] as const) { + const vanilla = readFileSync(join(sourceDir, vanillaName), "utf8"); + const dogfood = readFileSync(join(sourceDir, dogfoodName), "utf8"); + expect(dogfood).not.toContain("#D9582C"); + const recolored = vanilla + .replaceAll('aria-label="OpenIDEA"', 'aria-label="OpenIDEA Dogfood"') + .replaceAll("#EAE7E0", "#E6E7E9") + .replaceAll("#D9582C", "#787B82") + .replaceAll("#1B1A1A", "#1B1C1E"); + expect(dogfood).toBe(recolored); + } + }); + + it("generates the dogfood icon bundle with a distinct macOS .icns name", () => { + const tempDir = mkdtempSync(join(tmpdir(), "openidea-dogfood-icons-")); + try { + const iconPaths = generateDogfoodIcons(root, tempDir, "darwin"); + expect(iconPaths).toHaveLength(5); + expect(iconPaths.some((p) => p.endsWith("/dogfood.icns"))).toBe(true); + expect(existsSync(join(tempDir, "dogfood.icns"))).toBe(true); + expect(existsSync(join(tempDir, "128x128@2x.png"))).toBe(true); + expect(existsSync(join(tempDir, "icon.ico"))).toBe(true); + const firstMtime = statSync(join(tempDir, "dogfood.icns")).mtimeMs; + expect(generateDogfoodIcons(root, tempDir, "darwin")).toEqual(iconPaths); + expect(statSync(join(tempDir, "dogfood.icns")).mtimeMs).toBe(firstMtime); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }, 30_000); + + it("installs a macOS .app bundle into DOGFOOD_INSTALL_DIR", () => { + if (process.platform !== "darwin") return; + const tempDir = mkdtempSync(join(tmpdir(), "openidea-dogfood-install-")); + const prevInstallDir = process.env.DOGFOOD_INSTALL_DIR; + try { + const fakeRoot = join(tempDir, "repo"); + const fakeBundle = join(fakeRoot, "target", "release", "bundle", "macos", "openidea_dogfood.app"); + mkdirSync(join(fakeBundle, "Contents", "MacOS"), { recursive: true }); + writeFileSync(join(fakeBundle, "Contents", "MacOS", "openidea"), "#!/bin/sh\n"); + const destDir = join(tempDir, "Applications"); + process.env.DOGFOOD_INSTALL_DIR = destDir; + installDogfood(fakeRoot, "openidea_dogfood", "darwin", join(fakeRoot, "target")); + expect(existsSync(join(destDir, "openidea_dogfood.app", "Contents", "MacOS", "openidea"))).toBe(true); + } finally { + if (prevInstallDir === undefined) delete process.env.DOGFOOD_INSTALL_DIR; + else process.env.DOGFOOD_INSTALL_DIR = prevInstallDir; + rmSync(tempDir, { recursive: true, force: true }); + } + }); +}); diff --git a/scripts/dogfood.ts b/scripts/dogfood.ts new file mode 100644 index 000000000..2a507ef78 --- /dev/null +++ b/scripts/dogfood.ts @@ -0,0 +1,319 @@ +import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { + chmodSync, + copyFileSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + rmSync, + utimesSync, + writeFileSync, +} from "node:fs"; +import { createRequire } from "node:module"; +import { homedir } from "node:os"; +import { dirname, join, relative } from "node:path"; +import { fileURLToPath } from "node:url"; + +export const DEFAULT_DOGFOOD_APP_NAME = "openidea_dogfood"; + +const LSREGISTER_PATH = + "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"; + +export interface DogfoodCommand { + action: "build" | "install"; + skipBuild: boolean; +} + +export interface DogfoodPaths { + bundlePath: string; + installPath: string; +} + +export function parseDogfoodArgs(args: readonly string[]): DogfoodCommand { + const filtered = args.filter((arg) => arg !== "--"); + const action = filtered[0]; + if (action !== "build" && action !== "install") { + throw new Error(`Expected 'build' or 'install', got ${action ? `'${action}'` : "no command"}`); + } + let skipBuild = false; + for (const flag of filtered.slice(1)) { + if (action === "install" && flag === "--no-build") { + skipBuild = true; + } else { + throw new Error(`Unexpected option '${flag}' for '${action}'`); + } + } + return { action, skipBuild }; +} + +/** + * Tauri config override for local dogfood builds. + * + * Keeps `identifier` untouched (`ai.l0ng.openidea` from `src-tauri/tauri.conf.json`) so the + * dogfood app shares configuration, settings, WebView storage, MCP servers, Skills, agent + * sessions, credentials, plugins and caches with the vanilla release app. + * + * Clears the updater endpoints: the next release would otherwise replace the dogfood bundle + * with the official app. + */ +export function dogfoodTauriConfig(appName: string, iconPaths: readonly string[]) { + return { + productName: appName, + bundle: { + createUpdaterArtifacts: false, + icon: [...iconPaths], + }, + plugins: { + updater: { endpoints: [] }, + }, + }; +} + +export function dogfoodBuildEnv(baseEnv: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv { + return { + ...baseEnv, + CARGO_PROFILE_RELEASE_LTO: baseEnv.CARGO_PROFILE_RELEASE_LTO ?? "off", + CARGO_PROFILE_RELEASE_CODEGEN_UNITS: baseEnv.CARGO_PROFILE_RELEASE_CODEGEN_UNITS ?? "16", + }; +} + +export function dogfoodCliArgs( + appName: string, + iconPaths: readonly string[], + platform: NodeJS.Platform = process.platform, +): string[] { + const config = JSON.stringify(dogfoodTauriConfig(appName, iconPaths)); + if (platform === "darwin") { + return ["build", "--bundles", "app", "--config", config]; + } + return ["build", "--no-bundle", "--config", config]; +} + +/** Cargo's target directory, which a `CARGO_TARGET_DIR` or `build.target-dir` setting can move. */ +export function cargoTargetDir(root: string): string { + const result = spawnSync("cargo", ["metadata", "--format-version", "1", "--no-deps"], { + cwd: root, + encoding: "utf8", + maxBuffer: 64 * 1024 * 1024, + }); + if (result.status === 0) { + try { + const dir = (JSON.parse(result.stdout) as { target_directory?: unknown }).target_directory; + if (typeof dir === "string" && dir) return dir; + } catch { + // Fall back to the default below. + } + } + return join(root, "target"); +} + +export function dogfoodPaths( + targetDir: string, + appName: string, + platform: NodeJS.Platform = process.platform, + env: NodeJS.ProcessEnv = process.env, +): DogfoodPaths { + const customInstallPath = env.DOGFOOD_INSTALL_PATH?.trim(); + const customInstallDir = env.DOGFOOD_INSTALL_DIR?.trim(); + if (platform === "darwin") { + return { + bundlePath: join(targetDir, "release", "bundle", "macos", `${appName}.app`), + installPath: customInstallPath || join(customInstallDir || "/Applications", `${appName}.app`), + }; + } + if (platform === "win32") { + const localAppData = env.LOCALAPPDATA?.trim() || join(homedir(), "AppData", "Local"); + return { + bundlePath: join(targetDir, "release", "openidea.exe"), + installPath: customInstallPath || join(customInstallDir || localAppData, appName), + }; + } + return { + bundlePath: join(targetDir, "release", "openidea"), + installPath: customInstallPath || join(customInstallDir || join(homedir(), ".local", "bin"), appName), + }; +} + +function toPosixPath(value: string): string { + return value.replaceAll("\\", "/"); +} + +function runTauriIcon(root: string, cli: string, svgPath: string, outputDir: string) { + const result = spawnSync(process.execPath, [cli, "icon", svgPath, "--output", outputDir], { + cwd: root, + stdio: "inherit", + }); + if (result.error) throw result.error; + if (result.status !== 0) throw new Error(`Dogfood icon generation failed (${result.status})`); +} + +export function generateDogfoodIcons( + root: string, + outputDir: string, + platform: NodeJS.Platform = process.platform, +): string[] { + const sourceDir = join(root, "src-tauri", "icons", "source"); + const svgPath = join(sourceDir, "openidea-dogfood.svg"); + const winSvgPath = join(sourceDir, "openidea-dogfood-windows.svg"); + const iconFiles = ["32x32.png", "128x128.png", "128x128@2x.png", "dogfood.icns", "icon.ico"]; + const stampPath = join(outputDir, ".source-sha256"); + const stamp = createHash("sha256") + .update(platform) + .update("\0") + .update(readFileSync(svgPath)) + .update("\0") + .update(readFileSync(winSvgPath)) + .digest("hex"); + + // Preserve file mtimes/bytes when SVGs are unchanged so `tauri-build` does not re-link `src-tauri`. + const upToDate = + existsSync(stampPath) && + readFileSync(stampPath, "utf8").trim() === stamp && + iconFiles.every((file) => existsSync(join(outputDir, file))); + + if (!upToDate) { + const require = createRequire(import.meta.url); + const cli = require.resolve("@tauri-apps/cli/tauri.js"); + mkdirSync(outputDir, { recursive: true }); + runTauriIcon(root, cli, svgPath, outputDir); + + if (platform === "win32") { + const winTemp = mkdtempSync(join(outputDir, "win-")); + try { + runTauriIcon(root, cli, winSvgPath, winTemp); + copyFileSync(join(winTemp, "icon.ico"), join(outputDir, "icon.ico")); + } finally { + rmSync(winTemp, { recursive: true, force: true }); + } + } + + // Distinct .icns filename avoids macOS LaunchServices icon-cache collisions with OpenIDEA.app. + copyFileSync(join(outputDir, "icon.icns"), join(outputDir, "dogfood.icns")); + writeFileSync(stampPath, `${stamp}\n`); + } + + const tauriDir = join(root, "src-tauri"); + return iconFiles.map((file) => toPosixPath(relative(tauriDir, join(outputDir, file)))); +} + +function runChecked(command: string, args: string[], options?: { cwd?: string; env?: NodeJS.ProcessEnv }) { + const result = spawnSync(command, args, { stdio: "inherit", ...options }); + if (result.error) throw result.error; + if (result.status !== 0) throw new Error(`${command} exited with status ${result.status}`); +} + +export function buildDogfood(root: string, appName: string) { + const iconsDir = join(root, "target", "dogfood-icons"); + const iconPaths = generateDogfoodIcons(root, iconsDir); + const args = dogfoodCliArgs(appName, iconPaths); + runChecked(process.execPath, [join(root, "scripts", "tauri.ts"), ...args], { + cwd: root, + env: dogfoodBuildEnv(), + }); +} + +const SHARED_DATA_NOTES = [ + "The dogfood app shares its single instance with OpenIDEA: quit one before opening the other.", + "It writes OpenIDEA's settings and data; a setting only newer builds know can make an older OpenIDEA ignore your settings file.", + "It never updates itself: run 'pnpm dogfood:install' again to pick up new changes.", +]; + +export function installDogfood( + root: string, + appName: string, + platform: NodeJS.Platform = process.platform, + targetDir: string = cargoTargetDir(root), +) { + const { bundlePath, installPath } = dogfoodPaths(targetDir, appName, platform); + installBundle(root, appName, platform, bundlePath, installPath); + for (const note of SHARED_DATA_NOTES) console.log(note); +} + +function installBundle( + root: string, + appName: string, + platform: NodeJS.Platform, + bundlePath: string, + installPath: string, +) { + if (!existsSync(bundlePath)) { + throw new Error(`Dogfood build output not found at ${bundlePath}; run 'mise run dogfood:build' first`); + } + + if (platform === "darwin") { + rmSync(installPath, { recursive: true, force: true }); + mkdirSync(dirname(installPath), { recursive: true }); + runChecked("cp", ["-R", bundlePath, installPath]); + runChecked("xattr", ["-cr", installPath]); + const now = new Date(); + utimesSync(installPath, now, now); + if (existsSync(LSREGISTER_PATH)) { + spawnSync(LSREGISTER_PATH, ["-f", installPath], { stdio: "ignore" }); + } + console.log(`Installed ${appName} to ${installPath}`); + return; + } + + if (platform === "win32") { + mkdirSync(installPath, { recursive: true }); + const targetExe = join(installPath, `${appName}.exe`); + copyFileSync(bundlePath, targetExe); + const webviewLoader = join(dirname(bundlePath), "WebView2Loader.dll"); + if (existsSync(webviewLoader)) { + copyFileSync(webviewLoader, join(installPath, "WebView2Loader.dll")); + } + console.log(`Installed ${appName} to ${targetExe}`); + return; + } + + mkdirSync(dirname(installPath), { recursive: true }); + copyFileSync(bundlePath, installPath); + chmodSync(installPath, 0o755); + const dataHome = process.env.XDG_DATA_HOME?.trim() || join(homedir(), ".local", "share"); + const iconSource = join(root, "target", "dogfood-icons", "128x128.png"); + if (existsSync(iconSource)) { + const iconDest = join(dataHome, "icons", "hicolor", "128x128", "apps", `${appName}.png`); + mkdirSync(dirname(iconDest), { recursive: true }); + copyFileSync(iconSource, iconDest); + } + const desktopFile = join(dataHome, "applications", `${appName}.desktop`); + mkdirSync(dirname(desktopFile), { recursive: true }); + writeFileSync( + desktopFile, + [ + "[Desktop Entry]", + "Type=Application", + `Name=${appName}`, + `Exec=${installPath} %F`, + `Icon=${appName}`, + "Categories=Development;IDE;", + "StartupWMClass=openidea", + "", + ].join("\n"), + ); + console.log(`Installed ${appName} to ${installPath}`); +} + +function main() { + const root = realpathSync(fileURLToPath(new URL("..", import.meta.url))); + const { action, skipBuild } = parseDogfoodArgs(process.argv.slice(2)); + const appName = process.env.DOGFOOD_APP_NAME?.trim() || DEFAULT_DOGFOOD_APP_NAME; + if (action === "build" || !skipBuild) { + buildDogfood(root, appName); + } + if (action === "install") { + installDogfood(root, appName); + } +} + +if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) { + try { + main(); + } catch (error) { + console.error(error instanceof Error ? error.message : error); + process.exitCode = 1; + } +} diff --git a/src-tauri/icons/source/openidea-dogfood-windows.svg b/src-tauri/icons/source/openidea-dogfood-windows.svg new file mode 100644 index 000000000..bffa7d365 --- /dev/null +++ b/src-tauri/icons/source/openidea-dogfood-windows.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src-tauri/icons/source/openidea-dogfood.svg b/src-tauri/icons/source/openidea-dogfood.svg new file mode 100644 index 000000000..6b7422726 --- /dev/null +++ b/src-tauri/icons/source/openidea-dogfood.svg @@ -0,0 +1 @@ +