resolve method

List<String> resolve(
  1. String componentName, {
  2. Set<String> alreadyInstalled = const {},
})

Returns an ordered installation list for componentName.

The list is topologically sorted so that dependencies appear before the components that need them. Components already present in alreadyInstalled are excluded from the result.

Implementation

List<String> resolve(
  String componentName, {
  Set<String> alreadyInstalled = const {},
}) {
  final brick = BrickRegistry.lookup(componentName);
  if (brick == null) {
    throw ArgumentError('Unknown component: $componentName');
  }

  final ordered = <String>[];
  final visited = <String>{};

  void visit(String name) {
    if (visited.contains(name)) return;
    visited.add(name);

    final info = BrickRegistry.lookup(name);
    if (info == null) {
      throw ArgumentError(
        'Unknown dependency "$name" referenced by the dependency graph.',
      );
    }

    for (final dep in info.dependencies) {
      visit(dep);
    }

    if (!alreadyInstalled.contains(name)) {
      ordered.add(name);
    }
  }

  visit(componentName);
  return ordered;
}