platform_plugins.dart 11.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';
import 'package:yaml/yaml.dart';

8 9
import 'base/common.dart';
import 'base/file_system.dart';
10
import 'globals.dart' as globals;
11

12 13 14 15 16 17
/// Constant for 'pluginClass' key in plugin maps.
const String kPluginClass = 'pluginClass';

/// Constant for 'pluginClass' key in plugin maps.
const String kDartPluginClass = 'dartPluginClass';

18 19 20 21 22 23 24
/// Marker interface for all platform specific plugin config impls.
abstract class PluginPlatform {
  const PluginPlatform();

  Map<String, dynamic> toMap();
}

25 26 27 28 29 30
abstract class NativeOrDartPlugin {
  /// Determines whether the plugin has a native implementation or if it's a
  /// Dart-only plugin.
  bool isNative();
}

31 32 33 34 35
/// Contains parameters to template an Android plugin.
///
/// The required fields include: [name] of the plugin, [package] of the plugin and
/// the [pluginClass] that will be the entry point to the plugin's native code.
class AndroidPlugin extends PluginPlatform {
36
  AndroidPlugin({
37 38 39
    @required this.name,
    @required this.package,
    @required this.pluginClass,
40
    @required this.pluginPath,
41 42
  });

43
  factory AndroidPlugin.fromYaml(String name, YamlMap yaml, String pluginPath) {
44 45 46
    assert(validate(yaml));
    return AndroidPlugin(
      name: name,
47 48
      package: yaml['package'] as String,
      pluginClass: yaml['pluginClass'] as String,
49
      pluginPath: pluginPath,
50 51 52 53 54 55 56 57 58 59 60 61
    );
  }

  static bool validate(YamlMap yaml) {
    if (yaml == null) {
      return false;
    }
    return yaml['package'] is String && yaml['pluginClass'] is String;
  }

  static const String kConfigKey = 'android';

62
  /// The plugin name defined in pubspec.yaml.
63
  final String name;
64 65

  /// The plugin package name defined in pubspec.yaml.
66
  final String package;
67 68

  /// The plugin main class defined in pubspec.yaml.
69 70
  final String pluginClass;

71 72 73
  /// The absolute path to the plugin in the pub cache.
  final String pluginPath;

74 75 76 77 78 79
  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
      'package': package,
      'class': pluginClass,
80 81 82
      // Mustache doesn't support complex types.
      'supportsEmbeddingV1': _supportedEmbedings.contains('1'),
      'supportsEmbeddingV2': _supportedEmbedings.contains('2'),
83 84
    };
  }
85

86
  Set<String> _cachedEmbeddingVersion;
87 88

  /// Returns the version of the Android embedding.
89
  Set<String> get _supportedEmbedings => _cachedEmbeddingVersion ??= _getSupportedEmbeddings();
90

91
  Set<String> _getSupportedEmbeddings() {
92
    assert(pluginPath != null);
93
    final Set<String> supportedEmbeddings = <String>{};
94
    final String baseMainPath = globals.fs.path.join(
95 96 97 98 99
      pluginPath,
      'android',
      'src',
      'main',
    );
100 101

    final List<String> mainClassCandidates = <String>[
102
      globals.fs.path.join(
103 104
        baseMainPath,
        'java',
105
        package.replaceAll('.', globals.fs.path.separator),
106
        '$pluginClass.java',
107 108 109 110 111 112
      ),
      globals.fs.path.join(
        baseMainPath,
        'kotlin',
        package.replaceAll('.', globals.fs.path.separator),
        '$pluginClass.kt',
113
      )
114 115 116 117 118 119 120 121 122 123 124 125 126 127
    ];

    File mainPluginClass;
    bool mainClassFound = false;
    for (final String mainClassCandidate in mainClassCandidates) {
      mainPluginClass = globals.fs.file(mainClassCandidate);
      if (mainPluginClass.existsSync()) {
        mainClassFound = true;
        break;
      }
    }
    if (!mainClassFound) {
      assert(mainClassCandidates.length <= 2);
      throwToolExit(
128 129
        "The plugin `$name` doesn't have a main class defined in ${mainClassCandidates.join(' or ')}. "
        "This is likely to due to an incorrect `androidPackage: $package` or `mainClass` entry in the plugin's pubspec.yaml.\n"
130 131
        'If you are the author of this plugin, fix the `androidPackage` entry or move the main class to any of locations used above. '
        'Otherwise, please contact the author of this plugin and consider using a different plugin in the meanwhile. '
132 133
      );
    }
134

135 136 137 138 139
    String mainClassContent;
    try {
      mainClassContent = mainPluginClass.readAsStringSync();
    } on FileSystemException {
      throwToolExit(
140
        "Couldn't read file ${mainPluginClass.path} even though it exists. "
141 142 143 144 145
        'Please verify that this file has read permission and try again.'
      );
    }
    if (mainClassContent
        .contains('io.flutter.embedding.engine.plugins.FlutterPlugin')) {
146 147 148
      supportedEmbeddings.add('2');
    } else {
      supportedEmbeddings.add('1');
149
    }
150 151 152 153 154
    if (mainClassContent.contains('PluginRegistry')
        && mainClassContent.contains('registerWith')) {
      supportedEmbeddings.add('1');
    }
    return supportedEmbeddings;
155
  }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

/// Contains the parameters to template an iOS plugin.
///
/// The required fields include: [name] of the plugin, the [pluginClass] that
/// will be the entry point to the plugin's native code.
class IOSPlugin extends PluginPlatform {
  const IOSPlugin({
    @required this.name,
    this.classPrefix,
    @required this.pluginClass,
  });

  factory IOSPlugin.fromYaml(String name, YamlMap yaml) {
    assert(validate(yaml));
    return IOSPlugin(
      name: name,
      classPrefix: '',
174
      pluginClass: yaml['pluginClass'] as String,
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
    );
  }

  static bool validate(YamlMap yaml) {
    if (yaml == null) {
      return false;
    }
    return yaml['pluginClass'] is String;
  }

  static const String kConfigKey = 'ios';

  final String name;

  /// Note, this is here only for legacy reasons. Multi-platform format
  /// always sets it to empty String.
  final String classPrefix;
  final String pluginClass;

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
      'prefix': classPrefix,
      'class': pluginClass,
    };
  }
}

/// Contains the parameters to template a macOS plugin.
///
206 207 208
/// The [name] of the plugin is required. Either [dartPluginClass] or [pluginClass] are required.
/// [pluginClass] will be the entry point to the plugin's native code.
class MacOSPlugin extends PluginPlatform implements NativeOrDartPlugin {
209 210
  const MacOSPlugin({
    @required this.name,
211 212
    this.pluginClass,
    this.dartPluginClass,
213 214 215 216
  });

  factory MacOSPlugin.fromYaml(String name, YamlMap yaml) {
    assert(validate(yaml));
217 218 219 220 221
    // Treat 'none' as not present. See https://github.com/flutter/flutter/issues/57497.
    String pluginClass = yaml[kPluginClass] as String;
    if (pluginClass == 'none') {
      pluginClass = null;
    }
222 223
    return MacOSPlugin(
      name: name,
224
      pluginClass: pluginClass,
225
      dartPluginClass: yaml[kDartPluginClass] as String,
226 227 228 229 230 231 232
    );
  }

  static bool validate(YamlMap yaml) {
    if (yaml == null) {
      return false;
    }
233
    return yaml[kPluginClass] is String || yaml[kDartPluginClass] is String;
234 235 236 237 238 239
  }

  static const String kConfigKey = 'macos';

  final String name;
  final String pluginClass;
240 241 242 243
  final String dartPluginClass;

  @override
  bool isNative() => pluginClass != null;
244 245 246 247 248

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
249 250
      if (pluginClass != null) 'class': pluginClass,
      if (dartPluginClass != null) 'dartPluginClass': dartPluginClass,
251 252 253
    };
  }
}
254

255 256
/// Contains the parameters to template a Windows plugin.
///
257 258 259
/// The [name] of the plugin is required. Either [dartPluginClass] or [pluginClass] are required.
/// [pluginClass] will be the entry point to the plugin's native code.
class WindowsPlugin extends PluginPlatform implements NativeOrDartPlugin{
260 261
  const WindowsPlugin({
    @required this.name,
262 263 264
    this.pluginClass,
    this.dartPluginClass,
  }) : assert(pluginClass != null || dartPluginClass != null);
265 266 267

  factory WindowsPlugin.fromYaml(String name, YamlMap yaml) {
    assert(validate(yaml));
268 269 270 271 272
    // Treat 'none' as not present. See https://github.com/flutter/flutter/issues/57497.
    String pluginClass = yaml[kPluginClass] as String;
    if (pluginClass == 'none') {
      pluginClass = null;
    }
273 274
    return WindowsPlugin(
      name: name,
275
      pluginClass: pluginClass,
276
      dartPluginClass: yaml[kDartPluginClass] as String,
277 278 279 280 281 282 283
    );
  }

  static bool validate(YamlMap yaml) {
    if (yaml == null) {
      return false;
    }
284
    return yaml[kDartPluginClass] is String || yaml[kPluginClass] is String;
285 286 287 288 289 290
  }

  static const String kConfigKey = 'windows';

  final String name;
  final String pluginClass;
291 292 293 294
  final String dartPluginClass;

  @override
  bool isNative() => pluginClass != null;
295 296 297 298 299

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
300 301 302
      if (pluginClass != null) 'class': pluginClass,
      if (pluginClass != null) 'filename': _filenameForCppClass(pluginClass),
      if (dartPluginClass != null) 'dartPluginClass': dartPluginClass,
303 304 305 306 307 308
    };
  }
}

/// Contains the parameters to template a Linux plugin.
///
309 310 311
/// The [name] of the plugin is required. Either [dartPluginClass] or [pluginClass] are required.
/// [pluginClass] will be the entry point to the plugin's native code.
class LinuxPlugin extends PluginPlatform implements NativeOrDartPlugin {
312 313
  const LinuxPlugin({
    @required this.name,
314 315 316
    this.pluginClass,
    this.dartPluginClass,
  }) : assert(pluginClass != null || dartPluginClass != null);
317 318 319

  factory LinuxPlugin.fromYaml(String name, YamlMap yaml) {
    assert(validate(yaml));
320 321 322 323 324
    // Treat 'none' as not present. See https://github.com/flutter/flutter/issues/57497.
    String pluginClass = yaml[kPluginClass] as String;
    if (pluginClass == 'none') {
      pluginClass = null;
    }
325 326
    return LinuxPlugin(
      name: name,
327
      pluginClass: pluginClass,
328
      dartPluginClass: yaml[kDartPluginClass] as String,
329 330 331 332 333 334 335
    );
  }

  static bool validate(YamlMap yaml) {
    if (yaml == null) {
      return false;
    }
336
    return yaml[kPluginClass] is String || yaml[kDartPluginClass] is String;
337 338 339 340 341 342
  }

  static const String kConfigKey = 'linux';

  final String name;
  final String pluginClass;
343 344 345 346
  final String dartPluginClass;

  @override
  bool isNative() => pluginClass != null;
347 348 349 350 351

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
352 353 354
      if (pluginClass != null) 'class': pluginClass,
      if (pluginClass != null) 'filename': _filenameForCppClass(pluginClass),
      if (dartPluginClass != null) 'dartPluginClass': dartPluginClass,
355 356 357 358
    };
  }
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/// Contains the parameters to template a web plugin.
///
/// The required fields include: [name] of the plugin, the [pluginClass] that will
/// be the entry point to the plugin's implementation, and the [fileName]
/// containing the code.
class WebPlugin extends PluginPlatform {
  const WebPlugin({
    @required this.name,
    @required this.pluginClass,
    @required this.fileName,
  });

  factory WebPlugin.fromYaml(String name, YamlMap yaml) {
    assert(validate(yaml));
    return WebPlugin(
      name: name,
375 376
      pluginClass: yaml['pluginClass'] as String,
      fileName: yaml['fileName'] as String,
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
    );
  }

  static bool validate(YamlMap yaml) {
    if (yaml == null) {
      return false;
    }
    return yaml['pluginClass'] is String && yaml['fileName'] is String;
  }

  static const String kConfigKey = 'web';

  /// The name of the plugin.
  final String name;

  /// The class containing the plugin implementation details.
  ///
  /// This class should have a static `registerWith` method defined.
  final String pluginClass;

  /// The name of the file containing the class implementation above.
  final String fileName;

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
      'class': pluginClass,
      'file': fileName,
    };
  }
}
409 410 411 412 413 414 415 416

final RegExp _internalCapitalLetterRegex = RegExp(r'(?=(?!^)[A-Z])');
String _filenameForCppClass(String className) {
  return className.splitMapJoin(
    _internalCapitalLetterRegex,
    onMatch: (_) => '_',
    onNonMatch: (String n) => n.toLowerCase());
}