From 51d4ab33243c7329d816dce2466a906739bcb902 Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Fri, 27 May 2016 11:05:10 -0700 Subject: [PATCH] Fix install for iOS simulator, and add ability to uninstall (#4223) --- .../lib/src/android/android_device.dart | 16 +++++++ .../flutter_tools/lib/src/commands/drive.dart | 2 + .../lib/src/commands/install.dart | 7 ++- packages/flutter_tools/lib/src/device.dart | 9 ++-- .../flutter_tools/lib/src/ios/devices.dart | 48 ++++++++++++------- .../flutter_tools/lib/src/ios/simulators.dart | 48 ++++++++++++------- 6 files changed, 90 insertions(+), 40 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 1ae3359d65..b7051e3bf4 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -232,6 +232,22 @@ class AndroidDevice extends Device { return true; } + @override + bool uninstallApp(ApplicationPackage app) { + if (!_checkForSupportedAdbVersion() || !_checkForSupportedAndroidVersion()) + return false; + + String uninstallOut = runCheckedSync(adbCommandForDevice(['uninstall', app.id])); + RegExp failureExp = new RegExp(r'^Failure.*$', multiLine: true); + String failure = failureExp.stringMatch(uninstallOut); + if (failure != null) { + printError('Package uninstall error: $failure'); + return false; + } + + return true; + } + Future _forwardPort(String service, int devicePort, int port) async { try { // Set up port forwarding for observatory. diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index b9b2747739..e5c754e868 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -274,6 +274,8 @@ Future startApp(DriveCommand command) async { printTrace('Installing application package.'); ApplicationPackage package = command.applicationPackages .getPackageForPlatform(command.device.platform); + if (command.device.isAppInstalled(package)) + command.device.uninstallApp(package); command.device.installApp(package); Map platformArgs = {}; diff --git a/packages/flutter_tools/lib/src/commands/install.dart b/packages/flutter_tools/lib/src/commands/install.dart index c3ae09c1b8..ab2ebb16b1 100644 --- a/packages/flutter_tools/lib/src/commands/install.dart +++ b/packages/flutter_tools/lib/src/commands/install.dart @@ -34,8 +34,11 @@ bool installApp(Device device, ApplicationPackage package) { if (package == null) return false; - if (device.isAppInstalled(package)) - return true; + if (device.isAppInstalled(package)) { + printStatus('Uninstalling old version...'); + if (!device.uninstallApp(package)) + printError('Warning: uninstalling old version failed'); + } return device.installApp(package); } diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index f67c0d372b..d0451f05c1 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -149,9 +149,15 @@ abstract class Device { /// Whether it is an emulated device running on localhost. bool get isLocalEmulator; + /// Check if a version of the given app is already installed + bool isAppInstalled(ApplicationPackage app); + /// Install an app package on the current device bool installApp(ApplicationPackage app); + /// Uninstall an app package from the current device + bool uninstallApp(ApplicationPackage app); + /// Check if the device is supported by Flutter bool isSupported(); @@ -159,9 +165,6 @@ abstract class Device { // supported by Flutter, and, if not, why. String supportMessage() => isSupported() ? "Supported" : "Unsupported"; - /// Check if the current version of the given app is already installed - bool isAppInstalled(ApplicationPackage app); - TargetPlatform get platform; /// Get the log reader for this device. diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 321ab3ac9a..c86cf80b0f 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -123,21 +123,6 @@ class IOSDevice extends Device { }); } - @override - bool installApp(ApplicationPackage app) { - try { - IOSApp iosApp = app; - runCheckedSync([installerPath, '-i', iosApp.deviceBundlePath]); - return true; - } catch (e) { - return false; - } - return false; - } - - @override - bool isSupported() => true; - @override bool isAppInstalled(ApplicationPackage app) { try { @@ -151,6 +136,36 @@ class IOSDevice extends Device { return false; } + @override + bool installApp(ApplicationPackage app) { + IOSApp iosApp = app; + Directory bundle = new Directory(iosApp.deviceBundlePath); + if (!bundle.existsSync()) { + printError("Could not find application bundle at ${bundle.path}; have you run 'flutter build ios'?"); + return false; + } + + try { + runCheckedSync([installerPath, '-i', iosApp.deviceBundlePath]); + return true; + } catch (e) { + return false; + } + } + + @override + bool uninstallApp(ApplicationPackage app) { + try { + runCheckedSync([installerPath, '-U', app.id]); + return true; + } catch (e) { + return false; + } + } + + @override + bool isSupported() => true; + @override Future startApp( ApplicationPackage app, @@ -174,8 +189,7 @@ class IOSDevice extends Device { // Step 2: Check that the application exists at the specified path. IOSApp iosApp = app; Directory bundle = new Directory(iosApp.deviceBundlePath); - bool bundleExists = bundle.existsSync(); - if (!bundleExists) { + if (!bundle.existsSync()) { printError('Could not find the built application bundle at ${bundle.path}.'); return new LaunchResult.failed(); } diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index f566a14af2..58ae15bb3e 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -274,10 +274,24 @@ class SimControl { bool _isAnyConnected() => getConnectedDevices().isNotEmpty; + bool isInstalled(String appId) { + return exitsHappy([ + _xcrunPath, + 'simctl', + 'get_app_container', + 'booted', + appId, + ]); + } + void install(String deviceId, String appPath) { runCheckedSync([_xcrunPath, 'simctl', 'install', deviceId, appPath]); } + void uninstall(String deviceId, String appId) { + runCheckedSync([_xcrunPath, 'simctl', 'uninstall', deviceId, appId]); + } + void launch(String deviceId, String appIdentifier, [List launchArgs]) { List args = [_xcrunPath, 'simctl', 'launch', deviceId, appIdentifier]; if (launchArgs != null) @@ -361,6 +375,11 @@ class IOSSimulator extends Device { return path.join(simulatorPath, 'data'); } + @override + bool isAppInstalled(ApplicationPackage app) { + return SimControl.instance.isInstalled(app.id); + } + @override bool installApp(ApplicationPackage app) { try { @@ -372,6 +391,16 @@ class IOSSimulator extends Device { } } + @override + bool uninstallApp(ApplicationPackage app) { + try { + SimControl.instance.uninstall(id, app.id); + return true; + } catch (e) { + return false; + } + } + @override bool isSupported() { if (!Platform.isMacOS) { @@ -423,17 +452,6 @@ class IOSSimulator extends Device { return _supportMessage != null ? _supportMessage : "Unknown"; } - @override - bool isAppInstalled(ApplicationPackage app) { - try { - // TODO(tvolkert): This logic is wrong; simulatorHomeDirectory always exists - String simulatorHomeDirectory = _getSimulatorAppHomeDirectory(app); - return FileSystemEntity.isDirectorySync(simulatorHomeDirectory); - } catch (e) { - return false; - } - } - @override Future startApp( ApplicationPackage app, @@ -505,13 +523,7 @@ class IOSSimulator extends Device { } bool _applicationIsInstalledAndRunning(ApplicationPackage app) { - bool isInstalled = exitsHappy([ - 'xcrun', - 'simctl', - 'get_app_container', - 'booted', - app.id, - ]); + bool isInstalled = isAppInstalled(app); bool isRunning = exitsHappy([ '/usr/bin/killall',