import { z } from 'zod';
import { xcodebuildList } from '../executor/xcodebuild.js';
import { resolveProjectArgs, getWorkingDirectory } from '../utils/project-resolver.js';
export const xcodebuildListSchema = 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)'),
});
export type XcodebuildListInput = z.infer<typeof xcodebuildListSchema>;
function validateInput(input: XcodebuildListInput): void {
if (input.workspace && input.project) {
throw new Error('Cannot specify both workspace and project');
}
}
export const xcodebuildListTool = {
name: 'xcodebuild_list',
description: 'List schemes, targets, and configurations for an Xcode project or workspace. Auto-detects project if not specified.',
inputSchema: xcodebuildListSchema,
handler: async (input: XcodebuildListInput) => {
validateInput(input);
const projectArgs = resolveProjectArgs(input);
const cwd = getWorkingDirectory(input);
const output = xcodebuildList(projectArgs, { cwd });
// Format the output nicely
const lines: string[] = [];
if (output.workspace) {
lines.push(`Workspace: ${output.workspace.name}`);
lines.push('');
lines.push('Schemes:');
for (const scheme of output.workspace.schemes) {
lines.push(` - ${scheme}`);
}
}
if (output.project) {
lines.push(`Project: ${output.project.name}`);
lines.push('');
lines.push('Targets:');
for (const target of output.project.targets) {
lines.push(` - ${target}`);
}
lines.push('');
lines.push('Schemes:');
for (const scheme of output.project.schemes) {
lines.push(` - ${scheme}`);
}
lines.push('');
lines.push('Configurations:');
for (const config of output.project.configurations) {
lines.push(` - ${config}`);
}
}
return {
content: [
{
type: 'text' as const,
text: lines.join('\n'),
},
],
};
},
};