fix: correct path on copy project

This commit is contained in:
Hugo Pointcheval 2023-01-26 22:37:34 +01:00
parent 79ae49ed57
commit 43b3df453f
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
5 changed files with 86 additions and 56 deletions

View File

@ -65,7 +65,7 @@ apps
Then run command with project path Then run command with project path
```sh ```sh
dart ./tools/brick_generator/bin/brickgen.dart ./apps/brick_name dart ./tools/brick_generator/bin/brickgen.dart ./apps/brick_name ./bricks/
``` ```
Available options: Available options:
@ -318,7 +318,7 @@ brickgen:
``` ```
```sh ```sh
dart ./tools/brick_generator/bin/brickgen.dart ./apps/brick_1 dart ./tools/brick_generator/bin/brickgen.dart ./apps/brick_1 ./bricks/
``` ```
Will generate: Will generate:
@ -360,7 +360,7 @@ brickgen:
``` ```
```sh ```sh
dart ./tools/brick_generator/bin/brickgen.dart ./apps/brick_2 dart ./tools/brick_generator/bin/brickgen.dart ./apps/brick_2 ./bricks/
``` ```
Will generate: Will generate:

View File

@ -89,8 +89,10 @@ class Brickgen {
Logger.debug('Define `targetPath`: $targetPath'); Logger.debug('Define `targetPath`: $targetPath');
// Check paths // Check paths
if (toBrickifyPath == null || targetPath == null if (toBrickifyPath == null ||
|| masonConfigPath == null || hooksOutputPath == null) { targetPath == null ||
masonConfigPath == null ||
hooksOutputPath == null) {
throw Exception('An error occures during path definition.'); throw Exception('An error occures during path definition.');
} }
Logger.info('Paths defined.'); Logger.info('Paths defined.');
@ -126,6 +128,8 @@ class Brickgen {
FileSystem.renameCompilableFilesInFolder(config, targetPath!); FileSystem.renameCompilableFilesInFolder(config, targetPath!);
Logger.info('Files renamed with variables.'); Logger.info('Files renamed with variables.');
// Clean empty folders created from copy/moves
FileSystem.removeEmptyFolders(targetPath!);
// Create Mason config // Create Mason config
FileSystem.writeFile(masonConfigPath!, config.toMason()); FileSystem.writeFile(masonConfigPath!, config.toMason());

View File

@ -53,7 +53,18 @@ abstract class FileSystem {
throw ArgumentError('Path must be a directory', 'from'); throw ArgumentError('Path must be a directory', 'from');
} }
final List<String> copiedFolders = []; Future<void> cp(FileSystemEntity entity) async {
final absolutPath = entity.path;
final relativePath = absolutPath.replaceFirst(from, '');
final newPath = ((to + relativePath).split('/')..removeLast()).join('/');
if (ignoreList != null && ignoreList.contains(from, absolutPath)) {
Logger.debug('Ignoring $absolutPath');
} else {
Logger.debug('cp -Rf $absolutPath $newPath');
await Shell.cp(absolutPath, newPath);
}
}
// Copy folders // Copy folders
await Future.wait<void>( await Future.wait<void>(
@ -61,38 +72,14 @@ abstract class FileSystem {
.listSync(recursive: true) .listSync(recursive: true)
.whereType<Directory>() .whereType<Directory>()
.map((directory) async { .map((directory) async {
final absolutPath = directory.path; await cp(directory);
final name = absolutPath.split('/').last;
if (ignoreList != null && ignoreList.contains(from, absolutPath)) {
Logger.debug('Ignoring $absolutPath');
} else {
Logger.debug('cp -Rf $absolutPath $to/$name');
copiedFolders.add(absolutPath);
await Shell.cp(absolutPath, '$to/$name');
}
}), }),
); );
// Copy files // Copy root files
await Future.wait<void>( await Future.wait<void>(
Directory(from) Directory(from).listSync().whereType<File>().map((file) async {
.listSync(recursive: true) await cp(file);
.whereType<File>()
.map((file) async {
final absolutPath = file.path;
final name = absolutPath.split('/').last;
if (ignoreList != null && ignoreList.contains(from, absolutPath)) {
Logger.debug('Ignoring $absolutPath');
} else {
if (copiedFolders.any(absolutPath.startsWith)) {
Logger.debug('$absolutPath already copied!');
} else {
Logger.debug('cp -Rf $absolutPath $to/$name');
await Shell.cp(absolutPath, '$to/$name');
}
}
}), }),
); );
} }
@ -259,13 +246,17 @@ abstract class FileSystem {
for (final syntax in VariableStringSyntax.values) { for (final syntax in VariableStringSyntax.values) {
final toReplace = variable.syntax?[syntax.mapKey]; final toReplace = variable.syntax?[syntax.mapKey];
if (toReplace != null) { if (toReplace != null) {
contents = contents!.replaceAll( if (contents?.contains(toReplace) ?? false) {
toReplace, contents = contents!.replaceAll(
'{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}', toReplace,
); '{{#${syntax.id}}}{{${variable.name}}}{{/${syntax.id}}}',
);
Logger.debug(
'Variable `${variable.name}` replaced in ${file.path}',
);
}
} }
} }
Logger.debug('Variable `${variable.name}` replaced in ${file.path}');
}); });
file.writeAsStringSync(contents!); file.writeAsStringSync(contents!);
@ -289,21 +280,43 @@ abstract class FileSystem {
}); });
} }
/// Removes empty folder dynamically:
///
/// For example, for this structure:
/// ```
/// ./brick/__brick__/folder/empty1/
/// ./brick/__brick__/folder/empty2/
/// ./brick/__brick__/dummy.txt
/// ```
/// 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) { static void removeEmptyFolders(String targetPath) {
if (!FileSystemEntity.isDirectorySync(targetPath)) { if (!FileSystemEntity.isDirectorySync(targetPath)) {
throw ArgumentError('Target must be a directory', 'targetPath'); throw ArgumentError('Target must be a directory', 'targetPath');
} }
Directory(targetPath) bool containsEmptyFoldersFunction() => Directory(targetPath)
.listSync(recursive: true) .listSync(recursive: true)
.whereType<Directory>() .whereType<Directory>()
.forEach((directory) { .any(
if (directory.existsSync() && (directory) =>
directory.listSync(recursive: true).isEmpty) { directory.existsSync() &&
directory.deleteSync(recursive: true); directory.listSync(recursive: true).isEmpty,
Logger.debug('${directory.path} (empty) deleted.'); );
}
}); while (containsEmptyFoldersFunction()) {
Directory(targetPath)
.listSync(recursive: true)
.whereType<Directory>()
.forEach((directory) {
if (directory.existsSync() &&
directory.listSync(recursive: true).isEmpty) {
directory.deleteSync(recursive: true);
Logger.debug('${directory.path} (empty) deleted.');
}
});
}
} }
/// Transforms compilable file/folder names: /// Transforms compilable file/folder names:

