From 3605341c8f558cf02265f1d67a8e525180e4a611 Mon Sep 17 00:00:00 2001 From: Milind Goel <45682747+milindgoel15@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:02:20 +0530 Subject: [PATCH] Support for custom config path option (#38) * add custom config path option * update readme based on new option * add basic help option for package * use logger instead of print * removed extra spaces, separate code block * remove extra space --- README.md | 32 +++++++++++++++++++++-- lib/messages.dart | 6 +++++ lib/package_rename.dart | 56 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0cd0e4e..6bc9339 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ dev_dependencies: You can create configurations by adding `package_rename_config` key in: 1. Root `pubspec.yaml` file -1. `package_rename_config.yaml` file at root of your project +1. `package_rename_config.yaml` file at root of your project or a custom folder in the project ## Usage @@ -73,12 +73,28 @@ package_rename_config: #### Running Package Rename -Execute the follow command at the root of your project: +Execute the command as per your config location: + +if config file exists in either pubspec.yaml or root path: ```bash dart run package_rename ``` +OR + +if config file exists in a custom folder: + +```bash +dart run package_rename --path="path/to/package_rename_config.yaml" +``` + +or + +```bash +dart run package_rename -p "path/to/package_rename_config.yaml" +``` + ## Flavour Support Package Rename supports flavours. You can add flavour specific configurations by adding `flavour_name` in configuration key. @@ -96,6 +112,18 @@ And then run the following command: dart run package_rename --flavour=flavour_name ``` +or + +```bash +dart run package_rename -f flavour_name +``` + +With custom config file location: + +```bash +dart run package_rename --flavour=flavour_name --path="path/to/package_rename_config.yaml" +``` + ## And that's it! 🎉 Now you can deploy your production ready app to change the _WORLD!_ diff --git a/lib/messages.dart b/lib/messages.dart index 9ad71eb..df43c0c 100644 --- a/lib/messages.dart +++ b/lib/messages.dart @@ -1,5 +1,11 @@ part of package_rename; +const _packageRenameCommands = ''' +╔═════════════════════════════════════╗ +║ Package Rename Commands ║ +╚═════════════════════════════════════╝ +'''; + const _successMessage = ''' ╔═════════════════════════════════════════════════════════════╗ ║ 🥳🥳🥳 Done! Now go ahead and build your app 🥳🥳🥳 ║ diff --git a/lib/package_rename.dart b/lib/package_rename.dart index 06969c0..8d15974 100644 --- a/lib/package_rename.dart +++ b/lib/package_rename.dart @@ -68,16 +68,35 @@ void set(List args) { // Create args parser to get flavour flag and its value final parser = ArgParser() + ..addOption( + 'path', + abbr: 'p', + help: 'The path for the config file', + ) ..addOption( 'flavour', abbr: 'f', help: 'The flavour of the configuration to be used.', aliases: ['flavor'], + ) + ..addFlag( + 'help', + abbr: 'h', + negatable: false, + help: 'Prints out available command usages', ); final results = parser.parse(args); + + if (results.wasParsed('help')) { + _logger + ..i(_packageRenameCommands) + ..i(parser.usage); + exit(0); + } final flavour = results['flavour'] as String?; + final path = results['path'] as String?; - final config = _getConfig(flavour: flavour); + final config = _getConfig(flavour: flavour, configFile: path); _setAndroidConfigurations(config['android']); _setIOSConfigurations(config['ios']); @@ -104,10 +123,25 @@ bool _configFileExists() { return configFile.existsSync() || pubspecFile.existsSync(); } -Map _getConfig({required String? flavour}) { - final yamlFile = File(_packageRenameConfigFileName).existsSync() - ? File(_packageRenameConfigFileName) - : File(_pubspecFileName); +Map _getConfig({ + required String? flavour, + String? configFile, +}) { + File yamlFile; + + if (configFile != null) { + if (File(configFile).existsSync()) { + _checkConfigContent(configFile); + yamlFile = File(configFile); + } else { + throw _PackageRenameErrors.filesNotFound; + } + } else if (File(_packageRenameConfigFileName).existsSync()) { + _checkConfigContent(_packageRenameConfigFileName); + yamlFile = File(_packageRenameConfigFileName); + } else { + yamlFile = File(_pubspecFileName); + } final yamlString = yamlFile.readAsStringSync(); final parsedYaml = yaml.loadYaml(yamlString) as Map; @@ -124,3 +158,15 @@ Map _getConfig({required String? flavour}) { return Map.from(rawConfig); } + +void _checkConfigContent(String yamlFile) { + final fileContent = File(yamlFile).readAsStringSync(); + + if (fileContent.isEmpty) { + throw _PackageRenameErrors.filesNotFound; + } + + if (yaml.loadYaml(fileContent) == null) { + throw _PackageRenameErrors.configNotFound; + } +}