Skip to content

Commit

Permalink
Revert "Reverts "Implement dartPluginClass support for plugins flutte…
Browse files Browse the repository at this point in the history
…r#74469" (flutter#78623)"

This reverts commit 5efc716.
  • Loading branch information
Emmanuel Garcia committed Apr 1, 2021
1 parent aff4e55 commit a5a7938
Show file tree
Hide file tree
Showing 29 changed files with 1,538 additions and 26 deletions.
10 changes: 10 additions & 0 deletions dev/devicelab/bin/tasks/dart_plugin_registry_test.dart
@@ -0,0 +1,10 @@
// 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:flutter_devicelab/tasks/dart_plugin_registry_tests.dart';
import 'package:flutter_devicelab/framework/framework.dart';

Future<void> main() async {
await task(dartPluginRegistryTest());
}
179 changes: 179 additions & 0 deletions dev/devicelab/lib/tasks/dart_plugin_registry_tests.dart
@@ -0,0 +1,179 @@
// 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 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/task_result.dart';
import 'package:flutter_devicelab/framework/utils.dart';

TaskFunction dartPluginRegistryTest({
String deviceIdOverride,
Map<String, String> environment,
}) {
final Directory tempDir = Directory.systemTemp
.createTempSync('flutter_devicelab_dart_plugin_test.');
return () async {
try {
section('Create implementation plugin');
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>[
'--template=plugin',
'--org',
'io.flutter.devicelab',
'--platforms',
'macos',
'plugin_platform_implementation',
],
environment: environment,
);
});

final File pluginMain = File(path.join(
tempDir.absolute.path,
'plugin_platform_implementation',
'lib',
'plugin_platform_implementation.dart',
));
if (!pluginMain.existsSync()) {
return TaskResult.failure('${pluginMain.path} does not exist');
}

// Patch plugin main dart file.
await pluginMain.writeAsString('''
class PluginPlatformInterfaceMacOS {
static void registerWith() {
print('PluginPlatformInterfaceMacOS.registerWith() was called');
}
}
''', flush: true);

// Patch plugin main pubspec file.
final File pluginImplPubspec = File(path.join(
tempDir.absolute.path,
'plugin_platform_implementation',
'pubspec.yaml',
));
String pluginImplPubspecContent = await pluginImplPubspec.readAsString();
pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst(
' pluginClass: PluginPlatformImplementationPlugin',
' pluginClass: PluginPlatformImplementationPlugin\n'
' dartPluginClass: PluginPlatformInterfaceMacOS\n',
);
pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst(
' platforms:\n',
' implements: plugin_platform_interface\n'
' platforms:\n');
await pluginImplPubspec.writeAsString(pluginImplPubspecContent,
flush: true);

section('Create interface plugin');
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>[
'--template=plugin',
'--org',
'io.flutter.devicelab',
'--platforms',
'macos',
'plugin_platform_interface',
],
environment: environment,
);
});
final File pluginInterfacePubspec = File(path.join(
tempDir.absolute.path,
'plugin_platform_interface',
'pubspec.yaml',
));
String pluginInterfacePubspecContent =
await pluginInterfacePubspec.readAsString();
pluginInterfacePubspecContent =
pluginInterfacePubspecContent.replaceFirst(
' pluginClass: PluginPlatformInterfacePlugin',
' default_package: plugin_platform_implementation\n');
pluginInterfacePubspecContent =
pluginInterfacePubspecContent.replaceFirst(
'dependencies:',
'dependencies:\n'
' plugin_platform_implementation:\n'
' path: ../plugin_platform_implementation\n');
await pluginInterfacePubspec.writeAsString(pluginInterfacePubspecContent,
flush: true);

section('Create app');

await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>[
'--template=app',
'--org',
'io.flutter.devicelab',
'--platforms',
'macos',
'app',
],
environment: environment,
);
});

final File appPubspec = File(path.join(
tempDir.absolute.path,
'app',
'pubspec.yaml',
));
String appPubspecContent = await appPubspec.readAsString();
appPubspecContent = appPubspecContent.replaceFirst(
'dependencies:',
'dependencies:\n'
' plugin_platform_interface:\n'
' path: ../plugin_platform_interface\n');
await appPubspec.writeAsString(appPubspecContent, flush: true);

section('Flutter run for macos');

await inDirectory(path.join(tempDir.path, 'app'), () async {
final Process run = await startProcess(
path.join(flutterDirectory.path, 'bin', 'flutter'),
flutterCommandArgs('run', <String>['-d', 'macos', '-v']),
environment: null,
);
Completer<void> registryExecutedCompleter = Completer<void>();
final StreamSubscription<void> subscription = run.stdout
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String line) {
if (line.contains(
'PluginPlatformInterfaceMacOS.registerWith() was called')) {
registryExecutedCompleter.complete();
}
print('stdout: $line');
});

section('Wait for registry execution');
await registryExecutedCompleter.future;

// Hot restart.
run.stdin.write('R');
registryExecutedCompleter = Completer<void>();

section('Wait for registry execution after hot restart');
await registryExecutedCompleter.future;

subscription.cancel();
run.kill();
});
return TaskResult.success(null);
} finally {
rmTree(tempDir);
}
};
}
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#include "generated_plugin_registrant.h"

#include <url_launcher_linux/url_launcher_plugin.h>
Expand Down
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

