simplify syntax model and conversion algorithm

This commit is contained in:
Malo Léon 2022-08-08 14:35:40 +01:00
parent b2f9aeb570
commit 1abf6ccb36
6 changed files with 50 additions and 166 deletions

View File

@ -1,38 +0,0 @@
brick_name: wyatt_feature_brick
path_to_brickify: lib/feature_brick_folder
variables:
feature_brick:
variable_name: feature_brick
type: string
syntax:
camel_case: featureBrick
constant_case: FEATURE_BRICK
dot_case: feature.brick
header_case: Feature-Brick
lower_case: feature brick
pascal_case: FeatureBrick
param_case: feature-brick
sentence_case: Feature brick
snake_case: feature_brick
title_case: Feature Brick
upper_case: FEATURE BRICK
description:
variable_name: description
type: string
syntax:
camel_case: description
constant_case: DESCRIPTION
dot_case: description
header_case: Description
lower_case: description
pascal_case: Description
param_case: description
sentence_case: Description
snake_case: description
title_case: Description
upper_case: DESCRIPTION
isBloc:
variable_name: bloc
type: bool

View File

@ -1,7 +0,0 @@
class FeatureBrick {
var featureBrick = 0;
}
class DescriptionClass {
var descriptionClass = 0;
}

View File

@ -8,11 +8,14 @@ in `lib/`.
```yaml ```yaml
brick_name: wyatt_feature_brick brick_name: wyatt_feature_brick
project_name: projet_name
path_to_brickify: lib/feature_brick_test_folder path_to_brickify: lib/feature_brick_folder
syntax: variables:
feature_brick:
variable_name: feature_brick
type: string
syntax:
camel_case: featureBrick camel_case: featureBrick
constant_case: FEATURE_BRICK constant_case: FEATURE_BRICK
dot_case: feature.brick dot_case: feature.brick
@ -24,6 +27,24 @@ syntax:
snake_case: feature_brick snake_case: feature_brick
title_case: Feature Brick title_case: Feature Brick
upper_case: FEATURE BRICK upper_case: FEATURE BRICK
description:
variable_name: description
type: string
syntax:
camel_case: description
constant_case: DESCRIPTION
dot_case: description
header_case: Description
lower_case: description
pascal_case: Description
param_case: description
sentence_case: Description
snake_case: description
title_case: Description
upper_case: DESCRIPTION
isBloc:
variable_name: bloc
type: bool
``` ```
then run command with project path then run command with project path

View File

