@@ -17,6 +17,11 @@ enum PlatformSupport {
1717 federated,
1818}
1919
20+ /// Returns true if [package] is a Flutter plugin.
21+ bool isFlutterPlugin (RepositoryPackage package) {
22+ return _readPluginPubspecSection (package) != null ;
23+ }
24+
2025/// Returns true if [package] is a Flutter [platform] plugin.
2126///
2227/// It checks this by looking for the following pattern in the pubspec:
@@ -40,46 +45,43 @@ bool pluginSupportsPlatform(
4045 platform == kPlatformMacos ||
4146 platform == kPlatformWindows ||
4247 platform == kPlatformLinux);
43- try {
44- final YamlMap ? platformEntry =
45- _readPlatformPubspecSectionForPlugin (platform, plugin);
46- if (platformEntry == null ) {
48+
49+ final YamlMap ? platformEntry =
50+ _readPlatformPubspecSectionForPlugin (platform, plugin);
51+ if (platformEntry == null ) {
52+ return false ;
53+ }
54+
55+ // If the platform entry is present, then it supports the platform. Check
56+ // for required mode if specified.
57+ if (requiredMode != null ) {
58+ final bool federated = platformEntry.containsKey ('default_package' );
59+ if (federated != (requiredMode == PlatformSupport .federated)) {
4760 return false ;
4861 }
62+ }
4963
50- // If the platform entry is present, then it supports the platform. Check
51- // for required mode if specified.
52- if (requiredMode != null ) {
53- final bool federated = platformEntry.containsKey ('default_package' );
54- if (federated != (requiredMode == PlatformSupport .federated)) {
64+ // If a variant is specified, check for that variant.
65+ if (variant != null ) {
66+ const String variantsKey = 'supportedVariants' ;
67+ if (platformEntry.containsKey (variantsKey)) {
68+ if (! (platformEntry['supportedVariants' ]! as YamlList )
69+ .contains (variant)) {
5570 return false ;
5671 }
57- }
58-
59- // If a variant is specified, check for that variant.
60- if (variant != null ) {
61- const String variantsKey = 'supportedVariants' ;
62- if (platformEntry.containsKey (variantsKey)) {
63- if (! (platformEntry['supportedVariants' ]! as YamlList )
64- .contains (variant)) {
65- return false ;
66- }
67- } else {
68- // Platforms with variants have a default variant when unspecified for
69- // backward compatibility. Must match the flutter tool logic.
70- const Map <String , String > defaultVariants = < String , String > {
71- kPlatformWindows: platformVariantWin32,
72- };
73- if (variant != defaultVariants[platform]) {
74- return false ;
75- }
72+ } else {
73+ // Platforms with variants have a default variant when unspecified for
74+ // backward compatibility. Must match the flutter tool logic.
75+ const Map <String , String > defaultVariants = < String , String > {
76+ kPlatformWindows: platformVariantWin32,
77+ };
78+ if (variant != defaultVariants[platform]) {
79+ return false ;
7680 }
7781 }
78-
79- return true ;
80- } on YamlException {
81- return false ;
8282 }
83+
84+ return true ;
8385}
8486
8587/// Returns true if [plugin] includes native code for [platform] , as opposed to
@@ -89,24 +91,18 @@ bool pluginHasNativeCodeForPlatform(String platform, RepositoryPackage plugin) {
8991 // Web plugins are always Dart-only.
9092 return false ;
9193 }
92- try {
93- final YamlMap ? platformEntry =
94- _readPlatformPubspecSectionForPlugin (platform, plugin);
95- if (platformEntry == null ) {
96- return false ;
97- }
98- // All other platforms currently use pluginClass for indicating the native
99- // code in the plugin.
100- final String ? pluginClass = platformEntry['pluginClass' ] as String ? ;
101- // TODO(stuartmorgan): Remove the check for 'none' once none of the plugins
102- // in the repository use that workaround. See
103- // https://github.com/flutter/flutter/issues/57497 for context.
104- return pluginClass != null && pluginClass != 'none' ;
105- } on FileSystemException {
106- return false ;
107- } on YamlException {
94+ final YamlMap ? platformEntry =
95+ _readPlatformPubspecSectionForPlugin (platform, plugin);
96+ if (platformEntry == null ) {
10897 return false ;
10998 }
99+ // All other platforms currently use pluginClass for indicating the native
100+ // code in the plugin.
101+ final String ? pluginClass = platformEntry['pluginClass' ] as String ? ;
102+ // TODO(stuartmorgan): Remove the check for 'none' once none of the plugins
103+ // in the repository use that workaround. See
104+ // https://github.com/flutter/flutter/issues/57497 for context.
105+ return pluginClass != null && pluginClass != 'none' ;
110106}
111107
112108/// Returns the
@@ -118,26 +114,33 @@ bool pluginHasNativeCodeForPlatform(String platform, RepositoryPackage plugin) {
118114/// or the pubspec couldn't be read.
119115YamlMap ? _readPlatformPubspecSectionForPlugin (
120116 String platform, RepositoryPackage plugin) {
121- try {
122- final File pubspecFile = plugin.pubspecFile;
123- final YamlMap pubspecYaml =
124- loadYaml (pubspecFile.readAsStringSync ()) as YamlMap ;
125- final YamlMap ? flutterSection = pubspecYaml['flutter' ] as YamlMap ? ;
126- if (flutterSection == null ) {
127- return null ;
128- }
129- final YamlMap ? pluginSection = flutterSection['plugin' ] as YamlMap ? ;
130- if (pluginSection == null ) {
131- return null ;
132- }
133- final YamlMap ? platforms = pluginSection['platforms' ] as YamlMap ? ;
134- if (platforms == null ) {
135- return null ;
136- }
137- return platforms[platform] as YamlMap ? ;
138- } on FileSystemException {
117+ final YamlMap ? pluginSection = _readPluginPubspecSection (plugin);
118+ if (pluginSection == null ) {
119+ return null ;
120+ }
121+ final YamlMap ? platforms = pluginSection['platforms' ] as YamlMap ? ;
122+ if (platforms == null ) {
123+ return null ;
124+ }
125+ return platforms[platform] as YamlMap ? ;
126+ }
127+
128+ /// Returns the
129+ /// flutter:
130+ /// plugin:
131+ /// platforms:
132+ /// section from [plugin] 's pubspec.yaml, or null if either it is not present,
133+ /// or the pubspec couldn't be read.
134+ YamlMap ? _readPluginPubspecSection (RepositoryPackage package) {
135+ final File pubspecFile = package.pubspecFile;
136+ if (! pubspecFile.existsSync ()) {
139137 return null ;
140- } on YamlException {
138+ }
139+ final YamlMap pubspecYaml =
140+ loadYaml (pubspecFile.readAsStringSync ()) as YamlMap ;
141+ final YamlMap ? flutterSection = pubspecYaml['flutter' ] as YamlMap ? ;
142+ if (flutterSection == null ) {
141143 return null ;
142144 }
145+ return flutterSection['plugin' ] as YamlMap ? ;
143146}
0 commit comments