generate method

Future<List<GeneratedFile>> generate(
  1. GeneratorTarget target, {
  2. Map<String, dynamic> vars = const <String, dynamic>{},
  3. FileConflictResolution? fileConflictResolution,
  4. Logger? logger,
})
inherited

Generates files based on the provided GeneratorTarget and vars. Returns a list of GeneratedFile.

Implementation

Future<List<GeneratedFile>> generate(
  GeneratorTarget target, {
  Map<String, dynamic> vars = const <String, dynamic>{},
  FileConflictResolution? fileConflictResolution,
  Logger? logger,
}) async {
  final overwriteRule = fileConflictResolution?.toOverwriteRule();
  final generatedFiles = <GeneratedFile>[];
  await Future.forEach<TemplateFile>(files, (TemplateFile file) async {
    final fileMatch = _fileRegExp.firstMatch(file.path);
    if (fileMatch != null) {
      final resultFile = await _fetch(vars[fileMatch[1]] as String);
      if (resultFile.path.isEmpty) return;
      final generatedFile = await target.createFile(
        p.basename(resultFile.path),
        resultFile.content,
        logger: logger,
        overwriteRule: overwriteRule,
      );
      generatedFiles.add(generatedFile);
    } else {
      final resultFiles = await _runSubstitutionAsync(
        file,
        Map<String, dynamic>.of(vars),
        Map<String, List<int>>.of(partials),
      );
      final root = RegExp(r'\w:\\|\w:\/');
      final separator = RegExp(r'\/|\\');
      final rootOrSeparator = RegExp('$root|$separator');
      final wasRoot = file.path.startsWith(rootOrSeparator);
      for (final file in resultFiles) {
        final isRoot = file.path.startsWith(rootOrSeparator);
        if (!wasRoot && isRoot) continue;
        if (file.path.isEmpty) continue;
        if (file.path.split(separator).contains('')) continue;
        final generatedFile = await target.createFile(
          file.path,
          file.content,
          logger: logger,
          overwriteRule: overwriteRule,
        );
        generatedFiles.add(generatedFile);
      }
    }
  });
  return generatedFiles;
}