Starts using the --source flag to compile the dart registrant. (#98046)

* Starts using the `--source` flag to compile the dart registrant.

* updated general.shard tests

* Fixed the resident compiler flow

* added integration test

* made the integration test self contained

* renamed generated_main to dart_plugin_registrant

* cleaned up for review

* added task runner for ci

* added bringup and TESTOWNERS

* updated failure message
This commit is contained in:
gaaclarke
2022-03-01 16:24:47 -08:00
committed by GitHub
parent 6c818d772a
commit fdcd14464c
14 changed files with 203 additions and 303 deletions

View File

@@ -159,7 +159,7 @@ void main() {
}
});
testUsingContext("doesn't generate generated_main.dart if there aren't Dart plugins", () async {
testUsingContext("doesn't generate dart_plugin_registrant.dart if there aren't Dart plugins", () async {
final Directory projectDir = fileSystem.directory('project')..createSync();
final Environment environment = Environment.test(
fileSystem.currentDirectory,
@@ -189,11 +189,11 @@ void main() {
final File generatedMain = projectDir
.childDirectory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart');
.childFile('dart_plugin_registrant.dart');
expect(generatedMain.existsSync(), isFalse);
});
testUsingContext('regenerates generated_main.dart', () async {
testUsingContext('regenerates dart_plugin_registrant.dart', () async {
final Directory projectDir = fileSystem.directory('project')..createSync();
final Environment environment = Environment.test(
fileSystem.currentDirectory,
@@ -231,7 +231,7 @@ void main() {
final File generatedMain = projectDir
.childDirectory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart');
.childFile('dart_plugin_registrant.dart');
final String mainContent = generatedMain.readAsStringSync();
expect(
mainContent,
@@ -243,10 +243,6 @@ void main() {
'\n'
'// @dart = 2.12\n'
'\n'
'// When `package:path_provider_example/main.dart` defines `main`, that definition is shadowed by the definition below.\n'
"export 'package:path_provider_example/main.dart';\n"
'\n'
"import 'package:path_provider_example/main.dart' as entrypoint;\n"
"import 'dart:io'; // flutter_ignore: dart_io_import.\n"
"import 'package:path_provider_linux/path_provider_linux.dart';\n"
'\n'
@@ -272,24 +268,12 @@ void main() {
' } else if (Platform.isWindows) {\n'
' }\n'
' }\n'
'\n'
'}\n'
'\n'
'typedef _UnaryFunction = dynamic Function(List<String> args);\n'
'typedef _NullaryFunction = dynamic Function();\n'
'\n'
'void main(List<String> args) {\n'
' if (entrypoint.main is _UnaryFunction) {\n'
' (entrypoint.main as _UnaryFunction)(args);\n'
' } else {\n'
' (entrypoint.main as _NullaryFunction)();\n'
' }\n'
'}\n'
),
);
});
testUsingContext('removes generated_main.dart if plugins are removed from pubspec.yaml', () async {
testUsingContext('removes dart_plugin_registrant.dart if plugins are removed from pubspec.yaml', () async {
final Directory projectDir = fileSystem.directory('project')..createSync();
final Environment environment = Environment.test(
fileSystem.currentDirectory,
@@ -321,7 +305,7 @@ void main() {
final File generatedMain = projectDir
.childDirectory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart');
.childFile('dart_plugin_registrant.dart');
final FlutterProject testProject = FlutterProject.fromDirectoryTest(projectDir);
await DartPluginRegistrantTarget.test(testProject).build(environment);
@@ -372,7 +356,7 @@ void main() {
final File generatedMain = projectDir
.childDirectory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart');
.childFile('dart_plugin_registrant.dart');
final String mainContent = generatedMain.readAsStringSync();
expect(
@@ -385,10 +369,6 @@ void main() {
'\n'
'// @dart = 2.12\n'
'\n'
'// When `file:///root/external.dart` defines `main`, that definition is shadowed by the definition below.\n'
"export 'file:///root/external.dart';\n"
'\n'
"import 'file:///root/external.dart' as entrypoint;\n"
"import 'dart:io'; // flutter_ignore: dart_io_import.\n"
"import 'package:path_provider_linux/path_provider_linux.dart';\n"
'\n'
@@ -414,18 +394,6 @@ void main() {
' } else if (Platform.isWindows) {\n'
' }\n'
' }\n'
'\n'
'}\n'
'\n'
'typedef _UnaryFunction = dynamic Function(List<String> args);\n'
'typedef _NullaryFunction = dynamic Function();\n'
'\n'
'void main(List<String> args) {\n'
' if (entrypoint.main is _UnaryFunction) {\n'
' (entrypoint.main as _UnaryFunction)(args);\n'
' } else {\n'
' (entrypoint.main as _NullaryFunction)();\n'
' }\n'
'}\n'
),
);

View File

@@ -395,7 +395,9 @@ void main() {
'--no-link-platform',
'--packages',
'.packages',
'.dart_tools/flutter_build/generated_main.dart',
'--source',
'.dart_tools/flutter_build/dart_plugin_registrant.dart',
'scheme:///main.dart',
], completer: completer),
]),
stdoutHandler: stdoutHandler,
@@ -405,7 +407,7 @@ void main() {
.childDirectory('flutter_build')
.childDirectory('test');
buildDir.parent.childFile('generated_main.dart').createSync(recursive: true);
buildDir.parent.childFile('dart_plugin_registrant.dart').createSync(recursive: true);
final Future<CompilerOutput?> output = kernelCompiler.compile(sdkRoot: '/path/to/sdkroot',
mainPath: '/foo/bar/fizz/main.dart',

View File

@@ -35,7 +35,7 @@ void main() {
..directory = directory
..flutterPluginsFile = directory.childFile('.flutter-plugins')
..flutterPluginsDependenciesFile = directory.childFile('.flutter-plugins-dependencies')
..dartPluginRegistrant = directory.childFile('generated_main.dart');
..dartPluginRegistrant = directory.childFile('dart_plugin_registrant.dart');
flutterProject.directory.childFile('.packages').createSync(recursive: true);
});
@@ -701,10 +701,6 @@ void main() {
'\n'
'// @dart = 2.8\n'
'\n'
'// When `package:app/main.dart` defines `main`, that definition is shadowed by the definition below.\n'
"export 'package:app/main.dart';\n"
'\n'
"import 'package:app/main.dart' as entrypoint;\n"
"import 'dart:io'; // flutter_ignore: dart_io_import.\n"
"import 'package:url_launcher_android/url_launcher_android.dart';\n"
"import 'package:url_launcher_ios/url_launcher_ios.dart';\n"
@@ -785,216 +781,7 @@ void main() {
'\n'
' }\n'
' }\n'
'\n'
'}\n'
'\n'
'typedef _UnaryFunction = dynamic Function(List<String> args);\n'
'typedef _NullaryFunction = dynamic Function();\n'
'\n'
'void main(List<String> args) {\n'
' if (entrypoint.main is _UnaryFunction) {\n'
' (entrypoint.main as _UnaryFunction)(args);\n'
' } else {\n'
' (entrypoint.main as _NullaryFunction)();\n'
' }\n'
'}\n',
);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('Rewires entrypoints', () async {
flutterProject.isModule = true;
createFakeDartPlugins(
flutterProject,
flutterManifest,
fs,
<String, String>{
'url_launcher_android': '''
flutter:
plugin:
implements: url_launcher
platforms:
android:
dartPluginClass: AndroidPlugin
''',
'url_launcher_ios': '''
flutter:
plugin:
implements: url_launcher
platforms:
ios:
dartPluginClass: IosPlugin
''',
'url_launcher_macos': '''
flutter:
plugin:
implements: url_launcher
platforms:
macos:
dartPluginClass: MacOSPlugin
''',
'url_launcher_linux': '''
flutter:
plugin:
implements: url_launcher
platforms:
linux:
dartPluginClass: LinuxPlugin
''',
'url_launcher_windows': '''
flutter:
plugin:
implements: url_launcher
platforms:
windows:
dartPluginClass: WindowsPlugin
''',
'awesome_macos': '''
flutter:
plugin:
implements: awesome
platforms:
macos:
dartPluginClass: AwesomeMacOS
'''
});
final Directory libDir = flutterProject.directory.childDirectory('lib');
libDir.createSync(recursive: true);
final File mainFile = libDir.childFile('main.dart');
mainFile.writeAsStringSync('''
// @dart = 2.8
void main() {
}
@pragma('vm:entry-point')
void dream() => run(interactive: false);
@pragma('vm:entry-point', foobar)
void dreamWithFlags() => run(interactive: false);
''');
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
flutterProject.directory.childDirectory('.dart_tool').childFile('package_config.json'),
logger: globals.logger,
throwOnError: false,
);
await generateMainDartWithPluginRegistrant(
flutterProject,
packageConfig,
'package:app/main.dart',
mainFile,
throwOnPluginPubspecError: true,
);
expect(flutterProject.dartPluginRegistrant.readAsStringSync(),
'//\n'
'// Generated file. Do not edit.\n'
'// This file is generated from template in file `flutter_tools/lib/src/flutter_plugins.dart`.\n'
'//\n'
'\n'
'// @dart = 2.8\n'
'\n'
'// When `package:app/main.dart` defines `main`, that definition is shadowed by the definition below.\n'
"export 'package:app/main.dart';\n"
'\n'
"import 'package:app/main.dart' as entrypoint;\n"
"import 'dart:io'; // flutter_ignore: dart_io_import.\n"
"import 'package:url_launcher_android/url_launcher_android.dart';\n"
"import 'package:url_launcher_ios/url_launcher_ios.dart';\n"
"import 'package:url_launcher_linux/url_launcher_linux.dart';\n"
"import 'package:awesome_macos/awesome_macos.dart';\n"
"import 'package:url_launcher_macos/url_launcher_macos.dart';\n"
"import 'package:url_launcher_windows/url_launcher_windows.dart';\n"
'\n'
"@pragma('vm:entry-point')\n"
'class _PluginRegistrant {\n'
'\n'
" @pragma('vm:entry-point')\n"
' static void register() {\n'
' if (Platform.isAndroid) {\n'
' try {\n'
' AndroidPlugin.registerWith();\n'
' } catch (err) {\n'
' print(\n'
" '`url_launcher_android` threw an error: \$err. '\n"
" 'The app may not function as expected until you remove this plugin from pubspec.yaml'\n"
' );\n'
' rethrow;\n'
' }\n'
'\n'
' } else if (Platform.isIOS) {\n'
' try {\n'
' IosPlugin.registerWith();\n'
' } catch (err) {\n'
' print(\n'
" '`url_launcher_ios` threw an error: \$err. '\n"
" 'The app may not function as expected until you remove this plugin from pubspec.yaml'\n"
' );\n'
' rethrow;\n'
' }\n'
'\n'
' } else if (Platform.isLinux) {\n'
' try {\n'
' LinuxPlugin.registerWith();\n'
' } catch (err) {\n'
' print(\n'
" '`url_launcher_linux` threw an error: \$err. '\n"
" 'The app may not function as expected until you remove this plugin from pubspec.yaml'\n"
' );\n'
' rethrow;\n'
' }\n'
'\n'
' } else if (Platform.isMacOS) {\n'
' try {\n'
' AwesomeMacOS.registerWith();\n'
' } catch (err) {\n'
' print(\n'
" '`awesome_macos` threw an error: \$err. '\n"
" 'The app may not function as expected until you remove this plugin from pubspec.yaml'\n"
' );\n'
' rethrow;\n'
' }\n'
'\n'
' try {\n'
' MacOSPlugin.registerWith();\n'
' } catch (err) {\n'
' print(\n'
" '`url_launcher_macos` threw an error: \$err. '\n"
" 'The app may not function as expected until you remove this plugin from pubspec.yaml'\n"
' );\n'
' rethrow;\n'
' }\n'
'\n'
' } else if (Platform.isWindows) {\n'
' try {\n'
' WindowsPlugin.registerWith();\n'
' } catch (err) {\n'
' print(\n'
" '`url_launcher_windows` threw an error: \$err. '\n"
" 'The app may not function as expected until you remove this plugin from pubspec.yaml'\n"
' );\n'
' rethrow;\n'
' }\n'
'\n'
' }\n'
' }\n'
'\n'
'}\n'
'\n'
'typedef _UnaryFunction = dynamic Function(List<String> args);\n'
'typedef _NullaryFunction = dynamic Function();\n'
'\n'
'void main(List<String> args) {\n'
' if (entrypoint.main is _UnaryFunction) {\n'
' (entrypoint.main as _UnaryFunction)(args);\n'
' } else {\n'
' (entrypoint.main as _NullaryFunction)();\n'
' }\n'
'}\n'
,
);
}, overrides: <Type, Generator>{
FileSystem: () => fs,

View File

@@ -1189,8 +1189,8 @@ void main() {
]
}
''');
// Start from an empty generated_main.dart file.
globals.fs.directory('.dart_tool').childDirectory('flutter_build').childFile('generated_main.dart').createSync(recursive: true);
// Start from an empty dart_plugin_registrant.dart file.
globals.fs.directory('.dart_tool').childDirectory('flutter_build').childFile('dart_plugin_registrant.dart').createSync(recursive: true);
await residentRunner.runSourceGenerators();
@@ -1267,9 +1267,9 @@ flutter:
final File generatedMain = globals.fs.directory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart');
.childFile('dart_plugin_registrant.dart');
expect(generatedMain.readAsStringSync(), contains('custom_main.dart'));
expect(generatedMain.existsSync(), isTrue);
expect(testLogger.errorText, isEmpty);
expect(testLogger.statusText, isEmpty);
}));

View File

@@ -936,7 +936,7 @@ void main() {
// While this file should be ignored on web, generating it here will cause a
// perf regression in hot restart.
testUsingContext('Does not generate generated_main.dart', () async {
testUsingContext('Does not generate dart_plugin_registrant.dart', () async {
// Create necessary files for [DartPluginRegistrantTarget]
final File packageConfig = globals.fs.directory('.dart_tool')
.childFile('package_config.json');
@@ -954,10 +954,10 @@ void main() {
]
}
''');
// Start with a generated_main.dart file.
// Start with a dart_plugin_registrant.dart file.
globals.fs.directory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart')
.childFile('dart_plugin_registrant.dart')
.createSync(recursive: true);
final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
@@ -965,7 +965,7 @@ void main() {
final ResidentRunner residentWebRunner = setUpResidentRunner(flutterDevice);
await residentWebRunner.runSourceGenerators();
// generated_main.dart should be untouched, indicating that its
// dart_plugin_registrant.dart should be untouched, indicating that its
// generation didn't run. If it had run, the file would have been removed as
// there are no plugins in the project.
expect(project.dartPluginRegistrant.existsSync(), true);

View File

@@ -115,7 +115,7 @@ void main() {
Logger: () => BufferLogger.test(),
});
testUsingContext('TestCompiler updates generated_main.dart', () async {
testUsingContext('TestCompiler updates dart_plugin_registrant.dart', () async {
final Directory fakeDartPlugin = fileSystem.directory('a_plugin');
fileSystem.file('pubspec.yaml').writeAsStringSync('''
name: foo
@@ -152,12 +152,12 @@ environment:
final File generatedMain = fileSystem
.directory('.dart_tool')
.childDirectory('flutter_build')
.childFile('generated_main.dart');
.childFile('dart_plugin_registrant.dart');
expect(generatedMain, exists);
expect(
generatedMain.readAsLinesSync(),
contains("import 'test/foo.dart' as entrypoint;")
generatedMain.readAsStringSync(),
contains('APlugin.registerWith();')
);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,