project.dart 40 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6

7
import 'package:meta/meta.dart';
Dan Field's avatar
Dan Field committed
8
import 'package:xml/xml.dart';
9
import 'package:yaml/yaml.dart';
10

11
import 'android/gradle_utils.dart' as gradle;
12
import 'artifacts.dart';
13
import 'base/common.dart';
14
import 'base/file_system.dart';
15
import 'base/logger.dart';
16
import 'build_info.dart';
17
import 'bundle.dart' as bundle;
18
import 'features.dart';
19
import 'flutter_manifest.dart';
20
import 'globals.dart' as globals;
21
import 'ios/plist_parser.dart';
22
import 'ios/xcodeproj.dart' as xcode;
23
import 'ios/xcodeproj.dart';
24
import 'platform_plugins.dart';
25
import 'plugins.dart';
26
import 'template.dart';
27

28
class FlutterProjectFactory {
29 30 31 32 33 34 35 36
  FlutterProjectFactory({
    @required Logger logger,
    @required FileSystem fileSystem,
  }) : _logger = logger,
       _fileSystem = fileSystem;

  final Logger _logger;
  final FileSystem _fileSystem;
37

38 39
  @visibleForTesting
  final Map<String, FlutterProject> projects =
40
      <String, FlutterProject>{};
41 42 43 44 45

  /// 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);
46
    return projects.putIfAbsent(directory.path, () {
47 48
      final FlutterManifest manifest = FlutterProject._readManifest(
        directory.childFile(bundle.defaultManifestPath).path,
49 50
        logger: _logger,
        fileSystem: _fileSystem,
51 52 53 54 55
      );
      final FlutterManifest exampleManifest = FlutterProject._readManifest(
        FlutterProject._exampleDirectory(directory)
            .childFile(bundle.defaultManifestPath)
            .path,
56 57
        logger: _logger,
        fileSystem: _fileSystem,
58 59 60
      );
      return FlutterProject(directory, manifest, exampleManifest);
    });
61 62 63
  }
}

64
/// Represents the contents of a Flutter project at the specified [directory].
65
///
66 67 68 69 70 71 72
/// [FlutterManifest] information is read from `pubspec.yaml` and
/// `example/pubspec.yaml` files on construction of a [FlutterProject] instance.
/// The constructed instance carries an immutable snapshot representation of the
/// presence and content of those files. Accordingly, [FlutterProject] instances
/// should be discarded upon changes to the `pubspec.yaml` files, but can be
/// used across changes to other files, as no other file-level information is
/// cached.
73
class FlutterProject {
74
  @visibleForTesting
75
  FlutterProject(this.directory, this.manifest, this._exampleManifest)
76 77 78
    : assert(directory != null),
      assert(manifest != null),
      assert(_exampleManifest != null);
79

80 81
  /// Returns a [FlutterProject] view of the given directory or a ToolExit error,
  /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
82
  static FlutterProject fromDirectory(Directory directory) => globals.projectFactory.fromDirectory(directory);
83

84 85
  /// Returns a [FlutterProject] view of the current directory or a ToolExit error,
  /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
86
  static FlutterProject current() => fromDirectory(globals.fs.currentDirectory);
87

88 89
  /// Returns a [FlutterProject] view of the given directory or a ToolExit error,
  /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
90
  static FlutterProject fromPath(String path) => fromDirectory(globals.fs.directory(path));
91 92 93 94

  /// The location of this project.
  final Directory directory;

95
  /// The manifest of this project.
96 97
  final FlutterManifest manifest;

98
  /// The manifest of the example sub-project of this project.
99
  final FlutterManifest _exampleManifest;
100

101
  /// The set of organization names found in this project as
102 103
  /// part of iOS product bundle identifier, Android application ID, or
  /// Gradle group ID.
104
  Future<Set<String>> get organizationNames async {
105
    final List<String> candidates = <String>[
106 107 108
      // Don't require iOS build info, this method is only
      // used during create as best-effort, use the
      // default target bundle identifier.
109 110 111 112 113 114 115 116 117 118
      if (ios.existsSync())
        await ios.productBundleIdentifier(null),
      if (android.existsSync()) ...<String>[
        android.applicationId,
        android.group,
      ],
      if (example.android.existsSync())
        example.android.applicationId,
      if (example.ios.existsSync())
        await example.ios.productBundleIdentifier(null),
119
    ];
120
    return Set<String>.of(candidates
121
        .map<String>(_organizationNameFromPackageName)
122
        .where((String name) => name != null));
123 124 125
  }

  String _organizationNameFromPackageName(String packageName) {
126
    if (packageName != null && 0 <= packageName.lastIndexOf('.')) {
127
      return packageName.substring(0, packageName.lastIndexOf('.'));
128 129
    }
    return null;
130 131 132
  }

  /// The iOS sub project of this project.
133 134
  IosProject _ios;
  IosProject get ios => _ios ??= IosProject.fromFlutter(this);
135 136

  /// The Android sub project of this project.
137 138
  AndroidProject _android;
  AndroidProject get android => _android ??= AndroidProject._(this);
139

140
  /// The web sub project of this project.
141 142
  WebProject _web;
  WebProject get web => _web ??= WebProject._(this);
143

144 145 146
  /// The MacOS sub project of this project.
  MacOSProject _macos;
  MacOSProject get macos => _macos ??= MacOSProject._(this);
147

148 149 150
  /// The Linux sub project of this project.
  LinuxProject _linux;
  LinuxProject get linux => _linux ??= LinuxProject._(this);
151

152 153 154 155 156 157 158
  /// The Windows sub project of this project.
  WindowsProject _windows;
  WindowsProject get windows => _windows ??= WindowsProject._(this);

