updatePubspec function

Future<Version> updatePubspec(
  1. Level level
)

Update the working directory's pubspec by the given level.

Implementation

Future<Version> updatePubspec(Level level) async {
  var pubspec;
  var lines;
  try {
    pubspec = await File('pubspec.yaml');
    lines = await pubspec.readAsLines();
  } catch (e) {
    die("Error: Couldn't find pubspec.yaml. Run pubump from your package root.");
  }
  final regExp = RegExp(r'(version:\s+)(\d+)\.(\d+)\.(\d+)(?:\+(\d+))?$');
  List<String> updatedLines = [];
  Version version;

  for (final line in lines) {
    final match = regExp.firstMatch(line);
    if (match == null) {
      updatedLines.add(line);
    } else {
      final prefix = match.group(1);
      final major = int.parse(match.group(2));
      final minor = int.parse(match.group(3));
      final patch = int.parse(match.group(4));
      final build = int.parse(match.group(5) ?? '0');
      final oldVersion = Version(major, minor, patch, build);
      version = oldVersion.bump(level);
      updatedLines.add(prefix + version.toString());
    }
  }

  await pubspec.writeAsString(updatedLines.join('\n') + '\n');
  return version;
}