expandVariables static method
Expand environment variables in text.
Replaces $VAR and ${VAR} with values from env or the current
platform environment.
Implementation
static String expandVariables(String text, [Map<String, String>? env]) {
final environment = env ?? Platform.environment;
var result = text;
// ${VAR} form
result = result.replaceAllMapped(
RegExp(r'\$\{([^}]+)\}'),
(m) => environment[m.group(1)] ?? m.group(0)!,
);
// $VAR form (must not be followed by another word char)
result = result.replaceAllMapped(
RegExp(r'\$([A-Za-z_][A-Za-z0-9_]*)'),
(m) => environment[m.group(1)] ?? m.group(0)!,
);
return result;
}