writePath static method

void writePath(
  1. String path,
  2. String pathValue
)

Writes the PATH entry of path's profile to pathValue, preserving any other env entries and surrounding comments. Creates the file (and its parent directory) from a template when it does not yet exist.

Implementation

static void writePath(String path, String pathValue) {
  final file = File(path);
  final existing = file.existsSync() ? file.readAsStringSync() : '';

  if (existing.trim().isEmpty) {
    file.parent.createSync(recursive: true);
    file.writeAsStringSync(_template(pathValue));
    return;
  }

  final doc = loadYaml(existing);
  if (doc is Map && doc['env'] is Map) {
    final editor = YamlEditor(existing)..update(['env', 'PATH'], pathValue);
    file.writeAsStringSync(editor.toString());
  } else if (doc is Map) {
    final editor = YamlEditor(existing)..update(['env'], {'PATH': pathValue});
    file.writeAsStringSync(editor.toString());
  } else {
    // Root is not a mapping (unexpected); replace with a fresh template.
    file.writeAsStringSync(_template(pathValue));
  }
}