mac.dart 24.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7
import 'package:meta/meta.dart';
8
import 'package:process/process.dart';
9 10

import '../application_package.dart';
11
import '../artifacts.dart';
12
import '../base/common.dart';
13
import '../base/file_system.dart';
14
import '../base/io.dart';
15
import '../base/logger.dart';
16
import '../base/process.dart';
17
import '../base/utils.dart';
18
import '../build_info.dart';
19
import '../cache.dart';
20
import '../flutter_manifest.dart';
21
import '../globals.dart' as globals;
22
import '../macos/cocoapod_utils.dart';
23
import '../macos/xcode.dart';
24
import '../project.dart';
25
import '../reporting/reporting.dart';
26
import 'code_signing.dart';
27
import 'migrations/ios_migrator.dart';
28
import 'migrations/project_base_configuration_migration.dart';
29
import 'migrations/remove_framework_link_and_embedding_migration.dart';
30
import 'migrations/xcode_build_system_migration.dart';
31
import 'xcodeproj.dart';
32

33
class IMobileDevice {
34 35 36 37 38 39 40 41 42 43
  IMobileDevice({
    @required Artifacts artifacts,
    @required Cache cache,
    @required ProcessManager processManager,
    @required Logger logger,
  }) : _idevicesyslogPath = artifacts.getArtifactPath(Artifact.idevicesyslog, platform: TargetPlatform.ios),
      _idevicescreenshotPath = artifacts.getArtifactPath(Artifact.idevicescreenshot, platform: TargetPlatform.ios),
      _dyLdLibEntry = cache.dyLdLibEntry,
      _processUtils = ProcessUtils(logger: logger, processManager: processManager),
      _processManager = processManager;
44

45 46
  final String _idevicesyslogPath;
  final String _idevicescreenshotPath;
47 48 49
  final MapEntry<String, String> _dyLdLibEntry;
  final ProcessManager _processManager;
  final ProcessUtils _processUtils;
50

51
  bool get isInstalled => _isInstalled ??= _processManager.canRun(_idevicescreenshotPath);
52
  bool _isInstalled;
53

54
  /// Starts `idevicesyslog` and returns the running process.
55
  Future<Process> startLogger(String deviceID) {
56
    return _processUtils.start(
57 58 59 60 61 62
      <String>[
        _idevicesyslogPath,
        '-u',
        deviceID,
      ],
      environment: Map<String, String>.fromEntries(
63
        <MapEntry<String, String>>[_dyLdLibEntry]
64 65 66
      ),
    );
  }
67

68
  /// Captures a screenshot to the specified outputFile.
69
  Future<void> takeScreenshot(File outputFile) {
70
    return _processUtils.run(
71 72
      <String>[
        _idevicescreenshotPath,
73
        outputFile.path,
74
      ],
75
      throwOnError: true,
76
      environment: Map<String, String>.fromEntries(
77
        <MapEntry<String, String>>[_dyLdLibEntry]
78 79
      ),
    );
80
  }
81 82
}

