feat: add boolean section support
This commit is contained in:
parent
1d72075410
commit
c47585a18e
@ -112,6 +112,7 @@ class Brickgen {
|
||||
|
||||
// Convert compilable values -> variables (in files)
|
||||
FileSystem.convertCompilableVariablesInFolder(config, targetPath!);
|
||||
FileSystem.convertBooleanVariablesInFolder(config, targetPath!);
|
||||
Logger.info('Files content converted with variables.');
|
||||
|
||||
// Create `.gitkeep` in empty folders (to keep them in brick generation)
|
||||
|
@ -19,6 +19,7 @@ import 'dart:io';
|
||||
import 'package:brick_generator/core/logger.dart';
|
||||
import 'package:brick_generator/core/shell.dart';
|
||||
import 'package:brick_generator/models/brick_config.dart';
|
||||
import 'package:brick_generator/models/brick_variable_boolean.dart';
|
||||
import 'package:brick_generator/models/brick_variable_string.dart';
|
||||
import 'package:brick_generator/models/ignore_list.dart';
|
||||
import 'package:path/path.dart';
|
||||
@ -262,6 +263,113 @@ abstract class FileSystem {
|
||||
});
|
||||
}
|
||||
|
||||
static void convertBooleanVariablesInFolder(
|
||||
BrickConfig config,
|
||||
String targetPath,
|
||||
) {
|
||||
if (!FileSystemEntity.isDirectorySync(targetPath)) {
|
||||
throw ArgumentError('Target must be a directory', 'targetPath');
|
||||
}
|
||||
|
||||
Directory(targetPath)
|
||||
.listSync(recursive: true)
|
||||
.whereType<File>()
|
||||
.forEach((file) {
|
||||
String? contents;
|
||||
|
||||
try {
|
||||
contents = file.readAsStringSync();
|
||||
} catch (e) {
|
||||
// Ignore decode error
|
||||
return;
|
||||
}
|
||||
|
||||
/// Defines differents prefixes used in comments in differents languages,
|
||||
/// such as Dart, Yaml or Python for example.
|
||||
final prefixes = [r'\/\/\/', '###'];
|
||||
|
||||
config.variables.whereType<BrickVariableBoolean>().forEach((variable) {
|
||||
for (final prefix in prefixes) {
|
||||
final trueGroups = RegExp(
|
||||
'^' +
|
||||
prefix +
|
||||
r'\s*{{#(.*)}}\n((?:.*\n)*?)' +
|
||||
prefix +
|
||||
r'\s*{{\/(.*)}}',
|
||||
multiLine: true,
|
||||
);
|
||||
final falseGroups = RegExp(
|
||||
'^' +
|
||||
prefix +
|
||||
r'\s*{{\^(.*)}}\n((?:.*\n)*?)' +
|
||||
prefix +
|
||||
r'\s*{{\/(.*)}}',
|
||||
multiLine: true,
|
||||
);
|
||||
|
||||
final trueContents = contents?.replaceAllMapped(trueGroups, (match) {
|
||||
if (match.group(1) == variable.name) {
|
||||
// Match a bloc define by the variable
|
||||
// Clean bloc content from his potential prefix
|
||||
final cleaned = match.group(2)?.splitMapJoin(
|
||||
'\n',
|
||||
onMatch: (m) => '${m[0]}',
|
||||
onNonMatch: (str) => str.replaceFirst(prefix, ''),
|
||||
) ??
|
||||
'';
|
||||
|
||||
Logger.debug(
|
||||
'Boolean section `${variable.name}` (true) '
|
||||
'converted in ${file.path} (match: $trueGroups)',
|
||||
);
|
||||
|
||||
return '''
|
||||
{{#${variable.name}}}
|
||||
$cleaned
|
||||
{{/${variable.name}}}
|
||||
''';
|
||||
} else {
|
||||
return match[0] ?? '';
|
||||
}
|
||||
});
|
||||
|
||||
contents = trueContents;
|
||||
|
||||
final falseContents =
|
||||
contents?.replaceAllMapped(falseGroups, (match) {
|
||||
if (match.group(1) == variable.name) {
|
||||
// Match a bloc define by the variable
|
||||
// Clean bloc content from his potential prefix
|
||||
final cleaned = match.group(2)?.splitMapJoin(
|
||||
'\n',
|
||||
onMatch: (m) => '${m[0]}',
|
||||
onNonMatch: (str) => str.replaceFirst(prefix, ''),
|
||||
) ??
|
||||
'';
|
||||
|
||||
Logger.debug(
|
||||
'Boolean section `${variable.name}` (false) '
|
||||
'converted in ${file.path} (match: $falseGroups)',
|
||||
);
|
||||
|
||||
return '''
|
||||
{{^${variable.name}}}
|
||||
$cleaned
|
||||
{{/${variable.name}}}
|
||||
''';
|
||||
} else {
|
||||
return match[0] ?? '';
|
||||
}
|
||||
});
|
||||
|
||||
contents = falseContents;
|
||||
}
|
||||
});
|
||||
|
||||
file.writeAsStringSync(contents!);
|
||||
});
|
||||
}
|
||||
|
||||
static void createGitKeepInEmptyFolders(String targetPath) {
|
||||
if (!FileSystemEntity.isDirectorySync(targetPath)) {
|
||||
throw ArgumentError('Target must be a directory', 'targetPath');
|
||||
|
Loading…
x
Reference in New Issue
Block a user