From 7c22f0d2140e0f40b050e7e20b42f0a6d481340b Mon Sep 17 00:00:00 2001 From: Darshan Rander Date: Fri, 29 Jan 2021 03:24:04 +0530 Subject: [PATCH] Added warning when not enabled platform is selected (#73205) --- .../lib/src/commands/create.dart | 63 +++++++++++++++-- .../commands.shard/permeable/create_test.dart | 69 +++++++++++++++++++ 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 99b3044614..bbbac69f28 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -300,13 +300,19 @@ class CreateCommand extends CreateBase { } else if (_getSupportedPlatformsInPlugin(projectDir).isEmpty){ _printNoPluginMessage(); } - _printAddPlatformMessage(relativePluginPath); + + final List platformsToWarn = _getPlatformWarningList(requestedPlatforms); + if (platformsToWarn.isNotEmpty) { + _printWarningDisabledPlatform(platformsToWarn); + } + _printPluginAddPlatformMessage(relativePluginPath); } else { // Tell the user the next steps. final FlutterProject project = FlutterProject.fromPath(projectDirPath); final FlutterProject app = project.hasExampleApp ? project.example : project; final String relativeAppPath = globals.fs.path.normalize(globals.fs.path.relative(app.directory.path)); final String relativeAppMain = globals.fs.path.join(relativeAppPath, 'lib', 'main.dart'); + final List requestedPlatforms = _getUserRequestedPlatforms(); // Let them know a summary of the state of their tooling. globals.printStatus(''' @@ -322,7 +328,13 @@ To enable null safety, type: Your $application code is in $relativeAppMain. '''); + // Show warning if any selected platform is not enabled + final List platformsToWarn = _getPlatformWarningList(requestedPlatforms); + if (platformsToWarn.isNotEmpty) { + _printWarningDisabledPlatform(platformsToWarn); + } } + return FlutterCommandResult.success(); } @@ -500,21 +512,64 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi void _printPluginUpdatePubspecMessage(String pluginPath, String platformsString) { globals.printStatus(''' You need to update $pluginPath/pubspec.yaml to support $platformsString. - ''', emphasis: true, color: TerminalColor.red); } void _printNoPluginMessage() { globals.printError(''' You've created a plugin project that doesn't yet support any platforms. - '''); } -void _printAddPlatformMessage(String pluginPath) { +void _printPluginAddPlatformMessage(String pluginPath) { globals.printStatus(''' To add platforms, run `flutter create -t plugin --platforms .` under $pluginPath. For more information, see https://flutter.dev/go/plugin-platforms. '''); } + +// returns a list disabled, but requested platforms +List _getPlatformWarningList(List requestedPlatforms) { + final List platformsToWarn = [ + if (requestedPlatforms.contains('web') && !featureFlags.isWebEnabled) + 'web', + if (requestedPlatforms.contains('macos') && !featureFlags.isMacOSEnabled) + 'macos', + if (requestedPlatforms.contains('windows') && !featureFlags.isWindowsEnabled) + 'windows', + if (requestedPlatforms.contains('linux') && !featureFlags.isLinuxEnabled) + 'linux', + ]; + + return platformsToWarn; +} + +void _printWarningDisabledPlatform(List platforms) { + final List desktop = []; + final List web = []; + + for (final String platform in platforms) { + if (platform == 'web') { + web.add(platform); + } else if (platform == 'macos' || platform == 'windows' || platform == 'linux') { + desktop.add(platform); + } + } + + if (desktop.isNotEmpty) { + final String platforms = desktop.length > 1 ? 'platforms' : 'platform'; + final String verb = desktop.length > 1 ? 'are' : 'is'; + + globals.printStatus(''' +The desktop $platforms: ${desktop.join(', ')} $verb currently not supported on your local environment. +For more details, see: https://flutter.dev/desktop +'''); + } + if (web.isNotEmpty) { + globals.printStatus(''' +The web is currently not supported on your local environment. +For more details, see: https://flutter.dev/docs/get-started/web +'''); + } +} diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index c3f882a698..293b7f050e 100755 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -37,6 +37,7 @@ import '../../src/testbed.dart'; const String _kNoPlatformsMessage = 'You\'ve created a plugin project that doesn\'t yet support any platforms.\n'; const String frameworkRevision = '12345678'; const String frameworkChannel = 'omega'; +const String _kDisabledPlatformRequestedMessage = 'currently not supported on your local environment.'; // TODO(fujino): replace FakePlatform.fromPlatform() with FakePlatform() final Generator _kNoColorTerminalPlatform = () => FakePlatform.fromPlatform(const LocalPlatform())..stdoutSupportsAnsi = false; final Map noColorTerminalOverride = { @@ -2352,6 +2353,74 @@ void main() { Logger: () => logger, }); + testUsingContext('should show warning when disabled platforms are selected while creating a plugin', () async { + Cache.flutterRoot = '../..'; + when(mockFlutterVersion.frameworkRevision).thenReturn(frameworkRevision); + when(mockFlutterVersion.channel).thenReturn(frameworkChannel); + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--no-pub', '--template=plugin', '--platforms=android,ios,web,windows,macos,linux', projectDir.path]); + await runner.run(['create', '--no-pub', '--template=plugin', projectDir.path]); + expect(logger.statusText, contains(_kDisabledPlatformRequestedMessage)); + + }, overrides: { + FeatureFlags: () => TestFeatureFlags(), + Logger: () => logger, + }); + + testUsingContext("shouldn't show warning when only enabled platforms are selected while creating a plugin", () async { + Cache.flutterRoot = '../..'; + when(mockFlutterVersion.frameworkRevision).thenReturn(frameworkRevision); + when(mockFlutterVersion.channel).thenReturn(frameworkChannel); + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--no-pub', '--template=plugin', '--platforms=android,ios,windows', projectDir.path]); + await runner.run(['create', '--no-pub', '--template=plugin', projectDir.path]); + expect(logger.statusText, isNot(contains(_kDisabledPlatformRequestedMessage))); + + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true), + Logger: () => logger, + }); + + testUsingContext('should show warning when disabled platforms are selected while creating a app', () async { + Cache.flutterRoot = '../..'; + when(mockFlutterVersion.frameworkRevision).thenReturn(frameworkRevision); + when(mockFlutterVersion.channel).thenReturn(frameworkChannel); + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--no-pub', '--platforms=android,ios,web,windows,macos,linux', projectDir.path]); + await runner.run(['create', '--no-pub', projectDir.path]); + expect(logger.statusText, contains(_kDisabledPlatformRequestedMessage)); + + }, overrides: { + FeatureFlags: () => TestFeatureFlags(), + Logger: () => logger, + }); + + testUsingContext("shouldn't show warning when only enabled platforms are selected while creating a app", () async { + Cache.flutterRoot = '../..'; + when(mockFlutterVersion.frameworkRevision).thenReturn(frameworkRevision); + when(mockFlutterVersion.channel).thenReturn(frameworkChannel); + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--no-pub', '--template=plugin', '--platforms=windows', projectDir.path]); + await runner.run(['create', '--no-pub', '--template=plugin', projectDir.path]); + expect(logger.statusText, isNot(contains(_kDisabledPlatformRequestedMessage))); + + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true, isAndroidEnabled: false, isIOSEnabled: false), + Logger: () => logger, + }); + testUsingContext('flutter create prints note about null safety', () async { await _createProject( projectDir,