Repository: flutter/flutter
File: packages/flutter_tools/lib/src/commands/create.dart (Dart)

Code before:
      // re-generate files for existing platforms
      templateContext[existingPlatform] = true;
    }

    final bool willAddPlatforms = platformsToAdd.isNotEmpty;
    templateContext['no_platforms'] = !willAddPlatforms;
    var generatedCount = 0;
    final String? description = argResults!.wasParsed('description')
        ? stringArg('description')
        : 'A new Flutter plugin project.';
    templateContext['description'] = description;

    final projectName = templateContext['projectName'] as String?;
    final bool includeDarwin = templateContext['darwin'] as bool? ?? false;
    final bool originalIos = templateContext['ios'] as bool? ?? false;
    final bool originalMacos = templateContext['macos'] as bool? ?? false;
    if (includeDarwin) {
      // Temporarily disable ios/macos for the plugin generation
      // so we don't get ios/ and macos/ directories in the plugin root.
      templateContext['ios'] = false;
      templateContext['macos'] = false;
    }

    final templates = <String>['plugin', 'plugin_shared'];

    final bool includeApplePlatforms =
        templateContext['ios'] == true || templateContext['macos'] == true || includeDarwin;

    // Check if this is an existing plugin with CocoaPods structure (Classes/ directory).
    // If so, preserve backward compatibility by using CocoaPods templates.
    // For new plugins, use SwiftPM structure.
    final bool hasExistingCocoaPodsStructure =
        directory.childDirectory('ios').childDirectory('Classes').existsSync() ||
        directory.childDirectory('macos').childDirectory('Classes').existsSync() ||
        directory.childDirectory('darwin').childDirectory('Classes').existsSync();

    final bool useSwiftPackageManagerStructure =
        includeApplePlatforms && !hasExistingCocoaPodsStructure;

    // Update withSwiftPackageManager to match the plugin structure we're generating.
    // This controls what path the podspec points to (Sources/ vs Classes/).
    // The example app will also use this value, but flutter run dynamically handles
    // SwiftPM/CocoaPods based on the current feature flag setting at runtime.
    if (includeApplePlatforms) {
      templateContext['withSwiftPackageManager'] = useSwiftPackageManagerStructure;
    }

    if (includeApplePlatforms) {
      if (useSwiftPackageManagerStructure) {
        // The podspec (also generated) will point to the SwiftPM source location,
        // allowing both SwiftPM and CocoaPods users to consume the plugin.

Review comment (suggestion):
Seems like there's a lot of iOS/macOS specific logic in this method now. Perhaps it would be good to break it into it's own function like `_updateContextAndTemplatesForDarwin(templateContext, templates)`

Code after:
    // Plugins only add a platform if it was requested explicitly by the user.
    if (!argResults!.wasParsed('platforms')) {
      for (final String platform in kAllCreatePlatforms) {
        templateContext[platform] = false;
      }
    }
    final List<String> platformsToAdd = _getSupportedPlatformsFromTemplateContext(templateContext);

    final List<String> existingPlatforms = _getSupportedPlatformsInPlugin(directory);
    for (final existingPlatform in existingPlatforms) {
      // re-generate files for existing platforms
      templateContext[existingPlatform] = true;
    }

    final bool willAddPlatforms = platformsToAdd.isNotEmpty;
    templateContext['no_platforms'] = !willAddPlatforms;
    var generatedCount = 0;
    final String? description = argResults!.wasParsed('description')
        ? stringArg('description')
        : 'A new Flutter plugin project.';
    templateContext['description'] = description;

    final projectName = templateContext['projectName'] as String?;
    final templates = <String>['plugin', 'plugin_shared'];

    final ({bool originalIos, bool originalMacos, bool? originalWithSwiftPackageManager})
    darwinState = _updateContextAndTemplatesForDarwin(
      templateContext: templateContext,
      templates: templates,
      directory: directory,
      projectName: projectName,
    );

    generatedCount += await renderMerged(
      templates,
      directory,
      templateContext,
      overwrite: overwrite,
      printStatusWhenWriting: printStatusWhenWriting,
    );
    // Restore the original ios, macos, and withSwiftPackageManager values.
    // This is necessary in case the user requested the darwin platform,
    // and we need to restore them for the example app generation.
    templateContext['ios'] = darwinState.originalIos;
    templateContext['macos'] = darwinState.originalMacos;
    if (darwinState.originalWithSwiftPackageManager != null) {
      templateContext['withSwiftPackageManager'] = darwinState.originalWithSwiftPackageManager;
    } else {
      templateContext.remove('withSwiftPackageManager');
    }