From 602a35617a0a265010543b14f060973f8dfdc0ce Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 17:11:49 -0800 Subject: [PATCH] Update `flutter start` for iOS simulator --- packages/flutter_tools/lib/src/device.dart | 90 +++++++++++++++------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index ffcbd5ae48..32ebf3c566 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -187,20 +187,12 @@ class IOSDevice extends Device { return false; } - Future buildPrecompiledApplication(ApplicationPackage app) async { - int result = await runCommandAndStreamOutput([ - '/usr/bin/env', 'xcrun', 'xcodebuild', '-target', 'Runner', '-configuration', 'Release' - ], workingDirectory: app.localPath); - - return result == 0; - } - @override Future startApp(ApplicationPackage app) async { logging.fine("Attempting to build and install ${app.name} on $id"); // Step 1: Install the precompiled application if necessary - bool buildResult = await buildPrecompiledApplication(app); + bool buildResult = await _buildIOSXcodeProject(app, true); if (!buildResult) { logging.severe('Could not build the precompiled application for the device'); @@ -257,11 +249,6 @@ class IOSDevice extends Device { ]); return true; } else { - // TODO(iansf): It may be possible to make this work on Linux. Since this - // functionality appears to be the only that prevents us from - // supporting iOS on Linux, it may be worth putting some time - // into investigating this. - // See https://bbs.archlinux.org/viewtopic.php?id=192655 return false; } return false; @@ -364,8 +351,6 @@ class IOSSimulator extends Device { List devices = []; String id = _getRunningSimulatorID(mockIOS); if (id != null) { - // TODO(iansf): get the simulator's name - // String name = _getDeviceName(id, mockIOS); devices.add(new IOSSimulator(id: id)); } return devices; @@ -448,20 +433,53 @@ class IOSSimulator extends Device { @override Future startApp(ApplicationPackage app) async { - if (!isAppInstalled(app)) { + logging.fine('Building ${app.name} for ${id}'); + + // Step 1: Build the Xcode project + bool buildResult = await _buildIOSXcodeProject(app, false); + if (!buildResult) { + logging.severe('Could not build the application for the simulator'); return false; } - try { - if (id == defaultDeviceID) { - runCheckedSync( - [xcrunPath, 'simctl', 'launch', 'booted', app.id]); - } else { - runCheckedSync([xcrunPath, 'simctl', 'launch', id, app.id]); - } - return true; - } catch (e) { + + // Step 2: Assert that the Xcode project was successfully built + Directory bundle = new Directory(path.join(app.localPath, 'build', 'Release-iphonesimulator', 'Runner.app')); + bool bundleExists = await bundle.exists(); + if (!bundleExists) { + logging.severe('Could not find the built application bundle at ${bundle.path}'); return false; } + + // Step 3: Install the updated bundle to the simulator + int installResult = await runCommandAndStreamOutput([ + xcrunPath, + 'simctl', + 'install', + id == defaultDeviceID ? 'booted' : id, + path.absolute(bundle.path) + ]); + + if (installResult != 0) { + logging.severe('Could not install the application bundle on the simulator'); + return false; + } + + // Step 4: Launch the updated application in the simulator + int launchResult = await runCommandAndStreamOutput([ + xcrunPath, + 'simctl', + 'launch', + id == defaultDeviceID ? 'booted' : id, + app.id + ]); + + if (launchResult != 0) { + logging.severe('Could not launch the freshly installed application on the simulator'); + return false; + } + + logging.fine('Successfully started ${app.name} on $id'); + return true; } @override @@ -1038,7 +1056,11 @@ class DeviceStore { break; case TargetPlatform.iOSSimulator: assert(iOSSimulator == null); - iOSSimulator = new IOSSimulator(); + iOSSimulator = _deviceForConfig(config, IOSSimulator.getAttachedDevices()); + if (iOSSimulator == null) { + // Creates a simulator with the default identifier + iOSSimulator = new IOSSimulator(); + } break; case TargetPlatform.mac: case TargetPlatform.linux: @@ -1049,3 +1071,17 @@ class DeviceStore { return new DeviceStore(android: android, iOS: iOS, iOSSimulator: iOSSimulator); } } + +Future _buildIOSXcodeProject(ApplicationPackage app, bool isDevice) async { + List command = [ + '/usr/bin/env', 'xcrun', 'xcodebuild', '-target', 'Runner', '-configuration', 'Release' + ]; + + if (!isDevice) { + command.addAll(['-sdk', 'iphonesimulator']); + } + + int result = await runCommandAndStreamOutput(command, + workingDirectory: app.localPath); + return result == 0; +}