import { z } from 'zod';
import { xcodebuildClean } from '../executor/xcodebuild.js';
import { resolveProjectArgs, getWorkingDirectory } from '../utils/project-resolver.js';
import { XCODEBUILD_TIMEOUTS } from '../types/xcodebuild.js';
export const xcodebuildCleanSchema = z.object({
workspace: z
.string()
.optional()
.describe('Explicit workspace path (.xcworkspace)'),
project: z
.string()
.optional()
.describe('Explicit project path (.xcodeproj)'),
directory: z
.string()
.optional()
.describe('Directory to search for workspace/project (defaults to cwd)'),
scheme: z
.string()
.describe('Scheme to clean (required)'),
});
export type XcodebuildCleanInput = z.infer<typeof xcodebuildCleanSchema>;
function validateInput(input: XcodebuildCleanInput): void {
if (input.workspace && input.project) {
throw new Error('Cannot specify both workspace and project');
}
}
export const xcodebuildCleanTool = {
name: 'xcodebuild_clean',
description: 'Clean build artifacts for an Xcode scheme.',
inputSchema: xcodebuildCleanSchema,
handler: async (input: XcodebuildCleanInput) => {
validateInput(input);
const projectArgs = resolveProjectArgs(input);
const cwd = getWorkingDirectory(input);
const args: string[] = [
...projectArgs,
'-scheme', input.scheme,
];
const result = xcodebuildClean(args, {
cwd,
timeout: XCODEBUILD_TIMEOUTS.clean,
});
if (result.exitCode !== 0) {
throw new Error(`Clean failed for scheme "${input.scheme}": ${result.stderr || result.stdout}`);
}
return {
content: [
{
type: 'text' as const,
text: `Successfully cleaned scheme "${input.scheme}"`,
},
],
};
},
};