[flutter_plugin_tools] Introduce a class for packages (#4252)

Packages are the primary conceptual object in the tool, but currently they are represented simply as Directory (or occasionally a path string). This introduces an object for packages and:
- moves a number of existing utility methods into it
- sweeps the code for the obvious cases of using `Directory` to represent a package, especially in method signatures and migrates them
- notes a few places where we should migrate later, to avoid ballooning the size of the PR

There are no doubt other cases not caught in the sweep, but this gives us a foundation both for new code, and to migrate incrementally toward as we find existing code that was missed.
This commit is contained in:
stuartmorgan
2021-08-24 16:29:56 -04:00
committed by GitHub
parent 74cf0287f9
commit 41f1c806f2
27 changed files with 440 additions and 334 deletions

View File

@ -10,6 +10,7 @@ import 'package:pubspec_parse/pubspec_parse.dart';
import 'common/core.dart';
import 'common/package_looping_command.dart';
import 'common/process_runner.dart';
import 'common/repository_package.dart';
/// A command to enforce pubspec conventions across the repository.
///
@ -64,8 +65,8 @@ class PubspecCheckCommand extends PackageLoopingCommand {
bool get includeSubpackages => true;
@override
Future<PackageResult> runForPackage(Directory package) async {
final File pubspec = package.childFile('pubspec.yaml');
Future<PackageResult> runForPackage(RepositoryPackage package) async {
final File pubspec = package.pubspecFile;
final bool passesCheck =
!pubspec.existsSync() || await _checkPubspec(pubspec, package: package);
if (!passesCheck) {
@ -76,7 +77,7 @@ class PubspecCheckCommand extends PackageLoopingCommand {
Future<bool> _checkPubspec(
File pubspecFile, {
required Directory package,
required RepositoryPackage package,
}) async {
final String contents = pubspecFile.readAsStringSync();
final Pubspec? pubspec = _tryParsePubspec(contents);
@ -154,7 +155,7 @@ class PubspecCheckCommand extends PackageLoopingCommand {
List<String> _checkForRepositoryLinkErrors(
Pubspec pubspec, {
required Directory package,
required RepositoryPackage package,
}) {
final List<String> errorMessages = <String>[];
if (pubspec.repository == null) {
@ -189,12 +190,12 @@ class PubspecCheckCommand extends PackageLoopingCommand {
// Should only be called on plugin packages.
String? _checkForImplementsError(
Pubspec pubspec, {
required Directory package,
required RepositoryPackage package,
}) {
if (_isImplementationPackage(package)) {
final String? implements =
pubspec.flutter!['plugin']!['implements'] as String?;
final String expectedImplements = package.parent.basename;
final String expectedImplements = package.directory.parent.basename;
if (implements == null) {
return 'Missing "implements: $expectedImplements" in "plugin" section.';
} else if (implements != expectedImplements) {
@ -207,13 +208,13 @@ class PubspecCheckCommand extends PackageLoopingCommand {
// Returns true if [packageName] appears to be an implementation package
// according to repository conventions.
bool _isImplementationPackage(Directory package) {
bool _isImplementationPackage(RepositoryPackage package) {
// An implementation package should be in a group folder...
final Directory parentDir = package.parent;
final Directory parentDir = package.directory.parent;
if (parentDir.path == packagesDir.path) {
return false;
}
final String packageName = package.basename;
final String packageName = package.directory.basename;
final String parentName = parentDir.basename;
// ... whose name is a prefix of the package name.
if (!packageName.startsWith(parentName)) {