runSubstitution method

Set<FileContents> runSubstitution(
  1. Map<String, dynamic> parameters,
  2. Map<String, List<int>> partials
)

Performs a substitution on the path based on the incoming parameters.

Implementation

Set<FileContents> runSubstitution(
  Map<String, dynamic> parameters,
  Map<String, List<int>> partials,
) {
  var filePath = path.replaceAll(r'\', '/');
  if (_loopRegExp().hasMatch(filePath)) {
    final matches = _loopKeyRegExp.allMatches(filePath);

    for (final match in matches) {
      final key = match.group(1);
      if (key == null || _lambdas.hasMatch(key)) continue;
      if (parameters[key] is! Iterable) continue;
      final value = _loopValueRegExp(key).firstMatch(filePath)![1];
      if (value == '.') {
        filePath = filePath.replaceFirst(_loopRegExp(key), '{{$key}}');
      } else {
        final inner = _loopInnerRegExp(key).firstMatch(filePath)![1];
        final target = inner!.replaceFirst(
          _loopValueReplaceRegExp,
          '{{$key.$value}}',
        );
        filePath = filePath.replaceFirst(_loopRegExp(key), target);
      }
    }

    final fileContents = <FileContents>{};
    final parameterKeys =
        parameters.keys.where((key) => parameters[key] is List).toList();
    final permutations = _Permutations<dynamic>(
      [
        ...parameters.entries
            .where((entry) => entry.value is List)
            .map((entry) => entry.value as List),
      ],
    ).generate();
    for (final permutation in permutations) {
      final param = Map<String, dynamic>.of(parameters);
      for (var i = 0; i < permutation.length; i++) {
        param.addAll(<String, dynamic>{parameterKeys[i]: permutation[i]});
      }
      final newPath = filePath.render(param);
      final newContents = TemplateFile(
        newPath,
        utf8.decode(content),
      )._createContent(parameters..addAll(param), partials);
      fileContents.add(FileContents(newPath, newContents));
    }

    return fileContents;
  } else {
    final newPath = filePath.render(parameters);
    final newContents = _createContent(parameters, partials);
    return {FileContents(newPath, newContents)};
  }
}