analyze method

Future<PackageApi> analyze(
  1. ArgResults argResults,
  2. PreparedPackageRef preparedRef, {
  3. bool doAnalyzePlatformConstraints = true,
  4. bool doRemoveExample = true,
})
inherited

Analyzes the given prepared Package ref. doMergeBaseClasses defines if base classes should be merged into derived ones. This allows to remove private base classes from the list of interface declarations. doAnalyzePlatformConstraints defines if the platform constraints of the package shall be analyzed.

Implementation

Future<PackageApi> analyze(
  ArgResults argResults,
  PreparedPackageRef preparedRef, {
  bool doAnalyzePlatformConstraints = true,
  bool doRemoveExample = true,
}) async {
  final stdoutSession = StdoutSession();

  final forceUseFlutterTool =
      (argResults[_flagNameForceUseFlutter] as bool?) ?? false;

  String? path;
  if (preparedRef.packageRef.isDirectoryPath) {
    path = preparedRef.packageRef.ref;
  }
  if (preparedRef.packageRef.isPubRef) {
    path = PubInteraction.getPackagePathInCache(
        preparedRef.packageRef.pubPackage!,
        preparedRef.packageRef.pubVersion);
  }
  if (path == null) {
    throw ArgumentError(
        'Don\'t know how to handle ${preparedRef.packageRef.ref}');
  }

  String packagePath = preparedRef.packageDirectory ?? path;
  // The analysis options might limit the scope of dart_apitool
  final analysisOptionsFile =
      File(p.join(packagePath, 'analysis_options.yaml'));
  if (await analysisOptionsFile.exists()) {
    await analysisOptionsFile.delete();
  }
  final exampleDirPath = p.join(packagePath, 'example');
  if (doRemoveExample && await Directory(exampleDirPath).exists()) {
    await Directory(exampleDirPath).delete(recursive: true);
  }

  // Check if the package_config.json is already present from the preparation step
  final packageConfig = File(_getPackageConfigPathForPackage(packagePath));
  if (!packageConfig.existsSync()) {
    await stdoutSession.writeln('Running pub get');
    await PubInteraction.runPubGet(
      packagePath,
      stdoutSession: stdoutSession,
      forceUseFlutterTool: forceUseFlutterTool,
    );
  } else {
    await stdoutSession
        .writeln('Omitting pub get (package config already present)');
  }

  await stdoutSession.writeln('Analyzing $path');
  final analyzer = PackageApiAnalyzer(
    packagePath: packagePath,
    doAnalyzePlatformConstraints: doAnalyzePlatformConstraints,
  );
  return await analyzer.analyze();
}