gatherLicenses function

Future<Map<String, GatheredLicense>> gatherLicenses(
  1. List<LocatedDependency> packages
)

Gather all licenses of provided packages

This will inspect the locations of packages and try to find the corresponding license. The result will be a Map <Name, License text>.

Implementation

Future<Map<String, GatheredLicense>> gatherLicenses(
        List<LocatedDependency> packages) async =>
    Map.fromEntries(await Future.wait(
      packages.map<Future<MapEntry<String, GatheredLicense>>>(
        (LocatedDependency package) async {
          // Iterate over all files in the root of the package to find license file
          final dir = Directory(package.path);
          if (!dir.existsSync()) {
            throw StateError(
              "Assumed location for package '${package.name}' does not exist: '${package.path}'",
            );
          }

          List<File> candidates = await _getCandidates(dir);
          if (candidates.isEmpty) {
            throw StateError(
              "Could not find license file for '${package.name}' in '${package.path}'",
            );
          } else if (candidates.length > 1) {
            throw StateError(
              "Found ${candidates.length} license files for '${package.name}' in '${package.path}'",
            );
          }

          return MapEntry(
            package.name,
            GatheredLicense.fromLocatedDependency(
              package,
              License(await candidates[0].readAsString()),
            ),
          );
        },
      ),
    ));