diff --git a/.github/workflows/scorecards-analysis.yml b/.github/workflows/scorecards-analysis.yml index 61cf485b52c46..738437d6cc1f6 100644 --- a/.github/workflows/scorecards-analysis.yml +++ b/.github/workflows/scorecards-analysis.yml @@ -51,6 +51,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@4355270be187e1b672a7a1c7c7bae5afdc1ab94a + uses: github/codeql-action/upload-sarif@d39d31e687223d841ef683f52467bd88e9b21c14 with: sarif_file: results.sarif diff --git a/packages/flutter_tools/lib/src/ios/migrations/uiapplicationmain_deprecation_migration.dart b/packages/flutter_tools/lib/src/ios/migrations/uiapplicationmain_deprecation_migration.dart new file mode 100644 index 0000000000000..c2fbbcf541721 --- /dev/null +++ b/packages/flutter_tools/lib/src/ios/migrations/uiapplicationmain_deprecation_migration.dart @@ -0,0 +1,51 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import '../../base/file_system.dart'; +import '../../base/project_migrator.dart'; +import '../../xcode_project.dart'; + +const String _appDelegateFileBefore = r''' +@UIApplicationMain +@objc class AppDelegate: FlutterAppDelegate { +'''; + +const String _appDelegateFileAfter = r''' +@main +@objc class AppDelegate: FlutterAppDelegate { +'''; + +/// Replace the deprecated `@UIApplicationMain` attribute with `@main`. +/// +/// See: +/// https://github.com/apple/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md +class UIApplicationmMainDeprecationMigration extends ProjectMigrator { + UIApplicationmMainDeprecationMigration( + IosProject project, + super.logger, + ) : _appDelegateSwift = project.appDelegateSwift; + + final File _appDelegateSwift; + + @override + Future migrate() async { + // Skip this migration if the project uses Objective-C. + if (!_appDelegateSwift.existsSync()) { + return true; + } + + // Migrate the ios/Runner/AppDelegate.swift file. + final String original = _appDelegateSwift.readAsStringSync(); + final String migrated = original.replaceFirst(_appDelegateFileBefore, _appDelegateFileAfter); + if (original == migrated) { + return true; + } + + logger.printStatus( + 'ios/Runner/AppDelegate.swift does not use the @main attribute, updating.', + ); + _appDelegateSwift.writeAsStringSync(migrated); + return true; + } +} diff --git a/packages/flutter_tools/lib/src/xcode_project.dart b/packages/flutter_tools/lib/src/xcode_project.dart index 16defd560f476..9852592fa7e60 100644 --- a/packages/flutter_tools/lib/src/xcode_project.dart +++ b/packages/flutter_tools/lib/src/xcode_project.dart @@ -178,15 +178,15 @@ class IosProject extends XcodeBasedProject { File get appFrameworkInfoPlist => _flutterLibRoot.childDirectory('Flutter').childFile('AppFrameworkInfo.plist'); + /// The 'AppDelegate.swift' file of the host app. This file might not exist if the app project uses Objective-C. + File get appDelegateSwift => _editableDirectory.childDirectory('Runner').childFile('AppDelegate.swift'); + File get infoPlist => _editableDirectory.childDirectory('Runner').childFile('Info.plist'); Directory get symlinks => _flutterLibRoot.childDirectory('.symlinks'); - /// True, if the app project is using swift. - bool get isSwift { - final File appDelegateSwift = _editableDirectory.childDirectory('Runner').childFile('AppDelegate.swift'); - return appDelegateSwift.existsSync(); - } + /// True if the app project uses Swift. + bool get isSwift => appDelegateSwift.existsSync(); /// Do all plugins support arm64 simulators to run natively on an ARM Mac? Future pluginsSupportArmSimulator() async { diff --git a/packages/flutter_tools/templates/app_shared/ios-swift.tmpl/Runner/AppDelegate.swift b/packages/flutter_tools/templates/app_shared/ios-swift.tmpl/Runner/AppDelegate.swift index 9074fee9290db..626664468b891 100644 --- a/packages/flutter_tools/templates/app_shared/ios-swift.tmpl/Runner/AppDelegate.swift +++ b/packages/flutter_tools/templates/app_shared/ios-swift.tmpl/Runner/AppDelegate.swift @@ -1,7 +1,7 @@ import Flutter import UIKit -@UIApplicationMain +@main @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication,