listTargets top-level property

FigGenerator listTargets
final

Implementation

final FigGenerator listTargets = FigGenerator(
  script: [
    'bash',
    '-c',
    "make -qp | awk -F':' '/^[a-zA-Z0-9][^\$#\\/\\t=]*:([^=]|\$)/ {split(\$1,A,/ /);for(i in A)print A[i]}' | sort -u; echo '---SEPARATOR---'; cat Makefile makefile 2>/dev/null"
  ],
  postProcess: (String out, [List<String>? tokens]) {
    final parts = out.split('---SEPARATOR---');
    final targetsOut = parts[0];
    final makefileContent = parts.length > 1 ? parts[1] : '';

    final targetSuggestions = <String, FigSuggestion>{};

    for (final target in targetsOut.split('\n')) {
      final t = target.trim();
      if (t.isEmpty || t == 'Makefile') continue;
      targetSuggestions[t] = FigSuggestion(
        name: t,
        description: 'Make target',
        icon: '🎯',
        priority: 80,
      );
    }

    final regex = RegExp(
      r'((?:^#.*\n)*)(?:^\.[A-Z_]+:.*\n)*(^\S*?):.*?(?:\s#+[ \t]*(.+))?$',
      multiLine: true,
    );

    final specialTargets = {
      '.PHONY',
      '.SUFFIXES',
      '.DEFAULT',
      '.PRECIOUS',
      '.INTERMEDIATE',
      '.SECONDARY',
      '.SECONDEXPANSION',
      '.DELETE_ON_ERROR',
      '.IGNORE',
      '.LOW_RESOLUTION_TIME',
      '.SILENT',
      '.EXPORT_ALL_VARIABLES',
      '.NOTPARALLEL',
      '.ONESHELL',
      '.POSIX'
    };

    for (final match in regex.allMatches(makefileContent)) {
      final leadingComment = match.group(1);
      final target = match.group(2);
      final inlineComment = match.group(3);

      if (target == null) continue;
      if (specialTargets.contains(target)) continue;
      if (target.contains(r'$(')) continue;

      final name = target.trim();

      String description = 'Make target';
      if (inlineComment != null && inlineComment.trim().isNotEmpty) {
        description = inlineComment.trim();
      } else if (leadingComment != null && leadingComment.trim().isNotEmpty) {
        description = leadingComment
            .replaceAll(RegExp(r'^#+\s*', multiLine: true), '')
            .trim();
      }

      targetSuggestions[name] = FigSuggestion(
        name: name,
        description: description,
        icon: '🎯',
        priority: 80,
      );
    }

    return targetSuggestions.values.toList();
  },
);