Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

```

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib/environment_config.dart
lib/environment_config.dart
.*rc
12 changes: 11 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 11 additions & 7 deletions lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -85,8 +85,8 @@ class Config {
}

/// Fields, that should be exported to `.env` file
Iterable<FieldConfig> get dotEnvFields =>
_fields.where((field) => field.isDotEnv);
Iterable<FieldConfig> get rcFields =>
_fields.where((field) => field.isExportToRc);

/// Fields, that should be exported to Dart config file
Iterable<FieldConfig> get classConfigFields =>
Expand All @@ -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];
}

Expand Down
10 changes: 8 additions & 2 deletions lib/config_field_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<String> EXTENDED_CONFIG_FIELDS = [
PATH,
RC_PATH,
CLASS,
];
}
16 changes: 8 additions & 8 deletions lib/config_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -69,15 +69,15 @@ class ConfigGenerator {
stdout.writeln('Config generated at "${config.filePath}"');
}

Future<void> _generateDotEnv() async {
final File configFile = File(config.dotEnvFilePath);
Future<void> _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}"');
}
}
10 changes: 7 additions & 3 deletions lib/field_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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 =>
Expand Down