dvStudioPubspecWithPlatforms function

String dvStudioPubspecWithPlatforms(
  1. String pubspec,
  2. Map<String, Object?> document
)

pubspec with dartvel.platforms.web.document set to document, every other line and comment kept. An empty document removes the setting. The same setting at its older home, dartvel.web.document, is removed, so the two homes can never disagree.

Throws DVStudioPlatformsRefusal for a project whose dartvel: names a Dart config file: the setting is a field of that class, and Studio does not rewrite Dart.

Implementation

String dvStudioPubspecWithPlatforms(String pubspec, Map<String, Object?> document) {
  final Object? root = loadYaml(pubspec);
  if (root is! Map) {
    throw DVStudioPlatformsRefusal(409, 'bad_pubspec', 'pubspec.yaml is not a map, so Studio cannot change it.');
  }
  final Object? dartvel = root['dartvel'];
  if (dartvel is String) {
    throw DVStudioPlatformsRefusal(
      409,
      'dart_config',
      "This project's settings are written in Dart, in $dartvel. Set the "
          '`platforms` field of its config class there; Studio shows it but '
          'does not rewrite Dart.',
    );
  }
  final bool empty = dvStudioDocumentIsEmpty(document);
  final YamlEditor editor = YamlEditor(pubspec);
  final Map<String, Object?> value = <String, Object?>{
    for (final MapEntry<String, Object?> slot in document.entries)
      if (slot.value is List && (slot.value! as List).isNotEmpty) slot.key: slot.value,
  };
  if (dartvel == null) {
    if (empty) return pubspec;
    editor.update(<Object>['dartvel'], <String, Object?>{
      'platforms': <String, Object?>{
        'web': <String, Object?>{'document': value},
      },
    });
    return editor.toString();
  }
  if (dartvel is! Map) {
    throw DVStudioPlatformsRefusal(409, 'bad_pubspec', 'dartvel: in pubspec.yaml is not a map.');
  }

  final Object? oldWeb = dartvel['web'];
  if (oldWeb is Map && oldWeb.containsKey('document')) {
    editor.remove(<Object>['dartvel', 'web', 'document']);
    if (oldWeb.length == 1) editor.remove(<Object>['dartvel', 'web']);
  }

  final Object? platforms = dartvel['platforms'];
  final Object? web = platforms is Map ? platforms['web'] : null;
  if (empty) {
    if (web is Map && web.containsKey('document')) {
      if (web.length > 1) {
        editor.remove(<Object>['dartvel', 'platforms', 'web', 'document']);
      } else if ((platforms! as Map).length > 1) {
        editor.remove(<Object>['dartvel', 'platforms', 'web']);
      } else {
        editor.remove(<Object>['dartvel', 'platforms']);
      }
    }
  } else if (platforms is List) {
    // The older list of targets becomes the map: each target a key, so the
    // project still says it builds for every one of them.
    editor.update(<Object>['dartvel', 'platforms'], <String, Object?>{
      for (final Object? name in platforms) '$name': '$name' == 'web' ? <String, Object?>{'document': value} : <String, Object?>{},
      if (!platforms.map((Object? name) => '$name').contains('web')) 'web': <String, Object?>{'document': value},
    });
  } else if (platforms is! Map) {
    editor.update(<Object>['dartvel', 'platforms'], <String, Object?>{
      'web': <String, Object?>{'document': value},
    });
  } else if (web is! Map) {
    editor.update(<Object>['dartvel', 'platforms', 'web'], <String, Object?>{'document': value});
  } else {
    editor.update(<Object>['dartvel', 'platforms', 'web', 'document'], value);
  }
  return editor.toString();
}