replaceVariables method
Iterable<String>
replaceVariables(
- String command, {
- required Map<
String, String?> sipVariables, - required ScriptsConfig config,
- OptionalFlags? flags,
Implementation
Iterable<String> replaceVariables(
String command, {
required Map<String, String?> sipVariables,
required ScriptsConfig config,
OptionalFlags? flags,
}) sync* {
final matches = variablePattern.allMatches(command);
if (matches.isEmpty) {
yield command;
return;
}
Iterable<String> resolvedCommands = [command];
for (final match in matches) {
final variable = match.group(1);
if (variable == null) {
continue;
}
if (variable.startsWith(r'$')) {
final scriptPath = variable.substring(1).split(':');
final found = config.find(scriptPath);
if (found == null) {
throw Exception('Script path $variable is invalid');
}
final resolved = replace(found, config, flags: flags);
final commandsToCopy = [...resolvedCommands];
resolvedCommands =
List.generate(resolvedCommands.length * resolved.length, (index) {
final commandIndex = index % resolved.length;
final command = resolved[commandIndex];
final commandsToCopyIndex = index ~/ resolved.length;
final commandsToCopyCommand = commandsToCopy[commandsToCopyIndex];
return commandsToCopyCommand.replaceAll(match.group(0)!, command);
});
continue;
}
if (variable.startsWith('-')) {
// flags are optional, so if not found, replace with empty string
final flag = flags?[variable] ?? '';
resolvedCommands =
resolvedCommands.map((e) => e.replaceAll(match.group(0)!, flag));
continue;
}
final sipValue = sipVariables[variable];
if (sipValue == null) {
throw Exception('Variable $variable is not defined');
}
resolvedCommands =
resolvedCommands.map((e) => e.replaceAll(match.group(0)!, sipValue));
}
yield* resolvedCommands;
return;
}