Migrate pub in flutter_tools to null safety (#80548)
This commit is contained in:
parent
72976f552c
commit
8d5f08fe02
@ -2,9 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:package_config/package_config.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
@ -22,7 +19,7 @@ import '../dart/package_map.dart';
|
||||
import '../reporting/reporting.dart';
|
||||
|
||||
/// The [Pub] instance.
|
||||
Pub get pub => context.get<Pub>();
|
||||
Pub get pub => context.get<Pub>()!;
|
||||
|
||||
/// The console environment key used by the pub tool.
|
||||
const String _kPubEnvironmentKey = 'PUB_ENVIRONMENT';
|
||||
@ -77,12 +74,12 @@ class PubContext {
|
||||
abstract class Pub {
|
||||
/// Create a default [Pub] instance.
|
||||
factory Pub({
|
||||
@required FileSystem fileSystem,
|
||||
@required Logger logger,
|
||||
@required ProcessManager processManager,
|
||||
@required Platform platform,
|
||||
@required BotDetector botDetector,
|
||||
@required Usage usage,
|
||||
required FileSystem fileSystem,
|
||||
required Logger logger,
|
||||
required ProcessManager processManager,
|
||||
required Platform platform,
|
||||
required BotDetector botDetector,
|
||||
required Usage usage,
|
||||
}) = _DefaultPub;
|
||||
|
||||
/// Runs `pub get`.
|
||||
@ -90,7 +87,7 @@ abstract class Pub {
|
||||
/// [context] provides extra information to package server requests to
|
||||
/// understand usage.
|
||||
Future<void> get({
|
||||
@required PubContext context,
|
||||
required PubContext context,
|
||||
String directory,
|
||||
bool skipIfAbsent = false,
|
||||
bool upgrade = false,
|
||||
@ -114,11 +111,11 @@ abstract class Pub {
|
||||
/// understand usage.
|
||||
Future<void> batch(
|
||||
List<String> arguments, {
|
||||
@required PubContext context,
|
||||
required PubContext context,
|
||||
String directory,
|
||||
MessageFilter filter,
|
||||
String failureMessage = 'pub failed',
|
||||
@required bool retry,
|
||||
required bool retry,
|
||||
bool showTraceForErrors,
|
||||
});
|
||||
|
||||
@ -129,7 +126,7 @@ abstract class Pub {
|
||||
Future<void> interactively(
|
||||
List<String> arguments, {
|
||||
String directory,
|
||||
@required io.Stdio stdio,
|
||||
required io.Stdio stdio,
|
||||
bool touchesPackageConfig = false,
|
||||
bool generateSyntheticPackage = false,
|
||||
});
|
||||
@ -137,12 +134,12 @@ abstract class Pub {
|
||||
|
||||
class _DefaultPub implements Pub {
|
||||
_DefaultPub({
|
||||
@required FileSystem fileSystem,
|
||||
@required Logger logger,
|
||||
@required ProcessManager processManager,
|
||||
@required Platform platform,
|
||||
@required BotDetector botDetector,
|
||||
@required Usage usage,
|
||||
required FileSystem fileSystem,
|
||||
required Logger logger,
|
||||
required ProcessManager processManager,
|
||||
required Platform platform,
|
||||
required BotDetector botDetector,
|
||||
required Usage usage,
|
||||
}) : _fileSystem = fileSystem,
|
||||
_logger = logger,
|
||||
_platform = platform,
|
||||
@ -162,13 +159,13 @@ class _DefaultPub implements Pub {
|
||||
|
||||
@override
|
||||
Future<void> get({
|
||||
@required PubContext context,
|
||||
String directory,
|
||||
required PubContext context,
|
||||
String? directory,
|
||||
bool skipIfAbsent = false,
|
||||
bool upgrade = false,
|
||||
bool offline = false,
|
||||
bool generateSyntheticPackage = false,
|
||||
String flutterRootOverride,
|
||||
String? flutterRootOverride,
|
||||
bool checkUpToDate = false,
|
||||
}) async {
|
||||
directory ??= _fileSystem.currentDirectory.path;
|
||||
@ -179,7 +176,7 @@ class _DefaultPub implements Pub {
|
||||
final File lastVersion = _fileSystem.file(
|
||||
_fileSystem.path.join(directory, '.dart_tool', 'version'));
|
||||
final File currentVersion = _fileSystem.file(
|
||||
_fileSystem.path.join(Cache.flutterRoot, 'version'));
|
||||
_fileSystem.path.join(Cache.flutterRoot!, 'version'));
|
||||
final File pubspecYaml = _fileSystem.file(
|
||||
_fileSystem.path.join(directory, 'pubspec.yaml'));
|
||||
final File pubLockFile = _fileSystem.file(
|
||||
@ -249,13 +246,13 @@ class _DefaultPub implements Pub {
|
||||
@override
|
||||
Future<void> batch(
|
||||
List<String> arguments, {
|
||||
@required PubContext context,
|
||||
String directory,
|
||||
MessageFilter filter,
|
||||
required PubContext context,
|
||||
String? directory,
|
||||
MessageFilter? filter,
|
||||
String failureMessage = 'pub failed',
|
||||
@required bool retry,
|
||||
bool showTraceForErrors,
|
||||
String flutterRootOverride,
|
||||
required bool retry,
|
||||
bool? showTraceForErrors,
|
||||
String? flutterRootOverride,
|
||||
}) async {
|
||||
showTraceForErrors ??= await _botDetector.isRunningOnBot;
|
||||
|
||||
@ -327,8 +324,8 @@ class _DefaultPub implements Pub {
|
||||
@override
|
||||
Future<void> interactively(
|
||||
List<String> arguments, {
|
||||
String directory,
|
||||
@required io.Stdio stdio,
|
||||
String? directory,
|
||||
required io.Stdio stdio,
|
||||
bool touchesPackageConfig = false,
|
||||
bool generateSyntheticPackage = false,
|
||||
}) async {
|
||||
@ -367,14 +364,15 @@ class _DefaultPub implements Pub {
|
||||
}
|
||||
|
||||
if (touchesPackageConfig) {
|
||||
final String targetDirectory = directory ?? _fileSystem.currentDirectory.path;
|
||||
final File packageConfigFile = _fileSystem.file(
|
||||
_fileSystem.path.join(directory, '.dart_tool', 'package_config.json'));
|
||||
_fileSystem.path.join(targetDirectory, '.dart_tool', 'package_config.json'));
|
||||
final Directory generatedDirectory = _fileSystem.directory(
|
||||
_fileSystem.path.join(directory, '.dart_tool', 'flutter_gen'));
|
||||
_fileSystem.path.join(targetDirectory, '.dart_tool', 'flutter_gen'));
|
||||
final File lastVersion = _fileSystem.file(
|
||||
_fileSystem.path.join(directory, '.dart_tool', 'version'));
|
||||
_fileSystem.path.join(targetDirectory, '.dart_tool', 'version'));
|
||||
final File currentVersion = _fileSystem.file(
|
||||
_fileSystem.path.join(Cache.flutterRoot, 'version'));
|
||||
_fileSystem.path.join(Cache.flutterRoot!, 'version'));
|
||||
lastVersion.writeAsStringSync(currentVersion.readAsStringSync());
|
||||
await _updatePackageConfig(
|
||||
packageConfigFile,
|
||||
@ -388,7 +386,7 @@ class _DefaultPub implements Pub {
|
||||
List<String> _pubCommand(List<String> arguments) {
|
||||
// TODO(jonahwilliams): refactor to use artifacts.
|
||||
final String sdkPath = _fileSystem.path.joinAll(<String>[
|
||||
Cache.flutterRoot,
|
||||
Cache.flutterRoot!,
|
||||
'bin',
|
||||
'cache',
|
||||
'dart-sdk',
|
||||
@ -410,7 +408,7 @@ class _DefaultPub implements Pub {
|
||||
Future<String> _getPubEnvironmentValue(PubContext pubContext) async {
|
||||
// DO NOT update this function without contacting kevmoo.
|
||||
// We have server-side tooling that assumes the values are consistent.
|
||||
final String existing = _platform.environment[_kPubEnvironmentKey];
|
||||
final String? existing = _platform.environment[_kPubEnvironmentKey];
|
||||
final List<String> values = <String>[
|
||||
if (existing != null && existing.isNotEmpty) existing,
|
||||
if (await _botDetector.isRunningOnBot) 'flutter_bot',
|
||||
@ -420,12 +418,12 @@ class _DefaultPub implements Pub {
|
||||
return values.join(':');
|
||||
}
|
||||
|
||||
String _getRootPubCacheIfAvailable() {
|
||||
String? _getRootPubCacheIfAvailable() {
|
||||
if (_platform.environment.containsKey(_kPubCacheEnvironmentKey)) {
|
||||
return _platform.environment[_kPubCacheEnvironmentKey];
|
||||
}
|
||||
|
||||
final String cachePath = _fileSystem.path.join(Cache.flutterRoot, '.pub-cache');
|
||||
final String cachePath = _fileSystem.path.join(Cache.flutterRoot!, '.pub-cache');
|
||||
if (_fileSystem.directory(cachePath).existsSync()) {
|
||||
_logger.printTrace('Using $cachePath for the pub cache.');
|
||||
return cachePath;
|
||||
@ -439,12 +437,12 @@ class _DefaultPub implements Pub {
|
||||
///
|
||||
/// [context] provides extra information to package server requests to
|
||||
/// understand usage.
|
||||
Future<Map<String, String>> _createPubEnvironment(PubContext context, [ String flutterRootOverride ]) async {
|
||||
Future<Map<String, String>> _createPubEnvironment(PubContext context, [ String? flutterRootOverride ]) async {
|
||||
final Map<String, String> environment = <String, String>{
|
||||
'FLUTTER_ROOT': flutterRootOverride ?? Cache.flutterRoot,
|
||||
'FLUTTER_ROOT': flutterRootOverride ?? Cache.flutterRoot!,
|
||||
_kPubEnvironmentKey: await _getPubEnvironmentValue(context),
|
||||
};
|
||||
final String pubCache = _getRootPubCacheIfAvailable();
|
||||
final String? pubCache = _getRootPubCacheIfAvailable();
|
||||
if (pubCache != null) {
|
||||
environment[_kPubCacheEnvironmentKey] = pubCache;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import 'package:flutter_tools/src/build_system/exceptions.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/context.dart';
|
||||
import '../../src/fake_process_manager.dart';
|
||||
import '../../src/testbed.dart';
|
||||
|
||||
final Platform windowsPlatform = FakePlatform(
|
||||
|
@ -16,7 +16,7 @@ import 'package:flutter_tools/src/build_system/targets/web.dart';
|
||||
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
|
||||
|
||||
import '../../../src/common.dart';
|
||||
import '../../../src/context.dart';
|
||||
import '../../../src/fake_process_manager.dart';
|
||||
import '../../../src/testbed.dart';
|
||||
|
||||
const List<String> kDart2jsLinuxArgs = <String>[
|
||||
|
@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/base/bot_detector.dart';
|
||||
@ -288,7 +286,7 @@ void main() {
|
||||
});
|
||||
|
||||
testWithoutContext('pub get 69', () async {
|
||||
String error;
|
||||
String? error;
|
||||
|
||||
const FakeCommand pubGetCommand = FakeCommand(
|
||||
command: <String>[
|
||||
@ -429,7 +427,7 @@ void main() {
|
||||
});
|
||||
|
||||
testWithoutContext('pub cache in root is used', () async {
|
||||
String error;
|
||||
String? error;
|
||||
final FileSystem fileSystem = MemoryFileSystem.test();
|
||||
final Directory pubCache = fileSystem.directory(Cache.flutterRoot).childDirectory('.pub-cache')..createSync();
|
||||
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
|
||||
@ -503,7 +501,7 @@ void main() {
|
||||
);
|
||||
|
||||
FakeAsync().run((FakeAsync time) {
|
||||
String error;
|
||||
String? error;
|
||||
pub.get(context: PubContext.flutterTests).then((void value) {
|
||||
error = 'test completed unexpectedly';
|
||||
}, onError: (dynamic thrownError) {
|
||||
|
@ -2,36 +2,33 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/dart/pub.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
class ThrowingPub implements Pub {
|
||||
@override
|
||||
Future<void> batch(List<String> arguments, {
|
||||
PubContext context,
|
||||
String directory,
|
||||
MessageFilter filter,
|
||||
String failureMessage = 'pub failed',
|
||||
bool retry,
|
||||
bool showTraceForErrors,
|
||||
PubContext? context,
|
||||
String? directory,
|
||||
MessageFilter? filter,
|
||||
String? failureMessage = 'pub failed',
|
||||
bool? retry,
|
||||
bool? showTraceForErrors,
|
||||
}) {
|
||||
throw UnsupportedError('Attempted to invoke pub during test.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> get({
|
||||
PubContext context,
|
||||
String directory,
|
||||
PubContext? context,
|
||||
String? directory,
|
||||
bool skipIfAbsent = false,
|
||||
bool upgrade = false,
|
||||
bool offline = false,
|
||||
bool checkLastModified = true,
|
||||
bool skipPubspecYamlCheck = false,
|
||||
bool generateSyntheticPackage = false,
|
||||
String flutterRootOverride,
|
||||
String? flutterRootOverride,
|
||||
bool checkUpToDate = false,
|
||||
}) {
|
||||
throw UnsupportedError('Attempted to invoke pub during test.');
|
||||
@ -40,8 +37,8 @@ class ThrowingPub implements Pub {
|
||||
@override
|
||||
Future<void> interactively(
|
||||
List<String> arguments, {
|
||||
String directory,
|
||||
@required Stdio stdio,
|
||||
String? directory,
|
||||
required Stdio stdio,
|
||||
bool touchesPackageConfig = false,
|
||||
bool generateSyntheticPackage = false,
|
||||
}) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user