Move dart pub deps call to <Pub>.deps and use it accordingly (#158524)

Closes https://github.com/flutter/flutter/issues/158396, closes https://github.com/flutter/flutter/pull/158370.
This commit is contained in:
Matan Lurey
2024-11-13 10:03:53 -08:00
committed by GitHub
parent 4de32b8702
commit 2d1a3efc38
16 changed files with 600 additions and 337 deletions

View File

@@ -2,11 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:process/process.dart';
import 'base/io.dart';
import 'base/logger.dart';
import 'convert.dart';
import 'dart/pub.dart';
import 'project.dart';
/// Returns dependencies of [project] that are _only_ used as `dev_dependency`.
///
@@ -15,54 +14,19 @@ import 'convert.dart';
/// `pubspec.yaml` file, and removing packages from that set that appear as
/// dependencies (implicitly non-dev) in any non-dev package depended on.
Future<Set<String>> computeExclusiveDevDependencies(
ProcessManager processes, {
Pub pub, {
required Logger logger,
required String projectPath,
required FlutterProject project,
}) async {
final ProcessResult processResult = await processes.run(
<String>['dart', 'pub', 'deps', '--json'],
workingDirectory: projectPath,
);
final Map<String, Object?> jsonResult = await pub.deps(project);
Never fail([String? reason]) {
final Object? stdout = processResult.stdout;
if (stdout is String && stdout.isNotEmpty) {
logger.printTrace(stdout);
}
final String stderr = processResult.stderr.toString();
logger.printTrace(const JsonEncoder.withIndent(' ').convert(jsonResult));
throw StateError(
'dart pub deps --json ${reason != null ? 'had unexpected output: $reason' : 'failed'}'
'${stderr.isNotEmpty ? '\n$stderr' : ''}',
);
}
// Guard against dart pub deps crashing.
final Map<String, Object?> jsonResult;
if (processResult.exitCode != 0 || processResult.stdout is! String) {
fail();
}
// Guard against dart pub deps having explicitly invalid output.
final String stdout;
try {
stdout = processResult.stdout as String;
// This is an indication that `FakeProcessManager.any` was used, which by
// contract emits exit code 0 and no output on either stdout or stderr. To
// avoid this code, we'd have to go and make this function injectable into
// every callsite and mock-it out manually, which at the time of this
// writing was 130+ unit test cases alone.
//
// So, this is the lesser of two evils.
if (stdout.isEmpty && processResult.stderr == '') {
return <String>{};
}
jsonResult = json.decode(stdout) as Map<String, Object?>;
} on FormatException catch (e) {
fail('$e');
}
List<T> asListOrFail<T>(Object? value, String name) {
// Allow omitting a list as empty to default to an empty list
if (value == null) {

View File

@@ -157,6 +157,13 @@ abstract class Pub {
PubOutputMode outputMode = PubOutputMode.all,
});
/// Runs, parses, and returns `pub deps --json` for [project].
///
/// While it is guaranteed that, if succcessful, that the result are a valid
/// JSON object, the exact contents returned are _not_ validated, and are left
/// as a responsibility of the caller.
Future<Map<String, Object?>> deps(FlutterProject project);
/// Runs pub in 'batch' mode.
///
/// forwarding complete lines written by pub to its stdout/stderr streams to
@@ -373,6 +380,48 @@ class _DefaultPub implements Pub {
await _updateVersionAndPackageConfig(project);
}
@override
Future<Map<String, Object?>> deps(FlutterProject project) async {
final List<String> pubCommand = <String>[
..._pubCommand,
'deps',
'--json',
];
final RunResult runResult = await _processUtils.run(
pubCommand,
workingDirectory: project.directory.path,
);
Never fail([String? reason]) {
final String stdout = runResult.stdout;
if (stdout.isNotEmpty) {
_logger.printTrace(stdout);
}
final String stderr = runResult.stderr;
throw StateError(
'${pubCommand.join(' ')} ${reason != null ? 'had unexpected output: $reason' : 'failed'}'
'${stderr.isNotEmpty ? '\n$stderr' : ''}',
);
}
// Guard against dart pub deps crashing.
if (runResult.exitCode != 0) {
fail();
}
// Guard against dart pub deps having explicitly invalid output.
try {
final Object? result = json.decode(runResult.stdout);
if (result is! Map<String, Object?>) {
fail('Not a JSON object');
}
return result;
} on FormatException catch (e) {
fail('$e');
}
}
/// Runs pub with [arguments] and [ProcessStartMode.inheritStdio] mode.
///
/// Uses [ProcessStartMode.normal] and [Pub._stdio] if [Pub.test] constructor

View File

@@ -21,6 +21,7 @@ import 'compute_dev_dependencies.dart';
import 'convert.dart';
import 'dart/language_version.dart';
import 'dart/package_map.dart';
import 'dart/pub.dart';
import 'features.dart';
import 'globals.dart' as globals;
import 'macos/darwin_dependency_management.dart';
@@ -128,9 +129,9 @@ Future<List<Plugin>> findPlugins(
devDependencies = <String>{};
} else {
devDependencies = await computeExclusiveDevDependencies(
globals.processManager,
pub,
logger: globals.logger,
projectPath: project.directory.path,
project: project,
);
}
for (final Package package in packageConfig.packages) {