  /// The Fuchsia sub project of this project.
  FuchsiaProject _fuchsia;
  FuchsiaProject get fuchsia => _fuchsia ??= FuchsiaProject._(this);
159

160 161 162 163 164 165
  /// The `pubspec.yaml` file of this project.
  File get pubspecFile => directory.childFile('pubspec.yaml');

  /// The `.packages` file of this project.
  File get packagesFile => directory.childFile('.packages');

166 167 168
  /// The `.metadata` file of this project.
  File get metadataFile => directory.childFile('.metadata');

169
  /// The `.flutter-plugins` file of this project.
170 171
  File get flutterPluginsFile => directory.childFile('.flutter-plugins');

172 173 174 175
  /// The `.flutter-plugins-dependencies` file of this project,
  /// which contains the dependencies each plugin depends on.
  File get flutterPluginsDependenciesFile => directory.childFile('.flutter-plugins-dependencies');

176 177 178
  /// The `.dart-tool` directory of this project.
  Directory get dartTool => directory.childDirectory('.dart_tool');

179 180
  /// The directory containing the generated code for this project.
  Directory get generated => directory
181
    .absolute
182 183 184 185 186
    .childDirectory('.dart_tool')
    .childDirectory('build')
    .childDirectory('generated')
    .childDirectory(manifest.appName);

187
  /// The example sub-project of this project.
188
  FlutterProject get example => FlutterProject(
189 190
    _exampleDirectory(directory),
    _exampleManifest,
191
    FlutterManifest.empty(logger: globals.logger),
192 193
  );

194 195
  /// True if this project is a Flutter module project.
  bool get isModule => manifest.isModule;
196

197
  /// True if the Flutter project is using the AndroidX support library.
198 199
  bool get usesAndroidX => manifest.usesAndroidX;

200
  /// True if this project has an example application.
201
  bool get hasExampleApp => _exampleDirectory(directory).existsSync();
202 203

  /// The directory that will contain the example if an example exists.
204
  static Directory _exampleDirectory(Directory directory) => directory.childDirectory('example');
205

206 207 208 209 210
  /// Reads and validates the `pubspec.yaml` file at [path], asynchronously
  /// returning a [FlutterManifest] representation of the contents.
  ///
  /// Completes with an empty [FlutterManifest], if the file does not exist.
  /// Completes with a ToolExit on validation error.
211 212 213 214
  static FlutterManifest _readManifest(String path, {
    @required Logger logger,
    @required FileSystem fileSystem,
  }) {
215 216
    FlutterManifest manifest;
    try {
217 218 219 220 221
      manifest = FlutterManifest.createFromPath(
        path,
        logger: logger,
        fileSystem: fileSystem,
      );
222
    } on YamlException catch (e) {
223 224
      logger.printStatus('Error detected in pubspec.yaml:', emphasis: true);
      logger.printError('$e');
225 226
    }
    if (manifest == null) {
227
      throwToolExit('Please correct the pubspec.yaml file at $path');
228
    }
229 230 231
    return manifest;
  }

232
  /// Generates project files necessary to make Gradle builds work on Android
233
  /// and CocoaPods+Xcode work on iOS, for app and module projects only.
234 235 236
  // TODO(cyanglaz): The param `checkProjects` is confusing. We should give it a better name
  // or add some documentation explaining what it does, or both.
  // https://github.com/flutter/flutter/issues/60023
237 238
  Future<void> ensureReadyForPlatformSpecificTooling({bool checkProjects = false}) async {
    if (!directory.existsSync() || hasExampleApp) {
239
      return;
240
    }
241
    await refreshPluginsList(this);
242 243 244 245 246 247
    if ((android.existsSync() && checkProjects) || !checkProjects) {
      await android.ensureReadyForPlatformSpecificTooling();
    }
    if ((ios.existsSync() && checkProjects) || !checkProjects) {
      await ios.ensureReadyForPlatformSpecificTooling();
    }
248 249 250 251 252 253
    // TODO(stuartmorgan): Revisit conditions once there is a plan for handling
    // non-default platform projects. For now, always treat checkProjects as
    // true for desktop.
    if (featureFlags.isLinuxEnabled && linux.existsSync()) {
      await linux.ensureReadyForPlatformSpecificTooling();
    }
254
    if (featureFlags.isMacOSEnabled && macos.existsSync()) {
255 256
      await macos.ensureReadyForPlatformSpecificTooling();
    }
257 258 259
    if (featureFlags.isWindowsEnabled && windows.existsSync()) {
      await windows.ensureReadyForPlatformSpecificTooling();
    }
260
    if (featureFlags.isWebEnabled && web.existsSync()) {
261 262
      await web.ensureReadyForPlatformSpecificTooling();
    }
263
    await injectPlugins(this, checkProjects: checkProjects);
264
  }
265 266
}

267 268 269 270 271 272 273 274 275 276
/// Base class for projects per platform.
abstract class FlutterProjectPlatform {

  /// Plugin's platform config key, e.g., "macos", "ios".
  String get pluginConfigKey;

  /// Whether the platform exists in the project.
  bool existsSync();
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/// Represents an Xcode-based sub-project.
///
/// This defines interfaces common to iOS and macOS projects.
abstract class XcodeBasedProject {
  /// The parent of this project.
  FlutterProject get parent;

