From 8d5f08fe02f233d2d0cc9b4f1dfa1b7b3effc0e0 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Fri, 16 Apr 2021 17:23:00 -0700 Subject: [PATCH] Migrate pub in flutter_tools to null safety (#80548) --- packages/flutter_tools/lib/src/dart/pub.dart | 84 +++++++++---------- .../build_system/source_test.dart | 2 +- .../build_system/targets/web_test.dart | 2 +- .../test/general.shard/dart/pub_get_test.dart | 8 +- .../flutter_tools/test/src/throwing_pub.dart | 25 +++--- 5 files changed, 57 insertions(+), 64 deletions(-) diff --git a/packages/flutter_tools/lib/src/dart/pub.dart b/packages/flutter_tools/lib/src/dart/pub.dart index f48658e6a1..b16ec68095 100644 --- a/packages/flutter_tools/lib/src/dart/pub.dart +++ b/packages/flutter_tools/lib/src/dart/pub.dart @@ -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 get pub => context.get()!; /// 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 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 batch( List 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 interactively( List 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 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 batch( List 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 interactively( List 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 _pubCommand(List arguments) { // TODO(jonahwilliams): refactor to use artifacts. final String sdkPath = _fileSystem.path.joinAll([ - Cache.flutterRoot, + Cache.flutterRoot!, 'bin', 'cache', 'dart-sdk', @@ -410,7 +408,7 @@ class _DefaultPub implements Pub { Future _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 values = [ 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> _createPubEnvironment(PubContext context, [ String flutterRootOverride ]) async { + Future> _createPubEnvironment(PubContext context, [ String? flutterRootOverride ]) async { final Map environment = { - '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; } diff --git a/packages/flutter_tools/test/general.shard/build_system/source_test.dart b/packages/flutter_tools/test/general.shard/build_system/source_test.dart index 248ca0e731..46e1014630 100644 --- a/packages/flutter_tools/test/general.shard/build_system/source_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/source_test.dart @@ -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( diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart index dc426b6653..a0fdf01c4e 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart @@ -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 kDart2jsLinuxArgs = [ diff --git a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart index ab7ccce922..b7685c9c08 100644 --- a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart +++ b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart @@ -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: [ @@ -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([ @@ -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) { diff --git a/packages/flutter_tools/test/src/throwing_pub.dart b/packages/flutter_tools/test/src/throwing_pub.dart index b5dfd85809..b4ff8a12ec 100644 --- a/packages/flutter_tools/test/src/throwing_pub.dart +++ b/packages/flutter_tools/test/src/throwing_pub.dart @@ -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 batch(List 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 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 interactively( List arguments, { - String directory, - @required Stdio stdio, + String? directory, + required Stdio stdio, bool touchesPackageConfig = false, bool generateSyntheticPackage = false, }) {