Address some comments from #38632 (2019) if they still apply (#171461)

Closes https://github.com/flutter/flutter/pull/38632.

Mostly clarifies how validation works, and reuses some code.
This commit is contained in:
Matan Lurey 2025-07-01 20:10:20 -07:00 committed by GitHub
parent c2aa5180f0
commit 1c3afdf630
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -76,11 +76,12 @@ class Plugin {
if (errors.isNotEmpty) {
throwToolExit('Invalid plugin specification $name.\n${errors.join('\n')}');
}
if (pluginYaml != null && pluginYaml['platforms'] != null) {
if (pluginYaml?['platforms'] != null) {
// SAFETY: Assumes that validatePluginYaml(pluginYaml) has been called.
return Plugin._fromMultiPlatformYaml(
name,
path,
pluginYaml,
pluginYaml!,
flutterConstraint,
dependencies,
fileSystem,
@ -110,11 +111,10 @@ class Plugin {
bool isDirectDependency, {
required bool isDevDependency,
}) {
assert(pluginYaml['platforms'] != null, 'Invalid multi-platform plugin specification $name.');
// SAFETY: This constructor is only invoked from .fromYaml, which validates.
final YamlMap platformsYaml = pluginYaml['platforms'] as YamlMap;
assert(
_validateMultiPlatformYaml(platformsYaml).isEmpty,
_validateMultiPlatformYaml(parentMap: pluginYaml).isEmpty,
'Invalid multi-platform plugin specification $name.',
);
@ -303,20 +303,26 @@ class Plugin {
}
if (usesNewPluginFormat) {
if (yaml['platforms'] != null && yaml['platforms'] is! YamlMap) {
const String errorMessage =
'flutter.plugin.platforms should be a map with the platform name as the key';
return <String>[errorMessage];
}
return _validateMultiPlatformYaml(yaml['platforms'] as YamlMap?);
return _validateMultiPlatformYaml(parentMap: yaml);
} else {
return _validateLegacyYaml(yaml);
}
}
static List<String> _validateMultiPlatformYaml(YamlMap? yaml) {
static List<String> _validateMultiPlatformYaml({required YamlMap parentMap}) {
final Object? platforms = parentMap['platforms'];
if (platforms is! YamlMap?) {
const String errorMessage =
'flutter.plugin.platforms should be a map with the platform name as the key';
return <String>[errorMessage];
}
if (platforms == null) {
return <String>['Invalid "platforms" specification.'];
}
final YamlMap yaml = platforms;
bool isInvalid(String key, bool Function(YamlMap) validate) {
if (!yaml!.containsKey(key)) {
if (!yaml.containsKey(key)) {
return false;
}
final dynamic yamlValue = yaml[key];
@ -329,9 +335,6 @@ class Plugin {
return !validate(yamlValue);
}
if (yaml == null) {
return <String>['Invalid "platforms" specification.'];
}
return <String>[
if (isInvalid(AndroidPlugin.kConfigKey, AndroidPlugin.validate))
'Invalid "android" plugin specification.',