View File

@ -34,12 +34,20 @@ class BooleanFileSystem {
} }
final variableName = key; final variableName = key;
final folders = BooleanFileSystemVariable.fromYaml(
source.nodes[_foldersKey] as YamlMap?, final foldersMap = source.nodes[_foldersKey] as YamlMap?;
); final folders = (foldersMap != null)
final files = BooleanFileSystemVariable.fromYaml( ? BooleanFileSystemVariable.fromYaml(
source.nodes[_filesKey] as YamlMap?, source.nodes[_foldersKey] as YamlMap?,
); )
: BooleanFileSystemVariable.empty();
final filesMap = source.nodes[_filesKey] as YamlMap?;
final files = (filesMap != null)
? BooleanFileSystemVariable.fromYaml(
source.nodes[_filesKey] as YamlMap?,
)
: BooleanFileSystemVariable.empty();
return BooleanFileSystem( return BooleanFileSystem(
booleanName: variableName, booleanName: variableName,

View File

@ -43,6 +43,11 @@ class BooleanFileSystemVariable {
); );
} }
factory BooleanFileSystemVariable.empty() => BooleanFileSystemVariable(
onTrueNames: [],
onFalseNames: [],
);
final List<String> onTrueNames; final List<String> onTrueNames;
final List<String> onFalseNames; final List<String> onFalseNames;