diff --git a/packages/flutter_tools/lib/src/asset.dart b/packages/flutter_tools/lib/src/asset.dart index 22b3481e4e..a49c084d18 100644 --- a/packages/flutter_tools/lib/src/asset.dart +++ b/packages/flutter_tools/lib/src/asset.dart @@ -7,6 +7,7 @@ import 'dart:convert'; import 'package:yaml/yaml.dart'; +import 'base/context.dart'; import 'base/file_system.dart'; import 'build_info.dart'; import 'cache.dart'; @@ -15,8 +16,46 @@ import 'devfs.dart'; import 'flutter_manifest.dart'; import 'globals.dart'; -/// A bundle of assets. -class AssetBundle { +const AssetBundleFactory _kManifestFactory = const _ManifestAssetBundleFactory(); + +/// Injected factory class for spawning [AssetBundle] instances. +abstract class AssetBundleFactory { + /// The singleton instance, pulled from the [AppContext]. + static AssetBundleFactory get instance => context == null + ? _kManifestFactory + : context.putIfAbsent(AssetBundleFactory, () => _kManifestFactory); + + /// Creates a new [AssetBundle]. + AssetBundle createBundle(); +} + +abstract class AssetBundle { + factory AssetBundle.fixed(String projectRoot, String projectAssets) => + new _ManifestAssetBundle.fixed(projectRoot, projectAssets); + + Map get entries; + + bool needsBuild({String manifestPath: _ManifestAssetBundle.defaultManifestPath}); + + /// Returns 0 for success; non-zero for failure. + Future build({ + String manifestPath: _ManifestAssetBundle.defaultManifestPath, + String workingDirPath, + String packagesPath, + bool includeDefaultFonts: true, + bool reportLicensedPackages: false + }); +} + +class _ManifestAssetBundleFactory implements AssetBundleFactory { + const _ManifestAssetBundleFactory(); + + @override + AssetBundle createBundle() => new _ManifestAssetBundle(); +} + +class _ManifestAssetBundle implements AssetBundle { + @override final Map entries = {}; static const String defaultManifestPath = 'pubspec.yaml'; @@ -28,14 +67,14 @@ class AssetBundle { bool _fixed = false; DateTime _lastBuildTimestamp; - /// Constructs an [AssetBundle] that gathers the set of assets from the + /// Constructs an [_ManifestAssetBundle] that gathers the set of assets from the /// pubspec.yaml manifest. - AssetBundle(); + _ManifestAssetBundle(); - /// Constructs an [AssetBundle] with a fixed set of assets. + /// Constructs an [_ManifestAssetBundle] with a fixed set of assets. /// [projectRoot] The absolute path to the project root. /// [projectAssets] comma separated list of assets. - AssetBundle.fixed(String projectRoot, String projectAssets) { + _ManifestAssetBundle.fixed(String projectRoot, String projectAssets) { _fixed = true; if ((projectRoot == null) || (projectAssets == null)) return; @@ -50,6 +89,7 @@ class AssetBundle { } } + @override bool needsBuild({String manifestPath: defaultManifestPath}) { if (_fixed) return false; @@ -63,6 +103,7 @@ class AssetBundle { return stat.modified.isAfter(_lastBuildTimestamp); } + @override Future build({ String manifestPath: defaultManifestPath, String workingDirPath, @@ -191,11 +232,6 @@ class AssetBundle { return 0; } - - void dump() { - printTrace('Dumping AssetBundle:'); - (entries.keys.toList()..sort()).forEach(printTrace); - } } class _Asset { @@ -369,7 +405,7 @@ List> _parseFonts( }) { final List> fonts = >[]; if (manifest.usesMaterialDesign && includeDefaultFonts) { - fonts.addAll(_getMaterialFonts(AssetBundle._kFontSetMaterial)); + fonts.addAll(_getMaterialFonts(_ManifestAssetBundle._kFontSetMaterial)); } if (packageName == null) { fonts.addAll(manifest.fontsDescriptor); diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 785d649a6b..b4b34a4910 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -109,10 +109,6 @@ class RunCommand extends RunCommandBase { hide: !verboseHelp, help: 'Turn on strong mode semantics.\n' 'Valid only when --preview-dart-2 is also specified'); - argParser.addOption('packages', - hide: !verboseHelp, - valueHelp: 'path', - help: 'Specify the path to the .packages file.'); argParser.addOption('project-root', hide: !verboseHelp, help: 'Specify the project root directory.'); @@ -260,7 +256,7 @@ class RunCommand extends RunCommandBase { previewDart2: argResults['preview-dart-2'], strongMode: argResults['strong'], projectRootPath: argResults['project-root'], - packagesFilePath: argResults['packages'], + packagesFilePath: globalResults['packages'], projectAssets: argResults['project-assets'], ipv6: ipv6, ); @@ -313,7 +309,7 @@ class RunCommand extends RunCommandBase { previewDart2: argResults['preview-dart-2'], strongMode: argResults['strong'], projectRootPath: argResults['project-root'], - packagesFilePath: argResults['packages'], + packagesFilePath: globalResults['packages'], projectAssets: argResults['project-assets'], stayResident: stayResident, ipv6: ipv6, diff --git a/packages/flutter_tools/lib/src/flx.dart b/packages/flutter_tools/lib/src/flx.dart index 2bc48e46c0..ad0982bdbb 100644 --- a/packages/flutter_tools/lib/src/flx.dart +++ b/packages/flutter_tools/lib/src/flx.dart @@ -114,7 +114,7 @@ Future> assemble({ printTrace('Building $outputPath'); // Build the asset bundle. - final AssetBundle assetBundle = new AssetBundle(); + final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle(); final int result = await assetBundle.build( manifestPath: manifestPath, workingDirPath: workingDirPath, diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 8bc490c1a3..3f42d0be6d 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -420,7 +420,7 @@ abstract class ResidentRunner { if (projectAssets != null) _assetBundle = new AssetBundle.fixed(_projectRootPath, projectAssets); else - _assetBundle = new AssetBundle(); + _assetBundle = AssetBundleFactory.instance.createBundle(); } final List flutterDevices; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 569e57061c..f8962b3345 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -11,6 +11,7 @@ import 'package:quiver/strings.dart'; import '../application_package.dart'; import '../base/common.dart'; +import '../base/context.dart'; import '../base/file_system.dart'; import '../base/utils.dart'; import '../build_info.dart'; @@ -62,6 +63,11 @@ class FlutterOptions { } abstract class FlutterCommand extends Command { + /// The currently executing command (or sub-command). + /// + /// Will be `null` until the top-most command has begun execution. + static FlutterCommand get current => context[FlutterCommand]; + @override ArgParser get argParser => _argParser; final ArgParser _argParser = new ArgParser(allowTrailingOptions: false); @@ -203,6 +209,8 @@ abstract class FlutterCommand extends Command { Future run() async { final DateTime startTime = clock.now(); + context.setVariable(FlutterCommand, this); + if (flutterUsage.isFirstRun) flutterUsage.printWelcome(); diff --git a/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart b/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart index 509d706cb5..ec1a99693b 100644 --- a/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart +++ b/packages/flutter_tools/test/asset_bundle_package_fonts_test.dart @@ -57,7 +57,7 @@ $fontsSection List packages, String expectedAssetManifest, ) async { - final AssetBundle bundle = new AssetBundle(); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); for (String packageName in packages) { @@ -121,7 +121,7 @@ $fontsSection writePackagesFile('test_package:p/p/lib/'); writePubspecFile('p/p/pubspec.yaml', 'test_package'); - final AssetBundle bundle = new AssetBundle(); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); expect(bundle.entries.length, 2); // LICENSE, AssetManifest expect(bundle.entries.containsKey('FontManifest.json'), false); diff --git a/packages/flutter_tools/test/asset_bundle_package_test.dart b/packages/flutter_tools/test/asset_bundle_package_test.dart index ee40de2efb..dc9dfe681f 100644 --- a/packages/flutter_tools/test/asset_bundle_package_test.dart +++ b/packages/flutter_tools/test/asset_bundle_package_test.dart @@ -64,7 +64,7 @@ $assetsSection List packages, String expectedAssetManifest, ) async { - final AssetBundle bundle = new AssetBundle(); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); for (String packageName in packages) { @@ -122,7 +122,7 @@ $assetsSection writePackagesFile('test_package:p/p/lib/'); writePubspecFile('p/p/pubspec.yaml', 'test_package'); - final AssetBundle bundle = new AssetBundle(); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); expect(bundle.entries.length, 2); // LICENSE, AssetManifest final String expectedAssetManifest = '{}'; @@ -142,7 +142,7 @@ $assetsSection final List assets = ['a/foo']; writeAssets('p/p/', assets); - final AssetBundle bundle = new AssetBundle(); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); expect(bundle.entries.length, 2); // LICENSE, AssetManifest final String expectedAssetManifest = '{}'; diff --git a/packages/flutter_tools/test/asset_bundle_test.dart b/packages/flutter_tools/test/asset_bundle_test.dart index 57f842119c..b485fe637f 100644 --- a/packages/flutter_tools/test/asset_bundle_test.dart +++ b/packages/flutter_tools/test/asset_bundle_test.dart @@ -97,8 +97,8 @@ void main() { } }); - test('nonempty', () async { - final AssetBundle ab = new AssetBundle(); + testUsingContext('nonempty', () async { + final AssetBundle ab = AssetBundleFactory.instance.createBundle(); expect(await ab.build(), 0); expect(ab.entries.length, greaterThan(0)); }); @@ -108,7 +108,7 @@ void main() { ..createSync() ..writeAsStringSync(''); - final AssetBundle bundle = new AssetBundle(); + final AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); expect(bundle.entries.length, 1); final String expectedAssetManifest = '{}'; diff --git a/packages/flutter_tools/test/asset_bundle_variant_test.dart b/packages/flutter_tools/test/asset_bundle_variant_test.dart index b6c4dcbe97..5aa28a5649 100644 --- a/packages/flutter_tools/test/asset_bundle_variant_test.dart +++ b/packages/flutter_tools/test/asset_bundle_variant_test.dart @@ -71,7 +71,7 @@ flutter: ..writeAsStringSync(asset); } - AssetBundle bundle = new AssetBundle(); + AssetBundle bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); // The main asset file, /a/b/c/foo, and its variants exist. @@ -81,7 +81,7 @@ flutter: } fs.file('a/b/c/foo').deleteSync(); - bundle = new AssetBundle(); + bundle = AssetBundleFactory.instance.createBundle(); await bundle.build(manifestPath: 'pubspec.yaml'); // Now the main asset file, /a/b/c/foo, does not exist. This is OK because diff --git a/packages/flutter_tools/test/asset_test.dart b/packages/flutter_tools/test/asset_test.dart index b9fb826f90..204bce5d71 100644 --- a/packages/flutter_tools/test/asset_test.dart +++ b/packages/flutter_tools/test/asset_test.dart @@ -29,7 +29,7 @@ void main() { // This test intentionally does not use a memory file system to ensure // that AssetBundle with fonts also works on Windows. testUsingContext('app font uses local font file', () async { - final AssetBundle asset = new AssetBundle(); + final AssetBundle asset = AssetBundleFactory.instance.createBundle(); await asset.build( manifestPath : fs.path.join(dataPath, 'main', 'pubspec.yaml'), packagesPath: fs.path.join(dataPath, 'main', '.packages'), diff --git a/packages/flutter_tools/test/devfs_test.dart b/packages/flutter_tools/test/devfs_test.dart index f931e9b2f1..19eaa3e914 100644 --- a/packages/flutter_tools/test/devfs_test.dart +++ b/packages/flutter_tools/test/devfs_test.dart @@ -27,7 +27,7 @@ void main() { Directory tempDir; String basePath; DevFS devFS; - final AssetBundle assetBundle = new AssetBundle(); + final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle(); setUpAll(() { fs = new MemoryFileSystem();