|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import '../../base/file_system.dart'; |
| 6 | +import '../../base/project_migrator.dart'; |
| 7 | +import '../../xcode_project.dart'; |
| 8 | + |
| 9 | +const String _appDelegateFileBefore = r''' |
| 10 | +@UIApplicationMain |
| 11 | +@objc class AppDelegate: FlutterAppDelegate { |
| 12 | +'''; |
| 13 | + |
| 14 | +const String _appDelegateFileAfter = r''' |
| 15 | +@main |
| 16 | +@objc class AppDelegate: FlutterAppDelegate { |
| 17 | +'''; |
| 18 | + |
| 19 | +/// Replace the deprecated `@UIApplicationMain` attribute with `@main`. |
| 20 | +/// |
| 21 | +/// See: |
| 22 | +/// https://github.com/apple/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md |
| 23 | +class UIApplicationmMainDeprecationMigration extends ProjectMigrator { |
| 24 | + UIApplicationmMainDeprecationMigration( |
| 25 | + IosProject project, |
| 26 | + super.logger, |
| 27 | + ) : _appDelegateSwift = project.appDelegateSwift; |
| 28 | + |
| 29 | + final File _appDelegateSwift; |
| 30 | + |
| 31 | + @override |
| 32 | + Future<bool> migrate() async { |
| 33 | + // Skip this migration if the project uses Objective-C. |
| 34 | + if (!_appDelegateSwift.existsSync()) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + // Migrate the ios/Runner/AppDelegate.swift file. |
| 39 | + final String original = _appDelegateSwift.readAsStringSync(); |
| 40 | + final String migrated = original.replaceFirst(_appDelegateFileBefore, _appDelegateFileAfter); |
| 41 | + if (original == migrated) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + logger.printStatus( |
| 46 | + 'ios/Runner/AppDelegate.swift does not use the @main attribute, updating.', |
| 47 | + ); |
| 48 | + _appDelegateSwift.writeAsStringSync(migrated); |
| 49 | + return true; |
| 50 | + } |
| 51 | +} |
0 commit comments