gitGenerateSpec function

Future<FigSpec> gitGenerateSpec(
  1. List<String> tokens,
  2. ExecuteCommandFunction executeShellCommand
)

Generates git spec with subcommands from git help -a (external commands), merged with optionalCommands. Uses adapter-provided executeShellCommand; the runtime passes it when invoking generateSpec.

Implementation

Future<FigSpec> gitGenerateSpec(
    List<String> tokens, ExecuteCommandFunction executeShellCommand) async {
  final result = await executeShellCommand(const ExecuteCommandInput(
    command: 'git',
    args: ['help', '-a'],
  ));
  final stdout = result.stdout;
  final lines = stdout.trim().split('\n');
  final start = lines.indexWhere((val) =>
      RegExp(r'external commands', caseSensitive: false).hasMatch(val));
  final commands = <String>[];
  if (start >= 0) {
    for (var i = start + 1; i < lines.length; i++) {
      final line = lines[i].trim();
      if (line.isEmpty) break;
      final command = line.split(RegExp(r'\s+'))[0];
      commands.add(command);
    }
  }
  return FigSpec(
    name: 'git',
    description: 'Distributed version control system',
    subcommands: commands.map((name) {
      final opt = optionalCommands[name];
      if (opt != null) {
        return Subcommand(
          name: name,
          description: opt['description'] as String? ?? 'Run git-$name',
          options: (opt['options'] as List<FigOption>?) ?? [],
          loadSpec: opt['loadSpec'] as String?,
        );
      }
      return Subcommand(name: name, description: 'Run git-$name');
    }).toList(),
  );
}