From c47585a18ed3c7f1d4960a562050f9a9fde46a69 Mon Sep 17 00:00:00 2001 From: Hugo Pointcheval Date: Fri, 27 Jan 2023 15:09:33 +0100 Subject: [PATCH] feat: add boolean section support --- tools/brick_generator/bin/brickgen.dart | 1 + .../brick_generator/lib/core/file_system.dart | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/tools/brick_generator/bin/brickgen.dart b/tools/brick_generator/bin/brickgen.dart index f8e9432..babd186 100644 --- a/tools/brick_generator/bin/brickgen.dart +++ b/tools/brick_generator/bin/brickgen.dart @@ -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) diff --git a/tools/brick_generator/lib/core/file_system.dart b/tools/brick_generator/lib/core/file_system.dart index 8781fe1..f8288b9 100644 --- a/tools/brick_generator/lib/core/file_system.dart +++ b/tools/brick_generator/lib/core/file_system.dart @@ -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() + .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().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');