Skip to content

Commit

Permalink
[#245] Clean up the output folder, even at the root, to have only the…
Browse files Browse the repository at this point in the history
… final generated project in the end
  • Loading branch information
luongvo committed Oct 11, 2023
1 parent 793666d commit 393c8a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bricks/template/hooks/post_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ import 'dart:io';
import 'package:mason/mason.dart';

import 'bundles/permission_handler_bundle.dart';
import 'pre_gen.dart';

Future<void> run(HookContext context) async {
try {
await generateBricks(context);
} catch (e) {
context.logger.err(e.toString());
}

// Clean up the output folder, even at the root, to have only the final generated project in the end
print("> Clean up the output folder (post): " + Directory.current.path);
final List<FileSystemEntity> entities = [
Directory(Directory.current.path + '/bricks'),
];
await deleteFileSystemEntities(entities);
}

Future<void> generateBricks(HookContext context) async {
Expand Down
29 changes: 29 additions & 0 deletions bricks/template/hooks/pre_gen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import 'dart:io';

import 'package:mason/mason.dart';

import 'bundles/permission_handler_bundle.dart';

Future<void> run(HookContext context) async {
// Clean up the output folder, even at the root, to have only the final generated project in the end
print("> Clean up the output folder (pre): " + Directory.current.path);
final entities = Directory.current.listSync(recursive: false);
final exclusions = [
// we should not delete .git
Directory.current.path + '/.git',
// the bricks folder can be delete at post_gen only
Directory.current.path + '/bricks',
];
await deleteFileSystemEntities(entities, exclusions: exclusions);

try {
final additionalVars = {};
if (context.vars['add_permission_handler'] == true) {
Expand Down Expand Up @@ -31,3 +44,19 @@ Future<Map<String, dynamic>> addPermissionHandlerVariables() async {
});
return vars;
}

Future<void> deleteFileSystemEntities(
List<FileSystemEntity> entities, {
List<String> exclusions = const [],
}) async {
entities.forEach((entity) {
if (!exclusions.contains(entity.path)) {
print('Delete ' + entity.path);
if (entity is File) {
entity.deleteSync();
} else if (entity is Directory) {
entity.deleteSync(recursive: true);
}
}
});
}

0 comments on commit 393c8a4

Please sign in to comment.