Файли
openidea/src/features/run/xcodeFiles.ts
T
LU XIANGRUIтаwildcatDownstairs 719b73ecd9 feat(xcode): flag new source files an Xcode project doesn't build (#6888)
xcodebuild compiles only the files a target lists, so a source file created or renamed in the IDE inside a classic .xcodeproj group was silently left out of the build. After file operations in the IDE (one check per burst), an XcodeGen or Tuist project gets an offer to regenerate it, and a classic project without synchronized folders gets a warning naming the files project.pbxproj doesn't list, with Open in Xcode. Regenerate Xcode Project is also in Find Action and in the context menus of project.yml / Project.swift. Names are matched as whole file names, and file operations without source files skip the check.

Closes #6887

Co-authored-by: wildcatDownstairs <[email protected]>

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-30 11:20:17 +08:00

166 рядки
6.2 KiB
TypeScript

/**
* 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<string>();
let removed = new Set<string>();
let timer: ReturnType<typeof setTimeout> | 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());
};
}