forked from getsentry/XcodeBuildMCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools-cli.ts
More file actions
686 lines (593 loc) · 21.2 KB
/
tools-cli.ts
File metadata and controls
686 lines (593 loc) · 21.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#!/usr/bin/env node
/**
* XcodeBuildMCP Tools CLI
*
* A unified command-line tool that provides comprehensive information about
* XcodeBuildMCP tools and resources. Supports both runtime inspection
* (actual server state) and static analysis (source file analysis).
*
* Usage:
* npm run tools [command] [options]
* npx tsx src/cli/tools-cli.ts [command] [options]
*
* Commands:
* count, c Show tool and workflow counts
* list, l List all tools and resources
* static, s Show static source file analysis
* help, h Show this help message
*
* Options:
* --runtime, -r Use runtime inspection (respects env config)
* --static, -s Use static file analysis (development mode)
* --tools, -t Include tools in output
* --resources Include resources in output
* --workflows, -w Include workflow information
* --verbose, -v Show detailed information
* --json Output JSON format
* --help Show help for specific command
*
* Examples:
* npm run tools # Runtime summary with workflows
* npm run tools:count # Runtime tool count
* npm run tools:static # Static file analysis
* npm run tools:list # List runtime tools
* npx tsx src/cli/tools-cli.ts --json # JSON output
*/
import { spawn } from 'child_process';
import * as path from 'path';
import { fileURLToPath } from 'url';
import * as fs from 'fs';
import { getStaticToolAnalysis, type StaticAnalysisResult } from './analysis/tools-analysis.js';
// Get project paths
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// ANSI color codes
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
magenta: '\x1b[35m',
} as const;
// Types
interface CLIOptions {
runtime: boolean;
static: boolean;
tools: boolean;
resources: boolean;
workflows: boolean;
verbose: boolean;
json: boolean;
help: boolean;
}
interface RuntimeTool {
name: string;
description: string;
}
interface RuntimeResource {
uri: string;
name: string;
description: string;
}
interface RuntimeData {
tools: RuntimeTool[];
resources: RuntimeResource[];
toolCount: number;
resourceCount: number;
mode: 'runtime';
}
// CLI argument parsing
const args = process.argv.slice(2);
// Find the command (first non-flag argument)
let command = 'count'; // default
for (const arg of args) {
if (!arg.startsWith('-')) {
command = arg;
break;
}
}
const options: CLIOptions = {
runtime: args.includes('--runtime') || args.includes('-r'),
static: args.includes('--static') || args.includes('-s'),
tools: args.includes('--tools') || args.includes('-t'),
resources: args.includes('--resources'),
workflows: args.includes('--workflows') || args.includes('-w'),
verbose: args.includes('--verbose') || args.includes('-v'),
json: args.includes('--json'),
help: args.includes('--help') || args.includes('-h'),
};
// Set sensible defaults for each command
if (!options.runtime && !options.static) {
if (command === 'static' || command === 's') {
options.static = true;
} else {
// Default to static analysis for development-friendly usage
options.static = true;
}
}
// Set sensible content defaults
if (command === 'list' || command === 'l') {
if (!options.tools && !options.resources && !options.workflows) {
options.tools = true; // Default to showing tools for list command
}
} else if (!command || command === 'count' || command === 'c') {
// For no command or count, show comprehensive summary
if (!options.tools && !options.resources && !options.workflows) {
options.workflows = true; // Show workflows by default for summary
}
}
// Help text
const helpText = {
main: `
${colors.bright}${colors.blue}XcodeBuildMCP Tools CLI${colors.reset}
A unified command-line tool for XcodeBuildMCP tool and resource information.
${colors.bright}COMMANDS:${colors.reset}
count, c Show tool and workflow counts
list, l List all tools and resources
static, s Show static source file analysis
help, h Show this help message
${colors.bright}OPTIONS:${colors.reset}
--runtime, -r Use runtime inspection (respects env config)
--static, -s Use static file analysis (default, development mode)
--tools, -t Include tools in output
--resources Include resources in output
--workflows, -w Include workflow information
--verbose, -v Show detailed information
--json Output JSON format
${colors.bright}EXAMPLES:${colors.reset}
${colors.cyan}npm run tools${colors.reset} # Static summary with workflows (default)
${colors.cyan}npm run tools list${colors.reset} # List tools
${colors.cyan}npm run tools --runtime${colors.reset} # Runtime analysis (requires build)
${colors.cyan}npm run tools static${colors.reset} # Static analysis summary
${colors.cyan}npm run tools count --json${colors.reset} # JSON output
${colors.bright}ANALYSIS MODES:${colors.reset}
${colors.green}Runtime${colors.reset} Uses actual server inspection via Reloaderoo
- Respects XCODEBUILDMCP_ENABLED_WORKFLOWS environment variable
- Shows tools actually enabled at runtime
- Requires built server (npm run build)
${colors.yellow}Static${colors.reset} Scans source files directly using AST parsing
- Shows all tools in codebase regardless of config
- Development-time analysis with reliable description extraction
- No server build required
`,
count: `
${colors.bright}COUNT COMMAND${colors.reset}
Shows tool and workflow counts using runtime or static analysis.
${colors.bright}Usage:${colors.reset} npx tsx scripts/tools-cli.ts count [options]
${colors.bright}Options:${colors.reset}
--runtime, -r Count tools from running server
--static, -s Count tools from source files
--workflows, -w Include workflow directory counts
--json Output JSON format
${colors.bright}Examples:${colors.reset}
${colors.cyan}npx tsx scripts/tools-cli.ts count${colors.reset} # Runtime count
${colors.cyan}npx tsx scripts/tools-cli.ts count --static${colors.reset} # Static count
${colors.cyan}npx tsx scripts/tools-cli.ts count --workflows${colors.reset} # Include workflows
`,
list: `
${colors.bright}LIST COMMAND${colors.reset}
Lists tools and resources with optional details.
${colors.bright}Usage:${colors.reset} npx tsx scripts/tools-cli.ts list [options]
${colors.bright}Options:${colors.reset}
--runtime, -r List from running server
--static, -s List from source files
--tools, -t Show tool names
--resources Show resource URIs
--verbose, -v Show detailed information
--json Output JSON format
${colors.bright}Examples:${colors.reset}
${colors.cyan}npx tsx scripts/tools-cli.ts list --tools${colors.reset} # Runtime tool list
${colors.cyan}npx tsx scripts/tools-cli.ts list --resources${colors.reset} # Runtime resource list
${colors.cyan}npx tsx scripts/tools-cli.ts list --static --verbose${colors.reset} # Static detailed list
`,
static: `
${colors.bright}STATIC COMMAND${colors.reset}
Performs detailed static analysis of source files using AST parsing.
${colors.bright}Usage:${colors.reset} npx tsx scripts/tools-cli.ts static [options]
${colors.bright}Options:${colors.reset}
--tools, -t Show canonical tool details
--workflows, -w Show workflow directory analysis
--verbose, -v Show detailed file information
--json Output JSON format
${colors.bright}Examples:${colors.reset}
${colors.cyan}npx tsx scripts/tools-cli.ts static${colors.reset} # Basic static analysis
${colors.cyan}npx tsx scripts/tools-cli.ts static --verbose${colors.reset} # Detailed analysis
${colors.cyan}npx tsx scripts/tools-cli.ts static --workflows${colors.reset} # Include workflow info
`,
};
if (options.help) {
console.log(helpText[command as keyof typeof helpText] || helpText.main);
process.exit(0);
}
if (command === 'help' || command === 'h') {
const helpCommand = args[1];
console.log(helpText[helpCommand as keyof typeof helpText] || helpText.main);
process.exit(0);
}
/**
* Execute reloaderoo command and parse JSON response
*/
async function executeReloaderoo(reloaderooArgs: string[]): Promise<unknown> {
const buildPath = path.resolve(__dirname, '..', 'build', 'index.js');
if (!fs.existsSync(buildPath)) {
throw new Error('Build not found. Please run "npm run build" first.');
}
const tempFile = `/tmp/reloaderoo-output-${Date.now()}.json`;
const command = `npx -y reloaderoo@latest inspect ${reloaderooArgs.join(' ')} -- node "${buildPath}"`;
return new Promise((resolve, reject) => {
const child = spawn('bash', ['-c', `${command} > "${tempFile}"`], {
stdio: 'inherit',
});
child.on('close', (code) => {
try {
if (code !== 0) {
reject(new Error(`Command failed with code ${code}`));
return;
}
const content = fs.readFileSync(tempFile, 'utf8');
// Remove stderr log lines and find JSON
const lines = content.split('\n');
const cleanLines: string[] = [];
for (const line of lines) {
if (
line.match(/^\[\d{4}-\d{2}-\d{2}T/) ||
line.includes('[INFO]') ||
line.includes('[DEBUG]') ||
line.includes('[ERROR]')
) {
continue;
}
const trimmed = line.trim();
if (trimmed) {
cleanLines.push(line);
}
}
// Find JSON start
let jsonStartIndex = -1;
for (let i = 0; i < cleanLines.length; i++) {
if (cleanLines[i].trim().startsWith('{')) {
jsonStartIndex = i;
break;
}
}
if (jsonStartIndex === -1) {
reject(
new Error(`No JSON response found in output.\nOutput: ${content.substring(0, 500)}...`),
);
return;
}
const jsonText = cleanLines.slice(jsonStartIndex).join('\n');
const response = JSON.parse(jsonText);
resolve(response);
} catch (error) {
reject(new Error(`Failed to parse JSON response: ${(error as Error).message}`));
} finally {
try {
fs.unlinkSync(tempFile);
} catch {
// Ignore cleanup errors
}
}
});
child.on('error', (error) => {
reject(new Error(`Failed to spawn process: ${error.message}`));
});
});
}
/**
* Get runtime server information
*/
async function getRuntimeInfo(): Promise<RuntimeData> {
try {
const toolsResponse = (await executeReloaderoo(['list-tools'])) as {
tools?: { name: string; description: string }[];
};
const resourcesResponse = (await executeReloaderoo(['list-resources'])) as {
resources?: { uri: string; name: string; description?: string; title?: string }[];
};
let tools: RuntimeTool[] = [];
let toolCount = 0;
if (toolsResponse.tools && Array.isArray(toolsResponse.tools)) {
toolCount = toolsResponse.tools.length;
tools = toolsResponse.tools.map((tool) => ({
name: tool.name,
description: tool.description,
}));
}
let resources: RuntimeResource[] = [];
let resourceCount = 0;
if (resourcesResponse.resources && Array.isArray(resourcesResponse.resources)) {
resourceCount = resourcesResponse.resources.length;
resources = resourcesResponse.resources.map((resource) => ({
uri: resource.uri,
name: resource.name,
description: resource.title ?? resource.description ?? 'No description available',
}));
}
return {
tools,
resources,
toolCount,
resourceCount,
mode: 'runtime',
};
} catch (error) {
throw new Error(`Runtime analysis failed: ${(error as Error).message}`);
}
}
/**
* Display summary information
*/
function displaySummary(
runtimeData: RuntimeData | null,
staticData: StaticAnalysisResult | null,
): void {
if (options.json) {
return; // JSON output handled separately
}
console.log(`${colors.bright}${colors.blue}📊 XcodeBuildMCP Tools Summary${colors.reset}`);
console.log('═'.repeat(60));
if (runtimeData) {
console.log(`${colors.green}🚀 Runtime Analysis:${colors.reset}`);
console.log(` Tools: ${runtimeData.toolCount}`);
console.log(` Resources: ${runtimeData.resourceCount}`);
console.log(` Total: ${runtimeData.toolCount + runtimeData.resourceCount}`);
console.log();
}
if (staticData) {
console.log(`${colors.cyan}📁 Static Analysis:${colors.reset}`);
console.log(` Workflow directories: ${staticData.stats.workflowCount}`);
console.log(` Canonical tools: ${staticData.stats.canonicalTools}`);
console.log(` Re-export files: ${staticData.stats.reExportTools}`);
console.log(` Total tool files: ${staticData.stats.totalTools}`);
console.log();
}
}
/**
* Display workflow information
*/
function displayWorkflows(staticData: StaticAnalysisResult | null): void {
if (!options.workflows || !staticData || options.json) return;
console.log(`${colors.bright}📂 Workflow Directories:${colors.reset}`);
console.log('─'.repeat(40));
for (const workflow of staticData.workflows) {
const totalTools = workflow.toolCount;
console.log(`${colors.green}• ${workflow.displayName}${colors.reset} (${totalTools} tools)`);
if (options.verbose) {
const canonicalTools = workflow.tools.filter((t) => t.isCanonical).map((t) => t.name);
const reExportTools = workflow.tools.filter((t) => !t.isCanonical).map((t) => t.name);
if (canonicalTools.length > 0) {
console.log(` ${colors.cyan}Canonical:${colors.reset} ${canonicalTools.join(', ')}`);
}
if (reExportTools.length > 0) {
console.log(` ${colors.yellow}Re-exports:${colors.reset} ${reExportTools.join(', ')}`);
}
}
}
console.log();
}
/**
* Display tool lists
*/
function displayTools(
runtimeData: RuntimeData | null,
staticData: StaticAnalysisResult | null,
): void {
if (!options.tools || options.json) return;
if (runtimeData) {
console.log(`${colors.bright}🛠️ Runtime Tools (${runtimeData.toolCount}):${colors.reset}`);
console.log('─'.repeat(40));
if (runtimeData.tools.length === 0) {
console.log(' No tools available');
} else {
runtimeData.tools.forEach((tool) => {
if (options.verbose && tool.description) {
console.log(
` ${colors.green}•${colors.reset} ${colors.bright}${tool.name}${colors.reset}`,
);
console.log(` ${tool.description}`);
} else {
console.log(` ${colors.green}•${colors.reset} ${tool.name}`);
}
});
}
console.log();
}
if (staticData && options.static) {
const canonicalTools = staticData.tools.filter((tool) => tool.isCanonical);
console.log(`${colors.bright}📁 Static Tools (${canonicalTools.length}):${colors.reset}`);
console.log('─'.repeat(40));
if (canonicalTools.length === 0) {
console.log(' No tools found');
} else {
canonicalTools
.sort((a, b) => a.name.localeCompare(b.name))
.forEach((tool) => {
if (options.verbose) {
console.log(
` ${colors.green}•${colors.reset} ${colors.bright}${tool.name}${colors.reset} (${tool.workflow})`,
);
console.log(` ${tool.description}`);
console.log(` ${colors.cyan}${tool.relativePath}${colors.reset}`);
} else {
console.log(` ${colors.green}•${colors.reset} ${tool.name}`);
}
});
}
console.log();
}
}
/**
* Display resource lists
*/
function displayResources(runtimeData: RuntimeData | null): void {
if (!options.resources || !runtimeData || options.json) return;
console.log(`${colors.bright}📚 Resources (${runtimeData.resourceCount}):${colors.reset}`);
console.log('─'.repeat(40));
if (runtimeData.resources.length === 0) {
console.log(' No resources available');
} else {
runtimeData.resources.forEach((resource) => {
if (options.verbose) {
console.log(
` ${colors.magenta}•${colors.reset} ${colors.bright}${resource.uri}${colors.reset}`,
);
console.log(` ${resource.description}`);
} else {
console.log(` ${colors.magenta}•${colors.reset} ${resource.uri}`);
}
});
}
console.log();
}
/**
* Output JSON format - matches the structure of human-readable output
*/
function outputJSON(
runtimeData: RuntimeData | null,
staticData: StaticAnalysisResult | null,
): void {
const output: Record<string, unknown> = {};
// Add summary stats (equivalent to the summary table)
if (runtimeData) {
output.runtime = {
toolCount: runtimeData.toolCount,
resourceCount: runtimeData.resourceCount,
totalCount: runtimeData.toolCount + runtimeData.resourceCount,
};
}
if (staticData) {
output.static = {
workflowCount: staticData.stats.workflowCount,
canonicalTools: staticData.stats.canonicalTools,
reExportTools: staticData.stats.reExportTools,
totalTools: staticData.stats.totalTools,
};
}
// Add detailed data only if requested
if (options.workflows && staticData) {
output.workflows = staticData.workflows.map((w) => ({
name: w.displayName,
toolCount: w.toolCount,
canonicalCount: w.canonicalCount,
reExportCount: w.reExportCount,
}));
}
if (options.tools) {
if (runtimeData) {
output.runtimeTools = runtimeData.tools.map((t) => t.name);
}
if (staticData) {
output.staticTools = staticData.tools
.filter((t) => t.isCanonical)
.map((t) => t.name)
.sort();
}
}
if (options.resources && runtimeData) {
output.resources = runtimeData.resources.map((r) => r.uri);
}
console.log(JSON.stringify(output, null, 2));
}
/**
* Main execution function
*/
async function main(): Promise<void> {
try {
let runtimeData: RuntimeData | null = null;
let staticData: StaticAnalysisResult | null = null;
// Gather data based on options
if (options.runtime) {
if (!options.json) {
console.log(`${colors.cyan}🔍 Gathering runtime information...${colors.reset}`);
}
runtimeData = await getRuntimeInfo();
}
if (options.static) {
if (!options.json) {
console.log(`${colors.cyan}📁 Performing static analysis...${colors.reset}`);
}
staticData = await getStaticToolAnalysis();
}
// For default command or workflows option, always gather static data for workflow info
if (options.workflows && !staticData) {
if (!options.json) {
console.log(`${colors.cyan}📁 Gathering workflow information...${colors.reset}`);
}
staticData = await getStaticToolAnalysis();
}
if (!options.json) {
console.log(); // Blank line after gathering
}
// Handle JSON output
if (options.json) {
outputJSON(runtimeData, staticData);
return;
}
// Display based on command
switch (command) {
case 'count':
case 'c':
displaySummary(runtimeData, staticData);
displayWorkflows(staticData);
break;
case 'list':
case 'l':
displaySummary(runtimeData, staticData);
displayTools(runtimeData, staticData);
displayResources(runtimeData);
break;
case 'static':
case 's':
if (!staticData) {
console.log(`${colors.cyan}📁 Performing static analysis...${colors.reset}\n`);
staticData = await getStaticToolAnalysis();
}
displaySummary(null, staticData);
displayWorkflows(staticData);
if (options.verbose) {
displayTools(null, staticData);
const reExportTools = staticData.tools.filter((t) => !t.isCanonical);
console.log(
`${colors.bright}🔄 Re-export Files (${reExportTools.length}):${colors.reset}`,
);
console.log('─'.repeat(40));
reExportTools.forEach((file) => {
console.log(` ${colors.yellow}•${colors.reset} ${file.name} (${file.workflow})`);
console.log(` ${file.relativePath}`);
});
}
break;
default:
// Default case (no command) - show runtime summary with workflows
displaySummary(runtimeData, staticData);
displayWorkflows(staticData);
break;
}
if (!options.json) {
console.log(`${colors.green}✅ Analysis complete!${colors.reset}`);
}
} catch (error) {
if (options.json) {
console.error(
JSON.stringify(
{
success: false,
error: (error as Error).message,
timestamp: new Date().toISOString(),
},
null,
2,
),
);
} else {
console.error(`${colors.red}❌ Error: ${(error as Error).message}${colors.reset}`);
}
process.exit(1);
}
}
// Run the CLI
main();