From 7161dd2bacdd69aa53c5daea7e3d8e07fd5b9c5d Mon Sep 17 00:00:00 2001 From: OutdatedGuy Date: Fri, 7 Apr 2023 22:02:50 +0530 Subject: [PATCH 1/3] feat: command to generate config file template --- bin/generate.dart | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 bin/generate.dart diff --git a/bin/generate.dart b/bin/generate.dart new file mode 100644 index 0000000000..005f38d9d3 --- /dev/null +++ b/bin/generate.dart @@ -0,0 +1,97 @@ +import 'dart:io'; + +import 'package:args/args.dart'; + +import 'package:flutter_launcher_icons/constants.dart'; +import 'package:flutter_launcher_icons/src/version.dart'; + +/// The function will be called from command line +/// using the following command: +/// ```sh +/// flutter pub run flutter_launcher_icons:generate +/// ``` +/// +/// Calling this function will generate a flutter_launcher_icons.yaml file +/// with a default config template. +/// +/// This command can take 2 optional arguments: +/// - --override: This will override the current `flutter_launcher_icons.yaml` +/// file if it exists, if not provided, the file will not be overridden and +/// a message will be printed to the console. +/// +/// - --fileName: This flag will take a file name as an argument and +/// will generate the config format in that file instead of the default +/// `flutter_launcher_icons.yaml` file, if not provided, +/// the default file will be used. +void main(List arguments) { + print(introMessage(packageVersion)); + + final parser = ArgParser() + ..addFlag('override', abbr: 'o', defaultsTo: false) + ..addOption( + 'fileName', + abbr: 'f', + defaultsTo: './flutter_launcher_icons.yaml', + ); + + final results = parser.parse(arguments); + final override = results['override'] as bool; + final fileName = results['fileName'] as String; + + // Check if fileName is valid and has a .yaml extension + if (!fileName.endsWith('.yaml')) { + print('Invalid file name, please provide a valid file name'); + return; + } + + final file = File(fileName); + if (file.existsSync()) { + if (override) { + print('File already exists, overriding...'); + _generateConfigFile(file); + } else { + print( + 'File already exists, use --override flag to override the file, or use --fileName flag to use a different file name', + ); + } + } else { + file.createSync(recursive: true); + _generateConfigFile(file); + } +} + +void _generateConfigFile(File configFile) { + configFile.writeAsStringSync(_configFileTemplate); + print('Config file generated successfully'); +} + +const _configFileTemplate = ''' +# flutter pub run flutter_launcher_icons +flutter_icons: + image_path: "assets/icon/icon.png" + + android: "launcher_icon" + # image_path_android: "assets/icon/icon.png" + min_sdk_android: 21 # android min sdk min:16, default 21 + # adaptive_icon_background: "assets/icon/background.png" + # adaptive_icon_foreground: "assets/icon/foreground.png" + + ios: true + # image_path_ios: "assets/icon/icon.png" + remove_alpha_channel_ios: true + + web: + generate: true + image_path: "path/to/image.png" + background_color: "#hexcode" + theme_color: "#hexcode" + + windows: + generate: true + image_path: "path/to/image.png" + icon_size: 48 # min:48, max:256, default: 48 + + macos: + generate: true + image_path: "path/to/image.png" +'''; From ff497957110ef730b964e8c25f488bdd9d2b5841 Mon Sep 17 00:00:00 2001 From: OutdatedGuy Date: Fri, 7 Apr 2023 22:08:28 +0530 Subject: [PATCH 2/3] refactor: added exception handling --- bin/generate.dart | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index 005f38d9d3..b5e26a45ca 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -55,14 +55,22 @@ void main(List arguments) { ); } } else { - file.createSync(recursive: true); - _generateConfigFile(file); + try { + file.createSync(recursive: true); + _generateConfigFile(file); + } on Exception catch (e) { + print('Error creating file: $e'); + } } } void _generateConfigFile(File configFile) { - configFile.writeAsStringSync(_configFileTemplate); - print('Config file generated successfully'); + try { + configFile.writeAsStringSync(_configFileTemplate); + print('Config file generated successfully'); + } on Exception catch (e) { + print('Error generating config file: $e'); + } } const _configFileTemplate = ''' From 1bdb4b14c9631aedde317168610236e4a1aad845 Mon Sep 17 00:00:00 2001 From: OutdatedGuy Date: Mon, 10 Apr 2023 02:00:19 +0530 Subject: [PATCH 3/3] feat: display command to run to use config file If custom file name is used, then only add `-f` flag to keep it clear --- bin/generate.dart | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index b5e26a45ca..2e28c27cf0 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -5,6 +5,8 @@ import 'package:args/args.dart'; import 'package:flutter_launcher_icons/constants.dart'; import 'package:flutter_launcher_icons/src/version.dart'; +const _defaultConfigFileName = './flutter_launcher_icons.yaml'; + /// The function will be called from command line /// using the following command: /// ```sh @@ -31,7 +33,7 @@ void main(List arguments) { ..addOption( 'fileName', abbr: 'f', - defaultsTo: './flutter_launcher_icons.yaml', + defaultsTo: _defaultConfigFileName, ); final results = parser.parse(arguments); @@ -67,7 +69,13 @@ void main(List arguments) { void _generateConfigFile(File configFile) { try { configFile.writeAsStringSync(_configFileTemplate); - print('Config file generated successfully'); + + print('\nConfig file generated successfully 🎉'); + print( + 'You can now use this new config file by using the command below:\n\n' + 'flutter pub run flutter_launcher_icons' + '${configFile.path == _defaultConfigFileName ? '' : ' -f ${configFile.path}'}\n', + ); } on Exception catch (e) { print('Error generating config file: $e'); }