Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macOS] Migrate @NSApplicationMain attribute to @main
- Loading branch information
1 parent
500ed0b
commit a508d3e
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/flutter_tools/lib/src/macos/migrations/nsapplicationmain_deprecation_migration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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''' | ||
@NSApplicationMain | ||
class AppDelegate'''; | ||
|
||
const String _appDelegateFileAfter = r''' | ||
@main | ||
class AppDelegate'''; | ||
|
||
/// Replace the deprecated `@NSApplicationMain` attribute with `@main`. | ||
/// | ||
/// See: | ||
/// https://github.com/apple/swift-evolution/blob/main/proposals/0383-deprecate-uiapplicationmain-and-nsapplicationmain.md | ||
class NSApplicationMainDeprecationMigration extends ProjectMigrator { | ||
NSApplicationMainDeprecationMigration( | ||
MacOSProject project, | ||
super.logger, | ||
) : _appDelegateSwift = project.appDelegateSwift; | ||
|
||
final File _appDelegateSwift; | ||
|
||
@override | ||
Future<void> migrate() async { | ||
// Skip this migration if the project uses Objective-C. | ||
if (!_appDelegateSwift.existsSync()) { | ||
logger.printTrace( | ||
'macos/Runner/AppDelegate.swift not found, skipping @main migration.', | ||
); | ||
return; | ||
} | ||
|
||
// Migrate the macos/Runner/AppDelegate.swift file. | ||
final String original = _appDelegateSwift.readAsStringSync(); | ||
final String migrated = original.replaceFirst(_appDelegateFileBefore, _appDelegateFileAfter); | ||
if (original == migrated) { | ||
return; | ||
} | ||
|
||
logger.printWarning( | ||
'macos/Runner/AppDelegate.swift uses the deprecated @NSApplicationMain attribute, updating.', | ||
); | ||
_appDelegateSwift.writeAsStringSync(migrated); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/flutter_tools/templates/app_shared/macos.tmpl/Runner/AppDelegate.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters