substituteVariables function
Replaces ${VAR} tokens in script using variables first, then
Platform.environment as fallback. Unknown variables are left unchanged.
Implementation
String substituteVariables(String script, Map<String, String> variables) {
return script.replaceAllMapped(RegExp(r'\$\{(\w+)\}'), (match) {
final name = match.group(1)!;
final defined = variables[name];
if (defined != null) {
// (variables) values are spliced as text, so refuse to run when one
// carries a shell-active character (e.g. `$(...)` or `;`).
assertShellInert(defined, 'variable \${$name}');
return defined;
}
return Platform.environment[name] ?? match.group(0)!;
});
}