Commit ef15eac8 authored by Jonah Williams's avatar Jonah Williams Committed by Flutter GitHub Bot

[flutter_tools] Remove context from Xcode and most of Xcodeproj (#48661)

parent 7cf2ff1f
......@@ -145,8 +145,20 @@ Future<T> runInContext<T>(
VisualStudioValidator: () => const VisualStudioValidator(),
WebWorkflow: () => const WebWorkflow(),
WindowsWorkflow: () => const WindowsWorkflow(),
Xcode: () => Xcode(),
XcodeProjectInterpreter: () => XcodeProjectInterpreter(),
Xcode: () => Xcode(
logger: globals.logger,
processManager: globals.processManager,
platform: globals.platform,
fileSystem: globals.fs,
xcodeProjectInterpreter: xcodeProjectInterpreter,
),
XcodeProjectInterpreter: () => XcodeProjectInterpreter(
logger: globals.logger,
processManager: globals.processManager,
platform: globals.platform,
fileSystem: globals.fs,
terminal: globals.terminal,
),
XcodeValidator: () => const XcodeValidator(),
},
);
......
......@@ -458,7 +458,7 @@ Future<XcodeBuildResult> buildXcodeProject({
// e.g. `flutter build bundle`.
buildCommands.add('FLUTTER_SUPPRESS_ANALYTICS=true');
buildCommands.add('COMPILER_INDEX_STORE_ENABLE=NO');
buildCommands.addAll(environmentVariablesAsXcodeBuildSettings());
buildCommands.addAll(environmentVariablesAsXcodeBuildSettings(globals.platform));
final Stopwatch sw = Stopwatch()..start();
initialBuildStatus = globals.logger.startProgress('Running Xcode build...', timeout: timeoutConfiguration.fastOperation);
......
......@@ -5,6 +5,8 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
import 'package:process/process.dart';
import '../artifacts.dart';
import '../base/common.dart';
......@@ -14,6 +16,7 @@ import '../base/io.dart';
import '../base/logger.dart';
import '../base/os.dart';
import '../base/process.dart';
import '../base/terminal.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../cache.dart';
......@@ -221,15 +224,33 @@ XcodeProjectInterpreter get xcodeProjectInterpreter => context.get<XcodeProjectI
/// Interpreter of Xcode projects.
class XcodeProjectInterpreter {
XcodeProjectInterpreter({
@required Platform platform,
@required ProcessManager processManager,
@required Logger logger,
@required FileSystem fileSystem,
@required AnsiTerminal terminal,
}) : _platform = platform,
_fileSystem = fileSystem,
_terminal = terminal,
_logger = logger,
_processUtils = ProcessUtils(logger: logger, processManager: processManager);
final Platform _platform;
final FileSystem _fileSystem;
final ProcessUtils _processUtils;
final AnsiTerminal _terminal;
final Logger _logger;
static const String _executable = '/usr/bin/xcodebuild';
static final RegExp _versionRegex = RegExp(r'Xcode ([0-9.]+)');
void _updateVersion() {
if (!globals.platform.isMacOS || !globals.fs.file(_executable).existsSync()) {
if (!_platform.isMacOS || !_fileSystem.file(_executable).existsSync()) {
return;
}
try {
final RunResult result = processUtils.runSync(
final RunResult result = _processUtils.runSync(
<String>[_executable, '-version'],
);
if (result.exitCode != 0) {
......@@ -283,26 +304,26 @@ class XcodeProjectInterpreter {
Duration timeout = const Duration(minutes: 1),
}) async {
final Status status = Status.withSpinner(
timeout: timeoutConfiguration.fastOperation,
timeoutConfiguration: timeoutConfiguration,
platform: globals.platform,
timeout: const TimeoutConfiguration().fastOperation,
timeoutConfiguration: const TimeoutConfiguration(),
platform: _platform,
stopwatch: Stopwatch(),
supportsColor: globals.terminal.supportsColor,
supportsColor: _terminal.supportsColor,
);
final List<String> showBuildSettingsCommand = <String>[
_executable,
'-project',
globals.fs.path.absolute(projectPath),
_fileSystem.path.absolute(projectPath),
'-target',
target,
'-showBuildSettings',
...environmentVariablesAsXcodeBuildSettings()
...environmentVariablesAsXcodeBuildSettings(_platform)
];
try {
// showBuildSettings is reported to occasionally 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.
final RunResult result = await processUtils.run(
final RunResult result = await _processUtils.run(
showBuildSettingsCommand,
throwOnError: true,
workingDirectory: projectPath,
......@@ -317,7 +338,7 @@ class XcodeProjectInterpreter {
command: showBuildSettingsCommand.join(' '),
).send();
}
globals.printTrace('Unexpected failure to get the build settings: $error.');
_logger.printTrace('Unexpected failure to get the build settings: $error.');
return const <String, String>{};
} finally {
status.stop();
......@@ -325,7 +346,7 @@ class XcodeProjectInterpreter {
}
void cleanWorkspace(String workspacePath, String scheme) {
processUtils.runSync(<String>[
_processUtils.runSync(<String>[
_executable,
'-workspace',
workspacePath,
......@@ -333,8 +354,8 @@ class XcodeProjectInterpreter {
scheme,
'-quiet',
'clean',
...environmentVariablesAsXcodeBuildSettings()
], workingDirectory: globals.fs.currentDirectory.path);
...environmentVariablesAsXcodeBuildSettings(_platform)
], workingDirectory: _fileSystem.currentDirectory.path);
}
Future<XcodeProjectInfo> getInfo(String projectPath, {String projectFilename}) async {
......@@ -342,7 +363,7 @@ class XcodeProjectInterpreter {
// * -project is passed and the given project isn't there, or
// * no -project is passed and there isn't a project.
const int missingProjectExitCode = 66;
final RunResult result = await processUtils.run(
final RunResult result = await _processUtils.run(
<String>[
_executable,
'-list',
......@@ -363,9 +384,9 @@ class XcodeProjectInterpreter {
/// This allows developers to pass arbitrary build settings in without the tool needing to make a flag
/// for or be aware of each one. This could be used to set code signing build settings in a CI
/// environment without requiring settings changes in the Xcode project.
List<String> environmentVariablesAsXcodeBuildSettings() {
List<String> environmentVariablesAsXcodeBuildSettings(Platform platform) {
const String xcodeBuildSettingPrefix = 'FLUTTER_XCODE_';
return globals.platform.environment.entries.where((MapEntry<String, String> mapEntry) {
return platform.environment.entries.where((MapEntry<String, String> mapEntry) {
return mapEntry.key.startsWith(xcodeBuildSettingPrefix);
}).expand<String>((MapEntry<String, String> mapEntry) {
// Remove FLUTTER_XCODE_ prefix from the environment variable to get the build setting.
......
......@@ -85,7 +85,7 @@ Future<void> buildMacOS({
'OBJROOT=${globals.fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Intermediates.noindex')}',
'SYMROOT=${globals.fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Products')}',
'COMPILER_INDEX_STORE_ENABLE=NO',
...environmentVariablesAsXcodeBuildSettings()
...environmentVariablesAsXcodeBuildSettings(globals.platform)
], trace: true);
} finally {
status.cancel();
......
......@@ -4,11 +4,16 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
import 'package:process/process.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../globals.dart' as globals;
import '../ios/xcodeproj.dart';
const int kXcodeRequiredVersionMajor = 10;
......@@ -41,14 +46,31 @@ String getNameForSdk(SdkType sdk) {
return null;
}
/// A utility class for interacting with Xcode command line tools.
class Xcode {
bool get isInstalledAndMeetsVersionCheck => globals.platform.isMacOS && isInstalled && isVersionSatisfactory;
Xcode({
@required Platform platform,
@required ProcessManager processManager,
@required Logger logger,
@required FileSystem fileSystem,
@required XcodeProjectInterpreter xcodeProjectInterpreter,
}) : _platform = platform,
_fileSystem = fileSystem,
_xcodeProjectInterpreter = xcodeProjectInterpreter,
_processUtils = ProcessUtils(logger: logger, processManager: processManager);
final Platform _platform;
final ProcessUtils _processUtils;
final FileSystem _fileSystem;
final XcodeProjectInterpreter _xcodeProjectInterpreter;
bool get isInstalledAndMeetsVersionCheck => _platform.isMacOS && isInstalled && isVersionSatisfactory;
String _xcodeSelectPath;
String get xcodeSelectPath {
if (_xcodeSelectPath == null) {
try {
_xcodeSelectPath = processUtils.runSync(
_xcodeSelectPath = _processUtils.runSync(
<String>['/usr/bin/xcode-select', '--print-path'],
).stdout.trim();
} on ProcessException {
......@@ -64,21 +86,21 @@ class Xcode {
if (xcodeSelectPath == null || xcodeSelectPath.isEmpty) {
return false;
}
return xcodeProjectInterpreter.isInstalled;
return _xcodeProjectInterpreter.isInstalled;
}
int get majorVersion => xcodeProjectInterpreter.majorVersion;
int get majorVersion => _xcodeProjectInterpreter.majorVersion;
int get minorVersion => xcodeProjectInterpreter.minorVersion;
int get minorVersion => _xcodeProjectInterpreter.minorVersion;
String get versionText => xcodeProjectInterpreter.versionText;
String get versionText => _xcodeProjectInterpreter.versionText;
bool _eulaSigned;
/// Has the EULA been signed?
bool get eulaSigned {
if (_eulaSigned == null) {
try {
final RunResult result = processUtils.runSync(
final RunResult result = _processUtils.runSync(
<String>['/usr/bin/xcrun', 'clang'],
);
if (result.stdout != null && result.stdout.contains('license')) {
......@@ -103,7 +125,7 @@ class Xcode {
try {
// This command will error if additional components need to be installed in
// xcode 9.2 and above.
final RunResult result = processUtils.runSync(
final RunResult result = _processUtils.runSync(
<String>['/usr/bin/xcrun', 'simctl', 'list'],
);
_isSimctlInstalled = result.stderr == null || result.stderr == '';
......@@ -115,7 +137,7 @@ class Xcode {
}
bool get isVersionSatisfactory {
if (!xcodeProjectInterpreter.isInstalled) {
if (!_xcodeProjectInterpreter.isInstalled) {
return false;
}
if (majorVersion > kXcodeRequiredVersionMajor) {
......@@ -128,14 +150,14 @@ class Xcode {
}
Future<RunResult> cc(List<String> args) {
return processUtils.run(
return _processUtils.run(
<String>['xcrun', 'cc', ...args],
throwOnError: true,
);
}
Future<RunResult> clang(List<String> args) {
return processUtils.run(
return _processUtils.run(
<String>['xcrun', 'clang', ...args],
throwOnError: true,
);
......@@ -143,7 +165,7 @@ class Xcode {
Future<String> sdkLocation(SdkType sdk) async {
assert(sdk != null);
final RunResult runResult = await processUtils.run(
final RunResult runResult = await _processUtils.run(
<String>['xcrun', '--sdk', getNameForSdk(sdk), '--show-sdk-path'],
throwOnError: true,
);
......@@ -158,10 +180,10 @@ class Xcode {
return null;
}
final List<String> searchPaths = <String>[
globals.fs.path.join(xcodeSelectPath, 'Applications', 'Simulator.app'),
_fileSystem.path.join(xcodeSelectPath, 'Applications', 'Simulator.app'),
];
return searchPaths.where((String p) => p != null).firstWhere(
(String p) => globals.fs.directory(p).existsSync(),
(String p) => _fileSystem.directory(p).existsSync(),
orElse: () => null,
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment