fromMap static method

Future<Package?> fromMap({
  1. required String outerName,
  2. required Map packageJson,
  3. required String pubCacheDirPath,
  4. required String? flutterDir,
  5. required String pubspecLockPath,
})

Implementation

static Future<Package?> fromMap({
  required String outerName,
  required Map packageJson,
  required String pubCacheDirPath,
  required String? flutterDir,
  required String pubspecLockPath,
}) async {
  Directory directory;
  bool isSdk = false;
  final source = packageJson['source'];
  final desc = packageJson['description'];
  if (source == 'hosted') {
    final host = removePrefix(desc['url']);
    final name = desc['name'];
    final version = packageJson['version'];
    directory = Directory(path.join(pubCacheDirPath, 'hosted', host.replaceAll('/', '%47'), '$name-$version'));
  } else if (source == 'git') {
    final repo = gitRepoName(desc['url']);
    final commit = desc['resolved-ref'];
    directory = Directory(path.join(pubCacheDirPath, 'git/$repo-$commit', desc['path']));
  } else if (source == 'sdk' && flutterDir != null) {
    directory = Directory(path.join(flutterDir, 'packages', outerName));
    isSdk = true;
  } else if (source == 'path') {
    directory = Directory(path.absolute(path.dirname(pubspecLockPath), desc['path']));
    isSdk = true;
  } else {
    return null;
  }
  final isDirectDependency = packageJson['dependency'] == "direct main";

  String? license;
  bool isMarkdown = false;
  if (outerName == 'flutter' && flutterDir != null) {
    license = await File(path.join(flutterDir, 'LICENSE')).readAsString();
  } else {
    String licensePath = path.join(directory.path, 'LICENSE');
    try {
      license = await File(licensePath).readAsString();
    } catch (e) {
      if (await File('$licensePath.md').exists()) {
        license = await File('$licensePath.md').readAsString();
        isMarkdown = true;
      }
    }
  }

  if (license == '') {
    license = null;
  }

  dynamic yaml;
  try {
    yaml = loadYaml(await File(path.join(directory.path, 'pubspec.yaml')).readAsString());
  } catch (e) {
    // yaml may not be there
    yaml = {};
  }

  final name = yaml['name'];
  final description = yaml['description'];
  if (name is! String || description is! String) {
    return null;
  }

  final version = outerName == 'flutter' && flutterDir != null
      ? await File(path.join(flutterDir, 'version')).readAsString()
      : yaml['version'];
  if (version is! String) {
    return null;
  }

  return Package(
      directory: directory,
      packageYaml: yaml,
      name: name,
      description: description,
      homepage: yaml['homepage'],
      repository: yaml['repository'],
      authors: yaml['authors']?.cast<String>()?.toList() ?? (yaml['author'] != null ? [yaml['author']] : []),
      version: version.trim(),
      license: license?.trim().replaceAll('\r\n', '\n'),
      isMarkdown: isMarkdown,
      isSdk: isSdk,
      isDirectDependency: isDirectDependency);
}