From ca8070f904e4ca1b936c35ac371b85624e71ba60 Mon Sep 17 00:00:00 2001 From: John McCutchan Date: Wed, 28 Sep 2016 08:46:16 -0700 Subject: [PATCH] Fix flutter run --use-application-binary (#6106) When using --use-application-binary: - [x] Stop flutter run from checking for a pubspec.yaml in current directory - [x] Stop flutter run from invoking pub get - [x] Set 'shouldBuild' based on --use-application-binary - [x] Stop requiring 'lib/main.dart' to be present before running. - [x] Stop building an FLX when launching on Android --- .../lib/src/android/android_device.dart | 67 +++++++++++++------ .../flutter_tools/lib/src/commands/run.dart | 29 +++++++- packages/flutter_tools/lib/src/device.dart | 6 +- .../flutter_tools/lib/src/ios/devices.dart | 3 +- .../flutter_tools/lib/src/ios/simulators.dart | 3 +- packages/flutter_tools/lib/src/run.dart | 32 ++++++--- .../lib/src/runner/flutter_command.dart | 4 +- 7 files changed, 104 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 e4c2a5c04c..0234e07a34 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -274,12 +274,15 @@ class AndroidDevice extends Device { }) async { printTrace('$this startBundle'); - if (!FileSystemEntity.isFileSync(bundlePath)) { - printError('Cannot find $bundlePath'); - return new LaunchResult.failed(); - } + if (bundlePath != null) { + if (!FileSystemEntity.isFileSync(bundlePath)) { + printError('Cannot find $bundlePath'); + return new LaunchResult.failed(); + } - runCheckedSync(adbCommandForDevice(['push', bundlePath, _deviceBundlePath])); + runCheckedSync( + adbCommandForDevice(['push', bundlePath, _deviceBundlePath])); + } ProtocolDiscovery observatoryDiscovery; ProtocolDiscovery diagnosticDiscovery; @@ -289,13 +292,26 @@ class AndroidDevice extends Device { diagnosticDiscovery = new ProtocolDiscovery(logReader, ProtocolDiscovery.kDiagnosticService); } - List cmd = adbCommandForDevice([ - 'shell', 'am', 'start', - '-a', 'android.intent.action.RUN', - '-d', _deviceBundlePath, - '-f', '0x20000000', // FLAG_ACTIVITY_SINGLE_TOP - '--ez', 'enable-background-compilation', 'true', - ]); + List cmd; + + if (bundlePath != null) { + // Specify in the RUN intent the path to the local bundle pushed. + cmd = adbCommandForDevice([ + 'shell', 'am', 'start', + '-a', 'android.intent.action.RUN', + '-d', _deviceBundlePath, + '-f', '0x20000000', // FLAG_ACTIVITY_SINGLE_TOP + '--ez', 'enable-background-compilation', 'true', + ]); + } else { + cmd = adbCommandForDevice([ + 'shell', 'am', 'start', + '-a', 'android.intent.action.RUN', + '-f', '0x20000000', // FLAG_ACTIVITY_SINGLE_TOP + '--ez', 'enable-background-compilation', 'true', + ]); + } + if (traceStartup) cmd.addAll(['--ez', 'trace-startup', 'true']); if (route != null) @@ -372,19 +388,23 @@ class AndroidDevice extends Device { String mainPath, String route, DebuggingOptions debuggingOptions, - Map platformArgs + Map platformArgs, + bool prebuiltApplication: false }) async { if (!_checkForSupportedAdbVersion() || !_checkForSupportedAndroidVersion()) return new LaunchResult.failed(); - String localBundlePath = await flx.buildFlx( - mainPath: mainPath, - precompiledSnapshot: isAotBuildMode(debuggingOptions.buildMode), - includeRobotoFonts: false - ); + String localBundlePath; - if (localBundlePath == null) - return new LaunchResult.failed(); + if (!prebuiltApplication) { + localBundlePath = await flx.buildFlx( + mainPath: mainPath, + precompiledSnapshot: isAotBuildMode(debuggingOptions.buildMode), + includeRobotoFonts: false + ); + if (localBundlePath == null) + return new LaunchResult.failed(); + } printTrace('Starting bundle for $this.'); @@ -430,10 +450,15 @@ class AndroidDevice extends Device { ApplicationPackage package, LaunchResult result, { String mainPath, - VMService observatory + VMService observatory, + bool prebuiltApplication: false }) async { Directory tempDir = await Directory.systemTemp.createTemp('flutter_tools'); + if (prebuiltApplication) { + return false; + } + try { String snapshotPath = path.join(tempDir.path, 'snapshot_blob.bin'); int result = await flx.createSnapshot(mainPath: mainPath, snapshotPath: snapshotPath); diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 0680d7f2b9..8d11f752fd 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -81,6 +81,15 @@ class RunCommand extends RunCommandBase { // results out to 'refresh_benchmark.json', and exit. This flag is intended // for use in generating automated flutter benchmarks. argParser.addFlag('benchmark', negatable: false, hide: !verboseHelp); + + commandValidator = () { + if (!runningWithPrebuiltApplication) { + return commonCommandValidator(); + } + // When running with a prebuilt application, no command validation is + // necessary. + return true; + }; } Device device; @@ -108,10 +117,24 @@ class RunCommand extends RunCommandBase { } } - bool shouldUseHotMode() { - return argResults['hot'] && (getBuildMode() == BuildMode.debug); + @override + bool get shouldRunPub { + // If we are running with a prebuilt application, do not run pub. + if (runningWithPrebuiltApplication) + return false; + + return super.shouldRunPub; } + bool shouldUseHotMode() { + bool hotArg = argResults['hot'] ?? false; + final bool shouldUseHotMode = hotArg && !runningWithPrebuiltApplication; + return (getBuildMode() == BuildMode.debug) && shouldUseHotMode; + } + + bool get runningWithPrebuiltApplication => + argResults['use-application-binary'] != null; + @override Future verifyThenRunCommand() async { if (!commandValidator()) @@ -191,7 +214,7 @@ class RunCommand extends RunCommandBase { ); } - return runner.run(route: route, shouldBuild: argResults['build']); + return runner.run(route: route, shouldBuild: !runningWithPrebuiltApplication && argResults['build']); } } diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index c9d454ee27..4ca25054ec 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -182,7 +182,8 @@ abstract class Device { String mainPath, String route, DebuggingOptions debuggingOptions, - Map platformArgs + Map platformArgs, + bool prebuiltApplication: false }); /// Does this device implement support for hot reloading / restarting? @@ -205,7 +206,8 @@ abstract class Device { ApplicationPackage package, LaunchResult result, { String mainPath, - VMService observatory + VMService observatory, + bool prebuiltApplication: false }) async { throw 'unsupported'; } diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 3e4c37897f..5bd1a0cb8e 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -181,7 +181,8 @@ class IOSDevice extends Device { String mainPath, String route, DebuggingOptions debuggingOptions, - Map platformArgs + Map platformArgs, + bool prebuiltApplication: false }) async { // TODO(chinmaygarde): Use checked, mainPath, route. // TODO(devoncarew): Handle startPaused, debugPort. diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 981cc3da98..49201a3733 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -412,7 +412,8 @@ class IOSSimulator extends Device { String mainPath, String route, DebuggingOptions debuggingOptions, - Map platformArgs + Map platformArgs, + bool prebuiltApplication: false }) async { printTrace('Building ${app.name} for $id.'); diff --git a/packages/flutter_tools/lib/src/run.dart b/packages/flutter_tools/lib/src/run.dart index f9ace41180..b4e5d30866 100644 --- a/packages/flutter_tools/lib/src/run.dart +++ b/packages/flutter_tools/lib/src/run.dart @@ -38,6 +38,8 @@ class RunAndStayResident extends ResidentRunner { final bool benchmark; final String applicationBinary; + bool get prebuiltMode => applicationBinary != null; + @override Future run({ Completer connectionInfoCompleter, @@ -46,6 +48,7 @@ class RunAndStayResident extends ResidentRunner { }) { // Don't let uncaught errors kill the process. return runZoned(() { + assert(shouldBuild == !prebuiltMode); return _run( traceStartup: traceStartup, benchmark: benchmark, @@ -78,7 +81,8 @@ class RunAndStayResident extends ResidentRunner { _package, _result, mainPath: _mainPath, - observatory: vmService + observatory: vmService, + prebuiltApplication: prebuiltMode ); status.stop(showElapsedTime: true); @@ -99,13 +103,15 @@ class RunAndStayResident extends ResidentRunner { String route, bool shouldBuild: true }) async { - _mainPath = findMainDartFile(target); - if (!FileSystemEntity.isFileSync(_mainPath)) { - String message = 'Tried to run $_mainPath, but that file does not exist.'; - if (target == null) - message += '\nConsider using the -t option to specify the Dart file to start.'; - printError(message); - return 1; + if (!prebuiltMode) { + _mainPath = findMainDartFile(target); + if (!FileSystemEntity.isFileSync(_mainPath)) { + String message = 'Tried to run $_mainPath, but that file does not exist.'; + if (target == null) + message += '\nConsider using the -t option to specify the Dart file to start.'; + printError(message); + return 1; + } } _package = getApplicationPackageForPlatform(device.platform, applicationBinary: applicationBinary); @@ -153,7 +159,12 @@ class RunAndStayResident extends ResidentRunner { platformArgs = { 'trace-startup': traceStartup }; await startEchoingDeviceLog(); - printStatus('Running ${getDisplayPath(_mainPath)} on ${device.name}...'); + if (_mainPath == null) { + assert(prebuiltMode); + printStatus('Running ${_package.displayName} on ${device.name}'); + } else { + printStatus('Running ${getDisplayPath(_mainPath)} on ${device.name}...'); + } _result = await device.startApp( _package, @@ -161,7 +172,8 @@ class RunAndStayResident extends ResidentRunner { mainPath: _mainPath, debuggingOptions: debuggingOptions, platformArgs: platformArgs, - route: route + route: route, + prebuiltApplication: prebuiltMode ); if (!_result.started) { diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 6099969416..80869ecfc6 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -22,7 +22,7 @@ typedef bool Validator(); abstract class FlutterCommand extends Command { FlutterCommand() { - commandValidator = _commandValidator; + commandValidator = commonCommandValidator; } @override @@ -206,7 +206,7 @@ abstract class FlutterCommand extends Command { // This is a field so that you can modify the value for testing. Validator commandValidator; - bool _commandValidator() { + bool commonCommandValidator() { if (!PackageMap.isUsingCustomPackagesPath) { // Don't expect a pubspec.yaml file if the user passed in an explicit .packages file path. if (!FileSystemEntity.isFileSync('pubspec.yaml')) {