@@ -17,6 +17,11 @@ enum PlatformSupport {
17
17
federated,
18
18
}
19
19
20
+ /// Returns true if [package] is a Flutter plugin.
21
+ bool isFlutterPlugin (RepositoryPackage package) {
22
+ return _readPluginPubspecSection (package) != null ;
23
+ }
24
+
20
25
/// Returns true if [package] is a Flutter [platform] plugin.
21
26
///
22
27
/// It checks this by looking for the following pattern in the pubspec:
@@ -40,46 +45,43 @@ bool pluginSupportsPlatform(
40
45
platform == kPlatformMacos ||
41
46
platform == kPlatformWindows ||
42
47
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)) {
47
60
return false ;
48
61
}
62
+ }
49
63
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)) {
55
70
return false ;
56
71
}
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 ;
76
80
}
77
81
}
78
-
79
- return true ;
80
- } on YamlException {
81
- return false ;
82
82
}
83
+
84
+ return true ;
83
85
}
84
86
85
87
/// Returns true if [plugin] includes native code for [platform] , as opposed to
@@ -89,24 +91,18 @@ bool pluginHasNativeCodeForPlatform(String platform, RepositoryPackage plugin) {
89
91
// Web plugins are always Dart-only.
90
92
return false ;
91
93
}
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 ) {
108
97
return false ;
109
98
}
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' ;
110
106
}
111
107
112
108
/// Returns the
@@ -118,26 +114,33 @@ bool pluginHasNativeCodeForPlatform(String platform, RepositoryPackage plugin) {
118
114
/// or the pubspec couldn't be read.
119
115
YamlMap ? _readPlatformPubspecSectionForPlugin (
120
116
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 ()) {
139
137
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 ) {
141
143
return null ;
142
144
}
145
+ return flutterSection['plugin' ] as YamlMap ? ;
143
146
}
0 commit comments