From fd7222eea8ebd75dfa9bdf24ccb3a965fba30ca0 Mon Sep 17 00:00:00 2001 From: PJ Essien Date: Mon, 23 Apr 2018 02:03:46 +0100 Subject: [PATCH] Fix error with 'flutter packages get' in package projects (#16861) Package projects were erroneously being treated as apps --- .../lib/src/commands/packages.dart | 26 ++++++++++++++----- .../flutter_tools/lib/src/ios/xcodeproj.dart | 14 +++++----- packages/flutter_tools/lib/src/plugins.dart | 5 +--- packages/flutter_tools/lib/src/project.dart | 10 +++---- packages/flutter_tools/test/project_test.dart | 6 ++--- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/packages.dart b/packages/flutter_tools/lib/src/commands/packages.dart index 3600289b9f2..7d61ed21dcf 100644 --- a/packages/flutter_tools/lib/src/commands/packages.dart +++ b/packages/flutter_tools/lib/src/commands/packages.dart @@ -56,6 +56,15 @@ class PackagesGetCommand extends FlutterCommand { return '${runner.executableName} packages $name []'; } + Future _runPubGet (String directory) async { + await pubGet(context: PubContext.pubGet, + directory: directory, + upgrade: upgrade , + offline: argResults['offline'], + checkLastModified: false, + ); + } + @override Future runCommand() async { if (argResults.rest.length > 1) @@ -71,13 +80,16 @@ class PackagesGetCommand extends FlutterCommand { ); } - await pubGet(context: PubContext.pubGet, - directory: target, - upgrade: upgrade, - offline: argResults['offline'], - checkLastModified: false, - ); - new FlutterProject(fs.directory(target)).ensureReadyForPlatformSpecificTooling(); + await _runPubGet(target); + final FlutterProject rootProject = new FlutterProject(fs.directory(target)); + rootProject.ensureReadyForPlatformSpecificTooling(); + + // Get/upgrade packages in example app as well + if (rootProject.hasExampleApp) { + final FlutterProject exampleProject = rootProject.example; + await _runPubGet(exampleProject.directory.path); + exampleProject.ensureReadyForPlatformSpecificTooling(); + } } } diff --git a/packages/flutter_tools/lib/src/ios/xcodeproj.dart b/packages/flutter_tools/lib/src/ios/xcodeproj.dart index 32494d5208e..3b75183af48 100644 --- a/packages/flutter_tools/lib/src/ios/xcodeproj.dart +++ b/packages/flutter_tools/lib/src/ios/xcodeproj.dart @@ -28,17 +28,19 @@ String _generatedXcodePropertiesPath(String projectPath) { return fs.path.join(projectPath, 'ios', 'Flutter', 'Generated.xcconfig'); } -/// Writes default Xcode properties files in the Flutter project at -/// [projectPath], if such files do not already exist. +/// Writes default Xcode properties files in the Flutter project at [projectPath], +/// if project is an iOS project and such files do not already exist. void generateXcodeProperties(String projectPath) { - if (fs.file(_generatedXcodePropertiesPath(projectPath)).existsSync()) - return; - updateGeneratedXcodeProperties( + if (fs.isDirectorySync(fs.path.join(projectPath, 'ios'))) { + if (fs.file(_generatedXcodePropertiesPath(projectPath)).existsSync()) + return; + updateGeneratedXcodeProperties( projectPath: projectPath, buildInfo: BuildInfo.debug, target: bundle.defaultMainPath, previewDart2: false, - ); + ); + } } /// Writes or rewrites Xcode property files with the specified information. diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index 8a470b73673..61989c81d83 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -231,12 +231,9 @@ class InjectPluginsResult{ /// Injects plugins found in `pubspec.yaml` into the platform-specific projects. void injectPlugins({String directory}) { directory ??= fs.currentDirectory.path; - if (fs.file(fs.path.join(directory, 'example', 'pubspec.yaml')).existsSync()) { - // Switch to example app if in plugin project template. - directory = fs.path.join(directory, 'example'); - } final List plugins = _findPlugins(directory); final bool changed = _writeFlutterPluginsList(directory, plugins); + if (fs.isDirectorySync(fs.path.join(directory, 'android'))) _writeAndroidPluginRegistrant(directory, plugins); if (fs.isDirectorySync(fs.path.join(directory, 'ios'))) { diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index 2d2a1697f28..76056d7ab23 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -47,16 +47,16 @@ class FlutterProject { /// The Android sub project of this project. AndroidProject get android => new AndroidProject(directory.childDirectory('android')); - /// Returns true if this project is a plugin project. - bool get isPluginProject => directory.childDirectory('example').childFile('pubspec.yaml').existsSync(); + /// Returns true if this project has an example application + bool get hasExampleApp => directory.childDirectory('example').childFile('pubspec.yaml').existsSync(); - /// The example sub project of this (plugin) project. + /// The example sub project of this (package or plugin) project. FlutterProject get example => new FlutterProject(directory.childDirectory('example')); /// Generates project files necessary to make Gradle builds work on Android - /// and CocoaPods+Xcode work on iOS. + /// and CocoaPods+Xcode work on iOS, for app projects only void ensureReadyForPlatformSpecificTooling() { - if (!directory.existsSync() || isPluginProject) { + if (!directory.existsSync() || hasExampleApp) { return; } injectPlugins(directory: directory.path); diff --git a/packages/flutter_tools/test/project_test.dart b/packages/flutter_tools/test/project_test.dart index 00aad68009a..632018d55c2 100644 --- a/packages/flutter_tools/test/project_test.dart +++ b/packages/flutter_tools/test/project_test.dart @@ -23,11 +23,11 @@ void main() { project.ensureReadyForPlatformSpecificTooling(); expect(project.directory.existsSync(), isFalse); }); - testInMemory('does nothing in plugin root project', () async { + testInMemory('does nothing in plugin or package root project', () async { final FlutterProject project = aPluginProject(); project.ensureReadyForPlatformSpecificTooling(); - expect(project.example.ios.directory.childFile('Runner/GeneratedPluginRegistrant.h').existsSync(), isFalse); - expect(project.example.ios.directory.childFile('Flutter/Generated.xcconfig').existsSync(), isFalse); + expect(project.ios.directory.childFile('Runner/GeneratedPluginRegistrant.h').existsSync(), isFalse); + expect(project.ios.directory.childFile('Flutter/Generated.xcconfig').existsSync(), isFalse); }); testInMemory('injects plugins', () async { final FlutterProject project = aProjectWithIos();