137 lines
4.4 KiB
Dart
137 lines
4.4 KiB
Dart
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
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';
|
|
import 'package:brick_generator/shell.dart';
|
|
import 'package:path/path.dart';
|
|
|
|
class FileSystemUtils {
|
|
static Future<void> convertValuesToVariablesInFolder(
|
|
BrickConfig brickConfig,
|
|
String path,
|
|
) async {
|
|
await Future.wait(
|
|
Directory(path)
|
|
.listSync(recursive: true)
|
|
.whereType<File>()
|
|
.map((f) async {
|
|
var file = f;
|
|
|
|
try {
|
|
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.debug(
|
|
'Variable `${variable?.name}` replaced in ${file.path}',
|
|
);
|
|
}
|
|
}
|
|
|
|
// Replace content
|
|
file = await file.writeAsString(contents);
|
|
} catch (e) {
|
|
Logger.error(e);
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
|
|
static Future<void> renamePathsInFolder(
|
|
BrickConfig brickConfig,
|
|
String path,
|
|
) async {
|
|
await Future.wait(
|
|
Directory(path)
|
|
.listSync(recursive: true)
|
|
.whereType<File>()
|
|
.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);
|
|
Logger.debug(
|
|
'${file.path} renamed with variable `${variable.name}`',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
|
|
static Future<void> 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// For the moment ignored files are copied, then deleted...
|
|
static Future<void> deleteIgnoredFolders(
|
|
BrickConfig brickConfig,
|
|
String brickPath,
|
|
) async {
|
|
for (final String ignore in brickConfig.brickIgnore ?? []) {
|
|
final String toDelete = join(brickPath, ignore);
|
|
if (FileSystemEntity.isDirectorySync(toDelete)) {
|
|
await Shell.rm(toDelete, recursive: true);
|
|
} else {
|
|
await Shell.rm(toDelete);
|
|
}
|
|
Logger.debug('$toDelete ignored.');
|
|
}
|
|
}
|
|
}
|