|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io' as io; |
| 7 | + |
| 8 | +import 'package:file/file.dart'; |
| 9 | + |
| 10 | +import 'core.dart'; |
| 11 | +import 'process_runner.dart'; |
| 12 | + |
| 13 | +const String _xcodeBuildCommand = 'xcodebuild'; |
| 14 | +const String _xcRunCommand = 'xcrun'; |
| 15 | + |
| 16 | +/// A utility class for interacting with the installed version of Xcode. |
| 17 | +class Xcode { |
| 18 | + /// Creates an instance that runs commends with the given [processRunner]. |
| 19 | + /// |
| 20 | + /// If [log] is true, commands run by this instance will long various status |
| 21 | + /// messages. |
| 22 | + Xcode({ |
| 23 | + this.processRunner = const ProcessRunner(), |
| 24 | + this.log = false, |
| 25 | + }); |
| 26 | + |
| 27 | + /// The [ProcessRunner] used to run commands. Overridable for testing. |
| 28 | + final ProcessRunner processRunner; |
| 29 | + |
| 30 | + /// Whether or not to log when running commands. |
| 31 | + final bool log; |
| 32 | + |
| 33 | + /// Runs an `xcodebuild` in [directory] with the given parameters. |
| 34 | + Future<int> runXcodeBuild( |
| 35 | + Directory directory, { |
| 36 | + List<String> actions = const <String>['build'], |
| 37 | + required String workspace, |
| 38 | + required String scheme, |
| 39 | + String? configuration, |
| 40 | + List<String> extraFlags = const <String>[], |
| 41 | + }) { |
| 42 | + final List<String> args = <String>[ |
| 43 | + _xcodeBuildCommand, |
| 44 | + ...actions, |
| 45 | + if (workspace != null) ...<String>['-workspace', workspace], |
| 46 | + if (scheme != null) ...<String>['-scheme', scheme], |
| 47 | + if (configuration != null) ...<String>['-configuration', configuration], |
| 48 | + ...extraFlags, |
| 49 | + ]; |
| 50 | + final String completeTestCommand = '$_xcRunCommand ${args.join(' ')}'; |
| 51 | + if (log) { |
| 52 | + print(completeTestCommand); |
| 53 | + } |
| 54 | + return processRunner.runAndStream(_xcRunCommand, args, |
| 55 | + workingDir: directory); |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns true if [project], which should be an .xcodeproj directory, |
| 59 | + /// contains a target called [target], false if it does not, and null if the |
| 60 | + /// check fails (e.g., if [project] is not an Xcode project). |
| 61 | + Future<bool?> projectHasTarget(Directory project, String target) async { |
| 62 | + final io.ProcessResult result = |
| 63 | + await processRunner.run(_xcRunCommand, <String>[ |
| 64 | + _xcodeBuildCommand, |
| 65 | + '-list', |
| 66 | + '-json', |
| 67 | + '-project', |
| 68 | + project.path, |
| 69 | + ]); |
| 70 | + if (result.exitCode != 0) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + Map<String, dynamic>? projectInfo; |
| 74 | + try { |
| 75 | + projectInfo = (jsonDecode(result.stdout as String) |
| 76 | + as Map<String, dynamic>)['project'] as Map<String, dynamic>?; |
| 77 | + } on FormatException { |
| 78 | + return null; |
| 79 | + } |
| 80 | + if (projectInfo == null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + final List<String>? targets = |
| 84 | + (projectInfo['targets'] as List<dynamic>?)?.cast<String>(); |
| 85 | + return targets?.contains(target) ?? false; |
| 86 | + } |
| 87 | + |
| 88 | + /// Returns the newest available simulator (highest OS version, with ties |
| 89 | + /// broken in favor of newest device), if any. |
| 90 | + Future<String?> findBestAvailableIphoneSimulator() async { |
| 91 | + final List<String> findSimulatorsArguments = <String>[ |
| 92 | + 'simctl', |
| 93 | + 'list', |
| 94 | + 'devices', |
| 95 | + 'runtimes', |
| 96 | + 'available', |
| 97 | + '--json', |
| 98 | + ]; |
| 99 | + final String findSimulatorCompleteCommand = |
| 100 | + '$_xcRunCommand ${findSimulatorsArguments.join(' ')}'; |
| 101 | + if (log) { |
| 102 | + print('Looking for available simulators...'); |
| 103 | + print(findSimulatorCompleteCommand); |
| 104 | + } |
| 105 | + final io.ProcessResult findSimulatorsResult = |
| 106 | + await processRunner.run(_xcRunCommand, findSimulatorsArguments); |
| 107 | + if (findSimulatorsResult.exitCode != 0) { |
| 108 | + if (log) { |
| 109 | + printError( |
| 110 | + 'Error occurred while running "$findSimulatorCompleteCommand":\n' |
| 111 | + '${findSimulatorsResult.stderr}'); |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | + final Map<String, dynamic> simulatorListJson = |
| 116 | + jsonDecode(findSimulatorsResult.stdout as String) |
| 117 | + as Map<String, dynamic>; |
| 118 | + final List<Map<String, dynamic>> runtimes = |
| 119 | + (simulatorListJson['runtimes'] as List<dynamic>) |
| 120 | + .cast<Map<String, dynamic>>(); |
| 121 | + final Map<String, Object> devices = |
| 122 | + (simulatorListJson['devices'] as Map<String, dynamic>) |
| 123 | + .cast<String, Object>(); |
| 124 | + if (runtimes.isEmpty || devices.isEmpty) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + String? id; |
| 128 | + // Looking for runtimes, trying to find one with highest OS version. |
| 129 | + for (final Map<String, dynamic> rawRuntimeMap in runtimes.reversed) { |
| 130 | + final Map<String, Object> runtimeMap = |
| 131 | + rawRuntimeMap.cast<String, Object>(); |
| 132 | + if ((runtimeMap['name'] as String?)?.contains('iOS') != true) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + final String? runtimeID = runtimeMap['identifier'] as String?; |
| 136 | + if (runtimeID == null) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + final List<Map<String, dynamic>>? devicesForRuntime = |
| 140 | + (devices[runtimeID] as List<dynamic>?)?.cast<Map<String, dynamic>>(); |
| 141 | + if (devicesForRuntime == null || devicesForRuntime.isEmpty) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + // Looking for runtimes, trying to find latest version of device. |
| 145 | + for (final Map<String, dynamic> rawDevice in devicesForRuntime.reversed) { |
| 146 | + final Map<String, Object> device = rawDevice.cast<String, Object>(); |
| 147 | + id = device['udid'] as String?; |
| 148 | + if (id == null) { |
| 149 | + continue; |
| 150 | + } |
| 151 | + if (log) { |
| 152 | + print('device selected: $device'); |
| 153 | + } |
| 154 | + return id; |
| 155 | + } |
| 156 | + } |
| 157 | + return null; |
| 158 | + } |
| 159 | +} |
0 commit comments