From f6bd26a3f3fffbbe84aaf6acbb71b06ac2219cf2 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 17 Nov 2015 16:07:09 -0800 Subject: [PATCH] Make flutter run_mojo imply flutter build This makes run_mojo more similar to flutter start. --- .../flutter_tools/lib/src/commands/apk.dart | 22 +++++------- .../flutter_tools/lib/src/commands/build.dart | 8 +++-- .../lib/src/commands/run_mojo.dart | 34 +++++++++++++++---- .../flutter_tools/lib/src/file_system.dart | 15 ++++++++ 4 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 packages/flutter_tools/lib/src/file_system.dart diff --git a/packages/flutter_tools/lib/src/commands/apk.dart b/packages/flutter_tools/lib/src/commands/apk.dart index 3480d8decb5..1b610599676 100644 --- a/packages/flutter_tools/lib/src/commands/apk.dart +++ b/packages/flutter_tools/lib/src/commands/apk.dart @@ -9,6 +9,7 @@ import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; import '../build_configuration.dart'; +import '../file_system.dart'; import 'build.dart'; import 'flutter_command.dart'; import 'start.dart'; @@ -20,13 +21,6 @@ const String _kKeystorePassword = "chromium"; final Logger _logging = new Logger('flutter_tools.apk'); -/// Create the ancestor directories of a file path if they do not already exist. -void _ensureDirectoryExists(String filePath) { - Directory dir = new Directory(path.dirname(filePath)); - if (!dir.existsSync()) - dir.createSync(recursive: true); -} - /// Copies files into a new directory structure. class _AssetBuilder { final Directory outDir; @@ -40,7 +34,7 @@ class _AssetBuilder { void add(File asset, String relativePath) { String destPath = path.join(_assetDir.path, relativePath); - _ensureDirectoryExists(destPath); + ensureDirectoryExists(destPath); asset.copySync(destPath); } @@ -147,7 +141,7 @@ class ApkCommand extends FlutterCommand { } Directory tempDir = Directory.systemTemp.createTempSync('flutter_tools'); - try { + try { _AssetBuilder assetBuilder = new _AssetBuilder(tempDir, 'assets'); assetBuilder.add(icuData, 'icudtl.dat'); assetBuilder.add(new File(flxPath), 'app.flx'); @@ -163,7 +157,7 @@ class ApkCommand extends FlutterCommand { builder.sign(keystore, _kKeystorePassword, _kKeystoreKeyName, unalignedApk); File finalApk = new File(argResults['output-file']); - _ensureDirectoryExists(finalApk.path); + ensureDirectoryExists(finalApk.path); builder.align(unalignedApk, finalApk); return 0; @@ -203,10 +197,10 @@ class ApkCommand extends FlutterCommand { builder.inheritFromParent(this); int result; await builder.buildInTempDir( - mainPath: mainPath, - onBundleAvailable: (String localBundlePath) { - result = _buildApk(config, localBundlePath); - } + mainPath: mainPath, + onBundleAvailable: (String localBundlePath) { + result = _buildApk(config, localBundlePath); + } ); return result; diff --git a/packages/flutter_tools/lib/src/commands/build.dart b/packages/flutter_tools/lib/src/commands/build.dart index a9a2b7859a0..308e9feac01 100644 --- a/packages/flutter_tools/lib/src/commands/build.dart +++ b/packages/flutter_tools/lib/src/commands/build.dart @@ -12,6 +12,7 @@ import 'package:flx/signing.dart'; import 'package:path/path.dart' as path; import 'package:yaml/yaml.dart'; +import '../file_system.dart'; import '../toolchain.dart'; import 'flutter_command.dart'; @@ -109,8 +110,8 @@ ArchiveFile _createSnapshotFile(String snapshotPath) { const String _kDefaultAssetBase = 'packages/material_design_icons/icons'; const String _kDefaultMainPath = 'lib/main.dart'; const String _kDefaultManifestPath = 'flutter.yaml'; -const String _kDefaultOutputPath = 'app.flx'; -const String _kDefaultSnapshotPath = 'snapshot_blob.bin'; +const String _kDefaultOutputPath = 'build/app.flx'; +const String _kDefaultSnapshotPath = 'build/snapshot_blob.bin'; const String _kDefaultPrivateKeyPath = 'privatekey.der'; class BuildCommand extends FlutterCommand { @@ -186,6 +187,8 @@ class BuildCommand extends FlutterCommand { Archive archive = new Archive(); if (!precompiledSnapshot) { + ensureDirectoryExists(snapshotPath); + // In a precompiled snapshot, the instruction buffer contains script // content equivalents int result = await toolchain.compiler.compile(mainPath: mainPath, snapshotPath: snapshotPath); @@ -208,6 +211,7 @@ class BuildCommand extends FlutterCommand { AsymmetricKeyPair keyPair = keyPairFromPrivateKeyFileSync(privateKeyPath); Uint8List zipBytes = new Uint8List.fromList(new ZipEncoder().encode(archive)); + ensureDirectoryExists(outputPath); Bundle bundle = new Bundle.fromContent( path: outputPath, manifest: manifestDescriptor, diff --git a/packages/flutter_tools/lib/src/commands/run_mojo.dart b/packages/flutter_tools/lib/src/commands/run_mojo.dart index 8688dd44287..e82486d7310 100644 --- a/packages/flutter_tools/lib/src/commands/run_mojo.dart +++ b/packages/flutter_tools/lib/src/commands/run_mojo.dart @@ -11,7 +11,11 @@ import 'package:path/path.dart' as path; import '../artifacts.dart'; import '../build_configuration.dart'; import '../process.dart'; +import 'build.dart'; import 'flutter_command.dart'; +import 'start.dart'; + +const String _kDefaultBundlePath = 'build/app.flx'; final Logger _logging = new Logger('flutter_tools.run_mojo'); @@ -25,7 +29,11 @@ class RunMojoCommand extends FlutterCommand { argParser.addFlag('mojo-debug', negatable: false, help: 'Use Debug build of mojo'); argParser.addFlag('mojo-release', negatable: false, help: 'Use Release build of mojo (default)'); - argParser.addOption('app', defaultsTo: 'app.flx'); + argParser.addOption('target', + defaultsTo: '', + abbr: 't', + help: 'Target app path or filename to start.'); + argParser.addOption('app', help: 'Run this Flutter app instead of building the target.'); argParser.addOption('mojo-path', help: 'Path to directory containing mojo_shell and services.'); argParser.addOption('devtools-path', help: 'Path to mojo devtools\' mojo_run command.'); } @@ -72,7 +80,7 @@ class RunMojoCommand extends FlutterCommand { return result; } - Future> _getShellConfig() async { + Future> _getShellConfig(String bundlePath) async { List args = []; final bool useDevtools = _useDevtools(); @@ -82,7 +90,7 @@ class RunMojoCommand extends FlutterCommand { if (argResults['android']) { args.add('--android'); final String cloudStorageBaseUrl = ArtifactStore.getCloudStorageBaseUrl('shell', 'android-arm'); - final String appPath = _makePathAbsolute(argResults['app']); + final String appPath = _makePathAbsolute(bundlePath); final String appName = path.basename(appPath); final String appDir = path.dirname(appPath); args.add('http://app/$appName'); @@ -90,7 +98,7 @@ class RunMojoCommand extends FlutterCommand { args.add('--map-origin=http://flutter/=$cloudStorageBaseUrl'); args.add('--url-mappings=mojo:flutter=http://flutter/flutter.mojo'); } else { - final String appPath = _makePathAbsolute(argResults['app']); + final String appPath = _makePathAbsolute(bundlePath); String flutterPath; BuildConfiguration config = _getCurrentHostConfig(); if (config == null || config.type == BuildType.prebuilt) { @@ -136,7 +144,21 @@ class RunMojoCommand extends FlutterCommand { return 1; } - return await runCommandAndStreamOutput(await _getShellConfig()); - } + await downloadToolchain(); + String bundlePath = argResults['app']; + if (bundlePath == null) { + bundlePath = _kDefaultBundlePath; + + String mainPath = StartCommand.findMainDartFile(argResults['target']); + + BuildCommand builder = new BuildCommand(); + builder.inheritFromParent(this); + int result = await builder.build(mainPath: mainPath, outputPath: bundlePath); + if (result != 0) + return result; + } + + return await runCommandAndStreamOutput(await _getShellConfig(bundlePath)); + } } diff --git a/packages/flutter_tools/lib/src/file_system.dart b/packages/flutter_tools/lib/src/file_system.dart new file mode 100644 index 00000000000..2010803d9be --- /dev/null +++ b/packages/flutter_tools/lib/src/file_system.dart @@ -0,0 +1,15 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:io'; + +import 'package:path/path.dart' as path; + +/// Create the ancestor directories of a file path if they do not already exist. +void ensureDirectoryExists(String filePath) { + String dirPath = path.dirname(filePath); + if (FileSystemEntity.isDirectorySync(dirPath)) + return; + new Directory(dirPath).createSync(recursive: true); +}