resolveSource function

ResolvedSource resolveSource({
  1. required String prefixPath,
  2. String? explicitFilePath,
  3. required FLILogger logger,
})

Resolves which config source to use, following documented precedence:

  1. --file <path> (explicit; hard error if missing).
  2. <prefix>/flutter_launcher_icons_flavors.yaml.
  3. Any <prefix>/flutter_launcher_icons-*.yaml (legacy multi-flavor).
  4. <prefix>/flutter_launcher_icons.yaml (single-config).
  5. <prefix>/pubspec.yaml with a flutter_launcher_icons: / flutter_icons: block.

Throws NoConfigFoundException if none of the above exist.

Implementation

ResolvedSource resolveSource({
  required String prefixPath,
  String? explicitFilePath,
  required FLILogger logger,
}) {
  // (1) Explicit --file wins. No fallback.
  if (explicitFilePath != null) {
    final resolved = _resolveExplicitPath(prefixPath, explicitFilePath);
    final file = File(resolved);
    if (!file.existsSync()) {
      throw NoConfigFoundException(
        'Config file not found: $resolved (passed via --file).',
      );
    }
    final isMulti = _sniffMultiFlavor(file);
    return ResolvedSource(
      kind: ConfigSourceKind.explicitFile,
      path: resolved,
      isMultiFlavor: isMulti,
    );
  }

  // (2) Consolidated multi-flavor file.
  final consolidatedPath = path.join(
    prefixPath,
    constants.consolidatedFlavorsFileName,
  );
  if (File(consolidatedPath).existsSync()) {
    final ignoredLegacy = findLegacyFlavorPaths(prefixPath);
    if (ignoredLegacy.isNotEmpty) {
      logger.warn(
        'Both $consolidatedPath and legacy '
        'flutter_launcher_icons-<flavor>.yaml file(s) were found:\n  '
        '${ignoredLegacy.join('\n  ')}\n'
        'The consolidated file wins; legacy files are ignored. '
        'Run `dart run flutter_launcher_icons_flavors migrate` to '
        'consolidate them (coming in 0.15.x), or delete the legacy '
        'files to silence this warning.',
      );
    }
    return ResolvedSource(
      kind: ConfigSourceKind.consolidatedFlavors,
      path: consolidatedPath,
      ignoredLegacy: ignoredLegacy,
      isMultiFlavor: true,
    );
  }

  // (3) Legacy multi-flavor.
  final legacyFiles = findLegacyFlavorPaths(prefixPath);
  if (legacyFiles.isNotEmpty) {
    logger.warn(
      'Found legacy flutter_launcher_icons-<flavor>.yaml file(s). '
      'Consider migrating to a single ${constants.consolidatedFlavorsFileName} '
      'file. (Migration tooling arrives in a later 0.15.x release.)',
    );
    return ResolvedSource(
      kind: ConfigSourceKind.legacyFlavors,
      path: prefixPath,
    );
  }

  // (4) Single-config file.
  final singlePath = path.join(prefixPath, constants.singleConfigFileName);
  if (File(singlePath).existsSync()) {
    return ResolvedSource(kind: ConfigSourceKind.singleFile, path: singlePath);
  }

  // (5) pubspec inline.
  final pubspecPath = path.join(prefixPath, constants.pubspecFilePath);
  if (File(pubspecPath).existsSync() && _pubspecHasInlineConfig(pubspecPath)) {
    return ResolvedSource(
      kind: ConfigSourceKind.pubspecInline,
      path: pubspecPath,
    );
  }

  // Last-ditch friendly message: if there are no configs at the prefix but
  // there are legacy-style files anywhere under it (e.g. in `config/`),
  // list them so the user knows what to point `--file` at (upstream #279).
  final discovered = findLegacyFlavorFiles(prefixPath);
  if (discovered.isNotEmpty) {
    final lines = discovered
        .map((f) => '  - ${f.path} (flavor: ${f.flavor})')
        .join('\n');
    throw NoConfigFoundException(
      'No base flutter_launcher_icons config found in $prefixPath, but '
      'discovered ${discovered.length} flavor config(s):\n$lines\n'
      'Re-run with `--file <path>` or move one of these to the project '
      'root, or pass `--flavor <name>` / `--all-flavors` if you have a '
      'consolidated config elsewhere.',
    );
  }

  throw const NoConfigFoundException(
    'No flutter_launcher_icons config found '
    '(checked flutter_launcher_icons_flavors.yaml, '
    'flutter_launcher_icons-<flavor>.yaml, flutter_launcher_icons.yaml, '
    'and pubspec.yaml). Use --file to point at a custom config.',
  );
}