Add macos project auto migration code for FlutterApplication (#122336)
Add macos project auto migration code for FlutterApplication
This commit is contained in:
@@ -30,6 +30,9 @@ class PlistParser {
|
||||
static const String kCFBundleVersionKey = 'CFBundleVersion';
|
||||
static const String kCFBundleDisplayNameKey = 'CFBundleDisplayName';
|
||||
static const String kMinimumOSVersionKey = 'MinimumOSVersion';
|
||||
static const String kNSPrincipalClassKey = 'NSPrincipalClass';
|
||||
|
||||
static const String _plutilExecutable = '/usr/bin/plutil';
|
||||
|
||||
/// Returns the content, converted to XML, of the plist file located at
|
||||
/// [plistFilePath].
|
||||
@@ -39,12 +42,11 @@ class PlistParser {
|
||||
///
|
||||
/// The [plistFilePath] argument must not be null.
|
||||
String? plistXmlContent(String plistFilePath) {
|
||||
const String executable = '/usr/bin/plutil';
|
||||
if (!_fileSystem.isFileSync(executable)) {
|
||||
throw const FileNotFoundException(executable);
|
||||
if (!_fileSystem.isFileSync(_plutilExecutable)) {
|
||||
throw const FileNotFoundException(_plutilExecutable);
|
||||
}
|
||||
final List<String> args = <String>[
|
||||
executable, '-convert', 'xml1', '-o', '-', plistFilePath,
|
||||
_plutilExecutable, '-convert', 'xml1', '-o', '-', plistFilePath,
|
||||
];
|
||||
try {
|
||||
final String xmlContent = _processUtils.runSync(
|
||||
@@ -53,11 +55,42 @@ class PlistParser {
|
||||
).stdout.trim();
|
||||
return xmlContent;
|
||||
} on ProcessException catch (error) {
|
||||
_logger.printTrace('$error');
|
||||
_logger.printError('$error');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces the string key in the given plist file with the given value.
|
||||
///
|
||||
/// If the value is null, then the key will be removed.
|
||||
///
|
||||
/// Returns true if successful.
|
||||
bool replaceKey(String plistFilePath, {required String key, String? value }) {
|
||||
if (!_fileSystem.isFileSync(_plutilExecutable)) {
|
||||
throw const FileNotFoundException(_plutilExecutable);
|
||||
}
|
||||
final List<String> args;
|
||||
if (value == null) {
|
||||
args = <String>[
|
||||
_plutilExecutable, '-remove', key, plistFilePath,
|
||||
];
|
||||
} else {
|
||||
args = <String>[
|
||||
_plutilExecutable, '-replace', key, '-string', value, plistFilePath,
|
||||
];
|
||||
}
|
||||
try {
|
||||
_processUtils.runSync(
|
||||
args,
|
||||
throwOnError: true,
|
||||
);
|
||||
} on ProcessException catch (error) {
|
||||
_logger.printError('$error');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Parses the plist file located at [plistFilePath] and returns the
|
||||
/// associated map of key/value property list pairs.
|
||||
///
|
||||
|
||||
@@ -17,6 +17,7 @@ import '../migrations/xcode_script_build_phase_migration.dart';
|
||||
import '../migrations/xcode_thin_binary_build_phase_input_paths_migration.dart';
|
||||
import '../project.dart';
|
||||
import 'cocoapod_utils.dart';
|
||||
import 'migrations/flutter_application_migration.dart';
|
||||
import 'migrations/macos_deployment_target_migration.dart';
|
||||
import 'migrations/remove_macos_framework_link_and_embedding_migration.dart';
|
||||
|
||||
@@ -57,6 +58,7 @@ Future<void> buildMacOS({
|
||||
XcodeProjectObjectVersionMigration(flutterProject.macos, globals.logger),
|
||||
XcodeScriptBuildPhaseMigration(flutterProject.macos, globals.logger),
|
||||
XcodeThinBinaryBuildPhaseInputPathsMigration(flutterProject.macos, globals.logger),
|
||||
FlutterApplicationMigration(flutterProject.macos, globals.logger),
|
||||
];
|
||||
|
||||
final ProjectMigration migration = ProjectMigration(migrators);
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// 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 '../../globals.dart' as globals;
|
||||
import '../../ios/plist_parser.dart';
|
||||
import '../../xcode_project.dart';
|
||||
|
||||
/// Update the minimum macOS deployment version to the minimum allowed by Xcode without causing a warning.
|
||||
class FlutterApplicationMigration extends ProjectMigrator {
|
||||
FlutterApplicationMigration(
|
||||
MacOSProject project,
|
||||
super.logger,
|
||||
) : _infoPlistFile = project.defaultHostInfoPlist;
|
||||
|
||||
final File _infoPlistFile;
|
||||
|
||||
@override
|
||||
void migrate() {
|
||||
if (_infoPlistFile.existsSync()) {
|
||||
final String? principleClass =
|
||||
globals.plistParser.getStringValueFromFile(_infoPlistFile.path, PlistParser.kNSPrincipalClassKey);
|
||||
if (principleClass == null || principleClass == 'FlutterApplication') {
|
||||
// No NSPrincipalClass defined, or already converted, so no migration
|
||||
// needed.
|
||||
return;
|
||||
}
|
||||
if (principleClass != 'NSApplication') {
|
||||
// Only replace NSApplication values, since we don't know why they might
|
||||
// have substituted something else.
|
||||
logger.printTrace('${_infoPlistFile.basename} has an '
|
||||
'${PlistParser.kNSPrincipalClassKey} of $principleClass, not '
|
||||
'NSApplication, skipping FlutterApplication migration.\nYou will need '
|
||||
'to modify your application class to derive from FlutterApplication.');
|
||||
return;
|
||||
}
|
||||
logger.printStatus('Updating ${_infoPlistFile.basename} to use FlutterApplication instead of NSApplication.');
|
||||
final bool success = globals.plistParser.replaceKey(_infoPlistFile.path, key: PlistParser.kNSPrincipalClassKey, value: 'FlutterApplication');
|
||||
if (!success) {
|
||||
logger.printError('Updating ${_infoPlistFile.basename} failed.');
|
||||
}
|
||||
} else {
|
||||
logger.printTrace('${_infoPlistFile.basename} not found, skipping FlutterApplication migration.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user