deducePackageName function

String? deducePackageName(
  1. String mainDartFile
)

Given the path of a Dart file, mainDartFile, the name of the package will be deduced by locating and parsing its associated pubspec.yaml.

Implementation

String? deducePackageName(String mainDartFile) {
  final String? pubspecPath = _findPubspecPath(mainDartFile);
  if (pubspecPath == null) {
    return null;
  }

  try {
    final String text = File(pubspecPath).readAsStringSync();
    return (yaml.loadYaml(text) as Map<dynamic, dynamic>)['name'] as String?;
  } catch (_) {
    return null;
  }
}