  /// Whether the subproject (either iOS or macOS) exists in the Flutter project.
  bool existsSync();

  /// The Xcode project (.xcodeproj directory) of the host app.
  Directory get xcodeProject;

  /// The 'project.pbxproj' file of [xcodeProject].
  File get xcodeProjectInfoFile;

  /// The Xcode workspace (.xcworkspace directory) of the host app.
  Directory get xcodeWorkspace;

  /// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
  /// the Xcode build.
  File get generatedXcodePropertiesFile;

  /// The Flutter-managed Xcode config file for [mode].
  File xcodeConfigFor(String mode);

303 304 305 306 307 308
  /// The script that exports environment variables needed for Flutter tools.
  /// Can be run first in a Xcode Script build phase to make FLUTTER_ROOT,
  /// LOCAL_ENGINE, and other Flutter variables available to any flutter
  /// tooling (`flutter build`, etc) to convert into flags.
  File get generatedEnvironmentVariableExportScript;

309 310 311 312 313 314 315 316 317 318
  /// The CocoaPods 'Podfile'.
  File get podfile;

  /// The CocoaPods 'Podfile.lock'.
  File get podfileLock;

  /// The CocoaPods 'Manifest.lock'.
  File get podManifestLock;
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/// Represents a CMake-based sub-project.
///
/// This defines interfaces common to Windows and Linux projects.
abstract class CmakeBasedProject {
  /// The parent of this project.
  FlutterProject get parent;

  /// Whether the subproject (either Windows or Linux) exists in the Flutter project.
  bool existsSync();

  /// The native project CMake specification.
  File get cmakeFile;

  /// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
  /// the build.
  File get generatedCmakeConfigFile;

  /// Includable CMake with rules and variables for plugin builds.
  File get generatedPluginCmakeFile;
}

340 341 342
/// Represents the iOS sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `ios/` sub-folder of
343
/// Flutter applications and the `.ios/` sub-folder of Flutter module projects.
344
class IosProject extends FlutterProjectPlatform implements XcodeBasedProject {
345
  IosProject.fromFlutter(this.parent);
346

347
  @override
348 349
  final FlutterProject parent;

350 351 352
  @override
  String get pluginConfigKey => IOSPlugin.kConfigKey;

353
  static final RegExp _productBundleIdPattern = RegExp(r'''^\s*PRODUCT_BUNDLE_IDENTIFIER\s*=\s*(["']?)(.*?)\1;\s*$''');
354
  static const String _productBundleIdVariable = r'$(PRODUCT_BUNDLE_IDENTIFIER)';
355
  static const String _hostAppProjectName = 'Runner';
356

357
  Directory get ephemeralDirectory => parent.directory.childDirectory('.ios');
358 359 360 361
  Directory get _editableDirectory => parent.directory.childDirectory('ios');

  /// This parent folder of `Runner.xcodeproj`.
  Directory get hostAppRoot {
362
    if (!isModule || _editableDirectory.existsSync()) {
363
      return _editableDirectory;
364
    }
365
    return ephemeralDirectory;
366 367 368 369 370 371 372
  }

  /// The root directory of the iOS wrapping of Flutter and plugins. This is the
  /// parent of the `Flutter/` folder into which Flutter artifacts are written
  /// during build.
  ///
  /// This is the same as [hostAppRoot] except when the project is
373
  /// a Flutter module with an editable host app.
374
  Directory get _flutterLibRoot => isModule ? ephemeralDirectory : _editableDirectory;
375

376 377
  /// True, if the parent Flutter project is a module project.
  bool get isModule => parent.isModule;
378

379 380 381
  /// Whether the flutter application has an iOS project.
  bool get exists => hostAppRoot.existsSync();

382
  @override
383
  File xcodeConfigFor(String mode) => _flutterLibRoot.childDirectory('Flutter').childFile('$mode.xcconfig');
384

385 386 387
  @override
  File get generatedEnvironmentVariableExportScript => _flutterLibRoot.childDirectory('Flutter').childFile('flutter_export_environment.sh');

388
  @override
389
  File get podfile => hostAppRoot.childFile('Podfile');
390

391
  @override
392
  File get podfileLock => hostAppRoot.childFile('Podfile.lock');
393

394
  @override
395
  File get podManifestLock => hostAppRoot.childDirectory('Pods').childFile('Manifest.lock');
396

397
  /// The default 'Info.plist' file of the host app. The developer can change this location in Xcode.
398
  File get defaultHostInfoPlist => hostAppRoot.childDirectory(_hostAppProjectName).childFile('Info.plist');
399

400 401
  Directory get symlinks => _flutterLibRoot.childDirectory('.symlinks');

402
  @override
403
  Directory get xcodeProject => hostAppRoot.childDirectory('$_hostAppProjectName.xcodeproj');
404

405
  @override
406 407
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

408
  @override
409
  Directory get xcodeWorkspace => hostAppRoot.childDirectory('$_hostAppProjectName.xcworkspace');
410 411 412 413 414 415 416

  /// Xcode workspace shared data directory for the host app.
  Directory get xcodeWorkspaceSharedData => xcodeWorkspace.childDirectory('xcshareddata');

