generateFromTemplates function

Future<void> generateFromTemplates({
  1. String? fallbackDartSdkPath,
  2. required Set<String> rootDirPaths,
  3. Set<String> subDirPaths = const {},
  4. required Future<void> generateForFile(
    1. AnalysisContextCollection collection,
    2. String filePath,
    3. Map<String, String> templates,
    4. String? output,
    ),
  5. required Set<String> templateFilePaths,
  6. String begType = '',
  7. Set<String> pathPatterns = const {},
  8. bool deleteGeneratedFiles = false,
  9. void onDelete(
    1. String filePath
    )?,
  10. String? output,
})

A helper for generating code from templates.

  • fallbackDartSdkPath is used if the DART_SDK environment variable is not automatically detected.
  • rootDirPaths is a set of paths to search for Dart files.
  • subDirPaths is a subset of paths to search for Dart files.
  • generateForFile is a function that is called for each Dart file found.
  • templateFilePaths is a set of paths to Markdown files that contain templates used by generateForFile.
  • begType is a string that is used to filter the Dart files found.
  • pathPatterns is a set of patterns used to filter the Dart files found.
  • deleteGeneratedFiles can be set to true to delete generated files.
  • onDelete is a callback that is called when a file is deleted.
  • output is 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,
      );
    }
  }
}