checkIosToolchain function

Future<IosToolchain> checkIosToolchain(
  1. String? explicitBridge, {
  2. Map<String, String>? env,
  3. ProcessRunner run = boundedRun,
  4. BytesFetcher? fetch,
  5. void onPhase(
    1. String phase
    )?,
})

Locates (downloading when missing) and handshakes the bridge, and reads Xcode.

Implementation

Future<IosToolchain> checkIosToolchain(String? explicitBridge,
    {Map<String, String>? env,
    ProcessRunner run = boundedRun,
    BytesFetcher? fetch,
    void Function(String phase)? onPhase}) async {
  final environment = env ?? Platform.environment;
  var bridge = locateIosBridge(explicitBridge, env: environment);
  String? downloadError;
  if (!bridge.found) {
    final recent = _lastDownloadFailure;
    if (environment[noBridgeDownloadEnv] == 'true') {
      downloadError = '$noBridgeDownloadEnv is set';
    } else if (fetch == null && recent != null && DateTime.now().difference(recent.at) < _retryAfter) {
      downloadError = recent.error;
    } else {
      try {
        await downloadBridge(bridge.path, fetch: fetch, onPhase: onPhase);
        bridge = BridgeLocation(bridge.path, BridgeSource.cache);
      } on BridgeDownloadError catch (e) {
        downloadError = e.message;
      } on FileSystemException catch (e) {
        downloadError = 'could not save it to ${bridge.path}: ${e.message}';
      }
      if (downloadError != null) {
        _lastDownloadFailure = (error: downloadError, at: DateTime.now());
      }
    }
  } else if (!File(bridge.path).existsSync()) {
    bridge = BridgeLocation(bridge.path, BridgeSource.missing);
    downloadError = 'no file at ${bridge.path}';
  }
  final results = await Future.wait([
    detectXcode(run: run),
    if (bridge.found) bridgeHandshake(bridge.path, run: run),
  ]);
  var handshake = bridge.found ? results[1] as ({int? protocol, String? error}) : null;
  String? skippedBuild;
  final staleBecause =
      bridge.source == BridgeSource.build && handshake != null ? _staleBuild(bridge.path, handshake) : null;
  if (staleBecause != null && environment[noBridgeDownloadEnv] != 'true') {
    final cached = cachedBridgePath(env: environment);
    try {
      if (!File(cached).existsSync()) await downloadBridge(cached, fetch: fetch, onPhase: onPhase);
      final fresh = await bridgeHandshake(cached, run: run);
      if (fresh.protocol == expectedBridgeProtocol) {
        skippedBuild = 'skipped the local bridge build at ${bridge.path} ($staleBecause) and used the downloaded one; '
            'rebuild it (swift build -c release in native/ios_sim_bridge) to use a local build';
        bridge = BridgeLocation(cached, BridgeSource.cache);
        handshake = fresh;
      }
    } on Object {
      // no usable download either: keep the local build and its warning
    }
  }
  return IosToolchain(
    skippedBuild: skippedBuild,
    xcode: results[0] as XcodeInfo,
    bridge: bridge,
    bridgeProtocol: handshake?.protocol,
    bridgeError: handshake?.error,
    downloadError: downloadError,
    allowUntested: environment[allowUntestedXcodeEnv] == 'true',
  );
}