deployment_target_migration.dart 3.09 KB
Newer Older
1 2 3 4 5 6
// 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';
7
import '../../xcode_project.dart';
8 9 10 11 12

/// Update the minimum iOS deployment version to the minimum allowed by Xcode without causing a warning.
class DeploymentTargetMigration extends ProjectMigrator {
  DeploymentTargetMigration(
    IosProject project,
13
    super.logger,
14
  )   : _xcodeProjectInfoFile = project.xcodeProjectInfoFile,
15
        _podfile = project.podfile,
16
        _appFrameworkInfoPlist = project.appFrameworkInfoPlist;
17 18

  final File _xcodeProjectInfoFile;
19
  final File _podfile;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  final File _appFrameworkInfoPlist;

  @override
  bool migrate() {
    if (_xcodeProjectInfoFile.existsSync()) {
      processFileLines(_xcodeProjectInfoFile);
    } else {
      logger.printTrace('Xcode project not found, skipping iOS deployment target version migration.');
    }

    if (_appFrameworkInfoPlist.existsSync()) {
      processFileLines(_appFrameworkInfoPlist);
    } else {
      logger.printTrace('AppFrameworkInfo.plist not found, skipping minimum OS version migration.');
    }

36 37 38 39 40 41
    if (_podfile.existsSync()) {
      processFileLines(_podfile);
    } else {
      logger.printTrace('Podfile not found, skipping global platform version migration.');
    }

42 43 44 45 46
    return true;
  }

  @override
  String migrateFileContents(String fileContents) {
47
    const String minimumOSVersionOriginal8 = '''
48 49 50
  <key>MinimumOSVersion</key>
  <string>8.0</string>
''';
51
    const String minimumOSVersionOriginal9 = '''
52 53 54
  <key>MinimumOSVersion</key>
  <string>9.0</string>
''';
55 56 57 58
    const String minimumOSVersionReplacement = '''
  <key>MinimumOSVersion</key>
  <string>11.0</string>
''';
59

60 61 62
    return fileContents
        .replaceAll(minimumOSVersionOriginal8, minimumOSVersionReplacement)
        .replaceAll(minimumOSVersionOriginal9, minimumOSVersionReplacement);
63 64 65 66
  }

  @override
  String? migrateLine(String line) {
67 68 69 70 71 72 73 74 75 76
    // Xcode project file changes.
    const String deploymentTargetOriginal8 = 'IPHONEOS_DEPLOYMENT_TARGET = 8.0;';
    const String deploymentTargetOriginal9 = 'IPHONEOS_DEPLOYMENT_TARGET = 9.0;';

    // Podfile changes.
    const String podfilePlatformVersionOriginal = "platform :ios, '9.0'";

    if (line.contains(deploymentTargetOriginal8)
        || line.contains(deploymentTargetOriginal9)
        || line.contains(podfilePlatformVersionOriginal)) {
77 78
      if (!migrationRequired) {
        // Only print for the first discovered change found.
79
        logger.printStatus('Updating minimum iOS deployment target to 11.0.');
80
      }
81 82 83 84 85 86 87

      const String deploymentTargetReplacement = 'IPHONEOS_DEPLOYMENT_TARGET = 11.0;';
      const String podfilePlatformVersionReplacement = "platform :ios, '11.0'";
      return line
          .replaceAll(deploymentTargetOriginal8, deploymentTargetReplacement)
          .replaceAll(deploymentTargetOriginal9, deploymentTargetReplacement)
          .replaceAll(podfilePlatformVersionOriginal, podfilePlatformVersionReplacement);
88 89 90 91 92
    }

    return line;
  }
}