applyConstraintsTo method

String? applyConstraintsTo(
  1. String content, {
  2. Iterable<String> additionalKeys = const [],
  3. VersionBump bump = VersionBump.breaking,
  4. Iterable<String> packages = const [],
  5. bool pin = false,
})

Implementation

String? applyConstraintsTo(
  String content, {
  Iterable<String> additionalKeys = const [],
  VersionBump bump = VersionBump.breaking,
  Iterable<String> packages = const [],
  bool pin = false,
}) {
  final yaml = YamlEditor(content);

  final dependencies = [
    'dependencies',
    ...additionalKeys,
  ];

  var changesMade = false;

  final uniquePackages = packages.toSet();

  for (final key in dependencies) {
    logger.detail('Constraining versions for $key');
    if (yaml[key] case final YamlMap deps) {
      for (final MapEntry(key: name, value: version) in deps.entries) {
        if (uniquePackages.isNotEmpty && !uniquePackages.contains(name)) {
          continue;
        }

        final depConstraint = constraint(name, version, bump: bump, pin: pin);

        if (depConstraint == null) continue;
        if (depConstraint.version == version) continue;

        changesMade = true;

        yaml.update([key, depConstraint.name], depConstraint.version);
        logger.delayed('  - $name: $version -> ${depConstraint.version}');
      }
    }
  }

  if (!changesMade) {
    return null;
  }

  return yaml.toString();
}