selectAppleXcframeworkRoot function

Directory selectAppleXcframeworkRoot(
  1. Directory extractRoot
)

Implementation

Directory selectAppleXcframeworkRoot(Directory extractRoot) {
  final nestedCandidates = <Directory>[];
  if (extractRoot.existsSync()) {
    nestedCandidates.addAll(
      extractRoot
          .listSync(recursive: true, followLinks: false)
          .whereType<Directory>()
          .where(
            (directory) => p.basename(directory.path).endsWith('.xcframework'),
          ),
    );
  }
  final candidates = nestedCandidates.isNotEmpty
      ? nestedCandidates
      : <Directory>[
          if (p.basename(extractRoot.path).endsWith('.xcframework'))
            extractRoot,
        ];

  final uniqueCandidates = <String, Directory>{};
  for (final candidate in candidates) {
    uniqueCandidates[p.normalize(candidate.path)] = candidate;
  }
  final orderedCandidates = uniqueCandidates.values.toList()
    ..sort((a, b) => a.path.compareTo(b.path));

  if (orderedCandidates.isEmpty) {
    throw StateError(
      'Apple artifact must contain exactly one .xcframework root under '
      '${extractRoot.path}; none was found.',
    );
  }
  if (orderedCandidates.length > 1) {
    throw StateError(
      'Apple artifact contains multiple .xcframework roots: '
      '${orderedCandidates.map((directory) => directory.path).join(', ')}',
    );
  }
  return orderedCandidates.single;
}