digestCustomGeneratorPlaceholders static method

String digestCustomGeneratorPlaceholders(
  1. String input
)

If a custom generator is provided, replace variables with desired values Useful for hacking around const functions when duplicating logic

Implementation

static String digestCustomGeneratorPlaceholders(String input) {
  return input.replaceAllMapped(RegExp(r'%((?:[\w\d]+)+)%'), (placeholderMatch) {
    // Swap placeholders with values

    final placeholderName = placeholderMatch.group(1);
    final valueRegex = RegExp('@$placeholderName@([^@]+)@/$placeholderName@');
    if (placeholderName == null || !input.contains(valueRegex)) {
      throw InvalidGenerationSourceError('`$input` does not declare variable @$placeholderName@');
    }

    final valueMatch = valueRegex.firstMatch(input);
    if (valueMatch?.group(1) == null) {
      throw InvalidGenerationSourceError(
        '@$placeholderName@ requires a trailing value: @NAME@value@/NAME@',
      );
    }

    return valueMatch!.group(1)!;
  }).replaceAll(RegExp(r'@([\w\d]+)@.*@\/\1@'), ''); // Remove variable values
}