/** * Xcode's Product › Clean Build Folder and deleting the project's DerivedData (#6665 C6), for the * selected `xcode` configuration (else the first one), or the right-clicked `.xcodeproj` / * `.xcworkspace`; in Build › Xcode, Find Action and the Project view's context menu. Clean runs * `xcodebuild clean` in the Run tool window with the build flags a run uses (host: * `ide_run::xcode_clean`); Delete DerivedData asks first, then moves Xcode's `-` * folder and the IDE's run builds to the Trash. */ import { registerActions } from "@/platform/actions"; import { tx } from "@/platform/i18n"; import { call, commands, type RunConfig } from "@/platform/ipc"; import { notifications, registerMenuGroup, registerMenuSlotItem } from "@/workbench/contributions"; import { iosSimulatorFor } from "@/workbench/iosSimulators"; import { confirm } from "@/workbench/messages"; import { startRun, useRun } from "./store"; const CLEAN = "xcode.cleanBuildFolder"; const DERIVED_DATA = "xcode.deleteDerivedData"; const baseName = (path: string) => path.replace(/\/+$/, "").split("/").pop() ?? ""; /** The `xcode` configuration to act on: of the right-clicked container (`arg`), else the selected one, else the first. */ export function xcodeConfigFor( configs: readonly RunConfig[], selectedId: string | null, arg?: unknown, ): RunConfig | null { const container = typeof arg === "string" ? baseName(arg) : null; const xcode = configs.filter( (c) => c.kind === "xcode" && (!container || baseName(c.params?.container ?? "") === container), ); return xcode.find((c) => c.id === selectedId) ?? xcode[0] ?? null; } function target(arg?: unknown): RunConfig | null { const { configs, selectedId } = useRun.getState(); return xcodeConfigFor(configs, selectedId, arg); } /** The transient `xcode-clean` run of `config`: its scheme and build flags, on the toolbar's simulator. */ export function cleanConfig(config: RunConfig, simulator: string | null): RunConfig { return { ...config, id: `xcode:clean:${config.params?.container ?? ""}:${config.params?.scheme ?? ""}`, name: tx`Clean ${config.name}`, kind: "xcode-clean", // The host fills in `xcodebuild … clean`. args: [], env: {}, params: { ...config.params, simulator: simulator ?? "" }, isTest: false, debugAdapter: null, }; } async function cleanBuildFolder(arg?: unknown) { const config = target(arg); if (!config) return; const clean = cleanConfig(config, iosSimulatorFor(config)); const [applied] = await call(commands.runApplySettings([clean])); await startRun(applied ?? clean); } function notify(level: "info" | "error", title: string, body?: string) { notifications().notify({ group: "run", level, key: DERIVED_DATA, title, body }); } async function deleteDerivedData(arg?: unknown) { const config = target(arg); if (!config) return; const at = { cwd: config.cwd ?? null, params: config.params ?? {} }; try { const dirs = await call(commands.xcodeDerivedData(at.cwd, at.params)); if (!dirs.length) return notify("info", tx`No DerivedData to delete`); const ok = await confirm({ title: tx`Delete DerivedData`, message: `${tx`Move the DerivedData of ${config.params?.container ?? config.name} to the Trash? The next build starts from scratch.`}\n\n${dirs.join("\n")}`, ok: tx`Move to Trash`, icon: "warning", }); if (!ok) return; const deleted = await call(commands.xcodeDeleteDerivedData(at.cwd, at.params)); notify("info", tx`DerivedData moved to the Trash`, deleted.join("\n")); } catch (e) { notify("error", tx`Could not delete DerivedData`, e instanceof Error ? e.message : String(e)); } } /** The native Build menu's Xcode submenu (`ide_host::menu::DYNAMIC_XCODE_BUILD`). */ const XCODE_BUILD_SLOT = "xcodeBuild"; /** Lists the actions in Build › Xcode while the project has an `xcode` configuration. */ function syncBuildMenu(): () => void { let inMenu: (() => void)[] = []; const sync = () => { const xcode = !!target(); if (xcode === inMenu.length > 0) return; inMenu.forEach((d) => d()); inMenu = xcode ? [ registerMenuSlotItem(XCODE_BUILD_SLOT, { action: CLEAN, label: "Clean Build Folder" }), registerMenuSlotItem(XCODE_BUILD_SLOT, { action: DERIVED_DATA, label: "Delete DerivedData" }), ] : []; }; sync(); const off = useRun.subscribe((s, old) => { if (s.configs !== old.configs) sync(); }); return () => { off(); inMenu.forEach((d) => d()); }; } const isContainer = (path: string) => /\.(xcodeproj|xcworkspace)\/?$/.test(path); export function registerXcodeCleanActions(): () => void { const disposers = [ syncBuildMenu(), registerActions([ { id: CLEAN, title: "Clean Build Folder", category: "Build", keywords: ["xcode", "xcodebuild clean"], when: "projectOpen", enabled: () => !!target(), run: ({ arg }) => cleanBuildFolder(arg), }, { id: DERIVED_DATA, title: "Delete DerivedData", category: "Build", keywords: ["xcode", "derived data"], when: "projectOpen", enabled: () => !!target(), run: ({ arg }) => deleteDerivedData(arg), }, ]), registerMenuGroup({ id: "xcode.clean", menus: ["projectView"], order: 45, items: (_menu, { paths }) => { const path = paths.length === 1 ? paths[0]! : null; if (!path || !isContainer(path) || !target(path)) return []; return [ { kind: "action", action: CLEAN, arg: path }, { kind: "action", action: DERIVED_DATA, arg: path }, ]; }, }), ]; return () => disposers.reverse().forEach((d) => d()); }