run method

  1. @override
Future<void> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<void> run() async {
  // Parse arguments
  final bool commit = argResults?['commit'] as bool? ?? false;
  final bumpType = argResults!.parseBumpType();

  final packageDirectory = Directory(
    argResults?.rest.firstOrNull ??
        mainProject?.root.path ??
        Directory.current.path,
  );
  final package = DartPackage.fromDirectory(packageDirectory);
  if (package == null) {
    error("No Dart package with pubspec.yaml found "
        "in ${packageDirectory.path}");
  }
  final pubspecFile = package.pubspec;

  // Read current version
  final Version? currentVersion = readPubspecVersion(pubspecFile);
  if (currentVersion == null) {
    error("Can't bump version because "
        "${pubspecFile.path} has no current version");
  }

  // Bump version
  final Version bumpedVersion = currentVersion.bumpVersion(bumpType);

  Future<void> applyModifications() async {
    for (final modification in _modifications) {
      await modification.call(package, version, bumpedVersion);
    }
  }

  bool bumped = false;
  if (commit) {
    final detachedHEAD = 'git symbolic-ref -q HEAD'
        .start(progress: Progress.printStdErr(), nothrow: true);
    if (detachedHEAD.exitCode != 0) {
      throw 'You are in "detached HEAD" state, can not commit version bump';
    } else {
      await commitFileModifications(
        applyModifications,
        commitMessage: 'Bump version to $bumpedVersion',
      );
      bumped = true;
    }
  }
  if (!bumped) {
    await applyModifications();
  }

  print(
    green(
      '${package.name} version bumped '
      'from $currentVersion to $bumpedVersion',
    ),
  );
}