From 95b98dc2c23835ba315d7d5c32563254bd02ec09 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 24 Jun 2020 10:38:03 -0700 Subject: [PATCH] flutter create . on plugins also regenerates files for platforms supported in pubspec.yaml (#60159) --- .../lib/src/commands/create.dart | 22 ++- .../commands.shard/permeable/create_test.dart | 125 ++++++++++++++++++ 2 files changed, 140 insertions(+), 7 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index b20b1e99ba..068018ccd8 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -41,7 +41,7 @@ const List _kAvailablePlatforms = [ ]; const String _kNoPlatformsErrorMessage = ''' -The plugin project was generated without specifying the `--platforms` flag, no platforms are currently supported. +The plugin project was generated without specifying the `--platforms` flag, no new platforms are added. To add platforms, run `flutter create -t plugin --platforms .` under the same directory. You can also find detailed instructions on how to add platforms in the `pubspec.yaml` at https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin-platforms. '''; @@ -562,8 +562,20 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi templateContext['windows'] = false; globals.printError(_kNoPlatformsErrorMessage); } - final List platforms = _getSupportedPlatformsFromTemplateContext(templateContext); - final bool willAddPlatforms = platforms.isNotEmpty; + final List platformsToAdd = _getSupportedPlatformsFromTemplateContext(templateContext); + + final String pubspecPath = globals.fs.path.join(directory.absolute.path, 'pubspec.yaml'); + final FlutterManifest manifest = FlutterManifest.createFromPath(pubspecPath, fileSystem: globals.fs, logger: globals.logger); + List existingPlatforms = []; + if (manifest.supportedPlatforms != null) { + existingPlatforms = manifest.supportedPlatforms.keys.toList(); + for (final String existingPlatform in existingPlatforms) { + // re-generate files for existing platforms + templateContext[existingPlatform] = true; + } + } + + final bool willAddPlatforms = platformsToAdd.isNotEmpty; templateContext['no_platforms'] = !willAddPlatforms; int generatedCount = 0; final String description = argResults.wasParsed('description') @@ -583,10 +595,6 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi if (willAddPlatforms) { // If adding new platforms to an existing plugin project, prints // a help message containing the platforms maps need to be added to the `platforms` key in the pubspec. - final String pubspecPath = globals.fs.path.join(directory.absolute.path, 'pubspec.yaml'); - final List platformsToAdd = List.from(platforms); - final FlutterManifest manifest = FlutterManifest.createFromPath(pubspecPath, fileSystem: globals.fs, logger: globals.logger); - final List existingPlatforms = manifest.supportedPlatforms.keys.toList(); platformsToAdd.removeWhere(existingPlatforms.contains); final YamlMap platformsMapToPrint = Plugin.createPlatformsYamlMap(platformsToAdd, templateContext['pluginClass'] as String, templateContext['androidIdentifier'] as String); if (platformsMapToPrint.isNotEmpty) { 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 014e70aad2..2f52ae02a9 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -1820,6 +1820,131 @@ void main() { , throwsToolExit(message: 'The "--platforms" argument is not supported', exitCode: 2)); }); + testUsingContext('create a plugin with android, delete then re-create folders', () 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', projectDir.path]); + expect(projectDir.childDirectory('android').existsSync(), true); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), true); + + globals.fs.file(globals.fs.path.join(projectDir.path, 'android')).deleteSync(recursive: true); + globals.fs.file(globals.fs.path.join(projectDir.path, 'example/android')).deleteSync(recursive: true); + expect(projectDir.childDirectory('android').existsSync(), false); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), false); + + await runner.run(['create', '--no-pub', projectDir.path]); + + expect(projectDir.childDirectory('android').existsSync(), true); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), true); + }); + + testUsingContext('create a plugin with android, delete then re-create folders while also adding windows', () 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', projectDir.path]); + expect(projectDir.childDirectory('android').existsSync(), true); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), true); + + globals.fs.file(globals.fs.path.join(projectDir.path, 'android')).deleteSync(recursive: true); + globals.fs.file(globals.fs.path.join(projectDir.path, 'example/android')).deleteSync(recursive: true); + expect(projectDir.childDirectory('android').existsSync(), false); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), false); + + await runner.run(['create', '--no-pub', '--template=plugin', '--platforms=windows', projectDir.path]); + + expect(projectDir.childDirectory('android').existsSync(), true); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), true); + expect(projectDir.childDirectory('windows').existsSync(), true); + expect(projectDir.childDirectory('example').childDirectory('windows').existsSync(), true); + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true), + }); + + testUsingContext('flutter create . on and existing plugin does not add android folders if android is not supported in pubspec', () 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=ios', projectDir.path]); + + await runner.run(['create', '--no-pub', projectDir.path]); + expect(projectDir.childDirectory('android').existsSync(), false); + expect(projectDir.childDirectory('example').childDirectory('android').existsSync(), false); + }); + + testUsingContext('flutter create . on and existing plugin does not add windows folder even feature is enabled', () 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', projectDir.path]); + + await runner.run(['create', '--no-pub', projectDir.path]); + expect(projectDir.childDirectory('windows').existsSync(), false); + expect(projectDir.childDirectory('example').childDirectory('windows').existsSync(), false); + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true), + }); + + testUsingContext('flutter create . on and existing plugin does not add linux folder even feature is enabled', () 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', projectDir.path]); + + await runner.run(['create', '--no-pub', projectDir.path]); + expect(projectDir.childDirectory('linux').existsSync(), false); + expect(projectDir.childDirectory('example').childDirectory('linux').existsSync(), false); + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isLinuxEnabled: true), + }); + + testUsingContext('flutter create . on and existing plugin does not add web files even feature is enabled', () 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', projectDir.path]); + + await runner.run(['create', '--no-pub', projectDir.path]); + expect(projectDir.childDirectory('lib').childFile('flutter_project_web.dart').existsSync(), false); + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), + }); + + testUsingContext('flutter create . on and existing plugin does not add macos folder even feature is enabled', () 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', projectDir.path]); + + await runner.run(['create', '--no-pub', projectDir.path]); + expect(projectDir.childDirectory('macos').existsSync(), false); + expect(projectDir.childDirectory('example').childDirectory('macos').existsSync(), false); + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true), + }); + } Future _createProject(