Expand Down
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#include "generated_plugin_registrant.h"

#include <url_launcher_windows/url_launcher_plugin.h>
Expand Down
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

Expand Down
2 changes: 1 addition & 1 deletion dev/prod_builders.json
Expand Up @@ -916,7 +916,7 @@
"name": "Mac dart_plugin_registry_test",
"repo": "flutter",
"task_name": "dart_plugin_registry_test",
"flaky": true
"flaky": false
},
{
"name": "Mac tool_tests_general",
Expand Down
2 changes: 1 addition & 1 deletion dev/try_builders.json
Expand Up @@ -536,7 +536,7 @@
"name": "Mac dart_plugin_registry_test",
"repo": "flutter",
"task_name": "dart_plugin_registry_test",
"enabled": false,
"enabled": true,
"run_if": ["dev/**", "packages/flutter_tools/**", "bin/**"]
},
{
Expand Down
2 changes: 2 additions & 0 deletions examples/hello_world/ios/Runner/GeneratedPluginRegistrant.h
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GeneratedPluginRegistrant_h
#define GeneratedPluginRegistrant_h

Expand Down
2 changes: 2 additions & 0 deletions examples/hello_world/ios/Runner/GeneratedPluginRegistrant.m
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#import "GeneratedPluginRegistrant.h"

@implementation GeneratedPluginRegistrant
Expand Down
10 changes: 10 additions & 0 deletions packages/flutter_tools/lib/src/build_system/build_system.dart
Expand Up @@ -332,6 +332,7 @@ class Environment {
@required ProcessManager processManager,
@required Platform platform,
@required String engineVersion,
@required bool generateDartPluginRegistry,
Directory buildDir,
Map<String, String> defines = const <String, String>{},
Map<String, String> inputs = const <String, String>{},
Expand Down Expand Up @@ -372,6 +373,7 @@ class Environment {
platform: platform,
engineVersion: engineVersion,
inputs: inputs,
generateDartPluginRegistry: generateDartPluginRegistry,
);
}

Expand All @@ -389,6 +391,7 @@ class Environment {
Map<String, String> inputs = const <String, String>{},
String engineVersion,
Platform platform,
bool generateDartPluginRegistry = false,
@required FileSystem fileSystem,
@required Logger logger,
@required Artifacts artifacts,
Expand All @@ -408,6 +411,7 @@ class Environment {
processManager: processManager,
platform: platform ?? FakePlatform(),
engineVersion: engineVersion,
generateDartPluginRegistry: generateDartPluginRegistry,
);
}

Expand All @@ -426,6 +430,7 @@ class Environment {
@required this.artifacts,
@required this.engineVersion,
@required this.inputs,
@required this.generateDartPluginRegistry,
});

/// The [Source] value which is substituted with the path to [projectDir].
Expand Down Expand Up @@ -505,6 +510,11 @@ class Environment {

/// The version of the current engine, or `null` if built with a local engine.
final String engineVersion;

/// Whether to generate the Dart plugin registry.
/// When [true], the main entrypoint is wrapped and the wrapper becomes
/// the new entrypoint.
final bool generateDartPluginRegistry;
}

/// The result information from the build system.
Expand Down
Expand Up @@ -286,6 +286,8 @@ class KernelSnapshot extends Target {
fileSystemScheme: fileSystemScheme,
dartDefines: decodeDartDefines(environment.defines, kDartDefines),
packageConfig: packageConfig,
buildDir: environment.buildDir,
generateDartPluginRegistry: environment.generateDartPluginRegistry,
);
if (output == null || output.errorCount != 0) {
throw Exception();
Expand Down
1 change: 1 addition & 0 deletions packages/flutter_tools/lib/src/bundle.dart
Expand Up @@ -161,6 +161,7 @@ Future<void> buildWithAssemble({
logger: globals.logger,
processManager: globals.processManager,
platform: globals.platform,
generateDartPluginRegistry: true,
);
final Target target = buildMode == BuildMode.debug
? const CopyFlutterBundle()
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_tools/lib/src/commands/assemble.dart
Expand Up @@ -242,7 +242,8 @@ class AssembleCommand extends FlutterCommand {
platform: globals.platform,
engineVersion: globals.artifacts.isLocalEngine
? null
: globals.flutterVersion.engineRevision
: globals.flutterVersion.engineRevision,
generateDartPluginRegistry: true,
);
return result;
}
Expand Down
Expand Up @@ -386,6 +386,7 @@ end
engineVersion: globals.artifacts.isLocalEngine
? null
: globals.flutterVersion.engineRevision,
generateDartPluginRegistry: true,
);
Target target;
// Always build debug for simulator.
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/src/commands/packages.dart
Expand Up @@ -120,6 +120,7 @@ class PackagesGetCommand extends FlutterCommand {
processManager: globals.processManager,
platform: globals.platform,
projectDir: flutterProject.directory,
generateDartPluginRegistry: true,
);

await generateLocalizationsSyntheticPackage(
Expand Down Expand Up @@ -326,6 +327,7 @@ class PackagesInteractiveGetCommand extends FlutterCommand {
processManager: globals.processManager,
platform: globals.platform,
projectDir: flutterProject.directory,
generateDartPluginRegistry: true,
);

await generateLocalizationsSyntheticPackage(
Expand Down

0 comments on commit a5a7938

Please sign in to comment.