83
Future<XcodeBuildResult> buildXcodeProject({
84
  BuildableIOSApp app,
85
  BuildInfo buildInfo,
86
  String targetOverride,
87
  bool buildForDevice,
88
  DarwinArch activeArch,
89
  bool codesign = true,
90
  String deviceID,
91
}) async {
92
  if (!upgradePbxProjWithFlutterAssets(app.project, globals.logger)) {
93
    return XcodeBuildResult(success: false);
94
  }
95

96
  final List<IOSMigrator> migrators = <IOSMigrator>[
97 98
    RemoveFrameworkLinkAndEmbeddingMigration(app.project, globals.logger, globals.xcode, globals.flutterUsage),
    XcodeBuildSystemMigration(app.project, globals.logger),
99
    ProjectBaseConfigurationMigration(app.project, globals.logger),
100 101
  ];

102 103 104
  final IOSMigration migration = IOSMigration(migrators);
  if (!migration.run()) {
    return XcodeBuildResult(success: false);
105 106
  }

107
  if (!_checkXcodeVersion()) {
108
    return XcodeBuildResult(success: false);
109
  }
110

111 112
  await removeFinderExtendedAttributes(app.project.hostAppRoot, processUtils, globals.logger);

113
  final XcodeProjectInfo projectInfo = await globals.xcodeProjectInterpreter.getInfo(app.project.hostAppRoot.path);
114 115
  final String scheme = projectInfo.schemeFor(buildInfo);
  if (scheme == null) {
116
    globals.printError('');
117
    if (projectInfo.definesCustomSchemes) {
118 119
      globals.printError('The Xcode project defines schemes: ${projectInfo.schemes.join(', ')}');
      globals.printError('You must specify a --flavor option to select one of them.');
120
    } else {
121 122
      globals.printError('The Xcode project does not define custom schemes.');
      globals.printError('You cannot use the --flavor option.');
123
    }
124
    return XcodeBuildResult(success: false);
125 126 127
  }
  final String configuration = projectInfo.buildConfigurationFor(buildInfo, scheme);
  if (configuration == null) {
128 129 130 131 132 133 134
    globals.printError('');
    globals.printError('The Xcode project defines build configurations: ${projectInfo.buildConfigurations.join(', ')}');
    globals.printError('Flutter expects a build configuration named ${XcodeProjectInfo.expectedBuildConfigurationFor(buildInfo, scheme)} or similar.');
    globals.printError('Open Xcode to fix the problem:');
    globals.printError('  open ios/Runner.xcworkspace');
    globals.printError('1. Click on "Runner" in the project navigator.');
    globals.printError('2. Ensure the Runner PROJECT is selected, not the Runner TARGET.');
135
    if (buildInfo.isDebug) {
136
      globals.printError('3. Click the Editor->Add Configuration->Duplicate "Debug" Configuration.');
137
    } else {
138
      globals.printError('3. Click the Editor->Add Configuration->Duplicate "Release" Configuration.');
139
    }
140 141 142 143 144 145 146 147 148 149
    globals.printError('');
    globals.printError('   If this option is disabled, it is likely you have the target selected instead');
    globals.printError('   of the project; see:');
    globals.printError('   https://stackoverflow.com/questions/19842746/adding-a-build-configuration-in-xcode');
    globals.printError('');
    globals.printError('   If you have created a completely custom set of build configurations,');
    globals.printError('   you can set the FLUTTER_BUILD_MODE=${buildInfo.modeName.toLowerCase()}');
    globals.printError('   in the .xcconfig file for that configuration and run from Xcode.');
    globals.printError('');
    globals.printError('4. If you are not using completely custom build configurations, name the newly created configuration ${buildInfo.modeName}.');
150
    return XcodeBuildResult(success: false);
151 152
  }

153 154 155 156 157
  final FlutterManifest manifest = app.project.parent.manifest;
  final String buildName = parsedBuildName(manifest: manifest, buildInfo: buildInfo);
  final bool buildNameIsMissing = buildName == null || buildName.isEmpty;

  if (buildNameIsMissing) {
158
    globals.printStatus('Warning: Missing build name (CFBundleShortVersionString).');
159 160 161 162 163 164
  }

  final String buildNumber = parsedBuildNumber(manifest: manifest, buildInfo: buildInfo);
  final bool buildNumberIsMissing = buildNumber == null || buildNumber.isEmpty;

  if (buildNumberIsMissing) {
165
    globals.printStatus('Warning: Missing build number (CFBundleVersion).');
166 167
  }
  if (buildNameIsMissing || buildNumberIsMissing) {
168
    globals.printError('Action Required: You must set a build name and number in the pubspec.yaml '
169 170 171
      'file version field before submitting to the App Store.');
  }

172
  Map<String, String> autoSigningConfigs;
173
  if (codesign && buildForDevice) {
174 175 176
    autoSigningConfigs = await getCodeSigningIdentityDevelopmentTeam(
      iosApp: app,
      processManager: globals.processManager,
177 178
      logger: globals.logger,
      buildInfo: buildInfo,
179
    );
180
  }
181

182
  final FlutterProject project = FlutterProject.current();
183 184
  await updateGeneratedXcodeProperties(
    project: project,
185
    targetOverride: targetOverride,
186
    buildInfo: buildInfo,
187
  );
188
  await processPodsIfNeeded(project.ios, getIosBuildDirectory(), buildInfo.mode);
189

xster's avatar
xster committed
190
  final List<String> buildCommands = <String>[
191 192 193
    '/usr/bin/env',
    'xcrun',
    'xcodebuild',
194
    '-configuration', configuration,
195 196
  ];

197
  if (globals.logger.isVerbose) {
xster's avatar
xster committed
198 199 200 201 202 203 204 205
    // An environment variable to be passed to xcode_backend.sh determining
    // whether to echo back executed commands.
    buildCommands.add('VERBOSE_SCRIPT_LOGGING=YES');
  } else {
    // This will print warnings and errors only.
    buildCommands.add('-quiet');
  }

206
  if (autoSigningConfigs != null) {
207
    for (final MapEntry<String, String> signingConfig in autoSigningConfigs.entries) {
208 209
      buildCommands.add('${signingConfig.key}=${signingConfig.value}');
    }
xster's avatar
xster committed
210 211
    buildCommands.add('-allowProvisioningUpdates');
    buildCommands.add('-allowProvisioningDeviceRegistration');
212
  }
213

214
  final List<FileSystemEntity> contents = app.project.hostAppRoot.listSync();
215
  for (final FileSystemEntity entity in contents) {
216
    if (globals.fs.path.extension(entity.path) == '.xcworkspace') {
xster's avatar
xster committed
217
      buildCommands.addAll(<String>[
218
        '-workspace', globals.fs.path.basename(entity.path),
219
        '-scheme', scheme,
220
        'BUILD_DIR=${globals.fs.path.absolute(getIosBuildDirectory())}',
221 222 223 224 225
      ]);
      break;
    }
  }

226
  // Check if the project contains a watchOS companion app.
227 228 229 230
  final bool hasWatchCompanion = await app.project.containsWatchCompanion(
    projectInfo.targets,
    buildInfo,
  );
231 232 233 234 235 236 237 238 239 240 241 242 243 244
  if (hasWatchCompanion) {
    // The -sdk argument has to be omitted if a watchOS companion app exists.
    // Otherwise the build will fail as WatchKit dependencies cannot be build using the iOS SDK.
    globals.printStatus('Watch companion app found. Adjusting build settings.');
    if (!buildForDevice && (deviceID == null || deviceID == '')) {
      globals.printError('No simulator device ID has been set.');
      globals.printError('A device ID is required to build an app with a watchOS companion app.');
      globals.printError('Please run "flutter devices" to get a list of available device IDs');
      globals.printError('and specify one using the -d, --device-id flag.');
      return XcodeBuildResult(success: false);
    }
    if (!buildForDevice) {
      buildCommands.addAll(<String>['-destination', 'id=$deviceID']);
    }
245
  } else {
246 247 248 249 250
    if (buildForDevice) {
      buildCommands.addAll(<String>['-sdk', 'iphoneos']);
    } else {
      buildCommands.addAll(<String>['-sdk', 'iphonesimulator', '-arch', 'x86_64']);
    }
251 252
  }

253
  if (activeArch != null) {
254
    final String activeArchName = getNameForDarwinArch(activeArch);
255 256
    if (activeArchName != null) {
      buildCommands.add('ONLY_ACTIVE_ARCH=YES');
257 258 259 260 261
      // Setting ARCHS to $activeArchName will break the build if a watchOS companion app exists,
      // as it cannot be build for the architecture of the flutter app.
      if (!hasWatchCompanion) {
        buildCommands.add('ARCHS=$activeArchName');
      }
262 263 264
    }
  }

265
  if (!codesign) {
xster's avatar
xster committed
266
    buildCommands.addAll(
267 268 269
      <String>[
        'CODE_SIGNING_ALLOWED=NO',
        'CODE_SIGNING_REQUIRED=NO',
270
        'CODE_SIGNING_IDENTITY=""',
271
      ],
272 273 274
    );
  }

275 276
  Status buildSubStatus;
  Status initialBuildStatus;
277
  Directory tempDir;
278

279
  File scriptOutputPipeFile;
280 281
  if (globals.logger.hasTerminal) {
    tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_build_log_pipe.');
282
    scriptOutputPipeFile = tempDir.childFile('pipe_to_stdout');
283
    globals.os.makePipe(scriptOutputPipeFile.path);
284 285 286

    Future<void> listenToScriptOutputLine() async {
      final List<String> lines = await scriptOutputPipeFile.readAsLines();
287
      for (final String line in lines) {
288
        if (line == 'done' || line == 'all done') {
289 290
          buildSubStatus?.stop();
          buildSubStatus = null;
291 292 293
          if (line == 'all done') {
            // Free pipe file.
            tempDir?.deleteSync(recursive: true);
294
            return;
295
          }
296
        } else {
297 298
          initialBuildStatus?.cancel();
          initialBuildStatus = null;
299
          buildSubStatus = globals.logger.startProgress(
300
            line,
301
            timeout: timeoutConfiguration.slowOperation,
302
            progressIndicatorPadding: kDefaultStatusPadding - 7,
303 304 305
          );
        }
      }
306
      await listenToScriptOutputLine();
307 308 309
    }

    // Trigger the start of the pipe -> stdout loop. Ignore exceptions.
310
    unawaited(listenToScriptOutputLine());
311 312 313 314

    buildCommands.add('SCRIPT_OUTPUT_STREAM_FILE=${scriptOutputPipeFile.absolute.path}');
  }

315 316 317
  // Don't log analytics for downstream Flutter commands.
  // e.g. `flutter build bundle`.
  buildCommands.add('FLUTTER_SUPPRESS_ANALYTICS=true');
318
  buildCommands.add('COMPILER_INDEX_STORE_ENABLE=NO');
319
  buildCommands.addAll(environmentVariablesAsXcodeBuildSettings(globals.platform));
320

321
  final Stopwatch sw = Stopwatch()..start();
322
  initialBuildStatus = globals.logger.startProgress('Running Xcode build...', timeout: timeoutConfiguration.fastOperation);
323 324 325

  final RunResult buildResult = await _runBuildWithRetries(buildCommands, app);

326 327
  // Notifies listener that no more output is coming.
  scriptOutputPipeFile?.writeAsStringSync('all done');
328
  buildSubStatus?.stop();
329
  buildSubStatus = null;
330
  initialBuildStatus?.cancel();
331
  initialBuildStatus = null;
332
  globals.printStatus(
333
    'Xcode build done.'.padRight(kDefaultStatusPadding + 1)
334
        + getElapsedAsSeconds(sw.elapsed).padLeft(5),
335
  );
336
  globals.flutterUsage.sendTiming('build', 'xcode-ios', Duration(milliseconds: sw.elapsedMilliseconds));
xster's avatar
xster committed
337

338 339 340 341 342
  // Run -showBuildSettings again but with the exact same parameters as the
  // build. showBuildSettings is reported to ocassionally timeout. Here, we give
  // it a lot of wiggle room (locally on Flutter Gallery, this takes ~1s).
  // When there is a timeout, we retry once. See issue #35988.
  final List<String> showBuildSettingsCommand = (List<String>
343
      .of(buildCommands)
344 345 346 347 348 349 350 351 352 353 354 355 356
      ..add('-showBuildSettings'))
      // Undocumented behavior: xcodebuild craps out if -showBuildSettings
      // is used together with -allowProvisioningUpdates or
      // -allowProvisioningDeviceRegistration and freezes forever.
      .where((String buildCommand) {
        return !const <String>[
          '-allowProvisioningUpdates',
          '-allowProvisioningDeviceRegistration',
        ].contains(buildCommand);
      }).toList();
  const Duration showBuildSettingsTimeout = Duration(minutes: 1);
  Map<String, String> buildSettings;
  try {
357
    final RunResult showBuildSettingsResult = await processUtils.run(
358
      showBuildSettingsCommand,
359
      throwOnError: true,
360 361 362 363 364 365 366 367 368 369
      workingDirectory: app.project.hostAppRoot.path,
      timeout: showBuildSettingsTimeout,
      timeoutRetries: 1,
    );
    final String showBuildSettings = showBuildSettingsResult.stdout.trim();
    buildSettings = parseXcodeBuildSettings(showBuildSettings);
  } on ProcessException catch (e) {
    if (e.toString().contains('timed out')) {
      BuildEvent('xcode-show-build-settings-timeout',
        command: showBuildSettingsCommand.join(' '),
370
        flutterUsage: globals.flutterUsage,
371 372 373 374
      ).send();
    }
    rethrow;
  }
xster's avatar
xster committed
375 376

  if (buildResult.exitCode != 0) {
377
    globals.printStatus('Failed to build iOS app');
xster's avatar
xster committed
378
    if (buildResult.stderr.isNotEmpty) {
379 380
      globals.printStatus('Error output from Xcode build:\n↳');
      globals.printStatus(buildResult.stderr, indent: 4);
381
    }
xster's avatar
xster committed
382
    if (buildResult.stdout.isNotEmpty) {
383
      globals.printStatus("Xcode's output:\n↳");
384
      globals.printStatus(buildResult.stdout, indent: 4);
385
    }
386
    return XcodeBuildResult(
387
      success: false,
xster's avatar
xster committed
388 389
      stdout: buildResult.stdout,
      stderr: buildResult.stderr,
390
      xcodeBuildExecution: XcodeBuildExecution(
xster's avatar
xster committed
391
        buildCommands: buildCommands,
392
        appDirectory: app.project.hostAppRoot.path,
393
        buildForPhysicalDevice: buildForDevice,
xster's avatar
xster committed
394
        buildSettings: buildSettings,
395 396
      ),
    );
397
  } else {
398 399 400 401 402 403 404 405 406
    // If the app contains a watch companion target, the sdk argument of xcodebuild has to be omitted.
    // For some reason this leads to TARGET_BUILD_DIR always ending in 'iphoneos' even though the
    // actual directory will end with 'iphonesimulator' for simulator builds.
    // The value of TARGET_BUILD_DIR is adjusted to accommodate for this effect.
    String targetBuildDir = buildSettings['TARGET_BUILD_DIR'];
    if (hasWatchCompanion && !buildForDevice) {
      globals.printTrace('Replacing iphoneos with iphonesimulator in TARGET_BUILD_DIR.');
      targetBuildDir = targetBuildDir.replaceFirst('iphoneos', 'iphonesimulator');
    }
407
    final String expectedOutputDirectory = globals.fs.path.join(
408
      targetBuildDir,
xster's avatar
xster committed
409 410 411
      buildSettings['WRAPPER_NAME'],
    );

412
    String outputDir;
413
    if (globals.fs.isDirectorySync(expectedOutputDirectory)) {
414 415
      // Copy app folder to a place where other tools can find it without knowing
      // the BuildInfo.
xster's avatar
xster committed
416
      outputDir = expectedOutputDirectory.replaceFirst('/$configuration-', '/');
417
      if (globals.fs.isDirectorySync(outputDir)) {
418
        // Previous output directory might have incompatible artifacts
419
        // (for example, kernel binary files produced from previous run).
420
        globals.fs.directory(outputDir).deleteSync(recursive: true);
421
      }
422
      globals.fsUtils.copyDirectorySync(
423 424 425
        globals.fs.directory(expectedOutputDirectory),
        globals.fs.directory(outputDir),
      );
xster's avatar
xster committed
426
    } else {
427
      globals.printError('Build succeeded but the expected app at $expectedOutputDirectory not found');
428
    }
429 430 431 432 433 434 435 436 437 438
    return XcodeBuildResult(
        success: true,
        output: outputDir,
        xcodeBuildExecution: XcodeBuildExecution(
          buildCommands: buildCommands,
          appDirectory: app.project.hostAppRoot.path,
          buildForPhysicalDevice: buildForDevice,
          buildSettings: buildSettings,
      ),
    );
439 440 441
  }
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/// Extended attributes applied by Finder can cause code signing errors. Remove them.
/// https://developer.apple.com/library/archive/qa/qa1940/_index.html
@visibleForTesting
Future<void> removeFinderExtendedAttributes(Directory iosProjectDirectory, ProcessUtils processUtils, Logger logger) async {
  final bool success = await processUtils.exitsHappy(
    <String>[
      'xattr',
      '-r',
      '-d',
      'com.apple.FinderInfo',
      iosProjectDirectory.path,
    ]
  );
  // Ignore all errors, for example if directory is missing.
  if (!success) {
    logger.printTrace('Failed to remove xattr com.apple.FinderInfo from ${iosProjectDirectory.path}');
  }
}

461 462 463 464 465 466 467 468 469
Future<RunResult> _runBuildWithRetries(List<String> buildCommands, BuildableIOSApp app) async {
  int buildRetryDelaySeconds = 1;
  int remainingTries = 8;

  RunResult buildResult;
  while (remainingTries > 0) {
    remainingTries--;
    buildRetryDelaySeconds *= 2;

470
    buildResult = await processUtils.run(
471 472 473 474 475 476 477 478 479 480 481 482
      buildCommands,
      workingDirectory: app.project.hostAppRoot.path,
      allowReentrantFlutter: true,
    );

    // If the result is anything other than a concurrent build failure, exit
    // the loop after the first build.
    if (!_isXcodeConcurrentBuildFailure(buildResult)) {
      break;
    }

    if (remainingTries > 0) {
483
      globals.printStatus('Xcode build failed due to concurrent builds, '
484 485 486
        'will retry in $buildRetryDelaySeconds seconds.');
      await Future<void>.delayed(Duration(seconds: buildRetryDelaySeconds));
    } else {
487
      globals.printStatus(
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
        'Xcode build failed too many times due to concurrent builds, '
        'giving up.');
      break;
    }
  }

  return buildResult;
}

bool _isXcodeConcurrentBuildFailure(RunResult result) {
return result.exitCode != 0 &&
    result.stdout != null &&
    result.stdout.contains('database is locked') &&
    result.stdout.contains('there are two concurrent builds running');
}

504
Future<void> diagnoseXcodeBuildFailure(XcodeBuildResult result, Usage flutterUsage, Logger logger) async {
505 506 507
  if (result.xcodeBuildExecution != null &&
      result.xcodeBuildExecution.buildForPhysicalDevice &&
      result.stdout?.toUpperCase()?.contains('BITCODE') == true) {
508 509 510
    BuildEvent('xcode-bitcode-failure',
      command: result.xcodeBuildExecution.buildCommands.toString(),
      settings: result.xcodeBuildExecution.buildSettings.toString(),
511
      flutterUsage: flutterUsage,
512
    ).send();
513 514
  }

515 516 517 518 519 520
  // Building for iOS Simulator, but the linked and embedded framework 'App.framework' was built for iOS.
  // or
  // Building for iOS, but the linked and embedded framework 'App.framework' was built for iOS Simulator.
  if (result.stdout?.contains('Building for iOS') == true
      && result.stdout?.contains('but the linked and embedded framework') == true
      && result.stdout?.contains('was built for iOS') == true) {
521 522 523 524 525
    logger.printError('');
    logger.printError('Your Xcode project requires migration. See https://flutter.dev/docs/development/ios-project-migration for details.');
    logger.printError('');
    logger.printError('You can temporarily work around this issue by running:');
    logger.printError('  rm -rf ios/Flutter/App.framework');
526 527 528
    return;
  }

529 530
  if (result.xcodeBuildExecution != null &&
      result.xcodeBuildExecution.buildForPhysicalDevice &&
531 532
      result.stdout?.contains('BCEROR') == true &&
      // May need updating if Xcode changes its outputs.
533
      result.stdout?.contains("Xcode couldn't find a provisioning profile matching") == true) {
534
    logger.printError(noProvisioningProfileInstruction, emphasis: true);
535 536
    return;
  }
537 538 539
  // Make sure the user has specified one of:
  // * DEVELOPMENT_TEAM (automatic signing)
  // * PROVISIONING_PROFILE (manual signing)
540 541
  if (result.xcodeBuildExecution != null &&
      result.xcodeBuildExecution.buildForPhysicalDevice &&
xster's avatar
xster committed
542
      !<String>['DEVELOPMENT_TEAM', 'PROVISIONING_PROFILE'].any(
543
        result.xcodeBuildExecution.buildSettings.containsKey)) {
544
    logger.printError(noDevelopmentTeamInstruction, emphasis: true);
545 546
    return;
  }
547 548
  if (result.xcodeBuildExecution != null &&
      result.xcodeBuildExecution.buildForPhysicalDevice &&
549
      result.xcodeBuildExecution.buildSettings['PRODUCT_BUNDLE_IDENTIFIER']?.contains('com.example') == true) {
550 551 552 553
    logger.printError('');
    logger.printError('It appears that your application still contains the default signing identifier.');
    logger.printError("Try replacing 'com.example' with your signing id in Xcode:");
    logger.printError('  open ios/Runner.xcworkspace');
554
    return;
555 556
  }
  if (result.stdout?.contains('Code Sign error') == true) {
557 558 559 560 561 562 563
    logger.printError('');
    logger.printError('It appears that there was a problem signing your application prior to installation on the device.');
    logger.printError('');
    logger.printError('Verify that the Bundle Identifier in your project is your signing id in Xcode');
    logger.printError('  open ios/Runner.xcworkspace');
    logger.printError('');
    logger.printError("Also try selecting 'Product > Build' to fix the problem:");
564
    return;
565
  }
566 567 568
}

class XcodeBuildResult {
569 570 571 572 573 574 575
  XcodeBuildResult({
    @required this.success,
    this.output,
    this.stdout,
    this.stderr,
    this.xcodeBuildExecution,
  });
576

577 578
  final bool success;
  final String output;
579 580
  final String stdout;
  final String stderr;
581 582 583 584 585 586
  /// The invocation of the build that resulted in this result instance.
  final XcodeBuildExecution xcodeBuildExecution;
}

/// Describes an invocation of a Xcode build command.
class XcodeBuildExecution {
587 588 589 590 591 592
  XcodeBuildExecution({
    @required this.buildCommands,
    @required this.appDirectory,
    @required this.buildForPhysicalDevice,
    @required this.buildSettings,
  });
593 594 595 596 597

  /// The original list of Xcode build commands used to produce this build result.
  final List<String> buildCommands;
  final String appDirectory;
  final bool buildForPhysicalDevice;
xster's avatar
xster committed
598 599
  /// The build settings corresponding to the [buildCommands] invocation.
  final Map<String, String> buildSettings;
600 601
}

602
const String _xcodeRequirement = 'Xcode $kXcodeRequiredVersionMajor.$kXcodeRequiredVersionMinor or greater is required to develop for iOS.';
603 604

bool _checkXcodeVersion() {
605
  if (!globals.platform.isMacOS) {
606
    return false;
607
  }
608
  if (!globals.xcodeProjectInterpreter.isInstalled) {
609
    globals.printError('Cannot find "xcodebuild". $_xcodeRequirement');
610 611
    return false;
  }
612
  if (!globals.xcode.isVersionSatisfactory) {
613
    globals.printError('Found "${globals.xcodeProjectInterpreter.versionText}". $_xcodeRequirement');
614 615
    return false;
  }
616 617 618
  return true;
}

619
// TODO(jmagman): Refactor to IOSMigrator.
620
bool upgradePbxProjWithFlutterAssets(IosProject project, Logger logger) {
621
  final File xcodeProjectFile = project.xcodeProjectInfoFile;
622 623
  assert(xcodeProjectFile.existsSync());
  final List<String> lines = xcodeProjectFile.readAsLinesSync();
624

625 626
  final RegExp oldAssets = RegExp(r'\/\* (flutter_assets|app\.flx)');
  final StringBuffer buffer = StringBuffer();
627
  final Set<String> printedStatuses = <String>{};
628

629 630 631
  for (final String line in lines) {
    final Match match = oldAssets.firstMatch(line);
    if (match != null) {
632
      if (printedStatuses.add(match.group(1))) {
633
        logger.printStatus('Removing obsolete reference to ${match.group(1)} from ${project.xcodeProject?.basename}');
634
      }
635 636 637
    } else {
      buffer.writeln(line);
    }
638
  }
639
  xcodeProjectFile.writeAsStringSync(buffer.toString());
640
  return true;
641
}