|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:graphs/graphs.dart' as graphs; |
| 9 | +import 'package:package_config/package_config.dart'; |
| 10 | + |
| 11 | +class NativeAssetsBuildPlanner { |
| 12 | + final PackageGraph packageGraph; |
| 13 | + final List<Package> packagesWithNativeAssets; |
| 14 | + final Uri dartExecutable; |
| 15 | + |
| 16 | + NativeAssetsBuildPlanner({ |
| 17 | + required this.packageGraph, |
| 18 | + required this.packagesWithNativeAssets, |
| 19 | + required this.dartExecutable, |
| 20 | + }); |
| 21 | + |
| 22 | + static Future<NativeAssetsBuildPlanner> fromRootPackageRoot({ |
| 23 | + required Uri rootPackageRoot, |
| 24 | + required List<Package> packagesWithNativeAssets, |
| 25 | + required Uri dartExecutable, |
| 26 | + }) async { |
| 27 | + final result = await Process.run( |
| 28 | + dartExecutable.toFilePath(), |
| 29 | + [ |
| 30 | + 'pub', |
| 31 | + 'deps', |
| 32 | + '--json', |
| 33 | + ], |
| 34 | + workingDirectory: rootPackageRoot.toFilePath(), |
| 35 | + ); |
| 36 | + final packageGraph = |
| 37 | + PackageGraph.fromPubDepsJsonString(result.stdout as String); |
| 38 | + return NativeAssetsBuildPlanner( |
| 39 | + packageGraph: packageGraph, |
| 40 | + packagesWithNativeAssets: packagesWithNativeAssets, |
| 41 | + dartExecutable: dartExecutable, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + List<Package> plan() { |
| 46 | + final packageMap = { |
| 47 | + for (final package in packagesWithNativeAssets) package.name: package |
| 48 | + }; |
| 49 | + final packagesToBuild = packageMap.keys.toSet(); |
| 50 | + final stronglyConnectedComponents = packageGraph.computeStrongComponents(); |
| 51 | + final result = <Package>[]; |
| 52 | + for (final stronglyConnectedComponent in stronglyConnectedComponents) { |
| 53 | + final stronglyConnectedComponentWithNativeAssets = [ |
| 54 | + for (final packageName in stronglyConnectedComponent) |
| 55 | + if (packagesToBuild.contains(packageName)) packageName |
| 56 | + ]; |
| 57 | + if (stronglyConnectedComponentWithNativeAssets.length > 1) { |
| 58 | + throw Exception( |
| 59 | + 'Cyclic dependency for native asset builds in the following ' |
| 60 | + 'packages: $stronglyConnectedComponent.', |
| 61 | + ); |
| 62 | + } else if (stronglyConnectedComponentWithNativeAssets.length == 1) { |
| 63 | + result.add( |
| 64 | + packageMap[stronglyConnectedComponentWithNativeAssets.single]!); |
| 65 | + } |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +class PackageGraph { |
| 72 | + final Map<String, List<String>> map; |
| 73 | + |
| 74 | + PackageGraph(this.map); |
| 75 | + |
| 76 | + /// Construct a graph from the JSON produced by `dart pub deps --json`. |
| 77 | + factory PackageGraph.fromPubDepsJsonString(String json) => |
| 78 | + PackageGraph.fromPubDepsJson(jsonDecode(json) as Map<dynamic, dynamic>); |
| 79 | + |
| 80 | + /// Construct a graph from the JSON produced by `dart pub deps --json`. |
| 81 | + factory PackageGraph.fromPubDepsJson(Map<dynamic, dynamic> map) { |
| 82 | + final result = <String, List<String>>{}; |
| 83 | + final packages = map['packages'] as List<dynamic>; |
| 84 | + for (final package in packages) { |
| 85 | + final package_ = package as Map<dynamic, dynamic>; |
| 86 | + final name = package_['name'] as String; |
| 87 | + final dependencies = (package_['dependencies'] as List<dynamic>) |
| 88 | + .whereType<String>() |
| 89 | + .toList(); |
| 90 | + result[name] = dependencies; |
| 91 | + } |
| 92 | + return PackageGraph(result); |
| 93 | + } |
| 94 | + |
| 95 | + Iterable<String> neighborsOf(String vertex) => map[vertex] ?? []; |
| 96 | + |
| 97 | + Iterable<String> get vertices => map.keys; |
| 98 | + |
| 99 | + List<List<String>> computeStrongComponents() => |
| 100 | + graphs.stronglyConnectedComponents(vertices, neighborsOf); |
| 101 | +} |
0 commit comments