generateCode function
Implementation
void generateCode(Settings settings) {
final fileSystem = LocalFileSystem();
final assets = <String>[];
final buildGroup = settings.format.fold(single: () {
return BuildSingleGroupNode(
fileSystem: LocalFileSystem(),
options: settings,
);
}, multi: () {
return BuildMultiGroupNode(
fileSystem: LocalFileSystem(),
options: settings,
);
}, tree: () {
return BuildTreeGroupNode(
fileSystem: LocalFileSystem(),
options: settings,
);
});
final buildClasses = BuildGroupClasses(
settings: settings,
);
for (final groupSettings in settings.groups) {
final outputFile = fileSystem.file(groupSettings.resolveOutputFile(settings));
final filePaths = groupSettings.include.expand((glob) {
return Glob(pt.posix.join(groupSettings.inputDir, glob)).listFileSystemSync(fileSystem);
}).where((e) {
return e.statSync().type == FileSystemEntityType.file && !e.basename.startsWith('.');
}).map((file) {
return file.path.substring(groupSettings.inputDir.length + 3); // ./<PATH>/
}).toList();
assets.addAll(filePaths.map((path) => Utils.encodePosixPath(groupSettings.inputDir, path)));
print(assets);
if (filePaths.isEmpty) {
if (outputFile.existsSync()) outputFile.deleteSync();
continue;
}
final roots = buildGroup(groupSettings, filePaths);
final classes = roots.expand((root) => buildClasses(groupSettings, root));
final library = Library((b) => b..body.addAll(classes));
final emitter = DartEmitter(useNullSafetySyntax: true);
final formatter = DartFormatter(pageWidth: settings.pageWidth);
final rawCode = '// GENERATED CODE - DO NOT MODIFY BY HAND\n\n'
'// ignore_for_file: library_private_types_in_public_api\n\n'
'${library.accept(emitter)}';
final code = formatter.format(rawCode);
outputFile.createSync(recursive: true);
outputFile.writeAsStringSync(code);
}
final pubspecFile = fileSystem.file('pubspec.yaml');
if (pubspecFile.existsSync()) {
final pubspecContent = pubspecFile.readAsStringSync();
try {
final content = Utils.writeInTags(pubspecContent, 'mek_assets', () {
return assets.map((e) => ' - $e\n').join();
});
if (content != null) {
pubspecFile.writeAsStringSync(content);
} else {
lg.w('Not used code generation tags in pubspec.yaml');
}
} on InvalidTagsException {
lg.e('Invalid code generation tags in pubspec.yaml');
exit(-1);
}
} else {
lg.w('Not exist pubspec.yaml file');
}
lg.i('Completed!');
}