executeImpl method

  1. @override
Future executeImpl(
  1. Map<String, dynamic> params
)
override

Implement the actual tool logic

params are the validated parameters (never null here) Returns the result data to send in the response

Implementation

@override
Future<dynamic> executeImpl(Map<String, dynamic> params) async {
  final category = params['category'] as String?;
  final source = params['source'] as String? ?? 'all';

  final project = ServerPodLocator.getProject();

  if (project == null || !project.isValid) {
    return {
      'error': 'Not a valid ServerPod project',
      'hint': 'Run this command from a ServerPod project directory',
    };
  }

  try {
    // Start with built-in commands
    var commands = <Map<String, dynamic>>[
      ..._serverpodBuiltInCommands,
    ];

    // Add custom commands from bin/ if exists
    final customCommands = await _loadCustomCommands(project);
    commands.addAll(customCommands);

    // Apply category filter
    if (category != null) {
      commands = commands
          .where((cmd) => cmd['category'] == category)
          .toList();
    }

    // Apply source filter
    if (source == 'built-in') {
      commands = commands.where((cmd) => cmd['isBuiltIn'] == true).toList();
    } else if (source == 'custom') {
      commands = commands.where((cmd) => cmd['isBuiltIn'] == false).toList();
    }

    // Group by category
    final categories = <String, int>{};
    for (final cmd in commands) {
      final cat = cmd['category'] as String;
      categories[cat] = (categories[cat] ?? 0) + 1;
    }

    final builtInCount = commands.where((cmd) => cmd['isBuiltIn'] == true).length;
    final customCount = commands.where((cmd) => cmd['isBuiltIn'] == false).length;

    return {
      'commands': commands,
      'categories': categories,
      'count': commands.length,
      'builtInCount': builtInCount,
      'customCount': customCount,
      'serverpodVersion': await _getServerpodVersion(project),
    };
  } catch (e) {
    return {
      'error': 'Failed to list CLI commands: ${e.toString()}',
    };
  }
}