Skip to content

- Add flavor path configuration #648

@shahrayar-wellx

Description

@shahrayar-wellx

💬 Description

- Problem
The getFlavors() function only searched for flavor configuration files in the current working directory, making it impossible to use flavor files stored in subdirectories like /appicons/flutter_launcher_icons-development.yaml.

Solution
Added --flavor-path option to specify custom search directories for flavor configuration files
Made search recursive by default to discover flavor files in subdirectories
Maintained backward compatibility - existing usage without the new flag continues to work as before

Changes
Enhanced getFlavors() function:
Added optional searchPath parameter (defaults to '.')
Changed from non-recursive to recursive directory search
Now finds flavor files in subdirectories

Added command-line option:
New --flavor-path flag to specify custom search directory
Defaults to current directory (.) for backward compatibility

Old behavior (still works)

flutter pub run flutter_launcher_icons

New behavior - search in custom directory

flutter pub run flutter_launcher_icons --flavor-path=appicons/brand_1

Code change (main.dart):

Future<List<String>> getFlavors({String searchPath = '.'}) async {
  final List<String> flavors = [];
  
  // Recursively search through directories
  await for (var item in Directory(searchPath).list(recursive: true)) {
    if (item is File) {
      final name = path.basename(item.path);
      final match = RegExp(flavorConfigFilePattern).firstMatch(name);
      if (match != null) {
        flavors.add(match.group(1)!);
      }
    }
  }
  return flavors;
}


Future<void> createIconsFromArguments(List<String> arguments) async {
  final ArgParser parser = ArgParser(allowTrailingOptions: true);
  parser
    ..addFlag(helpFlag, abbr: 'h', help: 'Usage help', negatable: false)
    // Make default null to differentiate when it is explicitly set
    ..addOption(
      fileOption,
      abbr: 'f',
      help: 'Path to config file',
      defaultsTo: defaultConfigFile,
    )
    ..addFlag(verboseFlag, abbr: 'v', help: 'Verbose output', defaultsTo: false)
    ..addOption(
      prefixOption,
      abbr: 'p',
      help: 'Generates config in the given path. Only Supports web platform',
      defaultsTo: '.',
    )
    ..addOption(
      'flavor-path',
      help: 'Path to search for flavor configuration files',
      defaultsTo: '.',
    );

  final ArgResults argResults = parser.parse(arguments);
  // creating logger based on -v flag
  final logger = FLILogger(argResults[verboseFlag]);

  logger.verbose('Received args ${argResults.arguments}');

  if (argResults[helpFlag]) {
    stdout.writeln('Generates icons for iOS and Android');
    stdout.writeln(parser.usage);
    exit(0);
  }

  // Flavors management
  final flavors = await getFlavors(searchPath: argResults['flavor-path']);
  final hasFlavors = flavors.isNotEmpty;

  final String prefixPath = argResults[prefixOption];

  // Create icons
  if (!hasFlavors) {
    // Load configs from given file(defaults to ./flutter_launcher_icons.yaml) or from ./pubspec.yaml

    final flutterLauncherIconsConfigs =
        loadConfigFileFromArgResults(argResults);
    if (flutterLauncherIconsConfigs == null) {
      throw NoConfigFoundException(
        'No configuration found in $defaultConfigFile or in ${constants.pubspecFilePath}. '
        'In case file exists in different directory use --file option',
      );
    }
    try {
      await createIconsFromConfig(
        flutterLauncherIconsConfigs,
        logger,
        prefixPath,
      );
      print('\n✓ Successfully generated launcher icons');
    } catch (e) {
      stderr.writeln('\n✕ Could not generate launcher icons');
      stderr.writeln(e);
      exit(2);
    }
  } else {
    try {
      for (String flavor in flavors) {
        print('\nFlavor: $flavor');
        final flutterLauncherIconsConfigs =
            Config.loadConfigFromFlavor(flavor, prefixPath);
        if (flutterLauncherIconsConfigs == null) {
          throw NoConfigFoundException(
            'No configuration found for $flavor flavor.',
          );
        }
        await createIconsFromConfig(
          flutterLauncherIconsConfigs,
          logger,
          prefixPath,
          flavor,
        );
      }
      print('\n✓ Successfully generated launcher icons for flavors');
    } catch (e) {
      stderr.writeln('\n✕ Could not generate launcher icons for flavors');
      stderr.writeln(e);
      exit(2);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions