From 674fbd26bc6004c4603ba398b6acbb7593e0defe Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Thu, 21 Dec 2023 11:01:16 -0800 Subject: [PATCH] [flutter_tools] Ensure flutter daemon clients can detect preview device (#140112) Part of https://github.com/flutter/flutter/issues/130277 --- packages/flutter_tools/doc/daemon.md | 24 ++- .../lib/src/commands/daemon.dart | 197 +++++++++++++++--- packages/flutter_tools/lib/src/device.dart | 36 +--- .../flutter_tools/lib/src/preview_device.dart | 22 +- .../commands.shard/hermetic/daemon_test.dart | 159 +++++++++++++- .../general.shard/preview_device_test.dart | 2 +- 6 files changed, 359 insertions(+), 81 deletions(-) diff --git a/packages/flutter_tools/doc/daemon.md b/packages/flutter_tools/doc/daemon.md index 73a85c29a9..f570949064 100644 --- a/packages/flutter_tools/doc/daemon.md +++ b/packages/flutter_tools/doc/daemon.md @@ -62,14 +62,22 @@ The `shutdown()` command will terminate the flutter daemon. It is not necessary #### daemon.getSupportedPlatforms -The `getSupportedPlatforms()` command will enumerate all platforms supported by the project located at the provided `projectRoot`. It returns a Map with the key 'platforms' containing a List of strings which describe the set of all possibly supported platforms. Possible values include: - - android - - ios - - linux #experimental - - macos #experimental - - windows #experimental - - fuchsia #experimental - - web #experimental +The `getSupportedPlatforms()` command will enumerate all platforms supported +by the project located at the provided `projectRoot`. It returns a Map with +the key 'platformTypes' containing a Map of platform types to a Map with the +following entries: + +- isSupported (bool) - whether or not the platform type is supported +- reasons (List>, only included if isSupported == false) - a list of reasons why the platform is not supported + +The schema for each element in `reasons` is: + +- reasonText (String) - a description of why the platform is not supported +- fixText (String) - human readable instructions of how to fix this reason +- fixCode (String) - stringified version of the `_ReasonCode` enum. To be used +by daemon clients who intend to auto-fix. + +The possible platform types are the `PlatformType` enumeration in the lib/src/device.dart library. #### Events diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index b24dfa2c53..10ed234534 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -416,36 +416,167 @@ class DaemonDomain extends Domain { /// is correct. Future> getSupportedPlatforms(Map args) async { final String? projectRoot = _getStringArg(args, 'projectRoot', required: true); - final List result = []; + final List platformTypes = []; + final Map platformTypesMap = {}; try { final FlutterProject flutterProject = FlutterProject.fromDirectory(globals.fs.directory(projectRoot)); final Set supportedPlatforms = flutterProject.getSupportedPlatforms().toSet(); - if (featureFlags.isLinuxEnabled && supportedPlatforms.contains(SupportedPlatform.linux)) { - result.add('linux'); - } - if (featureFlags.isMacOSEnabled && supportedPlatforms.contains(SupportedPlatform.macos)) { - result.add('macos'); - } - if (featureFlags.isWindowsEnabled && supportedPlatforms.contains(SupportedPlatform.windows)) { - result.add('windows'); - } - if (featureFlags.isIOSEnabled && supportedPlatforms.contains(SupportedPlatform.ios)) { - result.add('ios'); - } - if (featureFlags.isAndroidEnabled && supportedPlatforms.contains(SupportedPlatform.android)) { - result.add('android'); - } - if (featureFlags.isWebEnabled && supportedPlatforms.contains(SupportedPlatform.web)) { - result.add('web'); - } - if (featureFlags.isFuchsiaEnabled && supportedPlatforms.contains(SupportedPlatform.fuchsia)) { - result.add('fuchsia'); - } - if (featureFlags.areCustomDevicesEnabled) { - result.add('custom'); + + void handlePlatformType( + PlatformType platform, + ) { + final List> reasons = >[]; + switch (platform) { + case PlatformType.linux: + if (!featureFlags.isLinuxEnabled) { + reasons.add({ + 'reasonText': 'the Linux feature is not enabled', + 'fixText': 'Run "flutter config --enable-linux-desktop"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.linux)) { + reasons.add({ + 'reasonText': 'the Linux platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=linux ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.macos: + if (!featureFlags.isMacOSEnabled) { + reasons.add({ + 'reasonText': 'the macOS feature is not enabled', + 'fixText': 'Run "flutter config --enable-macos-desktop"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.macos)) { + reasons.add({ + 'reasonText': 'the macOS platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=macos ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.windows: + if (!featureFlags.isWindowsEnabled) { + reasons.add({ + 'reasonText': 'the Windows feature is not enabled', + 'fixText': 'Run "flutter config --enable-windows-desktop"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.windows)) { + reasons.add({ + 'reasonText': 'the Windows platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=windows ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.ios: + if (!featureFlags.isIOSEnabled) { + reasons.add({ + 'reasonText': 'the iOS feature is not enabled', + 'fixText': 'Run "flutter config --enable-ios"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.ios)) { + reasons.add({ + 'reasonText': 'the iOS platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=ios ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.android: + if (!featureFlags.isAndroidEnabled) { + reasons.add({ + 'reasonText': 'the Android feature is not enabled', + 'fixText': 'Run "flutter config --enable-android"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.android)) { + reasons.add({ + 'reasonText': 'the Android platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=android ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.web: + if (!featureFlags.isWebEnabled) { + reasons.add({ + 'reasonText': 'the Web feature is not enabled', + 'fixText': 'Run "flutter config --enable-web"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.web)) { + reasons.add({ + 'reasonText': 'the Web platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=web ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.fuchsia: + if (!featureFlags.isFuchsiaEnabled) { + reasons.add({ + 'reasonText': 'the Fuchsia feature is not enabled', + 'fixText': 'Run "flutter config --enable-fuchsia"', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.fuchsia)) { + reasons.add({ + 'reasonText': 'the Fuchsia platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=fuchsia ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + case PlatformType.custom: + if (!featureFlags.areCustomDevicesEnabled) { + reasons.add({ + 'reasonText': 'the custom devices feature is not enabled', + 'fixText': 'Run "flutter config --enable-custom-devices"', + 'fixCode': _ReasonCode.config.name, + }); + } + case PlatformType.windowsPreview: + // TODO(fujino): detect if there any plugins with native code + if (!featureFlags.isPreviewDeviceEnabled) { + reasons.add({ + 'reasonText': 'the Preview Device feature is not enabled', + 'fixText': 'Run "flutter config --enable-flutter-preview', + 'fixCode': _ReasonCode.config.name, + }); + } + if (!supportedPlatforms.contains(SupportedPlatform.windows)) { + reasons.add({ + 'reasonText': 'the Windows platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=windows ." in your application directory', + 'fixCode': _ReasonCode.create.name, + }); + } + } + + if (reasons.isEmpty) { + platformTypes.add(platform.name); + platformTypesMap[platform.name] = const { + 'isSupported': true, + }; + } else { + platformTypesMap[platform.name] = { + 'isSupported': false, + 'reasons': reasons, + }; + } } + + PlatformType.values.forEach(handlePlatformType); + return { - 'platforms': result, + // TODO(fujino): delete this key https://github.com/flutter/flutter/issues/140473 + 'platforms': platformTypes, + 'platformTypes': platformTypesMap, }; } on Exception catch (err, stackTrace) { sendEvent('log', { @@ -454,12 +585,16 @@ class DaemonDomain extends Domain { 'error': true, }); // On any sort of failure, fall back to Android and iOS for backwards - // comparability. - return { + // compatibility. + return const { 'platforms': [ 'android', 'ios', ], + 'platformTypes': { + 'android': {'isSupported': true}, + 'ios': {'isSupported': true}, + }, }; } } @@ -470,6 +605,14 @@ class DaemonDomain extends Domain { } } +/// The reason a [PlatformType] is not currently supported. +/// +/// The [name] of this value will be sent as a response to daemon client. +enum _ReasonCode { + create, + config, +} + typedef RunOrAttach = Future Function({ Completer? connectionInfoCompleter, Completer? appStartedCompleter, diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index d316f90c53..3de2799072 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -45,34 +45,20 @@ enum Category { /// The platform sub-folder that a device type supports. enum PlatformType { - web._('web'), - android._('android'), - ios._('ios'), - linux._('linux'), - macos._('macos'), - windows._('windows'), - fuchsia._('fuchsia'), - custom._('custom'); - - const PlatformType._(this.value); - - final String value; + web, + android, + ios, + linux, + macos, + windows, + fuchsia, + custom, + windowsPreview; @override - String toString() => value; + String toString() => name; - static PlatformType? fromString(String platformType) { - return const { - 'web': web, - 'android': android, - 'ios': ios, - 'linux': linux, - 'macos': macos, - 'windows': windows, - 'fuchsia': fuchsia, - 'custom': custom, - }[platformType]; - } + static PlatformType? fromString(String platformType) => values.asNameMap()[platformType]; } /// A discovery mechanism for flutter-supported development devices. diff --git a/packages/flutter_tools/lib/src/preview_device.dart b/packages/flutter_tools/lib/src/preview_device.dart index 20b54b0468..71eba1fcb1 100644 --- a/packages/flutter_tools/lib/src/preview_device.dart +++ b/packages/flutter_tools/lib/src/preview_device.dart @@ -29,7 +29,7 @@ BundleBuilder _defaultBundleBuilder() { return BundleBuilder(); } -class PreviewDeviceDiscovery extends DeviceDiscovery { +class PreviewDeviceDiscovery extends PollingDeviceDiscovery { PreviewDeviceDiscovery({ required Platform platform, required Artifacts artifacts, @@ -42,7 +42,8 @@ class PreviewDeviceDiscovery extends DeviceDiscovery { _processManager = processManager, _fileSystem = fileSystem, _platform = platform, - _features = featureFlags; + _features = featureFlags, + super('Flutter preview device'); final Platform _platform; final Artifacts _artifacts; @@ -61,9 +62,8 @@ class PreviewDeviceDiscovery extends DeviceDiscovery { List get wellKnownIds => ['preview']; @override - Future> devices({ + Future> pollingGetDevices({ Duration? timeout, - DeviceDiscoveryFilter? filter, }) async { final File previewBinary = _fileSystem.file(_artifacts.getArtifactPath(Artifact.flutterPreviewDevice)); if (!previewBinary.existsSync()) { @@ -76,16 +76,8 @@ class PreviewDeviceDiscovery extends DeviceDiscovery { processManager: _processManager, previewBinary: previewBinary, ); - final bool matchesRequirements; - if (!_features.isPreviewDeviceEnabled) { - matchesRequirements = false; - } else if (filter == null) { - matchesRequirements = true; - } else { - matchesRequirements = await filter.matchesRequirements(device); - } return [ - if (matchesRequirements) + if (_features.isPreviewDeviceEnabled) device, ]; } @@ -114,7 +106,7 @@ class PreviewDevice extends Device { _fileSystem = fileSystem, _bundleBuilderFactory = builderFactory, _artifacts = artifacts, - super('preview', ephemeral: false, category: Category.desktop, platformType: PlatformType.custom); + super('preview', ephemeral: false, category: Category.desktop, platformType: PlatformType.windowsPreview); final ProcessManager _processManager; final Logger _logger; @@ -161,7 +153,7 @@ class PreviewDevice extends Device { bool isSupportedForProject(FlutterProject flutterProject) => true; @override - String get name => 'preview'; + String get name => 'Preview'; @override DevicePortForwarder get portForwarder => const NoOpDevicePortForwarder(); diff --git a/packages/flutter_tools/test/commands.shard/hermetic/daemon_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/daemon_test.dart index 1ddc2d4f5a..769792d08c 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/daemon_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/daemon_test.dart @@ -7,10 +7,12 @@ import 'dart:io' as io; import 'dart:typed_data'; import 'package:fake_async/fake_async.dart'; +import 'package:file/memory.dart'; import 'package:file/src/interface/file.dart'; import 'package:flutter_tools/src/android/android_device.dart'; import 'package:flutter_tools/src/android/android_workflow.dart'; import 'package:flutter_tools/src/application_package.dart'; +import 'package:flutter_tools/src/artifacts.dart'; import 'package:flutter_tools/src/base/dds.dart'; import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/utils.dart'; @@ -22,8 +24,10 @@ import 'package:flutter_tools/src/features.dart'; import 'package:flutter_tools/src/fuchsia/fuchsia_workflow.dart'; import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/ios/ios_workflow.dart'; +import 'package:flutter_tools/src/preview_device.dart'; import 'package:flutter_tools/src/resident_runner.dart'; import 'package:flutter_tools/src/vmservice.dart'; +import 'package:flutter_tools/src/windows/windows_workflow.dart'; import 'package:test/fake.dart'; import '../../src/common.dart'; @@ -121,10 +125,91 @@ void main() { expect(response.data['id'], 0); expect(response.data['result'], isNotEmpty); - expect((response.data['result']! as Map)['platforms'], {'macos'}); + expect( + response.data['result']! as Map, + const { + 'platforms': ['macos', 'windows', 'windowsPreview'], + 'platformTypes': >{ + 'web': { + 'isSupported': false, + 'reasons': >[ + { + 'reasonText': 'the Web feature is not enabled', + 'fixText': 'Run "flutter config --enable-web"', + 'fixCode': 'config', + }, + ], + }, + 'android': { + 'isSupported': false, + 'reasons': >[ + { + 'reasonText': 'the Android feature is not enabled', + 'fixText': 'Run "flutter config --enable-android"', + 'fixCode': 'config', + }, + ], + }, + 'ios': { + 'isSupported': false, + 'reasons': >[ + { + 'reasonText': 'the iOS feature is not enabled', + 'fixText': 'Run "flutter config --enable-ios"', + 'fixCode': 'config', + }, + ], + }, + 'linux': { + 'isSupported': false, + 'reasons': >[ + { + 'reasonText': 'the Linux feature is not enabled', + 'fixText': 'Run "flutter config --enable-linux-desktop"', + 'fixCode': 'config', + }, + ], + }, + 'macos': {'isSupported': true}, + 'windows': {'isSupported': true}, + 'fuchsia': { + 'isSupported': false, + 'reasons': >[ + { + 'reasonText': 'the Fuchsia feature is not enabled', + 'fixText': 'Run "flutter config --enable-fuchsia"', + 'fixCode': 'config', + }, + { + 'reasonText': 'the Fuchsia platform is not enabled for this project', + 'fixText': 'Run "flutter create --platforms=fuchsia ." in your application directory', + 'fixCode': 'create', + }, + ], + }, + 'custom': { + 'isSupported': false, + 'reasons': >[ + { + 'reasonText': 'the custom devices feature is not enabled', + 'fixText': 'Run "flutter config --enable-custom-devices"', + 'fixCode': 'config', + }, + ], + }, + 'windowsPreview': {'isSupported': true}, + }, + }, + ); }, overrides: { // Disable Android/iOS and enable macOS to make sure result is consistent and defaults are tested off. - FeatureFlags: () => TestFeatureFlags(isAndroidEnabled: false, isIOSEnabled: false, isMacOSEnabled: true), + FeatureFlags: () => TestFeatureFlags( + isAndroidEnabled: false, + isIOSEnabled: false, + isMacOSEnabled: true, + isPreviewDeviceEnabled: true, + isWindowsEnabled: true, + ), }); testUsingContext('printError should send daemon.logMessage event', () async { @@ -342,18 +427,75 @@ void main() { final FakePollingDeviceDiscovery discoverer = FakePollingDeviceDiscovery(); daemon.deviceDomain.addDeviceDiscoverer(discoverer); discoverer.addDevice(FakeAndroidDevice()); + final MemoryFileSystem fs = MemoryFileSystem.test(); + discoverer.addDevice(PreviewDevice( + processManager: FakeProcessManager.empty(), + logger: BufferLogger.test(), + fileSystem: fs, + previewBinary: fs.file(r'preview_device.exe'), + artifacts: Artifacts.test(fileSystem: fs), + builderFactory: () => throw UnimplementedError('TODO implement builder factory'), + )); - return daemonStreams.outputs.stream.skipWhile(_isConnectedEvent).first.then((DaemonMessage response) async { + final List> names = >[]; + await daemonStreams.outputs.stream.skipWhile(_isConnectedEvent).take(2).forEach((DaemonMessage response) async { expect(response.data['event'], 'device.added'); expect(response.data['params'], isMap); final Map params = castStringKeyedMap(response.data['params'])!; - expect(params['platform'], isNotEmpty); // the fake device has a platform of 'android-arm' + names.add(params); }); + await daemonStreams.outputs.close(); + expect( + names, + containsAll(const >[ + { + 'id': 'device', + 'name': 'android device', + 'platform': 'android-arm', + 'emulator': false, + 'category': 'mobile', + 'platformType': 'android', + 'ephemeral': false, + 'emulatorId': 'device', + 'sdk': 'Android 12', + 'capabilities': { + 'hotReload': true, + 'hotRestart': true, + 'screenshot': true, + 'fastStart': true, + 'flutterExit': true, + 'hardwareRendering': true, + 'startPaused': true, + }, + }, + { + 'id': 'preview', + 'name': 'Preview', + 'platform': 'windows-x64', + 'emulator': false, + 'category': 'desktop', + 'platformType': 'windowsPreview', + 'ephemeral': false, + 'emulatorId': null, + 'sdk': 'preview', + 'capabilities': { + 'hotReload': true, + 'hotRestart': true, + 'screenshot': false, + 'fastStart': false, + 'flutterExit': true, + 'hardwareRendering': true, + 'startPaused': true, + }, + }, + ]), + ); }, overrides: { AndroidWorkflow: () => FakeAndroidWorkflow(), IOSWorkflow: () => FakeIOSWorkflow(), FuchsiaWorkflow: () => FakeFuchsiaWorkflow(), + WindowsWorkflow: () => FakeWindowsWorkflow(), }); testUsingContext('device.discoverDevices should respond with list', () async { @@ -930,6 +1072,13 @@ bool _notEvent(DaemonMessage message) => message.data['event'] == null; bool _isConnectedEvent(DaemonMessage message) => message.data['event'] == 'daemon.connected'; +class FakeWindowsWorkflow extends Fake implements WindowsWorkflow { + FakeWindowsWorkflow({ this.canListDevices = true }); + + @override + final bool canListDevices; +} + class FakeFuchsiaWorkflow extends Fake implements FuchsiaWorkflow { FakeFuchsiaWorkflow({ this.canListDevices = true }); @@ -956,7 +1105,7 @@ class FakeAndroidDevice extends Fake implements AndroidDevice { final String id = 'device'; @override - final String name = 'device'; + final String name = 'android device'; @override Future get emulatorId async => 'device'; diff --git a/packages/flutter_tools/test/general.shard/preview_device_test.dart b/packages/flutter_tools/test/general.shard/preview_device_test.dart index 8b2d4296d9..f65648f509 100644 --- a/packages/flutter_tools/test/general.shard/preview_device_test.dart +++ b/packages/flutter_tools/test/general.shard/preview_device_test.dart @@ -52,7 +52,7 @@ void main() { ); expect(await device.isLocalEmulator, false); - expect(device.name, 'preview'); + expect(device.name, 'Preview'); expect(await device.sdkNameAndVersion, 'preview'); expect(await device.targetPlatform, TargetPlatform.windows_x64); expect(device.category, Category.desktop);