-
Notifications
You must be signed in to change notification settings - Fork 30.8k
Expand file tree
/
Copy pathplugins.dart
More file actions
551 lines (499 loc) · 18.9 KB
/
Copy pathplugins.dart
File metadata and controls
551 lines (499 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';
import 'base/common.dart';
import 'base/file_system.dart';
import 'platform_plugins.dart';
class Plugin {
Plugin({
required this.name,
required this.path,
required this.platforms,
required this.defaultPackagePlatforms,
required this.pluginDartClassPlatforms,
this.flutterConstraint,
required this.dependencies,
required this.isDirectDependency,
required this.isDevDependency,
this.implementsPackage,
});
/// Parses [Plugin] specification from the provided pluginYaml.
///
/// This currently supports two formats. Legacy and Multi-platform.
///
/// Example of the deprecated Legacy format.
///
/// flutter:
/// plugin:
/// androidPackage: io.flutter.plugins.sample
/// iosPrefix: FLT
/// pluginClass: SamplePlugin
///
/// Example Multi-platform format.
///
/// flutter:
/// plugin:
/// platforms:
/// android:
/// package: io.flutter.plugins.sample
/// pluginClass: SamplePlugin
/// ios:
/// # A plugin implemented through method channels.
/// pluginClass: SamplePlugin
/// linux:
/// # A plugin implemented purely in Dart code.
/// dartPluginClass: SamplePlugin
/// # Optional field to determine file containing dartPluginClass.
/// # This file will be used in imports in generated files, e.g.:
/// # import 'package:{{pluginName}}/{{dartFileName}}'
/// # instead of default:
/// # import 'package:{{pluginName}}/{{pluginName}}.dart'
/// dartFileName: src/sample_plugin.dart
/// macos:
/// # A plugin implemented with `dart:ffi`.
/// ffiPlugin: true
/// windows:
/// # A plugin using platform-specific Dart and method channels.
/// dartPluginClass: SamplePlugin
/// pluginClass: SamplePlugin
factory Plugin.fromYaml(
String name,
String path,
YamlMap? pluginYaml,
VersionConstraint? flutterConstraint,
List<String> dependencies, {
required FileSystem fileSystem,
required bool isDevDependency,
Set<String>? appDependencies,
}) {
final List<String> errors = validatePluginYaml(pluginYaml);
if (errors.isNotEmpty) {
throwToolExit('Invalid plugin specification $name.\n${errors.join('\n')}');
}
if (pluginYaml?['platforms'] != null) {
// SAFETY: Assumes that validatePluginYaml(pluginYaml) has been called.
return Plugin._fromMultiPlatformYaml(
name,
path,
pluginYaml!,
flutterConstraint,
dependencies,
fileSystem,
isDevDependency: isDevDependency,
appDependencies != null && appDependencies.contains(name),
);
}
return Plugin._fromLegacyYaml(
name,
path,
pluginYaml,
flutterConstraint,
dependencies,
fileSystem,
isDevDependency: isDevDependency,
appDependencies != null && appDependencies.contains(name),
);
}
factory Plugin._fromMultiPlatformYaml(
String name,
String path,
YamlMap pluginYaml,
VersionConstraint? flutterConstraint,
List<String> dependencies,
FileSystem fileSystem,
bool isDirectDependency, {
required bool isDevDependency,
}) {
// SAFETY: This constructor is only invoked from .fromYaml, which validates.
final platformsYaml = pluginYaml['platforms'] as YamlMap;
assert(
_validateMultiPlatformYaml(parentMap: pluginYaml).isEmpty,
'Invalid multi-platform plugin specification $name.',
);
final platforms = <String, PluginPlatform>{};
if (_providesImplementationForPlatform(platformsYaml, AndroidPlugin.kConfigKey)) {
platforms[AndroidPlugin.kConfigKey] = AndroidPlugin.fromYaml(
name,
platformsYaml[AndroidPlugin.kConfigKey] as YamlMap,
path,
fileSystem,
);
}
if (_providesImplementationForPlatform(platformsYaml, IOSPlugin.kConfigKey)) {
platforms[IOSPlugin.kConfigKey] = IOSPlugin.fromYaml(
name,
platformsYaml[IOSPlugin.kConfigKey] as YamlMap,
);
}
if (_providesImplementationForPlatform(platformsYaml, LinuxPlugin.kConfigKey)) {
platforms[LinuxPlugin.kConfigKey] = LinuxPlugin.fromYaml(
name,
platformsYaml[LinuxPlugin.kConfigKey] as YamlMap,
);
}
if (_providesImplementationForPlatform(platformsYaml, MacOSPlugin.kConfigKey)) {
platforms[MacOSPlugin.kConfigKey] = MacOSPlugin.fromYaml(
name,
platformsYaml[MacOSPlugin.kConfigKey] as YamlMap,
);
}
if (_providesImplementationForPlatform(platformsYaml, WebPlugin.kConfigKey)) {
platforms[WebPlugin.kConfigKey] = WebPlugin.fromYaml(
name,
platformsYaml[WebPlugin.kConfigKey] as YamlMap,
);
}
if (_providesImplementationForPlatform(platformsYaml, WindowsPlugin.kConfigKey)) {
platforms[WindowsPlugin.kConfigKey] = WindowsPlugin.fromYaml(
name,
platformsYaml[WindowsPlugin.kConfigKey] as YamlMap,
);
}
// TODO(stuartmorgan): Consider merging web into this common handling; the
// fact that its implementation of Dart-only plugins and default packages
// are separate is legacy.
final sharedHandlingPlatforms = <String>[
AndroidPlugin.kConfigKey,
IOSPlugin.kConfigKey,
LinuxPlugin.kConfigKey,
MacOSPlugin.kConfigKey,
WindowsPlugin.kConfigKey,
];
final defaultPackages = <String, String>{};
final dartPluginClasses = <String, DartPluginClassAndFilePair>{};
for (final platform in sharedHandlingPlatforms) {
final String? defaultPackage = _getDefaultPackageForPlatform(platformsYaml, platform);
if (defaultPackage != null) {
defaultPackages[platform] = defaultPackage;
}
final DartPluginClassAndFilePair? dartPair = _getPluginDartClassForPlatform(
platformsYaml,
platformKey: platform,
pluginName: name,
);
if (dartPair != null) {
dartPluginClasses[platform] = dartPair;
}
}
return Plugin(
name: name,
path: path,
platforms: platforms,
defaultPackagePlatforms: defaultPackages,
pluginDartClassPlatforms: dartPluginClasses,
flutterConstraint: flutterConstraint,
dependencies: dependencies,
isDirectDependency: isDirectDependency,
implementsPackage: pluginYaml['implements'] != null ? pluginYaml['implements'] as String : '',
isDevDependency: isDevDependency,
);
}
factory Plugin._fromLegacyYaml(
String name,
String path,
dynamic pluginYaml,
VersionConstraint? flutterConstraint,
List<String> dependencies,
FileSystem fileSystem,
bool isDirectDependency, {
required bool isDevDependency,
}) {
final platforms = <String, PluginPlatform>{};
final pluginClass = (pluginYaml as Map<dynamic, dynamic>)['pluginClass'] as String?;
if (pluginClass != null) {
final androidPackage = pluginYaml['androidPackage'] as String?;
if (androidPackage != null) {
platforms[AndroidPlugin.kConfigKey] = AndroidPlugin(
name: name,
package: androidPackage,
pluginClass: pluginClass,
pluginPath: path,
fileSystem: fileSystem,
);
}
final String iosPrefix = pluginYaml['iosPrefix'] as String? ?? '';
platforms[IOSPlugin.kConfigKey] = IOSPlugin(
name: name,
classPrefix: iosPrefix,
pluginClass: pluginClass,
);
}
return Plugin(
name: name,
path: path,
platforms: platforms,
defaultPackagePlatforms: <String, String>{},
pluginDartClassPlatforms: <String, DartPluginClassAndFilePair>{},
flutterConstraint: flutterConstraint,
dependencies: dependencies,
isDirectDependency: isDirectDependency,
isDevDependency: isDevDependency,
);
}
/// Create a YamlMap that represents the supported platforms.
///
/// For example, if the `platforms` contains 'ios' and 'android', the return map looks like:
///
/// android:
/// package: io.flutter.plugins.sample
/// pluginClass: SamplePlugin
/// ios:
/// pluginClass: SamplePlugin
static YamlMap createPlatformsYamlMap(
List<String> platforms,
String pluginClass,
String androidPackage,
) {
final map = <String, dynamic>{};
for (final platform in platforms) {
map[platform] = <String, String>{
'pluginClass': pluginClass,
...platform == 'android' ? <String, String>{'package': androidPackage} : <String, String>{},
};
}
return YamlMap.wrap(map);
}
static List<String> validatePluginYaml(YamlMap? yaml) {
if (yaml == null) {
return <String>['Invalid "plugin" specification.'];
}
final bool usesOldPluginFormat = const <String>{
'androidPackage',
'iosPrefix',
'pluginClass',
}.any(yaml.containsKey);
final bool usesNewPluginFormat = yaml.containsKey('platforms');
if (usesOldPluginFormat && usesNewPluginFormat) {
const errorMessage =
'The flutter.plugin.platforms key cannot be used in combination with the old '
'flutter.plugin.{androidPackage,iosPrefix,pluginClass} keys. '
'See: https://flutter.dev/to/pubspec-plugin-platforms';
return <String>[errorMessage];
}
if (!usesOldPluginFormat && !usesNewPluginFormat) {
const errorMessage =
'Cannot find the `flutter.plugin.platforms` key in the `pubspec.yaml` file. '
'An instruction to format the `pubspec.yaml` can be found here: '
'https://flutter.dev/to/pubspec-plugin-platforms';
return <String>[errorMessage];
}
if (usesNewPluginFormat) {
return _validateMultiPlatformYaml(parentMap: yaml);
} else {
return _validateLegacyYaml(yaml);
}
}
static List<String> _validateMultiPlatformYaml({required YamlMap parentMap}) {
final Object? platforms = parentMap['platforms'];
if (platforms is! YamlMap?) {
const 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)) {
return false;
}
final dynamic yamlValue = yaml[key];
if (yamlValue is! YamlMap) {
return true;
}
if (yamlValue.containsKey('default_package')) {
return false;
}
return !validate(yamlValue);
}
return <String>[
if (isInvalid(AndroidPlugin.kConfigKey, AndroidPlugin.validate))
'Invalid "android" plugin specification.',
if (isInvalid(IOSPlugin.kConfigKey, IOSPlugin.validate))
'Invalid "ios" plugin specification.',
if (isInvalid(LinuxPlugin.kConfigKey, LinuxPlugin.validate))
'Invalid "linux" plugin specification.',
if (isInvalid(MacOSPlugin.kConfigKey, MacOSPlugin.validate))
'Invalid "macos" plugin specification.',
if (isInvalid(WindowsPlugin.kConfigKey, WindowsPlugin.validate))
'Invalid "windows" plugin specification.',
];
}
static List<String> _validateLegacyYaml(YamlMap yaml) {
return <String>[
if (yaml['androidPackage'] is! String?)
'The "androidPackage" must either be null or a string.',
if (yaml['iosPrefix'] is! String?) 'The "iosPrefix" must either be null or a string.',
if (yaml['pluginClass'] is! String?) 'The "pluginClass" must either be null or a string.',
];
}
static bool _supportsPlatform(YamlMap platformsYaml, String platformKey) {
if (!platformsYaml.containsKey(platformKey)) {
return false;
}
if (platformsYaml[platformKey] is YamlMap) {
return true;
}
return false;
}
static String? _getDefaultPackageForPlatform(YamlMap platformsYaml, String platformKey) {
if (!_supportsPlatform(platformsYaml, platformKey)) {
return null;
}
if ((platformsYaml[platformKey] as YamlMap).containsKey(kDefaultPackage)) {
return (platformsYaml[platformKey] as YamlMap)[kDefaultPackage] as String;
}
return null;
}
static DartPluginClassAndFilePair? _getPluginDartClassForPlatform(
YamlMap platformsYaml, {
required String platformKey,
required String pluginName,
}) {
if (!_supportsPlatform(platformsYaml, platformKey)) {
return null;
}
if ((platformsYaml[platformKey] as YamlMap).containsKey(kDartPluginClass)) {
final dartClass = (platformsYaml[platformKey] as YamlMap)[kDartPluginClass] as String;
final String dartFileName =
(platformsYaml[platformKey] as YamlMap)[kDartFileName] as String? ?? '$pluginName.dart';
return (dartClass: dartClass, dartFileName: dartFileName);
}
return null;
}
static bool _providesImplementationForPlatform(YamlMap platformsYaml, String platformKey) {
if (!_supportsPlatform(platformsYaml, platformKey)) {
return false;
}
if ((platformsYaml[platformKey] as YamlMap).containsKey(kDefaultPackage)) {
return false;
}
return true;
}
final String name;
final String path;
/// The name of the interface package that this plugin implements.
/// If `null`, this plugin doesn't implement an interface.
final String? implementsPackage;
/// The required version of Flutter, if specified.
final VersionConstraint? flutterConstraint;
/// The name of the packages this plugin depends on.
final List<String> dependencies;
/// This is a mapping from platform config key to the plugin platform spec.
final Map<String, PluginPlatform> platforms;
/// This is a mapping from platform config key to the default package implementation.
final Map<String, String> defaultPackagePlatforms;
/// This is a mapping from platform config key to the Dart plugin class for the given platform.
final Map<String, DartPluginClassAndFilePair> pluginDartClassPlatforms;
/// Whether this plugin is a direct dependency of the app.
/// If `false`, the plugin is a dependency of another plugin.
final bool isDirectDependency;
/// Whether this plugin is exclusively used as a dev dependency of the app.
///
/// If `false`, the plugin is either:
/// - _Not_ a dev dependency
/// - _Not_ a dev dependency of some dependency that itself is not a dev
/// dependency
///
/// Dev dependencies are intended to be stripped out in release builds.
final bool isDevDependency;
/// Expected path to the plugin's swift package, which contains the Package.swift.
///
/// This path should be `/path/to/[package_name]/[platform]/[package_name]`
/// (e.g. `/path/to/my_plugin/ios/my_plugin`).
///
/// Returns null if the plugin does not support the [platform] or the
/// [platform] is not iOS or macOS.
///
/// If [overridePath] is provided, returns `[overridePath]/[platform]/[package_name]`.
String? pluginSwiftPackagePath(FileSystem fileSystem, String platform, {String? overridePath}) {
final String? platformDirectoryName = _darwinPluginDirectoryName(platform);
if (platformDirectoryName == null) {
return null;
}
if (overridePath != null) {
return fileSystem.path.join(overridePath, platformDirectoryName, name);
}
return fileSystem.path.join(path, platformDirectoryName, name);
}
/// Expected path to the plugin's Package.swift. Returns null if the plugin
/// does not support the [platform] or the [platform] is not iOS or macOS.
String? pluginSwiftPackageManifestPath(FileSystem fileSystem, String platform) {
final String? packagePath = pluginSwiftPackagePath(fileSystem, platform);
if (packagePath == null) {
return null;
}
return fileSystem.path.join(packagePath, 'Package.swift');
}
/// Returns true if the plugin supports the [platform] and a Package.swift exists.
bool supportSwiftPackageManagerForPlatform(FileSystem fileSystem, String platform) {
final String? manifestPath = pluginSwiftPackageManifestPath(fileSystem, platform);
return platforms[platform] != null &&
manifestPath != null &&
fileSystem.file(manifestPath).existsSync();
}
/// Returns true if the plugin supports the [platform] and a podspec exists.
bool supportCocoapodsForPlatform(FileSystem fileSystem, String platform) {
final String? podspecPath = pluginPodspecPath(fileSystem, platform);
return platforms[platform] != null &&
podspecPath != null &&
fileSystem.file(podspecPath).existsSync();
}
/// Expected path to the plugin's podspec. Returns null if the plugin does
/// not support the [platform] or the [platform] is not iOS or macOS.
String? pluginPodspecPath(FileSystem fileSystem, String platform) {
final String? platformDirectoryName = _darwinPluginDirectoryName(platform);
if (platformDirectoryName == null) {
return null;
}
return fileSystem.path.join(path, platformDirectoryName, '$name.podspec');
}
String? _darwinPluginDirectoryName(String platform) {
final PluginPlatform? platformPlugin = platforms[platform];
if (platformPlugin == null ||
(platform != IOSPlugin.kConfigKey && platform != MacOSPlugin.kConfigKey)) {
return null;
}
// iOS and macOS code can be shared in "darwin" directory, otherwise
// respectively in "ios" or "macos" directories.
if (platformPlugin is DarwinPlugin && (platformPlugin as DarwinPlugin).sharedDarwinSource) {
return 'darwin';
}
return platform;
}
}
/// Metadata associated with the resolution of a platform interface of a plugin.
class PluginInterfaceResolution {
PluginInterfaceResolution({required this.plugin, required this.platform});
/// The plugin.
final Plugin plugin;
/// The name of the platform that this plugin implements.
final String platform;
Map<String, String> toMap() {
return <String, String>{
'pluginName': plugin.name,
'platform': platform,
'dartClass': plugin.pluginDartClassPlatforms[platform]?.dartClass ?? '',
'dartFileName': plugin.pluginDartClassPlatforms[platform]?.dartFileName ?? '',
};
}
@override
String toString() {
return '<PluginInterfaceResolution ${plugin.name} for $platform>';
}
}
/// A record representing pair of dartPluginClass and dartFileName used as metadata
/// in [PluginInterfaceResolution].
///
/// The `dartClass` and `dartFileName` fields are guaranteed to be non-null:
///
/// - record should be created only if dartClassName exists in plugin configuration.
/// - dartFileName either taken from configuration, or, if absent, should be
/// constructed from plugin name.
///
/// See also:
/// - [PluginInterfaceResolution], which uses this record to create Map with metadata.
typedef DartPluginClassAndFilePair = ({String dartClass, String dartFileName});