Unverified Commit 95b98dc2 authored by Chris Yang's avatar Chris Yang Committed by GitHub

flutter create . on plugins also regenerates files for platforms supported in pubspec.yaml (#60159)

parent 5623fea5
......@@ -41,7 +41,7 @@ const List<String> _kAvailablePlatforms = <String>[
];
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 <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<String> platforms = _getSupportedPlatformsFromTemplateContext(templateContext);
final bool willAddPlatforms = platforms.isNotEmpty;
final List<String> 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<String> existingPlatforms = <String>[];
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<String> platformsToAdd = List<String>.from(platforms);
final FlutterManifest manifest = FlutterManifest.createFromPath(pubspecPath, fileSystem: globals.fs, logger: globals.logger);
final List<String> 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) {
......
......@@ -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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['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(<String>['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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['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(<String>['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: <Type, Generator>{
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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin', '--platforms=ios', projectDir.path]);
await runner.run(<String>['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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin', '--platforms=android', projectDir.path]);
await runner.run(<String>['create', '--no-pub', projectDir.path]);
expect(projectDir.childDirectory('windows').existsSync(), false);
expect(projectDir.childDirectory('example').childDirectory('windows').existsSync(), false);
}, overrides: <Type, Generator>{
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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin', '--platforms=android', projectDir.path]);
await runner.run(<String>['create', '--no-pub', projectDir.path]);
expect(projectDir.childDirectory('linux').existsSync(), false);
expect(projectDir.childDirectory('example').childDirectory('linux').existsSync(), false);
}, overrides: <Type, Generator>{
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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin', '--platforms=android', projectDir.path]);
await runner.run(<String>['create', '--no-pub', projectDir.path]);
expect(projectDir.childDirectory('lib').childFile('flutter_project_web.dart').existsSync(), false);
}, overrides: <Type, Generator>{
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<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=plugin', '--platforms=android', projectDir.path]);
await runner.run(<String>['create', '--no-pub', projectDir.path]);
expect(projectDir.childDirectory('macos').existsSync(), false);
expect(projectDir.childDirectory('example').childDirectory('macos').existsSync(), false);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
});
}
Future<void> _createProject(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment