incrementVersion static method

Future<void> incrementVersion()

Implementation

static Future<void> incrementVersion() async {
  final version = await appVersion;
  final parts = version.split('+');

  if (parts.length != 2) {
    throw Exception(
      'Invalid version "$version" in pubspec.yaml. '
      'Expected the format MAJOR.MINOR.PATCH+BUILD (e.g. 1.0.0+1).',
    );
  }

  final versionParts = parts[0].split('.');
  if (versionParts.length != 3) {
    throw Exception(
      'Invalid version "$version" in pubspec.yaml. '
      'Expected three numeric components MAJOR.MINOR.PATCH (e.g. 1.0.0+1).',
    );
  }

  final major = int.tryParse(versionParts[0]);
  final minor = int.tryParse(versionParts[1]);
  final patch = int.tryParse(versionParts[2]);
  final build = int.tryParse(parts[1]);

  if (major == null || minor == null || patch == null || build == null) {
    throw Exception(
      'Invalid version "$version" in pubspec.yaml. '
      'All of MAJOR, MINOR, PATCH and BUILD must be integers (e.g. 1.0.0+1).',
    );
  }

  final newVersion = '$major.$minor.${patch + 1}';
  final buildNumber = build + 1;

  await _writePubspec('$newVersion+$buildNumber');
  print('Incremented version to $newVersion, build number to $buildNumber');
}