diff --git a/README.md b/README.md index d4a10e9..6104b42 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ So by default `.env` file will be generated alongside with - [Field configuration](#field-configuration) - [Fields config examples](#fields-config-examples) - [Pattern example](#pattern-example) - - [DotEnv example](#dotenv-example) + - [RC file example](#rc-example) - [Global environment variable example](#global-environment-variable-example) - [Extensions](#extensions) - [Basic Extension config usage](#basic-extension-config-usage) @@ -61,7 +61,7 @@ Create `environment_config.yaml` or update `package.yaml` file with following co ```yaml environment_config: path: environment_config.dart # optional, result file path against `lib/` folder - dotenv_path: .env # optional, result file path for .env file against project root folder + rc_path: .env # optional, result file path for RC file against project root folder, if not .env name will be used class: EnvironmentConfig # optional, class name dev_extension: # optional, by default undefined, allows to specify command option to use extension @@ -72,7 +72,7 @@ environment_config: const: # optional, default to TRUE pattern: # optional, specified pattern for key value, use __VALUE__ to insert entered value anywhere in the pattern default: # optional, default value for key, if not provided key will be required during command run - dotenv: # optional, default to FALSE, if this field should be added to .env file + export_to_rc: # optional, default to FALSE, if this field should be added to RC file config_field: # optional, default to TRUE, if this field should be added to Dart file env_var: # optional, global environment variable name static: # options, default to TRUE, if this field should be static, if FALSE, `const` will be be ignored @@ -82,14 +82,19 @@ environment_config: extensions: # set of extensions for default field list some_extension: # extension name - some_key: - const: # optional, overrides `const` value for the field - pattern: # optional, overrides `pattern` value for the field - default: # optional, overrides `default` value for the field - env_var: # optional, overrides `env_var` value for the field + path: # optional, if not specified, original key is used + rc_path: # optional, if not specified, original key is used + class: # optional, if not specified, original key is used + fields: + some_key: + const: # optional, overrides `const` value for the field + pattern: # optional, overrides `pattern` value for the field + export_to_rc: # optional, overrides `export_to_rc` value for the field + default: # optional, overrides `default` value for the field + env_var: # optional, overrides `env_var` value for the field - imports: # optional, adds set of imports to main configulration - - package:some_other_package + imports: # optional, adds set of imports to main configuration + - package:some_other_package ``` @@ -193,7 +198,7 @@ flutter pub run environment_config:generate --config=path/to/file.yaml Class and file can be configured with next options - `path` - path to file against `lib` folder, by default it's `environment_config.dart` -- `dotenv_path` - path to file against root app folder, by default it's +- `rc_path` - path to file against root app folder, by default it's `.env` - `class` - class name, by default will be generated based on file name - `const` - optional, defines if class constructor should be @@ -209,7 +214,7 @@ If `class` is not specified value for class name will be generate based on file name in `path` field. It will convert `snake_case` into `CamelCase`. -Field `dotenv_path` will be used only if at least one field contains `dotenv: true` +Field `rc_path` will be used only if at least one field contains `export_to_rc: true` ### Config Examples @@ -291,15 +296,15 @@ Each field accepts next params, each param is **optional** - `default` - default value for the field. If not specified, field will be treated as required - `short_name` - short key name, that can be used during command run instead of full field name. Accepts 1 symbol values only -- `dotenv` - bool flag, if `TRUE` this field will be added to `.env` file. +- `export_to_rc` - bool flag, if `TRUE` this field will be added to RC file. - `env_var` - environment global variable name - `config_field` - default to `TRUE`, indicates if this field should be defined in Dart class config -**If you want to generate `.env` file in addition to class config, at least ONE -key should have `dotenv` to be TRUE. Otherwise `.env` file won't be generated** +**If you want to generate RC file in addition to class config, at least ONE +key should have `export_to_rc` to be TRUE. Otherwise RC file won't be generated** -**If field contains `dotenv: false` (which is default state) and +**If field contains `export_to_rc: false` (which is default state) and `config_field: false` field will be ignored** **Note** If field config doesn't have `default` key specified it will be @@ -351,22 +356,22 @@ class EnvironmentConfig { ``` -#### DotEnv example +#### RC example -To create `.env` at least one key should have `dotenv: true` attribute +To create RC file (by default it's going to be `.env`) at least one key should have `export_to_rc: true` attribute ```yaml environment_config: fields: first_key: # define field only in Dart class type: num second_key: - dotenv: true # will define field in Dart and in `.env` + export_to_rc: true # will define field in Dart and in RC file third_key: - dotenv: true # will define field in `.env` + export_to_rc: true # will define field in RC file config_field: false # will exclude field from Dart config file ``` -**Note** If `dotenv: false` and `config_field: false` this field won't +**Note** If `export_to_rc: false` and `config_field: false` this field won't be added to `.env` and Dart config class This command @@ -420,6 +425,11 @@ Generator will use next priority: Extensions allows you to define set of override rules for fields and imports. +Extension is able to override following main config items: +- path +- rc_path +- class + Primarily extensions are needed to override main configuration and provide some specific settings for environment like dev. That is why it allows to override just specific set of configuration keys for specific @@ -428,6 +438,7 @@ field: - pattern - default - env_var +- export_to_rc Extension won't override other field keys to keep config class signature consistent (if you need to add other keys in this list, feel free to diff --git a/example/.gitignore b/example/.gitignore index 94173d3..8155c66 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -1 +1,2 @@ -lib/environment_config.dart \ No newline at end of file +lib/environment_config.dart +.*rc diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 87f6685..76d4441 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -8,19 +8,29 @@ dependencies: environment_config: path: ../ -# Config example. Run `flutter pub run environment_config:generate --env=YOUR_VALUE` to see example +# Config example. Run `flutter pub run environment_config:generate --env=YOUR_VALUE --other=OTHER_VALUE` to see example environment_config: + rc_path: .somerc fields: env: + other: + export_to_rc: true + config_field: false imports: - some:package extensions: dev: + rc_path: .otherrc fields: env: default: some value + export_to_rc: true + config_field: false + other: + export_to_rc: false + config_field: true imports: - other:package \ No newline at end of file diff --git a/lib/config.dart b/lib/config.dart index b62be1b..f15f955 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -64,8 +64,8 @@ class Config { 'lib/${_getConfigValue(ConfigFieldType.PATH, 'environment_config.dart')}'; /// Target file for `.env` params - String get dotEnvFilePath => - _getConfigValue(ConfigFieldType.DOTENV_PATH, '.env')!; + String get rcFilePath => + _getConfigValue(ConfigFieldType.RC_PATH, '.env')!; /// Provides config class name String get className { @@ -85,8 +85,8 @@ class Config { } /// Fields, that should be exported to `.env` file - Iterable get dotEnvFields => - _fields.where((field) => field.isDotEnv); + Iterable get rcFields => + _fields.where((field) => field.isExportToRc); /// Fields, that should be exported to Dart config file Iterable get classConfigFields => @@ -102,17 +102,21 @@ class Config { bool get isClassConst => config[ConfigFieldType.CONST] ?? false; /// Defines if generator should try to create `.env` file - bool get createDotEnv => dotEnvFields.isNotEmpty; + bool get createRcFile => rcFields.isNotEmpty; /// Defines if generator should try to create Dart config file bool get createConfigClass => classConfigFields.isNotEmpty; String? _getConfigValue(key, [String? defaultValue]) { - if (arguments.containsKey(key) && !arguments[key].isEmpty) { + if (arguments.containsKey(key) && arguments[key].isNotEmpty) { return arguments[key]; } - if (config.containsKey(key) && !(config[key] ?? '').isEmpty) { + if (ConfigFieldType.EXTENDED_CONFIG_FIELDS.contains(key) && extConfig.containsKey(key) && (extConfig[key] ?? '').isNotEmpty) { + return extConfig[key]; + } + + if (config.containsKey(key) && (config[key] ?? '').isNotEmpty) { return config[key]; } diff --git a/lib/config_field_type.dart b/lib/config_field_type.dart index 0815804..f925ab9 100644 --- a/lib/config_field_type.dart +++ b/lib/config_field_type.dart @@ -6,7 +6,7 @@ class ConfigFieldType { static const String IMPORTS = 'imports'; static const String CLASS = 'class'; static const String PATH = 'path'; - static const String DOTENV_PATH = 'dotenv_path'; + static const String RC_PATH = 'rc_path'; static const String DEV_EXTENSION = 'dev_extension'; static const String EXTENSIONS = 'extensions'; @@ -15,8 +15,14 @@ class ConfigFieldType { static const String DEFAULT = 'default'; static const String CONST = 'const'; static const String SHORT_NAME = 'short_name'; - static const String IS_DOTENV = 'dotenv'; + static const String EXPORT_TO_RC = 'export_to_rc'; static const String ENV_VAR = 'env_var'; static const String CONFIG_FIELD = 'config_field'; static const String STATIC = 'static'; + + static const List EXTENDED_CONFIG_FIELDS = [ + PATH, + RC_PATH, + CLASS, + ]; } diff --git a/lib/config_generator.dart b/lib/config_generator.dart index 34c4eda..eb5a208 100644 --- a/lib/config_generator.dart +++ b/lib/config_generator.dart @@ -21,13 +21,13 @@ class ConfigGenerator { futures.add(_generateClass()); } - if (config.createDotEnv) { - futures.add(_generateDotEnv()); + if (config.createRcFile) { + futures.add(_generateRcFile()); } if (futures.isEmpty) { throw ValidationError(ConfigFieldType.FIELDS, - 'At least one field should be defined for `.env` or Dart config class'); + 'At least one field should be defined for RC or Dart config class'); } return Future.wait(futures); @@ -69,15 +69,15 @@ class ConfigGenerator { stdout.writeln('Config generated at "${config.filePath}"'); } - Future _generateDotEnv() async { - final File configFile = File(config.dotEnvFilePath); + Future _generateRcFile() async { + final File configFile = File(config.rcFilePath); - final String envString = config.dotEnvFields - .map((field) => '${field.name}=${field.dotEnvValue}') + final String envString = config.rcFields + .map((field) => '${field.name}=${field.rcValue}') .join("\r\n"); await configFile.writeAsString(envString, mode: FileMode.write); - stdout.writeln('.env config generated at "${config.dotEnvFilePath}"'); + stdout.writeln('RC config generated at "${config.rcFilePath}"'); } } diff --git a/lib/field_config.dart b/lib/field_config.dart index 480a2a3..10710c4 100644 --- a/lib/field_config.dart +++ b/lib/field_config.dart @@ -64,10 +64,14 @@ class FieldConfig { bool get isStatic => field[ConfigFieldType.STATIC] ?? true; /// Defines if this field should be exported to `.env` file - bool get isDotEnv => field[ConfigFieldType.IS_DOTENV] ?? false; + bool get isExportToRc => extField[ConfigFieldType.EXPORT_TO_RC] + ?? field[ConfigFieldType.EXPORT_TO_RC] + ?? false; /// Defines if this field should be exported to Dart config file - bool get isConfigField => field[ConfigFieldType.CONFIG_FIELD] ?? true; + bool get isConfigField => extField[ConfigFieldType.CONFIG_FIELD] + ?? field[ConfigFieldType.CONFIG_FIELD] + ?? true; /// Get value for config class /// @@ -87,7 +91,7 @@ class FieldConfig { } /// Value for key in `.env` file - String get dotEnvValue => + String get rcValue => _pattern?.replaceAll(_PATTERN_REGEXP, _fieldValue!) ?? _fieldValue!; String? get _pattern =>