forked from getsentry/XcodeBuildMCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_settings.ts
More file actions
245 lines (214 loc) · 8.5 KB
/
build_settings.ts
File metadata and controls
245 lines (214 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Build Settings and Scheme Tools - Tools for viewing build settings and listing schemes
*
* This module provides tools for retrieving build settings and listing available schemes
* from Xcode projects and workspaces. These tools are useful for debugging and exploring
* project configuration.
*
* Responsibilities:
* - Listing available schemes in Xcode projects and workspaces
* - Retrieving detailed build settings for specific schemes
* - Providing formatted output for build settings
* - Supporting both project and workspace-based operations
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { log } from '../utils/logger.js';
import { executeXcodeCommand } from '../utils/xcode.js';
import { validateRequiredParam, createTextResponse } from '../utils/validation.js';
import { ToolResponse } from '../types/common.js';
import {
registerTool,
workspacePathSchema,
projectPathSchema,
schemeSchema,
BaseWorkspaceParams,
BaseProjectParams,
} from './common.js';
// --- Private Helper Functions ---
/**
* Internal logic for showing build settings.
*/
async function _handleShowBuildSettingsLogic(params: {
workspacePath?: string;
projectPath?: string;
scheme: string;
}): Promise<ToolResponse> {
log('info', `Showing build settings for scheme ${params.scheme}`);
try {
// Create the command array for xcodebuild
const command = ['xcodebuild', '-showBuildSettings']; // -showBuildSettings as an option, not an action
// Add the workspace or project
if (params.workspacePath) {
command.push('-workspace', params.workspacePath);
} else if (params.projectPath) {
command.push('-project', params.projectPath);
}
// Add the scheme
command.push('-scheme', params.scheme);
// Execute the command directly
const result = await executeXcodeCommand(command, 'Show Build Settings');
if (!result.success) {
return createTextResponse(`Failed to show build settings: ${result.error}`, true);
}
return {
content: [
{
type: 'text',
text: `✅ Build settings for scheme ${params.scheme}:`,
},
{
type: 'text',
text: result.output || 'Build settings retrieved successfully.',
},
],
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
log('error', `Error showing build settings: ${errorMessage}`);
return createTextResponse(`Error showing build settings: ${errorMessage}`, true);
}
}
/**
* Internal logic for listing schemes.
*/
async function _handleListSchemesLogic(params: {
workspacePath?: string;
projectPath?: string;
}): Promise<ToolResponse> {
log('info', 'Listing schemes');
try {
// For listing schemes, we can't use executeXcodeBuild directly since it's not a standard action
// We need to create a custom command with -list flag
const command = ['xcodebuild', '-list'];
if (params.workspacePath) {
command.push('-workspace', params.workspacePath);
} else if (params.projectPath) {
command.push('-project', params.projectPath);
} // No else needed, one path is guaranteed by callers
const result = await executeXcodeCommand(command, 'List Schemes');
if (!result.success) {
return createTextResponse(`Failed to list schemes: ${result.error}`, true);
}
// Extract schemes from the output
const schemesMatch = result.output.match(/Schemes:([\s\S]*?)(?=\n\n|$)/);
if (!schemesMatch) {
return createTextResponse('No schemes found in the output', true);
}
const schemeLines = schemesMatch[1].trim().split('\n');
const schemes = schemeLines.map((line) => line.trim()).filter((line) => line);
// Prepare next steps with the first scheme if available
let nextStepsText = '';
if (schemes.length > 0) {
const firstScheme = schemes[0];
const projectOrWorkspace = params.workspacePath ? 'workspace' : 'project';
const path = params.workspacePath || params.projectPath;
nextStepsText = `Next Steps:
1. Build the app: ${projectOrWorkspace === 'workspace' ? 'macos_build_workspace' : 'macos_build_project'}({ ${projectOrWorkspace}Path: "${path}", scheme: "${firstScheme}" })
or for iOS: ${projectOrWorkspace === 'workspace' ? 'ios_simulator_build_by_name_workspace' : 'ios_simulator_build_by_name_project'}({ ${projectOrWorkspace}Path: "${path}", scheme: "${firstScheme}", simulatorName: "iPhone 16" })
2. Show build settings: ${projectOrWorkspace === 'workspace' ? 'show_build_set_ws' : 'show_build_set_proj'}({ ${projectOrWorkspace}Path: "${path}", scheme: "${firstScheme}" })`;
}
return {
content: [
{
type: 'text',
text: `✅ Available schemes:`,
},
{
type: 'text',
text: schemes.join('\n'),
},
{
type: 'text',
text: nextStepsText,
},
],
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
log('error', `Error listing schemes: ${errorMessage}`);
return createTextResponse(`Error listing schemes: ${errorMessage}`, true);
}
}
// --- Public Tool Definitions ---
/**
* Registers the show build settings workspace tool
*/
export function registerShowBuildSettingsWorkspaceTool(server: McpServer): void {
registerTool<BaseWorkspaceParams>(
server,
'show_build_set_ws',
"Shows build settings from a workspace using xcodebuild. IMPORTANT: Requires workspacePath and scheme. Example: show_build_set_ws({ workspacePath: '/path/to/MyProject.xcworkspace', scheme: 'MyScheme' })",
{
workspacePath: workspacePathSchema,
scheme: schemeSchema,
},
async (params: BaseWorkspaceParams) => {
// Validate required parameters
const workspaceValidation = validateRequiredParam('workspacePath', params.workspacePath);
if (!workspaceValidation.isValid) return workspaceValidation.errorResponse!;
const schemeValidation = validateRequiredParam('scheme', params.scheme);
if (!schemeValidation.isValid) return schemeValidation.errorResponse!;
return _handleShowBuildSettingsLogic(params);
},
);
}
/**
* Registers the show build settings project tool
*/
export function registerShowBuildSettingsProjectTool(server: McpServer): void {
registerTool<BaseProjectParams>(
server,
'show_build_set_proj',
"Shows build settings from a project file using xcodebuild. IMPORTANT: Requires projectPath and scheme. Example: show_build_set_proj({ projectPath: '/path/to/MyProject.xcodeproj', scheme: 'MyScheme' })",
{
projectPath: projectPathSchema,
scheme: schemeSchema,
},
async (params: BaseProjectParams) => {
// Validate required parameters
const projectValidation = validateRequiredParam('projectPath', params.projectPath);
if (!projectValidation.isValid) return projectValidation.errorResponse!;
const schemeValidation = validateRequiredParam('scheme', params.scheme);
if (!schemeValidation.isValid) return schemeValidation.errorResponse!;
return _handleShowBuildSettingsLogic(params);
},
);
}
/**
* Registers the list schemes workspace tool
*/
export function registerListSchemesWorkspaceTool(server: McpServer): void {
registerTool<BaseWorkspaceParams>(
server,
'list_schems_ws',
"Lists available schemes in the workspace. IMPORTANT: Requires workspacePath. Example: list_schems_ws({ workspacePath: '/path/to/MyProject.xcworkspace' })",
{
workspacePath: workspacePathSchema,
},
async (params: BaseWorkspaceParams) => {
// Validate required parameters
const workspaceValidation = validateRequiredParam('workspacePath', params.workspacePath);
if (!workspaceValidation.isValid) return workspaceValidation.errorResponse!;
return _handleListSchemesLogic(params);
},
);
}
/**
* Registers the list schemes project tool
*/
export function registerListSchemesProjectTool(server: McpServer): void {
registerTool<BaseProjectParams>(
server,
'list_schems_proj',
"Lists available schemes in the project file. IMPORTANT: Requires projectPath. Example: list_schems_proj({ projectPath: '/path/to/MyProject.xcodeproj' })",
{
projectPath: projectPathSchema,
},
async (params: BaseProjectParams) => {
// Validate required parameters
const projectValidation = validateRequiredParam('projectPath', params.projectPath);
if (!projectValidation.isValid) return projectValidation.errorResponse!;
return _handleListSchemesLogic(params);
},
);
}