From f1cdf6df39a986ea490211fdd2f3891a08f34566 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Mon, 21 Mar 2016 09:24:56 -0700 Subject: [PATCH] refactor; fix apk warning --- .../lib/src/commands/analyze.dart | 16 ++++----- .../lib/src/commands/build_apk.dart | 36 ++++++++++--------- .../lib/src/commands/build_flx.dart | 2 +- .../lib/src/commands/daemon.dart | 17 +++------ .../flutter_tools/lib/src/commands/logs.dart | 12 +++---- .../lib/src/commands/refresh.dart | 2 +- .../flutter_tools/lib/src/commands/run.dart | 18 +++++----- .../lib/src/commands/run_mojo.dart | 18 +++++----- .../flutter_tools/lib/src/commands/trace.dart | 16 ++++----- packages/flutter_tools/lib/src/flx.dart | 6 +++- .../flutter_tools/lib/src/ios/simulators.dart | 23 +++++------- .../lib/src/service_protocol.dart | 8 ++--- packages/flutter_tools/lib/src/toolchain.dart | 18 +++++----- packages/flutter_tools/test/src/mocks.dart | 4 +-- 14 files changed, 88 insertions(+), 108 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 1155170de0..7b378246d5 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -18,11 +18,6 @@ import '../dart/sdk.dart'; import '../globals.dart'; import '../runner/flutter_command.dart'; -// TODO(devoncarew): Possible improvements to flutter analyze --watch: -// - Auto-detect new issues introduced by changes and highlight then in the output. -// - Use ANSI codes to improve the display when the terminal supports it (screen -// clearing, cursor position manipulation, bold and faint codes, ...) - bool isDartFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('.dart'); bool isDartTestFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('_test.dart'); bool isDartBenchmarkFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('_bench.dart'); @@ -449,10 +444,13 @@ class AnalyzeCommand extends FlutterCommand { Future _analyzeWatch() async { List directories; - if (isFlutterRepo) { + if (argResults['flutter-repo']) { + String root = path.absolute(ArtifactStore.flutterRoot); + directories = []; - directories.addAll(_gatherProjectPaths(path.absolute('examples'))); - directories.addAll(_gatherProjectPaths(path.absolute('packages'))); + directories.addAll(_gatherProjectPaths(path.join(root, 'examples'))); + directories.addAll(_gatherProjectPaths(path.join(root, 'packages'))); + directories.addAll(_gatherProjectPaths(path.join(root, 'dev'))); printStatus('Analyzing Flutter repository (${directories.length} projects).'); for (String projectPath in directories) printTrace(' ${path.relative(projectPath)}'); @@ -511,8 +509,6 @@ class AnalyzeCommand extends FlutterCommand { int issueDiff = issueCount - lastErrorCount; lastErrorCount = issueCount; - // TODO(devoncarew): If there were no issues found, and no change in the - // issue count, do we want to print anything? if (firstAnalysis) errorsMessage = '$issueCount ${pluralize('issue', issueCount)} found'; else if (issueDiff > 0) diff --git a/packages/flutter_tools/lib/src/commands/build_apk.dart b/packages/flutter_tools/lib/src/commands/build_apk.dart index 2619888620..6b098e2f50 100644 --- a/packages/flutter_tools/lib/src/commands/build_apk.dart +++ b/packages/flutter_tools/lib/src/commands/build_apk.dart @@ -94,9 +94,8 @@ class _ApkBuilder { '-I', _androidJar.path, '-F', outputApk.path, ]; - if (resources != null) { + if (resources != null) packageArgs.addAll(['-S', resources.absolute.path]); - } packageArgs.add(artifacts.path); runCheckedSync(packageArgs); } @@ -146,7 +145,6 @@ class BuildApkCommand extends FlutterCommand { help: 'Android manifest XML file.'); argParser.addOption('resources', abbr: 'r', - defaultsTo: _kDefaultResourcesPath, help: 'Resources directory path.'); argParser.addOption('output-file', abbr: 'o', @@ -237,21 +235,15 @@ Future<_ApkComponents> _findApkComponents( components.jars = [new File(artifactPaths[1])]; components.libSkyShell = new File(artifactPaths[2]); components.debugKeystore = new File(artifactPaths[3]); - components.resources = new Directory(resources); + components.resources = resources == null ? null : new Directory(resources); await parseServiceConfigs(components.services, jars: components.jars); - if (!components.resources.existsSync()) { - // TODO(eseidel): This level should be higher when path is manually set. - printStatus('Cannot locate Resources: ${components.resources}, ignoring.'); - components.resources = null; - } - - for (File f in [ + for (File file in [ components.manifest, components.icuData, components.libSkyShell, components.debugKeystore ]..addAll(components.jars)) { - if (!f.existsSync()) { - printError('Cannot locate file: ${f.path}'); + if (!file.existsSync()) { + printError('Cannot locate file: ${file.path}'); return null; } } @@ -295,6 +287,7 @@ int _buildApk( ensureDirectoryExists(finalApk.path); builder.align(unalignedApk, finalApk); + printTrace('calculateSha: $outputFile'); File apkShaFile = new File('$outputFile.sha1'); apkShaFile.writeAsStringSync(calculateSha(finalApk)); @@ -370,7 +363,7 @@ Future buildAndroid({ String enginePath, bool force: false, String manifest: _kDefaultAndroidManifestPath, - String resources: _kDefaultResourcesPath, + String resources, String outputFile: _kDefaultOutputPath, String target: '', String flxPath, @@ -392,6 +385,16 @@ Future buildAndroid({ return 0; } + if (resources != null) { + if (!FileSystemEntity.isDirectorySync(resources)) { + printError('Resources directory "$resources" not found.'); + return 1; + } + } else { + if (FileSystemEntity.isDirectorySync(_kDefaultResourcesPath)) + resources = _kDefaultResourcesPath; + } + BuildConfiguration config = configs.firstWhere( (BuildConfiguration bc) => bc.targetPlatform == TargetPlatform.android_arm ); @@ -411,11 +414,10 @@ Future buildAndroid({ } return _buildApk(components, flxPath, keystore, outputFile); } else { - // Find the path to the main Dart file. + // Find the path to the main Dart file; build the FLX. String mainPath = findMainDartFile(target); - - // Build the FLX. String localBundlePath = await flx.buildFlx(toolchain, mainPath: mainPath); + return _buildApk(components, localBundlePath, keystore, outputFile); } } diff --git a/packages/flutter_tools/lib/src/commands/build_flx.dart b/packages/flutter_tools/lib/src/commands/build_flx.dart index 2cfb50ec4a..cd53de6a07 100644 --- a/packages/flutter_tools/lib/src/commands/build_flx.dart +++ b/packages/flutter_tools/lib/src/commands/build_flx.dart @@ -43,7 +43,7 @@ class BuildFlxCommand extends FlutterCommand { if (compilerPath == null) await downloadToolchain(); else - toolchain = new Toolchain(compiler: new Compiler(compilerPath)); + toolchain = new Toolchain(compiler: new SnapshotCompiler(compilerPath)); String outputPath = argResults['output-file']; diff --git a/packages/flutter_tools/lib/src/commands/daemon.dart b/packages/flutter_tools/lib/src/commands/daemon.dart index 6d8e1bdbcd..5784cc4269 100644 --- a/packages/flutter_tools/lib/src/commands/daemon.dart +++ b/packages/flutter_tools/lib/src/commands/daemon.dart @@ -8,6 +8,7 @@ import 'dart:io'; import '../android/android_device.dart'; import '../application_package.dart'; +import '../artifacts.dart'; import '../base/context.dart'; import '../base/logger.dart'; import '../device.dart'; @@ -386,24 +387,14 @@ class DeviceDomain extends Domain { } } -Map _deviceToMap(Device device) { - return { +Map _deviceToMap(Device device) { + return { 'id': device.id, 'name': device.name, - 'platform': _enumToString(device.platform) + 'platform': getNameForTargetPlatform(device.platform) }; } -/// Take an enum value and get the best string representation of that. -/// -/// toString() on enums returns 'EnumType.enumName'. -String _enumToString(dynamic enumValue) { - String str = '$enumValue'; - if (str.contains('.')) - return str.substring(str.indexOf('.') + 1); - return str; -} - dynamic _toJsonable(dynamic obj) { if (obj is String || obj is int || obj is bool || obj is Map || obj is List || obj == null) return obj; diff --git a/packages/flutter_tools/lib/src/commands/logs.dart b/packages/flutter_tools/lib/src/commands/logs.dart index 5190685de5..ab82161582 100644 --- a/packages/flutter_tools/lib/src/commands/logs.dart +++ b/packages/flutter_tools/lib/src/commands/logs.dart @@ -9,12 +9,6 @@ import '../globals.dart'; import '../runner/flutter_command.dart'; class LogsCommand extends FlutterCommand { - @override - final String name = 'logs'; - - @override - final String description = 'Show log output for running Flutter apps.'; - LogsCommand() { argParser.addFlag('clear', negatable: false, @@ -23,6 +17,12 @@ class LogsCommand extends FlutterCommand { ); } + @override + final String name = 'logs'; + + @override + final String description = 'Show log output for running Flutter apps.'; + @override bool get requiresProjectRoot => false; diff --git a/packages/flutter_tools/lib/src/commands/refresh.dart b/packages/flutter_tools/lib/src/commands/refresh.dart index 1849900f14..8a7f80161a 100644 --- a/packages/flutter_tools/lib/src/commands/refresh.dart +++ b/packages/flutter_tools/lib/src/commands/refresh.dart @@ -41,7 +41,7 @@ class RefreshCommand extends FlutterCommand { try { String snapshotPath = path.join(tempDir.path, 'snapshot_blob.bin'); - int result = await toolchain.compiler.compile( + int result = await toolchain.compiler.createSnapshot( mainPath: argResults['target'], snapshotPath: snapshotPath ); if (result != 0) { diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index f7c53959dc..e752c3f738 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -42,7 +42,7 @@ abstract class RunCommandBase extends FlutterCommand { defaultsTo: false, help: 'Start tracing during startup.'); argParser.addOption('route', - help: 'Which route to load when starting the app.'); + help: 'Which route to load when running the app.'); usesTargetOption(); } @@ -65,10 +65,10 @@ class RunCommand extends RunCommandBase { RunCommand() { argParser.addFlag('full-restart', defaultsTo: true, - help: 'Stop any currently running application process before starting the app.'); + help: 'Stop any currently running application process before running the app.'); argParser.addFlag('clear-logs', defaultsTo: true, - help: 'Clear log history before starting the app.'); + help: 'Clear log history before running the app.'); argParser.addFlag('start-paused', defaultsTo: false, negatable: false, @@ -214,14 +214,12 @@ Future startApp( await installApp(device, package); } - bool startedSomething = false; - Map platformArgs = {}; if (traceStartup != null) platformArgs['trace-startup'] = traceStartup; - printStatus('Starting ${_getDisplayPath(mainPath)} on ${device.name}...'); + printStatus('Running ${_getDisplayPath(mainPath)} on ${device.name}...'); bool result = await device.startApp( package, @@ -236,10 +234,8 @@ Future startApp( ); if (!result) { - printError('Error starting application on ${device.name}.'); + printError('Error running application on ${device.name}.'); } else { - startedSomething = true; - // If the user specified --start-paused (and the device supports it) then // wait for the observatory port to become available before returning from // `startApp()`. @@ -247,7 +243,7 @@ Future startApp( await delayUntilObservatoryAvailable('localhost', debugPort); } - return startedSomething ? 0 : 2; + return result ? 0 : 2; } /// Delay until the Observatory / service protocol is available. @@ -257,6 +253,8 @@ Future startApp( Future delayUntilObservatoryAvailable(String host, int port, { Duration timeout: const Duration(seconds: 10) }) async { + printTrace('Waiting until Observatory is available (port $port).'); + Stopwatch stopwatch = new Stopwatch()..start(); final String url = 'ws://$host:$port/ws'; diff --git a/packages/flutter_tools/lib/src/commands/run_mojo.dart b/packages/flutter_tools/lib/src/commands/run_mojo.dart index 4d905a542d..f19c208054 100644 --- a/packages/flutter_tools/lib/src/commands/run_mojo.dart +++ b/packages/flutter_tools/lib/src/commands/run_mojo.dart @@ -18,15 +18,6 @@ import 'run.dart'; const String _kDefaultBundlePath = 'build/app.flx'; class RunMojoCommand extends FlutterCommand { - @override - final String name = 'run_mojo'; - - @override - final String description = 'Run a Flutter app in mojo (from github.com/domokit/mojo).'; - - @override - final bool hidden; - RunMojoCommand({ this.hidden: false }) { argParser.addFlag('android', negatable: false, help: 'Run on an Android device'); argParser.addFlag('checked', negatable: false, help: 'Run Flutter in checked mode'); @@ -42,6 +33,15 @@ class RunMojoCommand extends FlutterCommand { argParser.addOption('devtools-path', help: 'Path to mojo devtools\' mojo_run command.'); } + @override + final String name = 'run_mojo'; + + @override + final String description = 'Run a Flutter app in mojo (from github.com/domokit/mojo).'; + + @override + final bool hidden; + @override bool get requiresProjectRoot => false; diff --git a/packages/flutter_tools/lib/src/commands/trace.dart b/packages/flutter_tools/lib/src/commands/trace.dart index fe6a54fb25..aa630a4f5e 100644 --- a/packages/flutter_tools/lib/src/commands/trace.dart +++ b/packages/flutter_tools/lib/src/commands/trace.dart @@ -10,6 +10,14 @@ import '../globals.dart'; import '../runner/flutter_command.dart'; class TraceCommand extends FlutterCommand { + TraceCommand() { + argParser.addFlag('start', negatable: false, help: 'Start tracing.'); + argParser.addFlag('stop', negatable: false, help: 'Stop tracing.'); + argParser.addOption('out', help: 'Specify the path of the saved trace file.'); + argParser.addOption('duration', + defaultsTo: '10', abbr: 'd', help: 'Duration in seconds to trace.'); + } + @override final String name = 'trace'; @@ -22,14 +30,6 @@ class TraceCommand extends FlutterCommand { 'time (controlled by --duration), and stop tracing. To explicitly control tracing, call trace\n' 'with --start and later with --stop.'; - TraceCommand() { - argParser.addFlag('start', negatable: false, help: 'Start tracing.'); - argParser.addFlag('stop', negatable: false, help: 'Stop tracing.'); - argParser.addOption('out', help: 'Specify the path of the saved trace file.'); - argParser.addOption('duration', - defaultsTo: '10', abbr: 'd', help: 'Duration in seconds to trace.'); - } - @override bool get androidOnly => true; diff --git a/packages/flutter_tools/lib/src/flx.dart b/packages/flutter_tools/lib/src/flx.dart index 79ca4d1a6e..ca293f5f33 100644 --- a/packages/flutter_tools/lib/src/flx.dart +++ b/packages/flutter_tools/lib/src/flx.dart @@ -177,7 +177,11 @@ Future build( // In a precompiled snapshot, the instruction buffer contains script // content equivalents - int result = await toolchain.compiler.compile(mainPath: mainPath, snapshotPath: snapshotPath, depfilePath: depfilePath, buildOutputPath: outputPath); + int result = await toolchain.compiler.createSnapshot( + mainPath: mainPath, + snapshotPath: snapshotPath, + depfilePath: depfilePath + ); if (result != 0) { printError('Failed to run the Flutter compiler. Exit code: $result'); return result; diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 567cdcd078..2c776d3842 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -99,14 +99,12 @@ class SimControl { SimDevice _createTestDevice() { SimDeviceType deviceType = _findSuitableDeviceType(); - if (deviceType == null) { + if (deviceType == null) return null; - } String runtime = _findSuitableRuntime(); - if (runtime == null) { + if (runtime == null) return null; - } // Delete any old test devices getDevices() @@ -131,10 +129,8 @@ class SimControl { if (usableTypes.isEmpty) { printError( - 'No suitable device type found.' - '\n' - 'You may launch an iOS Simulator manually and Flutter will attempt to ' - 'use it.' + 'No suitable device type found.\n' + 'You may launch an iOS Simulator manually and Flutter will attempt to use it.' ); } @@ -153,10 +149,8 @@ class SimControl { if (usableRuntimes.isEmpty) { printError( - 'No suitable iOS runtime found.' - '\n' - 'You may launch an iOS Simulator manually and Flutter will attempt to ' - 'use it.' + 'No suitable iOS runtime found.\n' + 'You may launch an iOS Simulator manually and Flutter will attempt to use it.' ); } @@ -165,7 +159,7 @@ class SimControl { void _deleteDevice(SimDevice device) { try { - List args = [_xcrunPath, 'simctl', 'delete', device.name]; + List args = [_xcrunPath, 'simctl', 'delete', device.name]; printTrace(args.join(' ')); runCheckedSync(args); } catch(e) { @@ -426,9 +420,8 @@ class IOSSimulator extends Device { @override String supportMessage() { - if (isSupported()) { + if (isSupported()) return "Supported"; - } return _supportMessage != null ? _supportMessage : "Unknown"; } diff --git a/packages/flutter_tools/lib/src/service_protocol.dart b/packages/flutter_tools/lib/src/service_protocol.dart index 70b6183204..5df2ebab90 100644 --- a/packages/flutter_tools/lib/src/service_protocol.dart +++ b/packages/flutter_tools/lib/src/service_protocol.dart @@ -21,11 +21,9 @@ class ServiceProtocolDiscovery { final DeviceLogReader _logReader; Completer _completer = new Completer(); - /// The [Future] returned by this function will complete when the next - /// service protocol port is found. - Future nextPort() { - return _completer.future; - } + /// The [Future] returned by this function will complete when the next service + /// protocol port is found. + Future nextPort() => _completer.future; void _onLine(String line) { int portNumber = 0; diff --git a/packages/flutter_tools/lib/src/toolchain.dart b/packages/flutter_tools/lib/src/toolchain.dart index c3bbf75826..7684a49bbd 100644 --- a/packages/flutter_tools/lib/src/toolchain.dart +++ b/packages/flutter_tools/lib/src/toolchain.dart @@ -11,12 +11,12 @@ import 'artifacts.dart'; import 'base/process.dart'; import 'build_configuration.dart'; -class Compiler { - Compiler(this._path); +class SnapshotCompiler { + SnapshotCompiler(this._path); - String _path; + final String _path; - Future compile({ + Future createSnapshot({ String mainPath, String snapshotPath, String depfilePath, @@ -28,12 +28,10 @@ class Compiler { '--package-root=${ArtifactStore.packageRoot}', '--snapshot=$snapshotPath' ]; - if (depfilePath != null) { + if (depfilePath != null) args.add('--depfile=$depfilePath'); - } - if (buildOutputPath != null) { + if (buildOutputPath != null) args.add('--build-output=$buildOutputPath'); - } return runCommandAndStreamOutput(args); } } @@ -56,13 +54,13 @@ Future _getCompilerPath(BuildConfiguration config) async { class Toolchain { Toolchain({ this.compiler }); - final Compiler compiler; + final SnapshotCompiler compiler; static Future forConfigs(List configs) async { for (BuildConfiguration config in configs) { String compilerPath = await _getCompilerPath(config); if (compilerPath != null) - return new Toolchain(compiler: new Compiler(compilerPath)); + return new Toolchain(compiler: new SnapshotCompiler(compilerPath)); } return null; } diff --git a/packages/flutter_tools/test/src/mocks.dart b/packages/flutter_tools/test/src/mocks.dart index c30764e743..1219a9dc60 100644 --- a/packages/flutter_tools/test/src/mocks.dart +++ b/packages/flutter_tools/test/src/mocks.dart @@ -28,11 +28,11 @@ class MockApplicationPackageStore extends ApplicationPackageStore { ); } -class MockCompiler extends Mock implements Compiler { +class MockSnapshotCompiler extends Mock implements SnapshotCompiler { } class MockToolchain extends Toolchain { - MockToolchain() : super(compiler: new MockCompiler()); + MockToolchain() : super(compiler: new MockSnapshotCompiler()); } class MockAndroidDevice extends Mock implements AndroidDevice {