/** * New source files in an Xcode project (#6665 C7): `xcodebuild` compiles only the files a target * lists. After files are created, renamed or deleted in the IDE (one check per burst), an XcodeGen * (`project.yml`) or Tuist (`Project.swift`) project gets an offer to regenerate it; a classic * `.xcodeproj` without synchronized folders says which new files it doesn't name, with Open in * Xcode. The IDE never edits `project.pbxproj`. */ import { registerActions } from "@/platform/actions"; import { onFileOperation, onFilesMoved } from "@/platform/fileOps"; import { tx } from "@/platform/i18n"; import { call, commands, isTauri, type RunConfig } from "@/platform/ipc"; import { isMacHost } from "@/platform/keymap"; import { useProject } from "@/platform/project"; import { maybeEditorService, notifications, registerMenuGroup } from "@/workbench/contributions"; import { startRun } from "./store"; const BURST_MS = 800; const KEY = "xcode-files"; const SPEC = /\/(project\.yml|Project\.swift)$/; /** Same list as the backend: only these make `xcodebuild` miss code. */ const SOURCE = /\.(swift|m|mm|h|c|cc|cpp|hpp|metal)$/; function regenerateCommand(tool: string): string[] { return tool === "tuist" ? ["tuist", "generate", "--no-open"] : ["xcodegen", "generate"]; } function commandConfig(dir: string, [command, ...args]: string[], name: string): RunConfig { return { id: `xcode-files:${command}:${dir}`, name, kind: "external", command: command!, args, cwd: dir, env: {}, detected: true, isTest: false, debugAdapter: null, }; } async function run(config: RunConfig) { const [applied] = await call(commands.runApplySettings([config])); await startRun(applied ?? config); } const parentOf = (path: string) => path.slice(0, path.lastIndexOf("/")); async function regenerate(dir: string, tool: string) { notifications().expire?.(KEY); const command = regenerateCommand(tool); await run(commandConfig(dir, command, command.join(" "))); } /** Regenerates the project of `arg` (a `project.yml` / `Project.swift` path), else of the active file or the root. */ async function regenerateFor(arg: unknown) { const root = useProject.getState().project?.root; const path = typeof arg === "string" ? arg : (maybeEditorService()?.activePath() ?? `${root}/_`); if (SPEC.test(path)) return regenerate(parentOf(path), path.endsWith("/Project.swift") ? "tuist" : "xcodegen"); // A removed source file next to the active one: the backend finds the spec above it. const sync = root ? await call(commands.xcodeFileSync([], [`${parentOf(path)}/_.swift`])) : null; if (sync?.kind === "generated") return regenerate(sync.dir, sync.tool); notifications().notify({ group: "run", level: "info", key: KEY, title: "No XcodeGen or Tuist project found", body: "Open a file of a project with project.yml or Project.swift.", }); } async function check(created: string[], removed: string[]) { const project = useProject.getState().project; if (!isTauri() || !project?.trusted) return; if (![...created, ...removed].some((p) => SOURCE.test(p))) return; const sync = await call(commands.xcodeFileSync(created, removed)); if (!sync || useProject.getState().project?.root !== project.root) return; if (sync.kind === "generated") { const { tool } = sync; const command = regenerateCommand(tool).join(" "); notifications().notify({ group: "run", level: "info", key: KEY, title: "Xcode project may be out of date", body: tx`Files changed in a project generated by ${tool}. Run '${command}' so Xcode builds them.`, actions: [{ label: "Regenerate", run: () => void regenerate(sync.dir, sync.tool) }], }); return; } const name = sync.project.slice(sync.project.lastIndexOf("/") + 1); const [file] = sync.files; const count = sync.files.length; const files = sync.files.join(", "); notifications().notify({ group: "run", level: "warning", key: KEY, title: count === 1 ? tx`${file} isn't in the Xcode project` : tx`${count} files aren't in the Xcode project`, body: count === 1 ? tx`xcodebuild doesn't compile it until it's in a target of ${name}. Add it in Xcode or convert the group to a folder.` : tx`xcodebuild doesn't compile ${files} until they're in a target of ${name}. Add them in Xcode or convert the group to a folder.`, actions: isMacHost() ? [ { label: "Open in Xcode", run: () => void run(commandConfig(parentOf(sync.project), ["open", "-a", "Xcode", name], "Open in Xcode")), }, ] : [], }); } export function registerXcodeFiles(): () => void { let created = new Set(); let removed = new Set(); let timer: ReturnType | undefined; const queue = (add: readonly string[], gone: readonly string[]) => { add.forEach((p) => created.add(p)); gone.forEach((p) => removed.add(p)); clearTimeout(timer); timer = setTimeout(() => { const paths = [...created]; const gonePaths = [...removed]; created = new Set(); removed = new Set(); void check(paths, gonePaths).catch(() => {}); }, BURST_MS); }; const disposers = [ onFileOperation((op) => (op.kind === "created" ? queue(op.paths, []) : queue([], op.paths))), onFilesMoved((moves) => queue( moves.map((m) => m.to), moves.map((m) => m.from), ), ), registerActions([ { id: "xcode.regenerateProject", title: "Regenerate Xcode Project", category: "Run", keywords: ["xcodegen generate", "tuist generate"], when: "projectOpen", run: ({ arg }) => regenerateFor(arg), }, ]), // project.yml / Project.swift in the editor, its tab and the project view. registerMenuGroup({ id: "xcode.regenerateProject", menus: ["editor", "editorTab", "projectView"], order: 30, items: (_menu, { paths, dirs }) => { const path = paths.length === 1 && !dirs?.[0] ? paths[0]! : null; if (!path || !SPEC.test(path)) return []; return [{ kind: "action", action: "xcode.regenerateProject", arg: path, label: "Regenerate Xcode Project" }]; }, }), ]; return () => { clearTimeout(timer); disposers.forEach((d) => d()); }; }