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>
default: <default>
prompt: <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

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_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<BrickVariableString>().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<BrickVariableString>().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',
);
}
}

View File

@ -37,8 +37,10 @@ class BrickVariableBoolean extends BrickVariable<bool> {
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,

View File

@ -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<String> {
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<String> {
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<String, String>? syntax;
Map<String, String>? syntaxes;
List<VariableStringSyntax> formats;
@override
String toString() => 'BrickVariableString(name: $name, '

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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;
}

View File

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