render static method

Map<String, String> render(
  1. Map<String, String> files, {
  2. required ProjectNames names,
  3. Map<String, String> values = const {},
  4. Map<String, List<String>> lines = const {},
})

files (path → text) with their placeholders filled for names, plus values and lines. A placeholder in lines is replaced, with the line break after it, by its lines, each ending in a line break (nothing at all for none). Throws ArgumentError for a title with a ProjectNames.titleProblem.

Implementation

static Map<String, String> render(
  Map<String, String> files, {
  required ProjectNames names,
  Map<String, String> values = const {},
  Map<String, List<String>> lines = const {},
}) {
  final titleProblem = ProjectNames.titleProblem(names.title);
  if (titleProblem != null) {
    throw ArgumentError.value(names.title, 'names.title', titleProblem);
  }
  final all = {...namesOf(names), ...values};
  String fill(String text) {
    // A block's lines end as the file's lines do.
    final fileEnding = text.contains('\r\n') ? '\r\n' : '\n';
    return text.replaceAllMapped(_placeholder, (m) {
      final key = m.group(1)!;
      final newline = m.group(2) ?? '';
      final block = lines[key];
      if (block != null) {
        final nl = newline.isEmpty ? fileEnding : newline;
        return block.isEmpty ? '' : '${block.join(nl)}$nl';
      }
      final value = all[key];
      return value == null ? m.group(0)! : '$value$newline';
    });
  }

  return {for (final e in files.entries) e.key: fill(e.value)};
}