|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:process/process.dart'; |
| 6 | + |
| 7 | +import 'base/io.dart'; |
| 8 | +import 'base/logger.dart'; |
| 9 | +import 'convert.dart'; |
| 10 | + |
| 11 | +/// Returns dependencies of [project] that are _only_ used as `dev_dependency`. |
| 12 | +/// |
| 13 | +/// That is, computes and returns a subset of dependencies, where the original |
| 14 | +/// set is based on packages listed as [`dev_dependency`][dev_deps] in the |
| 15 | +/// `pubspec.yaml` file, and removing packages from that set that appear as |
| 16 | +/// dependencies (implicitly non-dev) in any non-dev package depended on. |
| 17 | +Future<Set<String>> computeExclusiveDevDependencies( |
| 18 | + ProcessManager processes, { |
| 19 | + required Logger logger, |
| 20 | + required String projectPath, |
| 21 | +}) async { |
| 22 | + final ProcessResult processResult = await processes.run( |
| 23 | + <String>['dart', 'pub', 'deps', '--json'], |
| 24 | + workingDirectory: projectPath, |
| 25 | + ); |
| 26 | + |
| 27 | + Never fail([String? reason]) { |
| 28 | + final Object? stdout = processResult.stdout; |
| 29 | + if (stdout is String && stdout.isNotEmpty) { |
| 30 | + logger.printTrace(stdout); |
| 31 | + } |
| 32 | + final String stderr = processResult.stderr.toString(); |
| 33 | + throw StateError( |
| 34 | + 'dart pub deps --json ${reason != null ? 'had unexpected output: $reason' : 'failed'}' |
| 35 | + '${stderr.isNotEmpty ? '\n$stderr' : ''}', |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + // Guard against dart pub deps crashing. |
| 40 | + final Map<String, Object?> jsonResult; |
| 41 | + if (processResult.exitCode != 0 || processResult.stdout is! String) { |
| 42 | + fail(); |
| 43 | + } |
| 44 | + |
| 45 | + // Guard against dart pub deps having explicitly invalid output. |
| 46 | + final String stdout; |
| 47 | + try { |
| 48 | + stdout = processResult.stdout as String; |
| 49 | + |
| 50 | + // This is an indication that `FakeProcessManager.any` was used, which by |
| 51 | + // contract emits exit code 0 and no output on either stdout or stderr. To |
| 52 | + // avoid this code, we'd have to go and make this function injectable into |
| 53 | + // every callsite and mock-it out manually, which at the time of this |
| 54 | + // writing was 130+ unit test cases alone. |
| 55 | + // |
| 56 | + // So, this is the lesser of two evils. |
| 57 | + if (stdout.isEmpty && processResult.stderr == '') { |
| 58 | + return <String>{}; |
| 59 | + } |
| 60 | + |
| 61 | + jsonResult = json.decode(stdout) as Map<String, Object?>; |
| 62 | + } on FormatException catch (e) { |
| 63 | + fail('$e'); |
| 64 | + } |
| 65 | + |
| 66 | + List<T> asListOrFail<T>(Object? value, String name) { |
| 67 | + if (value is! List<Object?>) { |
| 68 | + fail('Expected field "$name" to be a list, got "$value"'); |
| 69 | + } |
| 70 | + return <T>[ |
| 71 | + for (final Object? any in value) |
| 72 | + if (any is T) any else fail('Expected element to be a $T, got "$any"') |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + // Parse the JSON roughly in the following format: |
| 77 | + // |
| 78 | + // ```json |
| 79 | + // { |
| 80 | + // "root": "my_app", |
| 81 | + // "packages": [ |
| 82 | + // { |
| 83 | + // "name": "my_app", |
| 84 | + // "kind": "root", |
| 85 | + // "dependencies": [ |
| 86 | + // "foo_plugin", |
| 87 | + // "bar_plugin" |
| 88 | + // ], |
| 89 | + // "directDependencies": [ |
| 90 | + // "foo_plugin" |
| 91 | + // ], |
| 92 | + // "devDependencies": [ |
| 93 | + // "bar_plugin" |
| 94 | + // ] |
| 95 | + // } |
| 96 | + // ] |
| 97 | + // } |
| 98 | + // ``` |
| 99 | + final List<Map<String, Object?>> packages = asListOrFail( |
| 100 | + jsonResult['packages'], |
| 101 | + 'packages', |
| 102 | + ); |
| 103 | + |
| 104 | + Map<String, Object?> packageWhere( |
| 105 | + bool Function(Map<String, Object?>) test, { |
| 106 | + required String reason, |
| 107 | + }) { |
| 108 | + return packages.firstWhere(test, orElse: () => fail(reason)); |
| 109 | + } |
| 110 | + |
| 111 | + final Map<String, Object?> rootPackage = packageWhere( |
| 112 | + (Map<String, Object?> package) => package['kind'] == 'root', |
| 113 | + reason: 'A package with kind "root" was not found.', |
| 114 | + ); |
| 115 | + |
| 116 | + // Start initially with every `devDependency` listed. |
| 117 | + final Set<String> devDependencies = asListOrFail<String>( |
| 118 | + rootPackage['devDependencies'], |
| 119 | + 'devDependencies', |
| 120 | + ).toSet(); |
| 121 | + |
| 122 | + // Then traverse and exclude non-dev dependencies that list that dependency. |
| 123 | + // |
| 124 | + // This avoids the pathalogical problem of using, say, `path_provider` in a |
| 125 | + // package's dev_dependencies:, but a (non-dev) dependency using it as a |
| 126 | + // standard dependency - in that case we would not want to report it is used |
| 127 | + // as a dev dependency. |
| 128 | + final Set<String> visited = <String>{}; |
| 129 | + void visitPackage(String packageName) { |
| 130 | + final bool wasAlreadyVisited = !visited.add(packageName); |
| 131 | + if (wasAlreadyVisited) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + final Map<String, Object?> package = packageWhere( |
| 136 | + (Map<String, Object?> package) => package['name'] == packageName, |
| 137 | + reason: 'A package with name "$packageName" was not found', |
| 138 | + ); |
| 139 | + |
| 140 | + // Do not traverse packages that themselves are dev dependencies. |
| 141 | + if (package['kind'] == 'dev') { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + final List<String> directDependencies = asListOrFail( |
| 146 | + package['directDependencies'], |
| 147 | + 'directDependencies', |
| 148 | + ); |
| 149 | + |
| 150 | + // Remove any listed dependency from dev dependencies; it might have been |
| 151 | + // a dev dependency for the app (root) package, but it is being used as a |
| 152 | + // real dependency for a dependend on package, so we would not want to send |
| 153 | + // a signal that the package can be ignored/removed. |
| 154 | + devDependencies.removeAll(directDependencies); |
| 155 | + |
| 156 | + // And continue visiting (visitPackage checks for circular loops). |
| 157 | + directDependencies.forEach(visitPackage); |
| 158 | + } |
| 159 | + |
| 160 | + // Start with the root package. |
| 161 | + visitPackage(rootPackage['name']! as String); |
| 162 | + |
| 163 | + return devDependencies; |
| 164 | +} |
0 commit comments