diff --git a/tools/brick_generator/README.md b/tools/brick_generator/README.md index 75b8593..ffcfb78 100644 --- a/tools/brick_generator/README.md +++ b/tools/brick_generator/README.md @@ -128,6 +128,8 @@ name: description: default: prompt: + formats: + - ... ``` - name: the variable name (will be in `brick.yaml`) @@ -135,6 +137,8 @@ name: - type: variable type, here string, (will be in `brick.yaml`) - default: default value (will be in `brick.yaml`) - prompt: the displayed prompt at the brick generation (will be in `brick.yaml`) + - formats: optional list of formats that are detected. + - if no formats provided, all possible syntaxes are detected. #### Boolean variable diff --git a/tools/brick_generator/lib/core/file_system.dart b/tools/brick_generator/lib/core/file_system.dart index 3e9c45d..8781fe1 100644 --- a/tools/brick_generator/lib/core/file_system.dart +++ b/tools/brick_generator/lib/core/file_system.dart @@ -21,7 +21,6 @@ import 'package:brick_generator/core/shell.dart'; import 'package:brick_generator/models/brick_config.dart'; import 'package:brick_generator/models/brick_variable_string.dart'; import 'package:brick_generator/models/ignore_list.dart'; -import 'package:brick_generator/models/variable_string_syntax.dart'; import 'package:path/path.dart'; abstract class FileSystem { @@ -238,24 +237,23 @@ abstract class FileSystem { try { contents = file.readAsStringSync(); } catch (e) { - Logger.error(e); + // Ignore decode error return; } config.variables.whereType().forEach((variable) { // Replace all string variables - for (final syntax in VariableStringSyntax.values) { - final toReplace = variable.syntax?[syntax.mapKey]; - if (toReplace != null) { - if (contents?.contains(toReplace) ?? false) { - contents = contents!.replaceAll( - toReplace, - '{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}', - ); - Logger.debug( - 'Variable `${variable.name}` replaced in ${file.path}', - ); - } + for (final syntax in variable.formats) { + final toReplace = variable.syntaxes?[syntax.mapKey]; + if (toReplace != null && (contents?.contains(toReplace) ?? false)) { + contents = contents!.replaceAll( + toReplace, + '{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}', + ); + Logger.debug( + 'Variable `${variable.name}` replaced ' + 'in ${file.path} by $toReplace', + ); } } }); @@ -282,7 +280,7 @@ abstract class FileSystem { } /// Removes empty folder dynamically: - /// + /// /// For example, for this structure: /// ``` /// ./brick/__brick__/folder/empty1/ @@ -291,7 +289,7 @@ abstract class FileSystem { /// ``` /// it will delete `empty1` and `empty2`...then it will delete `folder`, /// because by deleting his subfolders he's now empty too. - /// + /// static void removeEmptyFolders(String targetPath) { if (!FileSystemEntity.isDirectorySync(targetPath)) { throw ArgumentError('Target must be a directory', 'targetPath'); @@ -350,8 +348,8 @@ abstract class FileSystem { .forEach((file) { config.variables.whereType().forEach((variable) { // Replace all string variables - for (final syntax in VariableStringSyntax.values) { - final toReplace = variable.syntax?[syntax.mapKey]; + for (final syntax in variable.formats) { + final toReplace = variable.syntaxes?[syntax.mapKey]; if (toReplace != null && file.path.contains(toReplace)) { final newPath = file.path.replaceAll( toReplace, @@ -361,7 +359,8 @@ abstract class FileSystem { File(newPath).createSync(recursive: true); file.renameSync(newPath); Logger.debug( - '${file.path} renamed with variable `${variable.name}`', + '${file.path} renamed with variable ' + '`${variable.name}` using $syntax', ); } } diff --git a/tools/brick_generator/lib/models/brick_variable_boolean.dart b/tools/brick_generator/lib/models/brick_variable_boolean.dart index b286de9..f545192 100644 --- a/tools/brick_generator/lib/models/brick_variable_boolean.dart +++ b/tools/brick_generator/lib/models/brick_variable_boolean.dart @@ -37,8 +37,10 @@ class BrickVariableBoolean extends BrickVariable { throw ArgumentError.notNull('source'); } + final type = VariableType.fromString(source[_typeKey] as String?); + return BrickVariableBoolean( - type: VariableType.fromString(source[_typeKey] as String?), + type: type, name: key, description: source[_descriptionKey] as String? ?? '', defaultValue: source[_defaultKey] as bool? ?? false, diff --git a/tools/brick_generator/lib/models/brick_variable_string.dart b/tools/brick_generator/lib/models/brick_variable_string.dart index 4661b1b..abfbefc 100644 --- a/tools/brick_generator/lib/models/brick_variable_string.dart +++ b/tools/brick_generator/lib/models/brick_variable_string.dart @@ -16,10 +16,12 @@ import 'package:brick_generator/core/string_extension.dart'; import 'package:brick_generator/models/brick_variable.dart'; +import 'package:brick_generator/models/variable_string_syntax.dart'; import 'package:brick_generator/models/variable_type.dart'; import 'package:yaml/yaml.dart'; const _compilableKey = 'compilable'; +const _formatsKey = 'formats'; const _typeKey = 'type'; const _descriptionKey = 'description'; const _defaultKey = 'default'; @@ -28,7 +30,8 @@ const _promptKey = 'prompt'; class BrickVariableString extends BrickVariable { BrickVariableString({ required this.compilable, - required this.syntax, + required this.formats, + required this.syntaxes, required super.type, required super.name, required super.description, @@ -41,19 +44,30 @@ class BrickVariableString extends BrickVariable { throw ArgumentError.notNull('source'); } + final type = VariableType.fromString(source[_typeKey] as String?); + + final formats = (source[_formatsKey] as YamlList?) + ?.map((element) => element as String) + .map(VariableStringSyntax.fromString) ?? + VariableStringSyntax.values; + + final syntaxes = (source[_compilableKey] as String?)?.syntaxes ?? {}; + return BrickVariableString( compilable: source[_compilableKey] as String? ?? '', - type: VariableType.fromString(source[_typeKey] as String?), + type: type, + syntaxes: syntaxes, name: key, description: source[_descriptionKey] as String? ?? '', defaultValue: source[_defaultKey] as String? ?? '', prompt: source[_promptKey] as String? ?? '', - syntax: (source[_compilableKey] as String? ?? '').syntaxes, + formats: formats.toList(), ); } String compilable; - Map? syntax; + Map? syntaxes; + List formats; @override String toString() => 'BrickVariableString(name: $name, ' diff --git a/tools/brick_generator/lib/models/variable_string_syntax.dart b/tools/brick_generator/lib/models/variable_string_syntax.dart index 8c9bd9b..55829c8 100644 --- a/tools/brick_generator/lib/models/variable_string_syntax.dart +++ b/tools/brick_generator/lib/models/variable_string_syntax.dart @@ -15,6 +15,7 @@ // along with this program. If not, see . enum VariableStringSyntax { + none('ERROR', 'ERROR'), snakeCase('snake_case', 'snakeCase'), camelCase('camel_case', 'camelCase'), constantCase('constant_case', 'constantCase'), @@ -31,6 +32,15 @@ enum VariableStringSyntax { const VariableStringSyntax(this.mapKey, this.id); + factory VariableStringSyntax.fromString(String? source) { + for (final element in values) { + if (element.mapKey == source || element.id == source) { + return element; + } + } + return VariableStringSyntax.none; + } + final String mapKey; final String id; } diff --git a/tools/brick_generator/lib/models/variable_type.dart b/tools/brick_generator/lib/models/variable_type.dart index 750f1f7..da0af3b 100644 --- a/tools/brick_generator/lib/models/variable_type.dart +++ b/tools/brick_generator/lib/models/variable_type.dart @@ -19,15 +19,13 @@ enum VariableType { string, boolean; - static VariableType fromString(String? type) { - switch (type) { - case 'string': - return string; - case 'boolean': - return boolean; - default: - return none; + factory VariableType.fromString(String? source) { + for (final element in values) { + if (element.name == source || element.name == source) { + return element; + } } + return VariableType.none; } @override