  /// Xcode workspace shared workspace settings file for the host app.
  File get xcodeWorkspaceSharedSettings => xcodeWorkspaceSharedData.childFile('WorkspaceSettings.xcsettings');

417
  @override
418 419 420 421
  bool existsSync()  {
    return parent.isModule || _editableDirectory.existsSync();
  }

422 423
  /// The product bundle identifier of the host app, or null if not set or if
  /// iOS tooling needed to read it is not installed.
424 425 426 427 428 429
  Future<String> productBundleIdentifier(BuildInfo buildInfo) async {
    if (!existsSync()) {
      return null;
    }
    return _productBundleIdentifier ??= await _parseProductBundleIdentifier(buildInfo);
  }
430 431
  String _productBundleIdentifier;

432
  Future<String> _parseProductBundleIdentifier(BuildInfo buildInfo) async {
433
    String fromPlist;
434 435 436 437 438
    final File defaultInfoPlist = defaultHostInfoPlist;
    // Users can change the location of the Info.plist.
    // Try parsing the default, first.
    if (defaultInfoPlist.existsSync()) {
      try {
439
        fromPlist = globals.plistParser.getValueFromFile(
440 441 442 443 444 445
          defaultHostInfoPlist.path,
          PlistParser.kCFBundleIdentifierKey,
        );
      } on FileNotFoundException {
        // iOS tooling not found; likely not running OSX; let [fromPlist] be null
      }
446
      if (fromPlist != null && !fromPlist.contains(r'$')) {
447 448 449
        // Info.plist has no build variables in product bundle ID.
        return fromPlist;
      }
450
    }
451
    final Map<String, String> allBuildSettings = await buildSettingsForBuildInfo(buildInfo);
452 453 454 455 456 457
    if (allBuildSettings != null) {
      if (fromPlist != null) {
        // Perform variable substitution using build settings.
        return xcode.substituteXcodeVariables(fromPlist, allBuildSettings);
      }
      return allBuildSettings['PRODUCT_BUNDLE_IDENTIFIER'];
458
    }
459 460 461 462 463 464 465

    // On non-macOS platforms, parse the first PRODUCT_BUNDLE_IDENTIFIER from
    // the project file. This can return the wrong bundle identifier if additional
    // bundles have been added to the project and are found first, like frameworks
    // or companion watchOS projects. However, on non-macOS platforms this is
    // only used for display purposes and to regenerate organization names, so
    // best-effort is probably fine.
466
    final String fromPbxproj = _firstMatchInFile(xcodeProjectInfoFile, _productBundleIdPattern)?.group(2);
467 468 469
    if (fromPbxproj != null && (fromPlist == null || fromPlist == _productBundleIdVariable)) {
      return fromPbxproj;
    }
470

471
    return null;
472 473
  }

474
  /// The bundle name of the host app, `My App.app`.
475 476 477 478 479 480
  Future<String> hostAppBundleName(BuildInfo buildInfo) async {
    if (!existsSync()) {
      return null;
    }
    return _hostAppBundleName ??= await _parseHostAppBundleName(buildInfo);
  }
481 482
  String _hostAppBundleName;

483
  Future<String> _parseHostAppBundleName(BuildInfo buildInfo) async {
484 485 486 487 488 489
    // The product name and bundle name are derived from the display name, which the user
    // is instructed to change in Xcode as part of deploying to the App Store.
    // https://flutter.dev/docs/deployment/ios#review-xcode-project-settings
    // The only source of truth for the name is Xcode's interpretation of the build settings.
    String productName;
    if (globals.xcodeProjectInterpreter.isInstalled) {
490
      final Map<String, String> xcodeBuildSettings = await buildSettingsForBuildInfo(buildInfo);
491 492 493 494 495 496 497 498 499 500
      if (xcodeBuildSettings != null) {
        productName = xcodeBuildSettings['FULL_PRODUCT_NAME'];
      }
    }
    if (productName == null) {
      globals.printTrace('FULL_PRODUCT_NAME not present, defaulting to $_hostAppProjectName');
    }
    return productName ?? '$_hostAppProjectName.app';
  }

501
  /// The build settings for the host app of this project, as a detached map.
502 503
  ///
  /// Returns null, if iOS tooling is unavailable.
504
  Future<Map<String, String>> buildSettingsForBuildInfo(BuildInfo buildInfo) async {
505 506 507
    if (!existsSync()) {
      return null;
    }
508
    _buildSettingsByScheme ??= <String, Map<String, String>>{};
509 510 511 512 513 514 515 516 517 518
    final XcodeProjectInfo info = await projectInfo();
    if (info == null) {
      return null;
    }

    final String scheme = info.schemeFor(buildInfo);
    if (scheme == null) {
      info.reportFlavorNotFoundAndExit();
    }

519 520 521
    return _buildSettingsByScheme[scheme] ??= await _xcodeProjectBuildSettings(scheme);
  }
  Map<String, Map<String, String>> _buildSettingsByScheme;
522

523
  Future<XcodeProjectInfo> projectInfo() async {
524
    if (!xcodeProject.existsSync() || !globals.xcodeProjectInterpreter.isInstalled) {
525 526 527 528 529 530
      return null;
    }
    return _projectInfo ??= await globals.xcodeProjectInterpreter.getInfo(hostAppRoot.path);
  }
  XcodeProjectInfo _projectInfo;

531
  Future<Map<String, String>> _xcodeProjectBuildSettings(String scheme) async {
532
    if (!globals.xcodeProjectInterpreter.isInstalled) {
533
      return null;
534
    }
535
    final Map<String, String> buildSettings = await globals.xcodeProjectInterpreter.getBuildSettings(
536
      xcodeProject.path,
537
      scheme: scheme,
538
    );
539 540 541 542 543
    if (buildSettings != null && buildSettings.isNotEmpty) {
      // No timeouts, flakes, or errors.
      return buildSettings;
    }
    return null;
544
  }
545

546
  Future<void> ensureReadyForPlatformSpecificTooling() async {
547
    await _regenerateFromTemplateIfNeeded();
548
    if (!_flutterLibRoot.existsSync()) {
549
      return;
550
    }
551 552 553
    await _updateGeneratedXcodeConfigIfNeeded();
  }

554
  /// Check if one the [targets] of the project is a watchOS companion app target.
555 556
  Future<bool> containsWatchCompanion(List<String> targets, BuildInfo buildInfo) async {
    final String bundleIdentifier = await productBundleIdentifier(buildInfo);
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    // A bundle identifier is required for a companion app.
    if (bundleIdentifier == null) {
      return false;
    }
    for (final String target in targets) {
      // Create Info.plist file of the target.
      final File infoFile = hostAppRoot.childDirectory(target).childFile('Info.plist');
      // The Info.plist file of a target contains the key WKCompanionAppBundleIdentifier,
      // if it is a watchOS companion app.
      if (infoFile.existsSync() && globals.plistParser.getValueFromFile(infoFile.path, 'WKCompanionAppBundleIdentifier') == bundleIdentifier) {
        return true;
      }
    }
    return false;
  }

573
  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
574
    if (globals.cache.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
575 576 577 578 579 580
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        targetOverride: bundle.defaultMainPath,
      );
    }
581 582
  }

583
  Future<void> _regenerateFromTemplateIfNeeded() async {
584
    if (!isModule) {
585
      return;
586
    }
587
    final bool pubspecChanged = globals.fsUtils.isOlderThanReference(
588 589 590
      entity: ephemeralDirectory,
      referenceFile: parent.pubspecFile,
    );
591
    final bool toolingChanged = globals.cache.isOlderThanToolsStamp(ephemeralDirectory);
592
    if (!pubspecChanged && !toolingChanged) {
593
      return;
594
    }
595

596
    _deleteIfExistsSync(ephemeralDirectory);
597
    await _overwriteFromTemplate(
598 599 600
      globals.fs.path.join('module', 'ios', 'library'),
      ephemeralDirectory,
    );
601 602
    // Add ephemeral host app, if a editable host app does not already exist.
    if (!_editableDirectory.existsSync()) {
603
      await _overwriteFromTemplate(
604 605 606
        globals.fs.path.join('module', 'ios', 'host_app_ephemeral'),
        ephemeralDirectory,
      );
607
      if (hasPlugins(parent)) {
608
        await _overwriteFromTemplate(
609 610 611
          globals.fs.path.join('module', 'ios', 'host_app_ephemeral_cocoapods'),
          ephemeralDirectory,
        );
612
      }
613 614 615 616 617 618 619 620 621 622 623 624
      copyEngineArtifactToProject(BuildMode.debug);
    }
  }

