// Copyright (C) 2023 WYATT GROUP // Please see the AUTHORS file for details. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . import 'dart:io'; import 'package:brick_generator/logger.dart'; import 'package:brick_generator/models/brick_config.dart'; import 'package:brick_generator/models/variable_string_syntax.dart'; import 'package:brick_generator/models/variable_type.dart'; class FileSystemUtils { static Future convertValuesToVariablesInFolder( BrickConfig brickConfig, String path, ) async { await Future.wait( Directory(path) .listSync(recursive: true) .whereType() .map((_) async { var file = _; var contents = await file.readAsString(); // Transform all values in variables if (brickConfig.variables != null) { for (final variable in brickConfig.variables!) { // Replace all string variables if (variable?.type == VariableType.string) { for (final syntax in VariableStringSyntax.values) { final toReplace = variable?.syntax?[syntax.mapKey]; if (toReplace != null) { contents = contents.replaceAll( toReplace, '{{#${syntax.id}}}{{${variable?.name}}}{{/${syntax.id}}}', ); } } } Logger.info('Variable ${variable?.name} added in ${file.path}'); } } // Replace content file = await file.writeAsString(contents); }), ); } static Future renamePathsInFolder( BrickConfig brickConfig, String path, ) async { await Future.wait( Directory(path) .listSync(recursive: true) .whereType() .map((_) async { final file = _; // Rename file if needed if (brickConfig.variables != null) { for (final variable in brickConfig.variables!) { if (variable?.type == VariableType.string && variable?.syntax?[VariableStringSyntax.snakeCase.mapKey] != null) { final snake = variable!.syntax![VariableStringSyntax.snakeCase.mapKey]; if (snake == null) { final err = 'Invalid snake_case syntax'; Logger.throwError(ArgumentError(err), err); } final newPath = file.path.replaceAll( snake!, '{{${variable.name}.snakeCase()}}', ); await File(newPath).create(recursive: true); await file.rename(newPath); } } } }), ); } static Future deleteEmptyFolders(String path) async { for (final dir in Directory(path).listSync(recursive: true).reversed) { if (dir is Directory) { if (dir.existsSync() && await dir.list().isEmpty) { await dir.delete(recursive: true); } } } } }