load static method

NodeProfile load({
  1. String? path,
  2. String? home,
  3. Map<String, String>? environment,
})

Loads the profile from path (defaults to defaultPath).

A missing file yields empty. Throws FormatException when the file is not valid YAML or env is present but not a mapping. environment overrides the variables used for ${VAR} expansion (tests).

Implementation

static NodeProfile load({
  String? path,
  String? home,
  Map<String, String>? environment,
}) {
  final file = File(path ?? defaultPath(home: home));
  if (!file.existsSync()) return empty;

  final text = file.readAsStringSync();
  if (text.trim().isEmpty) return empty;

  final Object? doc;
  try {
    doc = loadYaml(text);
  } on YamlException catch (e) {
    throw FormatException('Invalid YAML in ${file.path}: ${e.message}');
  }
  if (doc == null) return empty;
  if (doc is! Map) {
    throw FormatException('Profile ${file.path} must be a YAML mapping');
  }

  final rawEnv = doc['env'];
  if (rawEnv == null) return empty;
  if (rawEnv is! Map) {
    throw FormatException('"env" in ${file.path} must be a mapping');
  }

  final base = environment ?? Platform.environment;
  final env = <String, String>{};
  for (final entry in rawEnv.entries) {
    final key = '${entry.key}';
    final value = entry.value == null ? '' : '${entry.value}';
    env[key] = _expand(value, base);
  }
  return NodeProfile(env);
}