diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 5118df7b69..86201d55a7 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -443,7 +443,7 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi offline: argResults['offline'], ); final FlutterProject project = await FlutterProject.fromDirectory(directory); - await project.ensureReadyForPlatformSpecificTooling(); + await project.ensureReadyForPlatformSpecificTooling(checkProjects: false); } return generatedCount; } @@ -510,7 +510,7 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi if (argResults['pub']) { await pubGet(context: PubContext.create, directory: directory.path, offline: argResults['offline']); - await project.ensureReadyForPlatformSpecificTooling(); + await project.ensureReadyForPlatformSpecificTooling(checkProjects: false); } gradle.updateLocalProperties(project: project, requireAndroidSdk: false); diff --git a/packages/flutter_tools/lib/src/commands/inject_plugins.dart b/packages/flutter_tools/lib/src/commands/inject_plugins.dart index d13c19b6b6..fca90844d6 100644 --- a/packages/flutter_tools/lib/src/commands/inject_plugins.dart +++ b/packages/flutter_tools/lib/src/commands/inject_plugins.dart @@ -29,8 +29,8 @@ class InjectPluginsCommand extends FlutterCommand { @override Future runCommand() async { final FlutterProject project = await FlutterProject.current(); - refreshPluginsList(project); - await injectPlugins(project); + refreshPluginsList(project, checkProjects: true); + await injectPlugins(project, checkProjects: true); final bool result = hasPlugins(project); if (result) { printStatus('GeneratedPluginRegistrants successfully written.'); diff --git a/packages/flutter_tools/lib/src/commands/make_host_app_editable.dart b/packages/flutter_tools/lib/src/commands/make_host_app_editable.dart index 9b63d598d1..24ba965913 100644 --- a/packages/flutter_tools/lib/src/commands/make_host_app_editable.dart +++ b/packages/flutter_tools/lib/src/commands/make_host_app_editable.dart @@ -43,7 +43,7 @@ class MakeHostAppEditableCommand extends FlutterCommand { @override Future runCommand() async { - await _project.ensureReadyForPlatformSpecificTooling(); + await _project.ensureReadyForPlatformSpecificTooling(checkProjects: false); final bool isAndroidRequested = argResults['android']; final bool isIOSRequested = argResults['ios']; diff --git a/packages/flutter_tools/lib/src/commands/packages.dart b/packages/flutter_tools/lib/src/commands/packages.dart index 2740a29fc8..7e7f837bf3 100644 --- a/packages/flutter_tools/lib/src/commands/packages.dart +++ b/packages/flutter_tools/lib/src/commands/packages.dart @@ -94,13 +94,13 @@ class PackagesGetCommand extends FlutterCommand { await _runPubGet(target); final FlutterProject rootProject = await FlutterProject.fromPath(target); - await rootProject.ensureReadyForPlatformSpecificTooling(); + await rootProject.ensureReadyForPlatformSpecificTooling(checkProjects: true); // Get/upgrade packages in example app as well if (rootProject.hasExampleApp) { final FlutterProject exampleProject = rootProject.example; await _runPubGet(exampleProject.directory.path); - await exampleProject.ensureReadyForPlatformSpecificTooling(); + await exampleProject.ensureReadyForPlatformSpecificTooling(checkProjects: true); } return null; diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index 306183c8a0..5d14fa7025 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -98,8 +98,9 @@ bool _writeFlutterPluginsList(FlutterProject project, List plugins) { if (pluginManifest.isNotEmpty) { pluginsFile.writeAsStringSync('$pluginManifest\n', flush: true); } else { - if (pluginsFile.existsSync()) + if (pluginsFile.existsSync()) { pluginsFile.deleteSync(); + } } final String newContents = _readFlutterPluginsList(project); return oldContents != newContents; @@ -282,31 +283,44 @@ Future _writeIOSPluginRegistrant(FlutterProject project, List plug /// Rewrites the `.flutter-plugins` file of [project] based on the plugin /// dependencies declared in `pubspec.yaml`. /// +/// If `checkProjects` is true, then plugins are only injected into directories +/// which already exist. +/// /// Assumes `pub get` has been executed since last change to `pubspec.yaml`. -void refreshPluginsList(FlutterProject project) { +void refreshPluginsList(FlutterProject project, {bool checkProjects = false}) { final List plugins = findPlugins(project); final bool changed = _writeFlutterPluginsList(project, plugins); - if (changed) + if (changed) { + if (checkProjects && !project.ios.existsSync()) { + return; + } cocoaPods.invalidatePodInstallOutput(project.ios); + } } /// Injects plugins found in `pubspec.yaml` into the platform-specific projects. /// +/// If `checkProjects` is true, then plugins are only injected into directories +/// which already exist. +/// /// Assumes [refreshPluginsList] has been called since last change to `pubspec.yaml`. -Future injectPlugins(FlutterProject project) async { +Future injectPlugins(FlutterProject project, {bool checkProjects = false}) async { final List plugins = findPlugins(project); - await _writeAndroidPluginRegistrant(project, plugins); - await _writeIOSPluginRegistrant(project, plugins); - if (!project.isModule && project.ios.hostAppRoot.existsSync()) { - final IosProject iosProject = IosProject.fromFlutter(project); + if ((checkProjects && project.android.existsSync()) || !checkProjects) { + await _writeAndroidPluginRegistrant(project, plugins); + } + if ((checkProjects && project.ios.existsSync()) || !checkProjects) { + await _writeIOSPluginRegistrant(project, plugins); + } + if (!project.isModule && ((project.ios.hostAppRoot.existsSync() && checkProjects) || !checkProjects)) { final CocoaPods cocoaPods = CocoaPods(); if (plugins.isNotEmpty) { cocoaPods.setupPodfile(project.ios); } /// The user may have a custom maintained Podfile that they're running `pod install` /// on themselves. - else if (iosProject.podfile.existsSync() && iosProject.podfileLock.existsSync()) { - cocoaPods.addPodsDependencyToFlutterXcconfig(iosProject); + else if (project.ios.podfile.existsSync() && project.ios.podfileLock.existsSync()) { + cocoaPods.addPodsDependencyToFlutterXcconfig(project.ios); } } } diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index d3875278e4..c009017a10 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -158,16 +158,21 @@ class FlutterProject { /// Generates project files necessary to make Gradle builds work on Android /// and CocoaPods+Xcode work on iOS, for app and module projects only. - Future ensureReadyForPlatformSpecificTooling() async { - if (!directory.existsSync() || hasExampleApp) + Future ensureReadyForPlatformSpecificTooling({bool checkProjects = false}) async { + if (!directory.existsSync() || hasExampleApp) { return; + } refreshPluginsList(this); - await android.ensureReadyForPlatformSpecificTooling(); - await ios.ensureReadyForPlatformSpecificTooling(); + if ((android.existsSync() && checkProjects) || !checkProjects) { + await android.ensureReadyForPlatformSpecificTooling(); + } + if ((ios.existsSync() && checkProjects) || !checkProjects) { + await ios.ensureReadyForPlatformSpecificTooling(); + } if (flutterWebEnabled) { await web.ensureReadyForPlatformSpecificTooling(); } - await injectPlugins(this); + await injectPlugins(this, checkProjects: checkProjects); } /// Return the set of builders used by this package. @@ -203,6 +208,8 @@ class IosProject { Directory get _ephemeralDirectory => parent.directory.childDirectory('.ios'); Directory get _editableDirectory => parent.directory.childDirectory('ios'); + bool existsSync() => parent.isModule || _editableDirectory.existsSync(); + /// This parent folder of `Runner.xcodeproj`. Directory get hostAppRoot { if (!isModule || _editableDirectory.existsSync()) @@ -381,6 +388,8 @@ class AndroidProject { return _ephemeralDirectory; } + bool existsSync() => parent.isModule || _flutterLibGradleRoot.existsSync(); + /// The Gradle root directory of the Android wrapping of Flutter and plugins. /// This is the same as [hostAppGradleRoot] except when the project is /// a Flutter module with an editable host app. @@ -486,6 +495,8 @@ class WebProject { final FlutterProject parent; + bool existsSync() => parent.directory.childDirectory('web').existsSync(); + Future ensureReadyForPlatformSpecificTooling() async { /// Generate index.html in build/web. Eventually we could support /// a custom html under the web sub directory. diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 554aa307cb..bd72f7784c 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -475,7 +475,7 @@ abstract class FlutterCommand extends Command { if (shouldRunPub) { await pubGet(context: PubContext.getVerifyContext(name)); final FlutterProject project = await FlutterProject.current(); - await project.ensureReadyForPlatformSpecificTooling(); + await project.ensureReadyForPlatformSpecificTooling(checkProjects: true); } setupApplicationPackages();