From cfe4d0d7b945ca7c13d356708b56f5d2b49456db Mon Sep 17 00:00:00 2001 From: Lau Ching Jun Date: Wed, 26 Jun 2019 13:29:53 -0700 Subject: [PATCH] Add FlutterProjectFactory so that it can be overridden internally. (#35092) Also update the comments since we don't return Future anymore. --- packages/flutter_tools/lib/src/project.dart | 45 +++++++++++++-------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index ef4a38d39e..0076299195 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -9,6 +9,7 @@ import 'package:yaml/yaml.dart'; import 'android/gradle.dart' as gradle; import 'base/common.dart'; +import 'base/context.dart'; import 'base/file_system.dart'; import 'build_info.dart'; import 'bundle.dart' as bundle; @@ -22,6 +23,27 @@ import 'plugins.dart'; import 'template.dart'; import 'web/workflow.dart'; +FlutterProjectFactory get projectFactory => context.get() ?? const FlutterProjectFactory(); + +class FlutterProjectFactory { + const FlutterProjectFactory(); + + /// Returns a [FlutterProject] view of the given directory or a ToolExit error, + /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid. + FlutterProject fromDirectory(Directory directory) { + assert(directory != null); + final FlutterManifest manifest = FlutterProject._readManifest( + directory.childFile(bundle.defaultManifestPath).path, + ); + final FlutterManifest exampleManifest = FlutterProject._readManifest( + FlutterProject._exampleDirectory(directory) + .childFile(bundle.defaultManifestPath) + .path, + ); + return FlutterProject(directory, manifest, exampleManifest); + } +} + /// Represents the contents of a Flutter project at the specified [directory]. /// /// [FlutterManifest] information is read from `pubspec.yaml` and @@ -38,25 +60,16 @@ class FlutterProject { assert(manifest != null), assert(_exampleManifest != null); - /// Returns a future that completes with a [FlutterProject] view of the given directory - /// or a ToolExit error, if `pubspec.yaml` or `example/pubspec.yaml` is invalid. - static FlutterProject fromDirectory(Directory directory) { - assert(directory != null); - final FlutterManifest manifest = _readManifest( - directory.childFile(bundle.defaultManifestPath).path, - ); - final FlutterManifest exampleManifest = _readManifest( - _exampleDirectory(directory).childFile(bundle.defaultManifestPath).path, - ); - return FlutterProject(directory, manifest, exampleManifest); - } + /// Returns a [FlutterProject] view of the given directory or a ToolExit error, + /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid. + static FlutterProject fromDirectory(Directory directory) => projectFactory.fromDirectory(directory); - /// Returns a future that completes with a [FlutterProject] view of the current directory. - /// or a ToolExit error, if `pubspec.yaml` or `example/pubspec.yaml` is invalid. + /// Returns a [FlutterProject] view of the current directory or a ToolExit error, + /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid. static FlutterProject current() => fromDirectory(fs.currentDirectory); - /// Returns a future that completes with a [FlutterProject] view of the given directory. - /// or a ToolExit error, if `pubspec.yaml` or `example/pubspec.yaml` is invalid. + /// Returns a [FlutterProject] view of the given directory or a ToolExit error, + /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid. static FlutterProject fromPath(String path) => fromDirectory(fs.directory(path)); /// The location of this project.