  void copyEngineArtifactToProject(BuildMode mode) {
    // Copy podspec and framework from engine cache. The actual build mode
    // doesn't actually matter as it will be overwritten by xcode_backend.sh.
    // However, cocoapods will run before that script and requires something
    // to be in this location.
    final Directory framework = globals.fs.directory(
      globals.artifacts.getArtifactPath(
        Artifact.flutterFramework,
625
        platform: TargetPlatform.ios,
626 627 628 629 630 631 632 633
        mode: mode,
      )
    );
    if (framework.existsSync()) {
      final Directory engineDest = ephemeralDirectory
          .childDirectory('Flutter')
          .childDirectory('engine');
      final File podspec = framework.parent.childFile('Flutter.podspec');
634
      globals.fsUtils.copyDirectorySync(
635 636 637 638
        framework,
        engineDest.childDirectory('Flutter.framework'),
      );
      podspec.copySync(engineDest.childFile('Flutter.podspec').path);
639
    }
640 641
  }

642
  @override
643 644 645
  File get generatedXcodePropertiesFile => _flutterLibRoot
    .childDirectory('Flutter')
    .childFile('Generated.xcconfig');
646

647 648 649 650
  Directory get compiledDartFramework => _flutterLibRoot
      .childDirectory('Flutter')
      .childDirectory('App.framework');

651
  Directory get pluginRegistrantHost {
652
    return isModule
653 654 655
        ? _flutterLibRoot
            .childDirectory('Flutter')
            .childDirectory('FlutterPluginRegistrant')
656
        : hostAppRoot.childDirectory(_hostAppProjectName);
657 658
  }

659
  Future<void> _overwriteFromTemplate(String path, Directory target) async {
660
    final Template template = await Template.fromName(path, fileSystem: globals.fs, templateManifest: null);
661 662 663
    template.render(
      target,
      <String, dynamic>{
664
        'ios': true,
665
        'projectName': parent.manifest.appName,
666
        'iosIdentifier': parent.manifest.iosBundleIdentifier,
667 668 669 670
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
671
  }
672 673
}

674 675 676
/// Represents the Android sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `android/` sub-folder of
677
/// Flutter applications and the `.android/` sub-folder of Flutter module projects.
678
class AndroidProject extends FlutterProjectPlatform {
679 680 681 682 683
  AndroidProject._(this.parent);

  /// The parent of this project.
  final FlutterProject parent;

684 685 686
  @override
  String get pluginConfigKey => AndroidPlugin.kConfigKey;

687
  static final RegExp _applicationIdPattern = RegExp('^\\s*applicationId\\s+[\'"](.*)[\'"]\\s*\$');
688
  static final RegExp _kotlinPluginPattern = RegExp('^\\s*apply plugin\\:\\s+[\'"]kotlin-android[\'"]\\s*\$');
689
  static final RegExp _groupPattern = RegExp('^\\s*group\\s+[\'"](.*)[\'"]\\s*\$');
690

691 692 693 694
  /// The Gradle root directory of the Android host app. This is the directory
  /// containing the `app/` subdirectory and the `settings.gradle` file that
  /// includes it in the overall Gradle project.
  Directory get hostAppGradleRoot {
695
    if (!isModule || _editableHostAppDirectory.existsSync()) {
696
      return _editableHostAppDirectory;
697
    }
698
    return ephemeralDirectory;
699 700 701 702
  }

  /// The Gradle root directory of the Android wrapping of Flutter and plugins.
  /// This is the same as [hostAppGradleRoot] except when the project is
703
  /// a Flutter module with an editable host app.
704
  Directory get _flutterLibGradleRoot => isModule ? ephemeralDirectory : _editableHostAppDirectory;
705

706
  Directory get ephemeralDirectory => parent.directory.childDirectory('.android');
707
  Directory get _editableHostAppDirectory => parent.directory.childDirectory('android');
708

709 710
  /// True if the parent Flutter project is a module.
  bool get isModule => parent.isModule;
711

712
  /// True if the Flutter project is using the AndroidX support library.
713 714
  bool get usesAndroidX => parent.usesAndroidX;

715 716 717 718 719 720
  /// True, if the app project is using Kotlin.
  bool get isKotlin {
    final File gradleFile = hostAppGradleRoot.childDirectory('app').childFile('build.gradle');
    return _firstMatchInFile(gradleFile, _kotlinPluginPattern) != null;
  }

721
  File get appManifestFile {
722
    return isUsingGradle
723
        ? globals.fs.file(globals.fs.path.join(hostAppGradleRoot.path, 'app', 'src', 'main', 'AndroidManifest.xml'))
724
        : hostAppGradleRoot.childFile('AndroidManifest.xml');
725 726
  }

727
  File get gradleAppOutV1File => gradleAppOutV1Directory.childFile('app-debug.apk');
728 729

  Directory get gradleAppOutV1Directory {
730
    return globals.fs.directory(globals.fs.path.join(hostAppGradleRoot.path, 'app', 'build', 'outputs', 'apk'));
731 732
  }

733
  /// Whether the current flutter project has an Android sub-project.
734
  @override
735 736 737 738
  bool existsSync() {
    return parent.isModule || _editableHostAppDirectory.existsSync();
  }

739
  bool get isUsingGradle {
740
    return hostAppGradleRoot.childFile('build.gradle').existsSync();
741
  }
742

743
  String get applicationId {
744
    final File gradleFile = hostAppGradleRoot.childDirectory('app').childFile('build.gradle');
745
    return _firstMatchInFile(gradleFile, _applicationIdPattern)?.group(1);
746 747
  }

748
  String get group {
749
    final File gradleFile = hostAppGradleRoot.childFile('build.gradle');
750
    return _firstMatchInFile(gradleFile, _groupPattern)?.group(1);
751
  }
752

753 754 755 756 757
  /// The build directory where the Android artifacts are placed.
  Directory get buildDirectory {
    return parent.directory.childDirectory('build');
  }

758
  Future<void> ensureReadyForPlatformSpecificTooling() async {
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
    if (getEmbeddingVersion() == AndroidEmbeddingVersion.v1) {
      globals.printStatus(
"""
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Warning
──────────────────────────────────────────────────────────────────────────────
Your Flutter application is created using an older version of the Android
embedding. It's being deprecated in favor of Android embedding v2. Follow the
steps at

https://flutter.dev/go/android-project-migration

to migrate your project.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
"""
      );
    }
776
    if (isModule && _shouldRegenerateFromTemplate()) {
777
      await _regenerateLibrary();
778 779
      // Add ephemeral host app, if an editable host app does not already exist.
      if (!_editableHostAppDirectory.existsSync()) {
780 781
        await _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'host_app_common'), ephemeralDirectory);
        await _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'host_app_ephemeral'), ephemeralDirectory);
782
      }
783
    }
