From b2b89823e91cea5c014dda30829d88c7612a191b Mon Sep 17 00:00:00 2001 From: Ratakondala Arun Date: Sun, 10 Jul 2022 10:43:42 +0530 Subject: [PATCH] feat(cli-option): added --prefix option --prefix Generates icons in the given path. Used for testing in virtual directory. Only supports web platform for now --- lib/abs/icon_generator.dart | 16 +++++++++++-- lib/flutter_launcher_icons_config.dart | 13 ++++++----- lib/main.dart | 31 ++++++++++++++++++-------- lib/web/web_icon_generator.dart | 18 +++++++-------- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/lib/abs/icon_generator.dart b/lib/abs/icon_generator.dart index dcefb01e5c..159d0528ed 100644 --- a/lib/abs/icon_generator.dart +++ b/lib/abs/icon_generator.dart @@ -25,8 +25,14 @@ class IconGeneratorContext { final FlutterLauncherIconsConfig config; final FLILogger logger; final String? flavor; + final String prefixPath; - IconGeneratorContext({required this.config, this.flavor, required this.logger}); + IconGeneratorContext({ + required this.config, + this.flavor, + required this.prefixPath, + required this.logger, + }); /// Shortcut for `config.webConfig` WebConfig? get webConfig => config.webConfig; @@ -36,11 +42,17 @@ class IconGeneratorContext { void generateIconsFor({ required FlutterLauncherIconsConfig config, required String? flavor, + required String prefixPath, required FLILogger logger, required List Function(IconGeneratorContext context) platforms, }) { try { - final platformList = platforms(IconGeneratorContext(config: config, logger: logger, flavor: flavor)); + final platformList = platforms(IconGeneratorContext( + config: config, + logger: logger, + prefixPath: prefixPath, + flavor: flavor, + )); if (platformList.isEmpty) { // ? maybe we can print help logger.info('No platform provided'); diff --git a/lib/flutter_launcher_icons_config.dart b/lib/flutter_launcher_icons_config.dart index 75e6417e50..b758795de9 100644 --- a/lib/flutter_launcher_icons_config.dart +++ b/lib/flutter_launcher_icons_config.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:checked_yaml/checked_yaml.dart' as yaml; import 'package:json_annotation/json_annotation.dart'; +import 'package:path/path.dart' as path; import 'constants.dart' as constants; import 'custom_exceptions.dart'; @@ -58,8 +59,8 @@ class FlutterLauncherIconsConfig { factory FlutterLauncherIconsConfig.fromJson(Map json) => _$FlutterLauncherIconsConfigFromJson(json); /// Loads flutter launcher icons configs from given [filePath] - static FlutterLauncherIconsConfig? loadConfigFromPath(String filePath) { - final configFile = File(filePath); + static FlutterLauncherIconsConfig? loadConfigFromPath(String filePath, String prefixPath) { + final configFile = File(path.join(prefixPath, filePath)); if (!configFile.existsSync()) { return null; } @@ -77,9 +78,9 @@ class FlutterLauncherIconsConfig { } /// Loads flutter launcher icons config from `pubspec.yaml` file - static FlutterLauncherIconsConfig? loadConfigFromPubSpec() { + static FlutterLauncherIconsConfig? loadConfigFromPubSpec(String prefix) { try { - final pubspecFile = File(constants.pubspecFilePath); + final pubspecFile = File(path.join(prefix, constants.pubspecFilePath)); if (!pubspecFile.existsSync()) { return null; } @@ -100,8 +101,8 @@ class FlutterLauncherIconsConfig { } } - static FlutterLauncherIconsConfig? loadConfigFromFlavor(String flavor) { - return FlutterLauncherIconsConfig.loadConfigFromPath(utils.flavorConfigFile(flavor)); + static FlutterLauncherIconsConfig? loadConfigFromFlavor(String flavor, String prefixPath) { + return FlutterLauncherIconsConfig.loadConfigFromPath(utils.flavorConfigFile(flavor), prefixPath); } Map toJson() => _$FlutterLauncherIconsConfigToJson(this); diff --git a/lib/main.dart b/lib/main.dart index 785bdb0abe..01f82fbc72 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -16,6 +16,7 @@ import 'package:flutter_launcher_icons/custom_exceptions.dart'; const String fileOption = 'file'; const String helpFlag = 'help'; const String verboseFlag = 'verbose'; +const String prefixOption = 'prefix'; const String defaultConfigFile = 'flutter_launcher_icons.yaml'; const String flavorConfigFilePattern = r'^flutter_launcher_icons-(.*).yaml$'; @@ -38,10 +39,18 @@ List getFlavors() { Future createIconsFromArguments(List 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 - parser.addOption(fileOption, abbr: 'f', help: 'Path to config file', defaultsTo: defaultConfigFile); - parser.addFlag(verboseFlag, abbr: 'v', help: 'Verbose output', defaultsTo: false); + 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: '.', + ); + final ArgResults argResults = parser.parse(arguments); // creating logger based on -v flag final logger = FLILogger(argResults[verboseFlag]); @@ -60,11 +69,13 @@ Future createIconsFromArguments(List arguments) async { // Load the config file final Map? yamlConfig = loadConfigFileFromArgResults(argResults, verbose: true); + final String prefixPath = argResults[prefixOption]; // Load configs from given file(defaults to ./flutter_launcher_icons.yaml) or from ./pubspec.yaml - final flutterLauncherIconsConfigs = FlutterLauncherIconsConfig.loadConfigFromPath(argResults[fileOption]) ?? - FlutterLauncherIconsConfig.loadConfigFromPubSpec(); + final flutterLauncherIconsConfigs = + FlutterLauncherIconsConfig.loadConfigFromPath(argResults[fileOption], prefixPath) ?? + FlutterLauncherIconsConfig.loadConfigFromPubSpec(prefixPath); if (yamlConfig == null || flutterLauncherIconsConfigs == null) { throw NoConfigFoundException( 'No configuration found in $defaultConfigFile or in ${constants.pubspecFilePath}. ' @@ -75,7 +86,7 @@ Future createIconsFromArguments(List arguments) async { // Create icons if (!hasFlavors) { try { - await createIconsFromConfig(yamlConfig, flutterLauncherIconsConfigs, logger); + await createIconsFromConfig(yamlConfig, flutterLauncherIconsConfigs, logger, prefixPath); print('\n✓ Successfully generated launcher icons'); } catch (e) { stderr.writeln('\n✕ Could not generate launcher icons'); @@ -87,7 +98,7 @@ Future createIconsFromArguments(List arguments) async { for (String flavor in flavors) { print('\nFlavor: $flavor'); final Map yamlConfig = loadConfigFile(flavorConfigFile(flavor), flavorConfigFile(flavor)); - await createIconsFromConfig(yamlConfig, flutterLauncherIconsConfigs, logger, flavor); + await createIconsFromConfig(yamlConfig, flutterLauncherIconsConfigs, logger, prefixPath, flavor); } print('\n✓ Successfully generated launcher icons for flavors'); } catch (e) { @@ -101,7 +112,8 @@ Future createIconsFromArguments(List arguments) async { Future createIconsFromConfig( Map config, FlutterLauncherIconsConfig flutterConfigs, - FLILogger logger, [ + FLILogger logger, + String prefixPath, [ String? flavor, ]) async { if (!isImagePathInConfig(config)) { @@ -135,6 +147,7 @@ Future createIconsFromConfig( generateIconsFor( config: flutterConfigs, logger: logger, + prefixPath: prefixPath, flavor: flavor, platforms: (context) => [ WebIconGenerator(context), diff --git a/lib/web/web_icon_generator.dart b/lib/web/web_icon_generator.dart index 6346c418e9..0324d151e3 100644 --- a/lib/web/web_icon_generator.dart +++ b/lib/web/web_icon_generator.dart @@ -43,7 +43,7 @@ class WebIconGenerator extends IconGenerator { @override void createIcons() { - final imgFilePath = context.webConfig!.imagePath ?? context.config.imagePath!; + final imgFilePath = path.join(context.prefixPath, context.webConfig!.imagePath ?? context.config.imagePath!); context.logger.verbose('Decoding and loading image file at $imgFilePath...'); final imgFile = utils.decodeImageFile(imgFilePath); @@ -61,7 +61,7 @@ class WebIconGenerator extends IconGenerator { _generateIcons(imgFile); // update manifest.json in web/mainfest.json - context.logger.verbose('Updating ${constants.webManifestFilePath}...'); + context.logger.verbose('Updating ${path.join(context.prefixPath, constants.webManifestFilePath)}...'); _updateManifestFile(); // todo: update index.html in web/index.html @@ -86,9 +86,9 @@ class WebIconGenerator extends IconGenerator { // verify web platform related files and directories exists final entitesToCheck = [ - constants.webDirPath, - constants.webManifestFilePath, - constants.webIndexFilePath, + path.join(context.prefixPath, constants.webDirPath), + path.join(context.prefixPath, constants.webManifestFilePath), + path.join(context.prefixPath, constants.webIndexFilePath), ]; // web platform related files must exist to continue @@ -102,16 +102,16 @@ class WebIconGenerator extends IconGenerator { void _generateFavicon(Image image) { final favIcon = utils.createResizedImage(constants.kFaviconSize, image); - final favIconFile = utils.createFileIfNotExist(constants.webFaviconFilePath); + final favIconFile = utils.createFileIfNotExist(path.join(context.prefixPath, constants.webFaviconFilePath)); favIconFile.writeAsBytesSync(encodePng(favIcon)); } void _generateIcons(Image image) { - final iconsDir = utils.createDirIfNotExist(constants.webIconsDirPath); + final iconsDir = utils.createDirIfNotExist(path.join(context.prefixPath, constants.webIconsDirPath)); // generate icons for (final template in _webIconSizeTemplates) { final resizedImg = utils.createResizedImage(template.size, image); - final iconFile = utils.createFileIfNotExist(path.join(iconsDir.path, template.iconFile)); + final iconFile = utils.createFileIfNotExist(path.join(context.prefixPath, iconsDir.path, template.iconFile)); iconFile.writeAsBytesSync(encodePng(resizedImg)); } } @@ -125,7 +125,7 @@ class WebIconGenerator extends IconGenerator { // } void _updateManifestFile() { - final manifestFile = utils.createFileIfNotExist(constants.webManifestFilePath); + final manifestFile = utils.createFileIfNotExist(path.join(context.prefixPath, constants.webManifestFilePath)); final manifestConfig = jsonDecode(manifestFile.readAsStringSync()) as Map; // update background_color