doRun method

  1. @override
Future<int> doRun()
override

Implementation

@override
Future<int> doRun() async {
  final args = argResults!;
  final rest = args.rest;

  if (rest.isEmpty) {
    return error(1,
        message: 'A part of the version to increment is required. '
            'Allowed values: ${VersionIncrement.namesList}. '
            'For example: alex pubspec version ${VersionIncrement.minor.name}.');
  }

  if (rest.length > 1) {
    return error(1,
        message: 'Only one part of the version can be incremented, '
            'but got: ${rest.join(', ')}.');
  }

  // Throws a RunException with the allowed values if the part is unknown.
  final increment = VersionIncrement.byName(rest.first);
  final incrementBuild = args[_argBuild] as bool? ?? false;

  const fs = IOFileSystem();

  if (!await Spec.exists(fs)) {
    return error(1,
        message: 'There is no ${Spec.fileName} in the current directory. '
            'Run the command in a directory of a project.');
  }

  final spec = await Spec.pub(fs);

  final version = spec.versionOrNull;
  if (version == null) {
    return error(1,
        message: 'There is no version in ${Spec.fileName}. '
            'Add a line like "version: 1.0.0+1" in the file.');
  }

  final newVersion = increment.apply(version, incrementBuild: incrementBuild);
  printVerbose(
      'Increment ${increment.name} version: $version -> $newVersion');

  final content = await fs.readString(Spec.fileName);
  await fs.writeString(
      Spec.fileName, Spec.replaceVersion(content, newVersion));

  return success(message: 'Version updated: $version -> $newVersion');
}