generateFromTemplates function
Future<void>
generateFromTemplates({
- String? fallbackDartSdkPath,
- required Set<
String> rootDirPaths, - Set<
String> subDirPaths = const {}, - required Future<
void> generateForFile(), - required Set<
String> templateFilePaths, - String begType = '',
- Set<
String> pathPatterns = const {}, - bool deleteGeneratedFiles = false,
- void onDelete(
- String filePath
- String? output,
A helper for generating code from templates.
fallbackDartSdkPathis used if theDART_SDKenvironment variable is not automatically detected.rootDirPathsis a set of paths to search for Dart files.subDirPathsis a subset of paths to search for Dart files.generateForFileis a function that is called for each Dart file found.templateFilePathsis a set of paths to Markdown files that contain templates used bygenerateForFile.begTypeis a string that is used to filter the Dart files found.pathPatternsis a set of patterns used to filter the Dart files found.deleteGeneratedFilescan be set totrueto delete generated files.onDeleteis a callback that is called when a file is deleted.outputis the output directory path.
Implementation
Future<void> generateFromTemplates({
String? fallbackDartSdkPath,
required Set<String> rootDirPaths,
Set<String> subDirPaths = const {},
required Future<void> Function(
AnalysisContextCollection collection,
String filePath,
Map<String, String> templates,
String? output,
) generateForFile,
required Set<String> templateFilePaths,
String begType = '',
Set<String> pathPatterns = const {},
bool deleteGeneratedFiles = false,
void Function(String filePath)? onDelete,
String? output,
}) async {
final combinedPaths = combinePathSets([rootDirPaths, subDirPaths]);
final collection = createAnalysisContextCollection(
combinedPaths,
fallbackDartSdkPath,
);
if (deleteGeneratedFiles) {
for (final dirPath in combinedPaths) {
await deleteGeneratedDartFiles(
dirPath,
onDelete: onDelete,
pathPatterns: pathPatterns,
);
}
}
for (final path in combinedPaths) {
final templates = <String, String>{};
for (final templateFilePath in templateFilePaths) {
templates[templateFilePath] =
(await readSnippetsFromMarkdownFile(templateFilePath)).join('\n');
}
final results = await findFiles(
path,
extensions: {'.dart'},
pathPatterns: pathPatterns,
onFileFound: (dirName, folderName, filePath) async {
final a = isMatchingFileName(filePath, begType, 'dart').status;
final b = isSourceDartFilePath(filePath);
if (a && b) {
return true;
}
return false;
},
);
for (final result in results) {
final filePath = result.filePath;
await generateForFile(
collection,
filePath,
templates,
output,
);
}
}
}