findPackages method

Future<List<String>> findPackages()

Finds the packages referred from the files in libFiles.

Implementation

Future<List<String>> findPackages() async {
  final packages = <String>{};
  RegExp regExp = RegExp(r'^([^:]+(:([^:]+))?):\s');
  for (var file in libFiles) {
    final lines = await executeResult('dpkg', ['-S', file]);
    final packages2 = <String, String>{};
    for (var line in lines) {
      if (line.trim().isNotEmpty) {
        final matcher = regExp.firstMatch(line);
        if (matcher != null) {
          packages2[matcher.group(1)!] = matcher.group(3) ?? '';
        }
      }
    }
    final keys = packages2.keys.toList();
    String? toAdd;
    switch (keys.length) {
      case 0:
        break;
      case 1:
        toAdd = keys[0];
        break;
      case 2:
      case 3:
        if (packages2[keys[0]] == preferredArchitecture) {
          toAdd = keys[0];
        } else if (packages2[keys[1]] == preferredArchitecture) {
          toAdd = keys[1];
        } else {
          toAdd = keys.join('|');
        }
        break;
      default:
        error('too many alternatives: ${keys.join("|")}');
        break;
    }
    if (toAdd != null) {
      packages.add(toAdd);
    }
  }
  if (packages.isEmpty) {
    log('No dependencies found');
    return [];
  } else {
    final packages3 = await reduceDoubles(packages);
    var sorted = packages3.toList();
    sorted.sort();
    sorted = sorted
        .map((e) => e.replaceFirst(':$preferredArchitecture', ''))
        .toList();
    log('Dependencies: ${sorted.length}');
    for (var package in sorted) {
      log(package);
    }
    return sorted;
  }
}