run method

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

Runs this command.

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

Implementation

@override
Future<int> run() async {
  final oldPackageRef = PackageRef(argResults![_optionNameOld]);
  final newPackageRef = PackageRef(argResults![_optionNameNew]);
  final outputFormatter = ReportFormat.values.firstWhere(
      (element) => element.name == argResults![_optionReportFormat]);
  final outputFile = argResults![_optionReportPath];

  if (outputFormatter != ReportFormat.cli && outputFile == null) {
    throw 'You need to define an output file using the $_optionReportPath parameter when not using the cli option';
  }

  if (outputFormatter == ReportFormat.cli && outputFile != null) {
    stdout.writeln(
        'WARNING: $_optionReportPath has no effect because $_optionReportFormat is set to cli');
  }

  final versionCheckMode = VersionCheckMode.values.firstWhere(
      (element) => element.name == argResults![_optionNameVersionCheckMode]);
  final ignorePrerelease = argResults![_optionNameIgnorePrerelease] as bool;
  final doCheckSdkVersion = argResults![_optionNameCheckSdkVersion] as bool;
  final noAnalyzePlatformConstraints =
      argResults![_optionNameNoAnalyzePlatformConstraints] as bool;
  if (argResults?.arguments
          .any((a) => a == '--$_optionNameDependencyCheckMode') ??
      false) {
    stdout.writeln(
        'You are using the option "$_optionNameDependencyCheckMode" that has no effect any more and will be removed in a future release (and will lead to an exception if specified)');
  }
  final doRemoveExample = argResults![_optionNameRemoveExample] as bool;
  final doIgnoreRequiredness =
      argResults![_optionNameIgnoreRequiredness] as bool;

  final preparedOldPackageRef = await prepare(
    argResults!,
    oldPackageRef,
  );
  final preparedNewPackageRef = await prepare(
    argResults!,
    newPackageRef,
  );

  final oldPackageApi = await analyze(
    argResults!,
    preparedOldPackageRef,
    doAnalyzePlatformConstraints: !noAnalyzePlatformConstraints,
    doRemoveExample: doRemoveExample,
  );
  final newPackageApi = await analyze(
    argResults!,
    preparedNewPackageRef,
    doAnalyzePlatformConstraints: !noAnalyzePlatformConstraints,
    doRemoveExample: doRemoveExample,
  );

  await cleanUp(preparedOldPackageRef);
  await cleanUp(preparedNewPackageRef);

  final differ = PackageApiDiffer(
    options: PackageApiDifferOptions(
      doCheckSdkVersion: doCheckSdkVersion,
      doIgnoreRequiredness: doIgnoreRequiredness,
    ),
  );
  final diffResult =
      differ.diff(oldApi: oldPackageApi, newApi: newPackageApi);

  DiffReporter reporter = (() {
    switch (outputFormatter) {
      case ReportFormat.cli:
        return ConsoleDiffReporter();
      case ReportFormat.markdown:
        return MarkdownDiffReporter(
            oldPackageRef: oldPackageRef,
            newPackageRef: newPackageRef,
            outputFile: File(outputFile));
      case ReportFormat.json:
        return JsonDiffReporter(
            oldPackageRef: oldPackageRef,
            newPackageRef: newPackageRef,
            outputFile: File(outputFile));
      default:
        throw 'Unknown format speicified $outputFormatter';
    }
  })();

  VersionCheckResult? versionCheckResult;
  if (versionCheckMode != VersionCheckMode.none) {
    versionCheckResult = VersionCheck.check(
      diffResult: diffResult,
      oldPackageApi: oldPackageApi,
      newPackageApi: newPackageApi,
      ignorePrerelease: ignorePrerelease,
      versionCheckMode: versionCheckMode,
    );
  }

  stdout.writeln('-- Generating report using: ${reporter.reporterName} --');
  await reporter.generateReport(diffResult, versionCheckResult);
  if (versionCheckResult?.success ?? true) {
    return 0;
  } else {
    return -1;
  }
}