process method

Future<String> process(
  1. String? input
)

Processes and substitutes variables in a string with their corresponding values.

This function replaces placeholders in the format ${{VAR_NAME}} or ${VAR_NAME} with the values of the corresponding variables from the variables map.

Parameters:

  • input - The input string containing placeholders (can be null)

Returns the string with placeholders replaced by their corresponding values. If input is null, returns an empty string.

Implementation

Future<String> process(String? input) async {
  if (input == null) return "";

  input = await substituteCLIArguments(input);

  final pattern = RegExp(r'\$\{\{(\w+)\}\}|\$\{(\w+)\}');

  // Two passes: collect the names first so the built-ins - which may have to
  // spawn `git` - can be awaited before the synchronous replacement runs.
  final builtins = <String, String>{};
  for (final match in pattern.allMatches(input)) {
    final name = (match.group(1) ?? match.group(2))?.trim();
    if (name == null) continue;
    if (variables.containsKey(name)) continue;
    if (builtins.containsKey(name)) continue;
    final value = await BuiltinVariables.resolve(name);
    if (value != null) builtins[name] = value;
  }

  input = input.replaceAllMapped(pattern, (match) {
    final varName = (match.group(1) ?? match.group(2))?.trim();
    // Explicit variables win over the built-ins so any of them can be pinned.
    final value = variables[varName] ?? builtins[varName];

    if (value != null) {
      return value.toString();
    } else {
      return match.group(0)!;
    }
  });
  return input;
}