784
    if (!hostAppGradleRoot.existsSync()) {
785
      return;
786 787
    }
    gradle.updateLocalProperties(project: parent, requireAndroidSdk: false);
788 789
  }

790
  bool _shouldRegenerateFromTemplate() {
791
    return globals.fsUtils.isOlderThanReference(
792 793 794
      entity: ephemeralDirectory,
      referenceFile: parent.pubspecFile,
    ) || globals.cache.isOlderThanToolsStamp(ephemeralDirectory);
795
  }
796

797 798
  File get localPropertiesFile => _flutterLibGradleRoot.childFile('local.properties');

799
  Directory get pluginRegistrantHost => _flutterLibGradleRoot.childDirectory(isModule ? 'Flutter' : 'app');
800

801
  Future<void> _regenerateLibrary() async {
802
    _deleteIfExistsSync(ephemeralDirectory);
803
    await _overwriteFromTemplate(globals.fs.path.join(
804 805
      'module',
      'android',
806
      'library_new_embedding',
807
    ), ephemeralDirectory);
808
    await _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'gradle'), ephemeralDirectory);
809
    gradle.gradleUtils.injectGradleWrapperIfNeeded(ephemeralDirectory);
810
  }
811

812
  Future<void> _overwriteFromTemplate(String path, Directory target) async {
813
    final Template template = await Template.fromName(path, fileSystem: globals.fs, templateManifest: null);
814 815 816
    template.render(
      target,
      <String, dynamic>{
817
        'android': true,
818 819
        'projectName': parent.manifest.appName,
        'androidIdentifier': parent.manifest.androidPackage,
820
        'androidX': usesAndroidX,
821 822 823 824 825
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
  }
826 827

  AndroidEmbeddingVersion getEmbeddingVersion() {
828 829 830 831 832
    if (isModule) {
      // A module type's Android project is used in add-to-app scenarios and
      // only supports the V2 embedding.
      return AndroidEmbeddingVersion.v2;
    }
833 834 835
    if (appManifestFile == null || !appManifestFile.existsSync()) {
      return AndroidEmbeddingVersion.v1;
    }
Dan Field's avatar
Dan Field committed
836
    XmlDocument document;
837
    try {
Dan Field's avatar
Dan Field committed
838 839
      document = XmlDocument.parse(appManifestFile.readAsStringSync());
    } on XmlParserException {
840 841 842 843 844 845
      throwToolExit('Error parsing $appManifestFile '
                    'Please ensure that the android manifest is a valid XML document and try again.');
    } on FileSystemException {
      throwToolExit('Error reading $appManifestFile even though it exists. '
                    'Please ensure that you have read permission to this file and try again.');
    }
Dan Field's avatar
Dan Field committed
846
    for (final XmlElement metaData in document.findAllElements('meta-data')) {
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
      final String name = metaData.getAttribute('android:name');
      if (name == 'flutterEmbedding') {
        final String embeddingVersionString = metaData.getAttribute('android:value');
        if (embeddingVersionString == '1') {
          return AndroidEmbeddingVersion.v1;
        }
        if (embeddingVersionString == '2') {
          return AndroidEmbeddingVersion.v2;
        }
      }
    }
    return AndroidEmbeddingVersion.v1;
  }
}

/// Iteration of the embedding Java API in the engine used by the Android project.
enum AndroidEmbeddingVersion {
  /// V1 APIs based on io.flutter.app.FlutterActivity.
  v1,
  /// V2 APIs based on io.flutter.embedding.android.FlutterActivity.
  v2,
868 869
}

870
/// Represents the web sub-project of a Flutter project.
871
class WebProject extends FlutterProjectPlatform {
872 873 874 875
  WebProject._(this.parent);

  final FlutterProject parent;

876 877 878
  @override
  String get pluginConfigKey => WebPlugin.kConfigKey;

879
  /// Whether this flutter project has a web sub-project.
880
  @override
881
  bool existsSync() {
882 883
    return parent.directory.childDirectory('web').existsSync()
      && indexFile.existsSync();
884
  }
885

886 887 888
  /// The 'lib' directory for the application.
  Directory get libDirectory => parent.directory.childDirectory('lib');

889 890 891
  /// The directory containing additional files for the application.
  Directory get directory => parent.directory.childDirectory('web');

892
  /// The html file used to host the flutter web application.
893 894 895
  File get indexFile => parent.directory
      .childDirectory('web')
      .childFile('index.html');
896

897
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
898 899
}

900 901
/// Deletes [directory] with all content.
void _deleteIfExistsSync(Directory directory) {
902
  if (directory.existsSync()) {
903
    directory.deleteSync(recursive: true);
904
  }
905 906 907 908
}


/// Returns the first line-based match for [regExp] in [file].
909 910
///
/// Assumes UTF8 encoding.
911 912
Match _firstMatchInFile(File file, RegExp regExp) {
  if (!file.existsSync()) {
913 914
    return null;
  }
915
  for (final String line in file.readAsLinesSync()) {
916 917 918 919 920 921
    final Match match = regExp.firstMatch(line);
    if (match != null) {
      return match;
    }
  }
  return null;
922
}
923 924

/// The macOS sub project.
925
class MacOSProject extends FlutterProjectPlatform implements XcodeBasedProject {
926
  MacOSProject._(this.parent);
927

928 929
  @override
  final FlutterProject parent;
930

931 932 933
  @override
  String get pluginConfigKey => MacOSPlugin.kConfigKey;

934
  static const String _hostAppProjectName = 'Runner';
935

936
  @override
937
  bool existsSync() => _macOSDirectory.existsSync();
938

939
  Directory get _macOSDirectory => parent.directory.childDirectory('macos');
940

941 942 943 944 945 946 947 948 949
  /// The directory in the project that is managed by Flutter. As much as
  /// possible, files that are edited by Flutter tooling after initial project
  /// creation should live here.
  Directory get managedDirectory => _macOSDirectory.childDirectory('Flutter');

  /// The subdirectory of [managedDirectory] that contains files that are
  /// generated on the fly. All generated files that are not intended to be
  /// checked in should live here.
  Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');
950

951 952 953 954 955 956 957 958
  /// The xcfilelist used to track the inputs for the Flutter script phase in
  /// the Xcode build.
  File get inputFileList => ephemeralDirectory.childFile('FlutterInputs.xcfilelist');

  /// The xcfilelist used to track the outputs for the Flutter script phase in
  /// the Xcode build.
  File get outputFileList => ephemeralDirectory.childFile('FlutterOutputs.xcfilelist');

959
  @override
960 961
  File get generatedXcodePropertiesFile => ephemeralDirectory.childFile('Flutter-Generated.xcconfig');

962
  @override
963
  File xcodeConfigFor(String mode) => managedDirectory.childFile('Flutter-$mode.xcconfig');
964

965
  @override
966
  File get generatedEnvironmentVariableExportScript => ephemeralDirectory.childFile('flutter_export_environment.sh');
967

968 969 970 971 972 973 974 975 976 977
  @override
  File get podfile => _macOSDirectory.childFile('Podfile');

  @override
  File get podfileLock => _macOSDirectory.childFile('Podfile.lock');

  @override
  File get podManifestLock => _macOSDirectory.childDirectory('Pods').childFile('Manifest.lock');

  @override
978
  Directory get xcodeProject => _macOSDirectory.childDirectory('$_hostAppProjectName.xcodeproj');
979

980 981 982 983
  @override
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

  @override
984
  Directory get xcodeWorkspace => _macOSDirectory.childDirectory('$_hostAppProjectName.xcworkspace');
985

986 987
  /// The file where the Xcode build will write the name of the built app.
  ///
Chris Bracken's avatar
Chris Bracken committed
988
  /// Ideally this will be replaced in the future with inspection of the Runner
989
  /// scheme's target.
990
  File get nameFile => ephemeralDirectory.childFile('.app_filename');
991 992 993 994 995 996 997

  Future<void> ensureReadyForPlatformSpecificTooling() async {
    // TODO(stuartmorgan): Add create-from-template logic here.
    await _updateGeneratedXcodeConfigIfNeeded();
  }

  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
998
    if (globals.cache.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
999 1000 1001 1002 1003 1004 1005 1006
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        useMacOSConfig: true,
        setSymroot: false,
      );
    }
  }
