From 9fd1711b437cacfec0c6d0620dd163916a22c239 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 11 Jan 2016 12:03:47 -0800 Subject: [PATCH 1/8] Add configurations for prebuilt iOS and iOS simulator targets --- .../flutter_tools/lib/src/application_package.dart | 2 -- .../lib/src/runner/flutter_command_runner.dart | 12 ++++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/application_package.dart b/packages/flutter_tools/lib/src/application_package.dart index ab62a4ef97..8a8104bfad 100644 --- a/packages/flutter_tools/lib/src/application_package.dart +++ b/packages/flutter_tools/lib/src/application_package.dart @@ -98,13 +98,11 @@ class ApplicationPackageStore { case TargetPlatform.iOS: assert(iOS == null); - assert(config.type != BuildType.prebuilt); iOS = new IOSApp(localPath: path.join(config.buildDir, IOSApp._defaultName)); break; case TargetPlatform.iOSSimulator: assert(iOSSimulator == null); - assert(config.type != BuildType.prebuilt); iOSSimulator = new IOSApp(localPath: path.join(config.buildDir, IOSApp._defaultName)); break; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart index f36e51f0cd..3489eb85c4 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart @@ -226,6 +226,18 @@ class FlutterCommandRunner extends CommandRunner { testable: true )); } + + if (hostPlatform == HostPlatform.mac) { + configs.add(new BuildConfiguration.prebuilt( + hostPlatform: HostPlatform.mac, + targetPlatform: TargetPlatform.iOS + )); + + configs.add(new BuildConfiguration.prebuilt( + hostPlatform: HostPlatform.mac, + targetPlatform: TargetPlatform.iOSSimulator + )); + } } else { if (!FileSystemEntity.isDirectorySync(enginePath)) logging.warning('$enginePath is not a valid directory'); From b7e388c5dd9777937bfa79e11971172cc956213a Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 15:08:31 -0800 Subject: [PATCH 2/8] Allow preconfigured packages for iOS simulator and devices --- .../flutter_tools/lib/src/application_package.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/flutter_tools/lib/src/application_package.dart b/packages/flutter_tools/lib/src/application_package.dart index 8a8104bfad..9857a24420 100644 --- a/packages/flutter_tools/lib/src/application_package.dart +++ b/packages/flutter_tools/lib/src/application_package.dart @@ -47,11 +47,11 @@ class AndroidApk extends ApplicationPackage { } class IOSApp extends ApplicationPackage { - static const String _defaultName = 'SkyShell.app'; - static const String _defaultId = 'com.google.SkyShell'; + static const String _defaultId = 'io.flutter.runner.Runner'; + static const String _defaultPath = 'ios'; IOSApp({ - String localPath, + String localPath: _defaultPath, String id: _defaultId }) : super(localPath: localPath, id: id); } @@ -98,12 +98,12 @@ class ApplicationPackageStore { case TargetPlatform.iOS: assert(iOS == null); - iOS = new IOSApp(localPath: path.join(config.buildDir, IOSApp._defaultName)); + iOS = new IOSApp(); break; case TargetPlatform.iOSSimulator: assert(iOSSimulator == null); - iOSSimulator = new IOSApp(localPath: path.join(config.buildDir, IOSApp._defaultName)); + iOSSimulator = new IOSApp(); break; case TargetPlatform.mac: From fbef955cb929b044932da1a5332d8c984d49168b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 15:09:56 -0800 Subject: [PATCH 3/8] Update IOSDevice::startApp for precompilation --- packages/flutter_tools/lib/src/device.dart | 61 ++++++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index a58fb10a16..57687f5425 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -95,8 +95,7 @@ class IOSDevice extends Device { 'To copy files to iOS devices, please install ios-deploy. ' 'You can do this using homebrew as follows:\n' '\$ brew tap flutter/flutter\n' - '\$ brew install ios-deploy', - 'Copying files to iOS devices is not currently supported on Linux.'); + '\$ brew install ios-deploy'); } static List getAttachedDevices([IOSDevice mockIOS]) { @@ -178,7 +177,7 @@ class IOSDevice extends Device { @override bool isAppInstalled(ApplicationPackage app) { try { - String apps = runCheckedSync([installerPath, '-l']); + String apps = runCheckedSync([installerPath, '--list-apps']); if (new RegExp(app.id, multiLine: true).hasMatch(apps)) { return true; } @@ -188,24 +187,52 @@ 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 { - if (!isAppInstalled(app)) { + logging.fine("Attempting to build and install ${app.name} on $id"); + + // Step 1: Install the precompiled application if necessary + bool buildResult = await buildPrecompiledApplication(app); + + if (!buildResult) { + logging.severe('Could not build the precompiled application for the device'); return false; } - // idevicedebug hangs forever after launching the app, so kill it after - // giving it plenty of time to send the launch command. - return await runAndKill( - [debuggerPath, 'run', app.id], - new Duration(seconds: 3) - ).then( - (_) { - return true; - }, onError: (e) { - logging.info('Failure running $debuggerPath: ', e); - return false; - } - ); + + // Step 2: Check that the application exists at the specified path + Directory bundle = new Directory(path.join(app.localPath, 'build', 'Release-iphoneos', '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: Attempt to install the application on the device + int installationResult = await runCommandAndStreamOutput([ + '/usr/bin/env', + 'ios-deploy', + '--id', + id, + '--bundle', + bundle.path, + ]); + + if (installationResult != 0) { + logging.severe('Could not install ${bundle.path} on $id'); + return false; + } + + logging.fine('Installation successful'); + return true; } @override From 53ea61942bf6b37dcecdbb7fa7f3e71d266c0c78 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 15:10:41 -0800 Subject: [PATCH 4/8] Invoke Device::startApp on the specific device --- .../flutter_tools/lib/src/commands/start.dart | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/start.dart b/packages/flutter_tools/lib/src/commands/start.dart index 132bdf6b68..61acc6674c 100644 --- a/packages/flutter_tools/lib/src/commands/start.dart +++ b/packages/flutter_tools/lib/src/commands/start.dart @@ -9,6 +9,7 @@ import 'package:path/path.dart' as path; import '../application_package.dart'; import '../base/logging.dart'; +import '../build_configuration.dart'; import '../device.dart'; import '../runner/flutter_command.dart'; import 'build.dart'; @@ -84,22 +85,31 @@ abstract class StartCommandBase extends FlutterCommand { continue; logging.fine('Running build command for $device.'); - BuildCommand builder = new BuildCommand(); - builder.inheritFromParent(this); - await builder.buildInTempDir( - mainPath: mainPath, - onBundleAvailable: (String localBundlePath) { - logging.fine('Starting bundle for $device.'); - final AndroidDevice androidDevice = device; // https://github.com/flutter/flutter/issues/1035 - if (androidDevice.startBundle(package, localBundlePath, - poke: poke, - checked: argResults['checked'], - traceStartup: argResults['trace-startup'], - route: argResults['route'], - clearLogs: clearLogs)) - startedSomething = true; + + if (device.platform == TargetPlatform.android) { + BuildCommand builder = new BuildCommand(); + builder.inheritFromParent(this); + await builder.buildInTempDir( + mainPath: mainPath, + onBundleAvailable: (String localBundlePath) { + logging.fine('Starting bundle for $device.'); + final AndroidDevice androidDevice = device; // https://github.com/flutter/flutter/issues/1035 + if (androidDevice.startBundle(package, localBundlePath, + poke: poke, + checked: argResults['checked'], + traceStartup: argResults['trace-startup'], + route: argResults['route'], + clearLogs: clearLogs)) + startedSomething = true; + } + ); + } else { + bool result = await device.startApp(package); + if (!result) { + logging.severe('Could not start \'${package.name}\' on \'${device.id}\''); + continue; } - ); + } } if (!startedSomething) { From e98b7f4c48c21c22dfeb631b9692e8b1232eb5e9 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 15:26:20 -0800 Subject: [PATCH 5/8] Make picking of either an iOS or Android device from a configuration follow a common code path --- packages/flutter_tools/lib/src/device.dart | 40 ++++++++++++++-------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 57687f5425..ffcbd5ae48 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -997,6 +997,30 @@ class DeviceStore { this.iOSSimulator }); + static Device _deviceForConfig(BuildConfiguration config, List devices) { + Device device = null; + + if (config.deviceId != null) { + // Step 1: If a device identifier is specified, try to find a device + // matching that specific identifier + device = devices.firstWhere( + (Device dev) => (dev.id == config.deviceId), + orElse: () => null); + if (device == null) { + logging.severe('Warning: Device ID ${config.deviceId} not found'); + } + } else if (devices.length == 1) { + // Step 2: If no identifier is specified and there is only one connected + // device, pick that one. + device = devices[0]; + } else if (devices.length > 1) { + // Step 3: D: + logging.severe('Warning: Multiple devices are connected, but no device ID was specified.'); + } + + return device; + } + factory DeviceStore.forConfigs(List configs) { AndroidDevice android; IOSDevice iOS; @@ -1006,23 +1030,11 @@ class DeviceStore { switch (config.targetPlatform) { case TargetPlatform.android: assert(android == null); - List androidDevices = AndroidDevice.getAttachedDevices(); - if (config.deviceId != null) { - android = androidDevices.firstWhere( - (AndroidDevice dev) => (dev.id == config.deviceId), - orElse: () => null); - if (android == null) { - print('Warning: Device ID ${config.deviceId} not found'); - } - } else if (androidDevices.length == 1) { - android = androidDevices[0]; - } else if (androidDevices.length > 1) { - print('Warning: Multiple Android devices are connected, but no device ID was specified.'); - } + android = _deviceForConfig(config, AndroidDevice.getAttachedDevices()); break; case TargetPlatform.iOS: assert(iOS == null); - iOS = new IOSDevice(); + iOS = _deviceForConfig(config, IOSDevice.getAttachedDevices()); break; case TargetPlatform.iOSSimulator: assert(iOSSimulator == null); From 02f423f5d36a94b9af0df8bf969f6466d242375b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 15:29:57 -0800 Subject: [PATCH 6/8] Minor: Remove unnecessary continue --- packages/flutter_tools/lib/src/commands/start.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/commands/start.dart b/packages/flutter_tools/lib/src/commands/start.dart index 61acc6674c..030a95e05b 100644 --- a/packages/flutter_tools/lib/src/commands/start.dart +++ b/packages/flutter_tools/lib/src/commands/start.dart @@ -107,7 +107,6 @@ abstract class StartCommandBase extends FlutterCommand { bool result = await device.startApp(package); if (!result) { logging.severe('Could not start \'${package.name}\' on \'${device.id}\''); - continue; } } } From 602a35617a0a265010543b14f060973f8dfdc0ce Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 17:11:49 -0800 Subject: [PATCH 7/8] 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; +} From c8672a40c1958c1471c52345e5707a6e3a4ee445 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 14 Jan 2016 17:19:12 -0800 Subject: [PATCH 8/8] Fix Dart analyzer warning --- packages/flutter_tools/lib/src/device.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 32ebf3c566..f113af1c59 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -433,7 +433,7 @@ class IOSSimulator extends Device { @override Future startApp(ApplicationPackage app) async { - logging.fine('Building ${app.name} for ${id}'); + logging.fine('Building ${app.name} for $id'); // Step 1: Build the Xcode project bool buildResult = await _buildIOSXcodeProject(app, false);