expandEnvironment function

String expandEnvironment(
  1. String value,
  2. Map<String, String> environment
)

Replaces ${NAME} with the value of the environment variable NAME.

Implementation

String expandEnvironment(String value, Map<String, String> environment) =>
    value.replaceAllMapped(RegExp(r'\$\{(\w+)\}'), (match) {
      final name = match[1]!;
      final resolved = environment[name];
      if (resolved == null) {
        throw StateError('Environment variable $name is not set.');
      }
      return resolved;
    });