Skip to content

Commit

Permalink
Support for custom config path option (#38)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
milindgoel15 committed Aug 16, 2023
1 parent 5847d55 commit 3605341
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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!_
Expand Down
6 changes: 6 additions & 0 deletions lib/messages.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
part of package_rename;

const _packageRenameCommands = '''
╔═════════════════════════════════════╗
║ Package Rename Commands ║
╚═════════════════════════════════════╝
''';

const _successMessage = '''
╔═════════════════════════════════════════════════════════════╗
║ 🥳🥳🥳 Done! Now go ahead and build your app 🥳🥳🥳 ║
Expand Down
56 changes: 51 additions & 5 deletions lib/package_rename.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,35 @@ void set(List<String> 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']);
Expand All @@ -104,10 +123,25 @@ bool _configFileExists() {
return configFile.existsSync() || pubspecFile.existsSync();
}

Map<String, dynamic> _getConfig({required String? flavour}) {
final yamlFile = File(_packageRenameConfigFileName).existsSync()
? File(_packageRenameConfigFileName)
: File(_pubspecFileName);
Map<String, dynamic> _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;
Expand All @@ -124,3 +158,15 @@ Map<String, dynamic> _getConfig({required String? flavour}) {

return Map<String, dynamic>.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;
}
}

0 comments on commit 3605341

Please sign in to comment.