This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Begin migrating tools to NNBD #3891
Merged
stuartmorgan-g
merged 8 commits into
flutter:master
from
stuartmorgan-g:nnbd-tool-initial
May 15, 2021
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a5cb81c
Upgrade all dependencies to null-safe versions (and sort them)
stuartmorgan-g 0529a3a
Introduce convenience methods for arg access
stuartmorgan-g e4ce35d
Merge branch 'master' into nnbd-tool-initial
stuartmorgan-g ba38415
Run migration and do minor fixup
stuartmorgan-g 0884c7e
Merge branch 'master' into nnbd-tool-initial
stuartmorgan-g 95fab43
Fix unnecessary nullable
stuartmorgan-g 208eba8
Fix mock matchers to work on all calls
stuartmorgan-g 36da02c
Don't skip Flutter upgrade for podspect lint task
stuartmorgan-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,13 @@ import 'package:colorize/colorize.dart'; | |
| import 'package:file/file.dart'; | ||
| import 'package:git/git.dart'; | ||
| import 'package:http/http.dart' as http; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:path/path.dart' as p; | ||
| import 'package:pub_semver/pub_semver.dart'; | ||
| import 'package:yaml/yaml.dart'; | ||
|
|
||
| /// The signature for a print handler for commands that allow overriding the | ||
| /// print destination. | ||
| typedef Print = void Function(Object object); | ||
| typedef Print = void Function(Object? object); | ||
|
|
||
| /// Key for windows platform. | ||
| const String kWindows = 'windows'; | ||
|
|
@@ -50,7 +49,7 @@ const String kEnableExperiment = 'enable-experiment'; | |
|
|
||
| /// Returns whether the given directory contains a Flutter package. | ||
| bool isFlutterPackage(FileSystemEntity entity, FileSystem fileSystem) { | ||
| if (entity == null || entity is! Directory) { | ||
| if (entity is! Directory) { | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -59,7 +58,7 @@ bool isFlutterPackage(FileSystemEntity entity, FileSystem fileSystem) { | |
| fileSystem.file(p.join(entity.path, 'pubspec.yaml')); | ||
| final YamlMap pubspecYaml = | ||
| loadYaml(pubspecFile.readAsStringSync()) as YamlMap; | ||
| final YamlMap dependencies = pubspecYaml['dependencies'] as YamlMap; | ||
| final YamlMap? dependencies = pubspecYaml['dependencies'] as YamlMap?; | ||
| if (dependencies == null) { | ||
| return false; | ||
| } | ||
|
|
@@ -87,7 +86,7 @@ bool pluginSupportsPlatform( | |
| platform == kMacos || | ||
| platform == kWindows || | ||
| platform == kLinux); | ||
| if (entity == null || entity is! Directory) { | ||
| if (entity is! Directory) { | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -96,15 +95,15 @@ bool pluginSupportsPlatform( | |
| fileSystem.file(p.join(entity.path, 'pubspec.yaml')); | ||
| final YamlMap pubspecYaml = | ||
| loadYaml(pubspecFile.readAsStringSync()) as YamlMap; | ||
| final YamlMap flutterSection = pubspecYaml['flutter'] as YamlMap; | ||
| final YamlMap? flutterSection = pubspecYaml['flutter'] as YamlMap?; | ||
| if (flutterSection == null) { | ||
| return false; | ||
| } | ||
| final YamlMap pluginSection = flutterSection['plugin'] as YamlMap; | ||
| final YamlMap? pluginSection = flutterSection['plugin'] as YamlMap?; | ||
| if (pluginSection == null) { | ||
| return false; | ||
| } | ||
| final YamlMap platforms = pluginSection['platforms'] as YamlMap; | ||
| final YamlMap? platforms = pluginSection['platforms'] as YamlMap?; | ||
| if (platforms == null) { | ||
| // Legacy plugin specs are assumed to support iOS and Android. | ||
| if (!pluginSection.containsKey('platforms')) { | ||
|
|
@@ -151,7 +150,7 @@ bool isLinuxPlugin(FileSystemEntity entity, FileSystem fileSystem) { | |
| } | ||
|
|
||
| /// Throws a [ToolExit] with `exitCode` and log the `errorMessage` in red. | ||
| void printErrorAndExit({@required String errorMessage, int exitCode = 1}) { | ||
| void printErrorAndExit({required String errorMessage, int exitCode = 1}) { | ||
| final Colorize redError = Colorize(errorMessage)..red(); | ||
| print(redError); | ||
| throw ToolExit(exitCode); | ||
|
|
@@ -236,30 +235,45 @@ abstract class PluginCommand extends Command<void> { | |
| /// The git directory to use. By default it uses the parent directory. | ||
| /// | ||
| /// This can be mocked for testing. | ||
| final GitDir gitDir; | ||
| final GitDir? gitDir; | ||
|
|
||
| int _shardIndex; | ||
| int _shardCount; | ||
| int? _shardIndex; | ||
| int? _shardCount; | ||
|
|
||
| /// The shard of the overall command execution that this instance should run. | ||
| int get shardIndex { | ||
| if (_shardIndex == null) { | ||
| _checkSharding(); | ||
| } | ||
| return _shardIndex; | ||
| return _shardIndex!; | ||
| } | ||
|
|
||
| /// The number of shards this command is divided into. | ||
| int get shardCount { | ||
| if (_shardCount == null) { | ||
| _checkSharding(); | ||
| } | ||
| return _shardCount; | ||
| return _shardCount!; | ||
| } | ||
|
|
||
| /// Convenience accessor for boolean arguments. | ||
| bool getBoolArg(String key) { | ||
| return (argResults![key] as bool?) ?? false; | ||
| } | ||
|
|
||
| /// Convenience accessor for String arguments. | ||
| String getStringArg(String key) { | ||
| return (argResults![key] as String?) ?? ''; | ||
| } | ||
|
|
||
| /// Convenience accessor for List<String> arguments. | ||
| List<String> getStringListArg(String key) { | ||
| return (argResults![key] as List<String>?) ?? <String>[]; | ||
| } | ||
|
|
||
| void _checkSharding() { | ||
| final int shardIndex = int.tryParse(argResults[_shardIndexArg] as String); | ||
| final int shardCount = int.tryParse(argResults[_shardCountArg] as String); | ||
| final int? shardIndex = int.tryParse(getStringArg(_shardIndexArg)); | ||
| final int? shardCount = int.tryParse(getStringArg(_shardCountArg)); | ||
| if (shardIndex == null) { | ||
| usageException('$_shardIndexArg must be an integer'); | ||
| } | ||
|
|
@@ -317,12 +331,10 @@ abstract class PluginCommand extends Command<void> { | |
| /// is a sibling of the packages directory. This is used for a small number | ||
| /// of packages in the flutter/packages repository. | ||
| Stream<Directory> _getAllPlugins() async* { | ||
| Set<String> plugins = | ||
| Set<String>.from(argResults[_pluginsArg] as List<String>); | ||
| Set<String> plugins = Set<String>.from(getStringListArg(_pluginsArg)); | ||
| final Set<String> excludedPlugins = | ||
| Set<String>.from(argResults[_excludeArg] as List<String>); | ||
| final bool runOnChangedPackages = | ||
| argResults[_runOnChangedPackagesArg] as bool; | ||
| Set<String>.from(getStringListArg(_excludeArg)); | ||
| final bool runOnChangedPackages = getBoolArg(_runOnChangedPackagesArg); | ||
| if (plugins.isEmpty && runOnChangedPackages) { | ||
| plugins = await _getChangedPackages(); | ||
| } | ||
|
|
@@ -429,9 +441,9 @@ abstract class PluginCommand extends Command<void> { | |
| /// Throws tool exit if [gitDir] nor root directory is a git directory. | ||
| Future<GitVersionFinder> retrieveVersionFinder() async { | ||
| final String rootDir = packagesDir.parent.absolute.path; | ||
| final String baseSha = argResults[_kBaseSha] as String; | ||
| final String baseSha = getStringArg(_kBaseSha); | ||
|
|
||
| GitDir baseGitDir = gitDir; | ||
| GitDir? baseGitDir = gitDir; | ||
| if (baseGitDir == null) { | ||
| if (!await GitDir.isGitDir(rootDir)) { | ||
| printErrorAndExit( | ||
|
|
@@ -490,7 +502,7 @@ class ProcessRunner { | |
| Future<int> runAndStream( | ||
| String executable, | ||
| List<String> args, { | ||
| Directory workingDir, | ||
| Directory? workingDir, | ||
| bool exitOnError = false, | ||
| }) async { | ||
| print( | ||
|
|
@@ -522,7 +534,7 @@ class ProcessRunner { | |
| /// | ||
| /// Returns the [io.ProcessResult] of the [executable]. | ||
| Future<io.ProcessResult> run(String executable, List<String> args, | ||
| {Directory workingDir, | ||
| {Directory? workingDir, | ||
| bool exitOnError = false, | ||
| bool logOnError = false, | ||
| Encoding stdoutEncoding = io.systemEncoding, | ||
|
|
@@ -550,15 +562,15 @@ class ProcessRunner { | |
| /// passing [workingDir]. | ||
| /// | ||
| /// Returns the started [io.Process]. | ||
| Future<io.Process> start(String executable, List<String> args, | ||
| {Directory workingDirectory}) async { | ||
| Future<io.Process?> start(String executable, List<String> args, | ||
| {Directory? workingDirectory}) async { | ||
| final io.Process process = await io.Process.start(executable, args, | ||
| workingDirectory: workingDirectory?.path); | ||
| return process; | ||
| } | ||
|
|
||
| String _getErrorString(String executable, List<String> args, | ||
| {Directory workingDir}) { | ||
| {Directory? workingDir}) { | ||
| final String workdir = workingDir == null ? '' : ' in ${workingDir.path}'; | ||
| return 'ERROR: Unable to execute "$executable ${args.join(' ')}"$workdir.'; | ||
| } | ||
|
|
@@ -569,7 +581,7 @@ class PubVersionFinder { | |
| /// Constructor. | ||
| /// | ||
| /// Note: you should manually close the [httpClient] when done using the finder. | ||
| PubVersionFinder({this.pubHost = defaultPubHost, @required this.httpClient}); | ||
| PubVersionFinder({this.pubHost = defaultPubHost, required this.httpClient}); | ||
|
|
||
| /// The default pub host to use. | ||
| static const String defaultPubHost = 'https://pub.dev'; | ||
|
|
@@ -584,7 +596,7 @@ class PubVersionFinder { | |
|
|
||
| /// Get the package version on pub. | ||
| Future<PubVersionFinderResponse> getPackageVersion( | ||
| {@required String package}) async { | ||
| {required String? package}) async { | ||
|
||
| assert(package != null && package.isNotEmpty); | ||
| final Uri pubHostUri = Uri.parse(pubHost); | ||
| final Uri url = pubHostUri.replace(path: '/packages/$package.json'); | ||
|
|
@@ -618,8 +630,8 @@ class PubVersionFinder { | |
| class PubVersionFinderResponse { | ||
| /// Constructor. | ||
| PubVersionFinderResponse({this.versions, this.result, this.httpResponse}) { | ||
| if (versions != null && versions.isNotEmpty) { | ||
| versions.sort((Version a, Version b) { | ||
| if (versions != null && versions!.isNotEmpty) { | ||
| versions!.sort((Version a, Version b) { | ||
| // TODO(cyanglaz): Think about how to handle pre-release version with [Version.prioritize]. | ||
| // https://github.com/flutter/flutter/issues/82222 | ||
| return b.compareTo(a); | ||
|
|
@@ -631,13 +643,13 @@ class PubVersionFinderResponse { | |
| /// | ||
| /// This is sorted by largest to smallest, so the first element in the list is the largest version. | ||
| /// Might be `null` if the [result] is not [PubVersionFinderResult.success]. | ||
| final List<Version> versions; | ||
| final List<Version>? versions; | ||
|
|
||
| /// The result of the version finder. | ||
| final PubVersionFinderResult result; | ||
| final PubVersionFinderResult? result; | ||
|
|
||
| /// The response object of the http request. | ||
| final http.Response httpResponse; | ||
| final http.Response? httpResponse; | ||
| } | ||
|
|
||
| /// An enum representing the result of [PubVersionFinder]. | ||
|
|
@@ -667,7 +679,7 @@ class GitVersionFinder { | |
| final GitDir baseGitDir; | ||
|
|
||
| /// The base sha used to get diff. | ||
| final String baseSha; | ||
| final String? baseSha; | ||
|
|
||
| static bool _isPubspec(String file) { | ||
| return file.trim().endsWith('pubspec.yaml'); | ||
|
|
@@ -684,8 +696,7 @@ class GitVersionFinder { | |
| final io.ProcessResult changedFilesCommand = await baseGitDir | ||
| .runCommand(<String>['diff', '--name-only', baseSha, 'HEAD']); | ||
| print('Determine diff with base sha: $baseSha'); | ||
| final String changedFilesStdout = | ||
| changedFilesCommand.stdout.toString() ?? ''; | ||
| final String changedFilesStdout = changedFilesCommand.stdout.toString(); | ||
| if (changedFilesStdout.isEmpty) { | ||
| return <String>[]; | ||
| } | ||
|
|
@@ -696,7 +707,8 @@ class GitVersionFinder { | |
|
|
||
| /// Get the package version specified in the pubspec file in `pubspecPath` and | ||
| /// at the revision of `gitRef` (defaulting to the base if not provided). | ||
| Future<Version> getPackageVersion(String pubspecPath, {String gitRef}) async { | ||
| Future<Version?> getPackageVersion(String pubspecPath, | ||
| {String? gitRef}) async { | ||
| final String ref = gitRef ?? (await _getBaseSha()); | ||
|
|
||
| io.ProcessResult gitShow; | ||
|
|
@@ -707,20 +719,19 @@ class GitVersionFinder { | |
| return null; | ||
| } | ||
| final String fileContent = gitShow.stdout as String; | ||
| final String versionString = loadYaml(fileContent)['version'] as String; | ||
| final String? versionString = loadYaml(fileContent)['version'] as String?; | ||
| return versionString == null ? null : Version.parse(versionString); | ||
| } | ||
|
|
||
| Future<String> _getBaseSha() async { | ||
| if (baseSha != null && baseSha.isNotEmpty) { | ||
| return baseSha; | ||
| if (baseSha != null && baseSha!.isNotEmpty) { | ||
| return baseSha!; | ||
| } | ||
|
|
||
| io.ProcessResult baseShaFromMergeBase = await baseGitDir.runCommand( | ||
| <String>['merge-base', '--fork-point', 'FETCH_HEAD', 'HEAD'], | ||
| throwOnError: false); | ||
| if (baseShaFromMergeBase == null || | ||
| baseShaFromMergeBase.stderr != null || | ||
| if (baseShaFromMergeBase.stderr != null || | ||
| baseShaFromMergeBase.stdout == null) { | ||
| baseShaFromMergeBase = await baseGitDir | ||
| .runCommand(<String>['merge-base', 'FETCH_HEAD', 'HEAD']); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this since it is done in the very beginning of the method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the very beginning of the method :)