feat: add format filter

This commit is contained in:
Hugo Pointcheval 2023-01-27 11:13:18 +01:00
parent ee93f54653
commit 145d53f6cc
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
6 changed files with 59 additions and 32 deletions

View File

@ -128,6 +128,8 @@ name:
description: <description> description: <description>
default: <default> default: <default>
prompt: <prompt> prompt: <prompt>
formats:
- ...
``` ```
- name: the variable name (will be in `brick.yaml`) - name: the variable name (will be in `brick.yaml`)
@ -135,6 +137,8 @@ name:
- type: variable type, here string, (will be in `brick.yaml`) - type: variable type, here string, (will be in `brick.yaml`)
- default: default value (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`) - 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 #### Boolean variable

View File

@ -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_config.dart';
import 'package:brick_generator/models/brick_variable_string.dart'; import 'package:brick_generator/models/brick_variable_string.dart';
import 'package:brick_generator/models/ignore_list.dart'; import 'package:brick_generator/models/ignore_list.dart';
import 'package:brick_generator/models/variable_string_syntax.dart';
import 'package:path/path.dart'; import 'package:path/path.dart';
abstract class FileSystem { abstract class FileSystem {
@ -238,24 +237,23 @@ abstract class FileSystem {
try { try {
contents = file.readAsStringSync(); contents = file.readAsStringSync();
} catch (e) { } catch (e) {
Logger.error(e); // Ignore decode error
return; return;
} }
config.variables.whereType<BrickVariableString>().forEach((variable) { config.variables.whereType<BrickVariableString>().forEach((variable) {
// Replace all string variables // Replace all string variables
for (final syntax in VariableStringSyntax.values) { for (final syntax in variable.formats) {
final toReplace = variable.syntax?[syntax.mapKey]; final toReplace = variable.syntaxes?[syntax.mapKey];
if (toReplace != null) { if (toReplace != null && (contents?.contains(toReplace) ?? false)) {
if (contents?.contains(toReplace) ?? false) { contents = contents!.replaceAll(
contents = contents!.replaceAll( toReplace,
toReplace, '{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}',
'{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}', );
); Logger.debug(
Logger.debug( 'Variable `${variable.name}` replaced '
'Variable `${variable.name}` replaced in ${file.path}', 'in ${file.path} by $toReplace',
); );
}
} }
} }
}); });
@ -350,8 +348,8 @@ abstract class FileSystem {
.forEach((file) { .forEach((file) {
config.variables.whereType<BrickVariableString>().forEach((variable) { config.variables.whereType<BrickVariableString>().forEach((variable) {
// Replace all string variables // Replace all string variables
for (final syntax in VariableStringSyntax.values) { for (final syntax in variable.formats) {
final toReplace = variable.syntax?[syntax.mapKey]; final toReplace = variable.syntaxes?[syntax.mapKey];
if (toReplace != null && file.path.contains(toReplace)) { if (toReplace != null && file.path.contains(toReplace)) {
final newPath = file.path.replaceAll( final newPath = file.path.replaceAll(
toReplace, toReplace,
@ -361,7 +359,8 @@ abstract class FileSystem {
File(newPath).createSync(recursive: true); File(newPath).createSync(recursive: true);
file.renameSync(newPath); file.renameSync(newPath);
Logger.debug( Logger.debug(
'${file.path} renamed with variable `${variable.name}`', '${file.path} renamed with variable '
'`${variable.name}` using $syntax',
); );
} }
} }

View File

@ -37,8 +37,10 @@ class BrickVariableBoolean extends BrickVariable<bool> {
throw ArgumentError.notNull('source'); throw ArgumentError.notNull('source');
} }
final type = VariableType.fromString(source[_typeKey] as String?);
return BrickVariableBoolean( return BrickVariableBoolean(
type: VariableType.fromString(source[_typeKey] as String?), type: type,
name: key, name: key,
description: source[_descriptionKey] as String? ?? '', description: source[_descriptionKey] as String? ?? '',
defaultValue: source[_defaultKey] as bool? ?? false, defaultValue: source[_defaultKey] as bool? ?? false,

View File

@ -16,10 +16,12 @@
import 'package:brick_generator/core/string_extension.dart'; import 'package:brick_generator/core/string_extension.dart';
import 'package:brick_generator/models/brick_variable.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:brick_generator/models/variable_type.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
const _compilableKey = 'compilable'; const _compilableKey = 'compilable';
const _formatsKey = 'formats';
const _typeKey = 'type'; const _typeKey = 'type';
const _descriptionKey = 'description'; const _descriptionKey = 'description';
const _defaultKey = 'default'; const _defaultKey = 'default';
@ -28,7 +30,8 @@ const _promptKey = 'prompt';
class BrickVariableString extends BrickVariable<String> { class BrickVariableString extends BrickVariable<String> {
BrickVariableString({ BrickVariableString({
required this.compilable, required this.compilable,
required this.syntax, required this.formats,
required this.syntaxes,
required super.type, required super.type,
required super.name, required super.name,
required super.description, required super.description,
@ -41,19 +44,30 @@ class BrickVariableString extends BrickVariable<String> {
throw ArgumentError.notNull('source'); 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( return BrickVariableString(
compilable: source[_compilableKey] as String? ?? '', compilable: source[_compilableKey] as String? ?? '',
type: VariableType.fromString(source[_typeKey] as String?), type: type,
syntaxes: syntaxes,
name: key, name: key,
description: source[_descriptionKey] as String? ?? '', description: source[_descriptionKey] as String? ?? '',
defaultValue: source[_defaultKey] as String? ?? '', defaultValue: source[_defaultKey] as String? ?? '',
prompt: source[_promptKey] as String? ?? '', prompt: source[_promptKey] as String? ?? '',
syntax: (source[_compilableKey] as String? ?? '').syntaxes, formats: formats.toList(),
); );
} }
String compilable; String compilable;
Map<String, String>? syntax; Map<String, String>? syntaxes;
List<VariableStringSyntax> formats;
@override @override
String toString() => 'BrickVariableString(name: $name, ' String toString() => 'BrickVariableString(name: $name, '

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
enum VariableStringSyntax { enum VariableStringSyntax {
none('ERROR', 'ERROR'),
snakeCase('snake_case', 'snakeCase'), snakeCase('snake_case', 'snakeCase'),
camelCase('camel_case', 'camelCase'), camelCase('camel_case', 'camelCase'),
constantCase('constant_case', 'constantCase'), constantCase('constant_case', 'constantCase'),
@ -31,6 +32,15 @@ enum VariableStringSyntax {
const VariableStringSyntax(this.mapKey, this.id); 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 mapKey;
final String id; final String id;
} }

View File

@ -19,15 +19,13 @@ enum VariableType {
string, string,
boolean; boolean;
static VariableType fromString(String? type) { factory VariableType.fromString(String? source) {
switch (type) { for (final element in values) {
case 'string': if (element.name == source || element.name == source) {
return string; return element;
case 'boolean': }
return boolean;
default:
return none;
} }
return VariableType.none;
} }
@override @override