1007 1008
}

1009
/// The Windows sub project.
1010 1011
class WindowsProject extends FlutterProjectPlatform implements CmakeBasedProject {
  WindowsProject._(this.parent);
1012

1013 1014
  @override
  final FlutterProject parent;
1015

1016 1017 1018 1019
  @override
  String get pluginConfigKey => WindowsPlugin.kConfigKey;

  @override
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  bool existsSync() => _editableDirectory.existsSync() && cmakeFile.existsSync();

  @override
  File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');

  @override
  File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');

  @override
  File get generatedPluginCmakeFile => managedDirectory.childFile('generated_plugins.cmake');
1030

1031
  Directory get _editableDirectory => parent.directory.childDirectory('windows');
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041
  /// The directory in the project that is managed by Flutter. As much as
  /// possible, files that are edited by Flutter tooling after initial project
  /// creation should live here.
  Directory get managedDirectory => _editableDirectory.childDirectory('flutter');

  /// The subdirectory of [managedDirectory] that contains files that are
  /// generated on the fly. All generated files that are not intended to be
  /// checked in should live here.
  Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');
1042

1043 1044 1045
  /// The directory to write plugin symlinks.
  Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');

1046
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
1047 1048 1049
}

/// The Linux sub project.
1050 1051
class LinuxProject extends FlutterProjectPlatform implements CmakeBasedProject {
  LinuxProject._(this.parent);
1052

1053 1054
  @override
  final FlutterProject parent;
1055

1056 1057 1058
  @override
  String get pluginConfigKey => LinuxPlugin.kConfigKey;

1059 1060
  static final RegExp _applicationIdPattern = RegExp(r'''^\s*set\s*\(\s*APPLICATION_ID\s*"(.*)"\s*\)\s*$''');

1061
  Directory get _editableDirectory => parent.directory.childDirectory('linux');
1062

1063 1064 1065 1066
  /// The directory in the project that is managed by Flutter. As much as
  /// possible, files that are edited by Flutter tooling after initial project
  /// creation should live here.
  Directory get managedDirectory => _editableDirectory.childDirectory('flutter');
1067

1068 1069 1070 1071 1072
  /// The subdirectory of [managedDirectory] that contains files that are
  /// generated on the fly. All generated files that are not intended to be
  /// checked in should live here.
  Directory get ephemeralDirectory => managedDirectory.childDirectory('ephemeral');

1073
  @override
1074
  bool existsSync() => _editableDirectory.existsSync();
1075

1076
  @override
1077
  File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
1078

1079
  @override
1080
  File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
1081

1082
  @override
1083
  File get generatedPluginCmakeFile => managedDirectory.childFile('generated_plugins.cmake');
1084

1085 1086 1087
  /// The directory to write plugin symlinks.
  Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');

1088
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
1089 1090 1091 1092

  String get applicationId {
    return _firstMatchInFile(cmakeFile, _applicationIdPattern)?.group(1);
  }
1093
}
1094

1095
/// The Fuchsia sub project.
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
class FuchsiaProject {
  FuchsiaProject._(this.project);

  final FlutterProject project;

  Directory _editableHostAppDirectory;
  Directory get editableHostAppDirectory =>
      _editableHostAppDirectory ??= project.directory.childDirectory('fuchsia');

  bool existsSync() => editableHostAppDirectory.existsSync();

  Directory _meta;
  Directory get meta =>
      _meta ??= editableHostAppDirectory.childDirectory('meta');
}