getPackageConfigPathForPackage static method

String getPackageConfigPathForPackage(
  1. String packagePath, {
  2. required StdoutSession stdoutSession,
  3. required bool doCheckWorkspace,
})

Implementation

static String getPackageConfigPathForPackage(
  String packagePath, {
  required StdoutSession stdoutSession,
  required bool doCheckWorkspace,
}) {
  String packageConfigPackagePath = packagePath;
  final packageDir = Directory(packagePath);
  if (doCheckWorkspace && packageDir.existsSync()) {
    // if the package directory exists (source) then we check if we have to deal with a workspace
    try {
      final pubspec = PubSpec.load(directory: packagePath);
      final resolutionSection =
          pubspec.document.findSectionForKey('resolution');
      if (!resolutionSection.missing) {
        bool resolvesWithWorkspace = false;
        for (final line in resolutionSection.lines) {
          if (line.text.contains('resolution:') &&
              line.text.trim().endsWith('workspace')) {
            resolvesWithWorkspace = true;
            break;
          }
        }
        if (resolvesWithWorkspace) {
          final workspacePath = _findWorkspacePath(packagePath);
          if (workspacePath == null) {
            stdoutSession
                .writeln('Could not find workspace for package $packagePath');
          } else {
            packageConfigPackagePath = workspacePath;
          }
        }
      }
    } catch (e) {
      stdoutSession
          .writeln('Error loading pubspec.yaml, continuing anyways: $e');
    }
  }
  return path.join(
      packageConfigPackagePath, '.dart_tool', 'package_config.json');
}