@ -68,11 +68,11 @@ Future<void> main(List<String> arguments) async {
for (final variable in brickConfig.variables!) { for (final variable in brickConfig.variables!) {
if (variable?.type == VariabelType.string) { if (variable?.type == VariabelType.string) {
for (final syntax in VariableStringSyntax.values) { for (final syntax in VariableStringSyntax.values) {
final toReplace = variable?.syntax?.getValue(syntax); final toReplace = variable?.syntax?[syntax.mapKey] as String?;
if (toReplace != null) { if (toReplace != null) {
contents = contents.replaceAll( contents = contents.replaceAll(
variable!.syntax!.getValue(syntax)!, toReplace,
'{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}', '{{#${syntax.id}}}{{${variable?.name}}}{{/${syntax.id}}}',
); );
} }
} }
@ -89,14 +89,21 @@ Future<void> main(List<String> arguments) async {
if (brickConfig.variables != null) { if (brickConfig.variables != null) {
for (final variable in brickConfig.variables!) { for (final variable in brickConfig.variables!) {
if (variable?.type == VariabelType.string) { if (variable?.type == VariabelType.string &&
if (filePath.contains(variable!.syntax!.snakeCase!)) { variable?.syntax?[VariableStringSyntax.snakeCase.mapKey] !=
null) {
print(variable?.syntax?[VariableStringSyntax.snakeCase.mapKey]);
if (filePath.contains(
variable!.syntax![VariableStringSyntax.snakeCase.mapKey]
as String,
)) {
var pathList = var pathList =
filePath.split(Platform.pathSeparator).sublist(3); filePath.split(Platform.pathSeparator).sublist(3);
pathList = pathList pathList = pathList
.map( .map(
(segment) => segment.replaceAll( (segment) => segment.replaceAll(
variable.syntax!.snakeCase!, variable.syntax![VariableStringSyntax.snakeCase.mapKey]
as String,
'{{${variable.name}.snakeCase()}}', '{{${variable.name}.snakeCase()}}',
), ),
) )

View File

@ -1,4 +1,3 @@
import 'package:brick_generator/models/variable_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';
@ -9,7 +8,7 @@ const _syntaxKey = 'syntax';
class BrickVariable { class BrickVariable {
String? name; String? name;
VariabelType? type; VariabelType? type;
VariableSyntax? syntax; YamlMap? syntax;
BrickVariable({ BrickVariable({
required this.name, required this.name,
@ -25,7 +24,7 @@ class BrickVariable {
? BrickVariable( ? BrickVariable(
name: data[_variableNameKey] as String?, name: data[_variableNameKey] as String?,
type: VariabelType.stringToEnum(data[_typeKey] as String?), type: VariabelType.stringToEnum(data[_typeKey] as String?),
syntax: VariableSyntax.from(data[_syntaxKey] as YamlMap?), syntax: data[_syntaxKey] as YamlMap?,
) )
: null; : null;
@ -35,8 +34,5 @@ class BrickVariable {
'Yaml file is not conform', 'Yaml file is not conform',
); );
} }
if (type == VariabelType.string) {
syntax?.checkFormat();
}
} }
} }

View File

@ -1,95 +0,0 @@
import 'package:brick_generator/models/variable_string_syntax.dart';
import 'package:yaml/yaml.dart';
class VariableSyntax {
String? camelCase;
String? constantCase;
String? dotCase;
String? headerCase;
String? lowerCase;
String? pascalCase;
String? paramCase;
String? sentenceCase;
String? snakeCase;
String? titleCase;
String? upperCase;
VariableSyntax({
required this.camelCase,
required this.constantCase,
required this.dotCase,
required this.headerCase,
required this.lowerCase,
required this.pascalCase,
required this.paramCase,
required this.sentenceCase,
required this.snakeCase,
required this.titleCase,
required this.upperCase,
});
VariableSyntax._();
factory VariableSyntax.parser() => VariableSyntax._();
static VariableSyntax? from(YamlMap? data) => data != null
? VariableSyntax(
camelCase: data[VariableStringSyntax.camelCase.mapKey] as String?,
constantCase:
data[VariableStringSyntax.constantCase.mapKey] as String?,
dotCase: data[VariableStringSyntax.dotCase.mapKey] as String?,
headerCase: data[VariableStringSyntax.headerCase.mapKey] as String?,
lowerCase: data[VariableStringSyntax.lowerCase.mapKey] as String?,
pascalCase: data[VariableStringSyntax.pascalCase.mapKey] as String?,
paramCase: data[VariableStringSyntax.paramCase.mapKey] as String?,
sentenceCase:
data[VariableStringSyntax.sentenceCase.mapKey] as String?,
snakeCase: data[VariableStringSyntax.snakeCase.mapKey] as String?,
titleCase: data[VariableStringSyntax.titleCase.mapKey] as String?,
upperCase: data[VariableStringSyntax.upperCase.mapKey] as String?,
)
: null;
String? getValue(VariableStringSyntax key) {
switch (key) {
case VariableStringSyntax.camelCase:
return camelCase;
case VariableStringSyntax.constantCase:
return constantCase;
case VariableStringSyntax.dotCase:
return dotCase;
case VariableStringSyntax.headerCase:
return headerCase;
case VariableStringSyntax.lowerCase:
return lowerCase;
case VariableStringSyntax.pascalCase:
return pascalCase;
case VariableStringSyntax.paramCase:
return paramCase;
case VariableStringSyntax.sentenceCase:
return sentenceCase;
case VariableStringSyntax.snakeCase:
return snakeCase;
case VariableStringSyntax.titleCase:
return titleCase;
case VariableStringSyntax.upperCase:
return upperCase;
}
}
void checkFormat() {
if (camelCase == null ||
constantCase == null ||
dotCase == null ||
headerCase == null ||
lowerCase == null ||
pascalCase == null ||
paramCase == null ||
sentenceCase == null ||
snakeCase == null ||
titleCase == null ||
upperCase == null) {
throw ArgumentError(
'Yaml file is not conform',
);
}
}
}