Skip to content

Commit

Permalink
feat(cli-option): added --prefix option
Browse files Browse the repository at this point in the history
--prefix Generates icons in the given path. Used for testing in virtual directory.

Only supports web platform for now
  • Loading branch information
RatakondalaArun committed Jul 10, 2022
1 parent 4a49ff8 commit b2b8982
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
16 changes: 14 additions & 2 deletions lib/abs/icon_generator.dart
Expand Up @@ -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;
Expand All @@ -36,11 +42,17 @@ class IconGeneratorContext {
void generateIconsFor({
required FlutterLauncherIconsConfig config,
required String? flavor,
required String prefixPath,
required FLILogger logger,
required List<IconGenerator> 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');
Expand Down
13 changes: 7 additions & 6 deletions lib/flutter_launcher_icons_config.dart
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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<String, dynamic> toJson() => _$FlutterLauncherIconsConfigToJson(this);
Expand Down
31 changes: 22 additions & 9 deletions lib/main.dart
Expand Up @@ -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$';

Expand All @@ -38,10 +39,18 @@ List<String> getFlavors() {

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
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]);
Expand All @@ -60,11 +69,13 @@ Future<void> createIconsFromArguments(List<String> arguments) async {

// Load the config file
final Map<String, dynamic>? 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}. '
Expand All @@ -75,7 +86,7 @@ Future<void> createIconsFromArguments(List<String> 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');
Expand All @@ -87,7 +98,7 @@ Future<void> createIconsFromArguments(List<String> arguments) async {
for (String flavor in flavors) {
print('\nFlavor: $flavor');
final Map<String, dynamic> 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) {
Expand All @@ -101,7 +112,8 @@ Future<void> createIconsFromArguments(List<String> arguments) async {
Future<void> createIconsFromConfig(
Map<String, dynamic> config,
FlutterLauncherIconsConfig flutterConfigs,
FLILogger logger, [
FLILogger logger,
String prefixPath, [
String? flavor,
]) async {
if (!isImagePathInConfig(config)) {
Expand Down Expand Up @@ -135,6 +147,7 @@ Future<void> createIconsFromConfig(
generateIconsFor(
config: flutterConfigs,
logger: logger,
prefixPath: prefixPath,
flavor: flavor,
platforms: (context) => [
WebIconGenerator(context),
Expand Down
18 changes: 9 additions & 9 deletions lib/web/web_icon_generator.dart
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}
}
Expand All @@ -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<String, dynamic>;

// update background_color
Expand Down

0 comments on commit b2b8982

Please sign in to comment.