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 packageRef = PackageRef(argResults![_optionNameInput]);
  final noAnalyzePlatformConstraints =
      argResults![_optionNameNoAnalyzePlatformConstraints] as bool;
  final doRemoveExample = argResults![_optionNameRemoveExample] as bool;
  final doSetExitCodeOnMissingExport =
      argResults![_optionNameSetExitCodeOnMissingExport] as bool;

  final preparedPackageRef = await prepare(
    argResults!,
    packageRef,
  );
  final packageApi = await analyze(
    argResults!,
    preparedPackageRef,
    doAnalyzePlatformConstraints: !noAnalyzePlatformConstraints,
    doRemoveExample: doRemoveExample,
  );
  await cleanUp(preparedPackageRef);

  final jsonString = PackageApiStorage.packageApitoStorageJson(
    packageApi,
    pretty: true,
  );
  final outFilePath = argResults![_optionNameOutput] as String?;
  if (outFilePath != null) {
    final outFile = File(outFilePath);
    if (await outFile.exists()) {
      await outFile.delete();
    }
    await outFile.writeAsString(jsonString, mode: FileMode.write);
    stdout.writeln('Public API of "$packageRef" written to $outFilePath');
  } else {
    stdout.writeln(jsonString);
  }

  final declarationsWithoutEntryPointsOutsideTests =
      packageApi.rootDeclarationsWithoutEntryPointsAndVisibleOutsideTests;

  if (declarationsWithoutEntryPointsOutsideTests.isNotEmpty) {
    if (outFilePath == null) {
      stdout.writeln(
          'The following declarations do not have an entry point (did you miss to export them?):');
      for (final declaration in declarationsWithoutEntryPointsOutsideTests) {
        stdout.writeln('  ${declaration.name}');
        if (declaration is InterfaceDeclaration) {
          final filteredUsages = declaration.typeUsages
              .where((tu) => !tu.isVisibleForTesting)
              .toList();
          if (filteredUsages.isNotEmpty) {
            stdout.writeln('    Usage(s):');
            for (final typeUsage in filteredUsages) {
              stdout.writeln('    - ${typeUsage.referringElementName}');
            }
          }
        }
      }
    }
    if (doSetExitCodeOnMissingExport) {
      return -1;
    }
  }
  return 0;
}