project.dart 37 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';
8
import 'package:xml/xml.dart' as xml;
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/context.dart';
15
import 'base/file_system.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 'platform_plugins.dart';
24
import 'plugins.dart';
25
import 'template.dart';
26

27
FlutterProjectFactory get projectFactory => context.get<FlutterProjectFactory>() ?? FlutterProjectFactory();
28 29

class FlutterProjectFactory {
30 31
  FlutterProjectFactory();

32 33
  @visibleForTesting
  final Map<String, FlutterProject> projects =
34
      <String, FlutterProject>{};
35 36 37 38 39

  /// 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);
40
    return projects.putIfAbsent(directory.path, /* ifAbsent */ () {
41 42 43 44 45 46 47 48 49 50
      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);
    });
51 52 53
  }
}

54
/// Represents the contents of a Flutter project at the specified [directory].
55
///
56 57 58 59 60 61 62
/// [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.
63
class FlutterProject {
64
  @visibleForTesting
65
  FlutterProject(this.directory, this.manifest, this._exampleManifest)
66 67 68
    : assert(directory != null),
      assert(manifest != null),
      assert(_exampleManifest != null);
69

70 71 72
  /// 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);
73

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

78 79
  /// Returns a [FlutterProject] view of the given directory or a ToolExit error,
  /// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
80
  static FlutterProject fromPath(String path) => fromDirectory(globals.fs.directory(path));
81 82 83 84

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

85
  /// The manifest of this project.
86 87
  final FlutterManifest manifest;

88
  /// The manifest of the example sub-project of this project.
89
  final FlutterManifest _exampleManifest;
90

91
  /// The set of organization names found in this project as
92 93
  /// part of iOS product bundle identifier, Android application ID, or
  /// Gradle group ID.
94
  Future<Set<String>> get organizationNames async {
95
    final List<String> candidates = <String>[
96
      await ios.productBundleIdentifier,
97 98 99
      android.applicationId,
      android.group,
      example.android.applicationId,
100
      await example.ios.productBundleIdentifier,
101
    ];
102
    return Set<String>.from(candidates
103
        .map<String>(_organizationNameFromPackageName)
104
        .where((String name) => name != null));
105 106 107
  }

  String _organizationNameFromPackageName(String packageName) {
108
    if (packageName != null && 0 <= packageName.lastIndexOf('.')) {
109
      return packageName.substring(0, packageName.lastIndexOf('.'));
110 111
    }
    return null;
112 113 114
  }

  /// The iOS sub project of this project.
115 116
  IosProject _ios;
  IosProject get ios => _ios ??= IosProject.fromFlutter(this);
117 118

  /// The Android sub project of this project.
119 120
  AndroidProject _android;
  AndroidProject get android => _android ??= AndroidProject._(this);
121

122
  /// The web sub project of this project.
123 124
  WebProject _web;
  WebProject get web => _web ??= WebProject._(this);
125

126 127 128
  /// The MacOS sub project of this project.
  MacOSProject _macos;
  MacOSProject get macos => _macos ??= MacOSProject._(this);
129

130 131 132
  /// The Linux sub project of this project.
  LinuxProject _linux;
  LinuxProject get linux => _linux ??= LinuxProject._(this);
133

134 135 136 137 138 139 140
  /// 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);
141

142 143 144 145 146 147 148
  /// 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');

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

151 152 153 154
  /// The `.flutter-plugins-dependencies` file of this project,
  /// which contains the dependencies each plugin depends on.
  File get flutterPluginsDependenciesFile => directory.childFile('.flutter-plugins-dependencies');

155 156 157
  /// The `.dart-tool` directory of this project.
  Directory get dartTool => directory.childDirectory('.dart_tool');

158 159
  /// The directory containing the generated code for this project.
  Directory get generated => directory
160
    .absolute
161 162 163 164 165
    .childDirectory('.dart_tool')
    .childDirectory('build')
    .childDirectory('generated')
    .childDirectory(manifest.appName);

166
  /// The example sub-project of this project.
167
  FlutterProject get example => FlutterProject(
168 169 170 171 172
    _exampleDirectory(directory),
    _exampleManifest,
    FlutterManifest.empty(),
  );

173 174
  /// True if this project is a Flutter module project.
  bool get isModule => manifest.isModule;
175

176 177 178
  /// True if the Flutter project is using the AndroidX support library
  bool get usesAndroidX => manifest.usesAndroidX;

179
  /// True if this project has an example application.
180
  bool get hasExampleApp => _exampleDirectory(directory).existsSync();
181 182

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

185 186 187 188 189
  /// 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.
190
  static FlutterManifest _readManifest(String path) {
191 192 193 194
    FlutterManifest manifest;
    try {
      manifest = FlutterManifest.createFromPath(path);
    } on YamlException catch (e) {
195 196
      globals.printStatus('Error detected in pubspec.yaml:', emphasis: true);
      globals.printError('$e');
197 198
    }
    if (manifest == null) {
199
      throwToolExit('Please correct the pubspec.yaml file at $path');
200
    }
201 202 203
    return manifest;
  }

204
  /// Generates project files necessary to make Gradle builds work on Android
205
  /// and CocoaPods+Xcode work on iOS, for app and module projects only.
206 207
  Future<void> ensureReadyForPlatformSpecificTooling({bool checkProjects = false}) async {
    if (!directory.existsSync() || hasExampleApp) {
208
      return;
209
    }
210
    refreshPluginsList(this);
211 212 213 214 215 216
    if ((android.existsSync() && checkProjects) || !checkProjects) {
      await android.ensureReadyForPlatformSpecificTooling();
    }
    if ((ios.existsSync() && checkProjects) || !checkProjects) {
      await ios.ensureReadyForPlatformSpecificTooling();
    }
217 218 219 220 221 222
    // 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();
    }
223
    if (featureFlags.isMacOSEnabled && macos.existsSync()) {
224 225
      await macos.ensureReadyForPlatformSpecificTooling();
    }
226 227 228
    if (featureFlags.isWindowsEnabled && windows.existsSync()) {
      await windows.ensureReadyForPlatformSpecificTooling();
    }
229
    if (featureFlags.isWebEnabled && web.existsSync()) {
230 231
      await web.ensureReadyForPlatformSpecificTooling();
    }
232
    await injectPlugins(this, checkProjects: checkProjects);
233
  }
234 235

  /// Return the set of builders used by this package.
236 237 238 239
  YamlMap get builders {
    if (!pubspecFile.existsSync()) {
      return null;
    }
240
    final YamlMap pubspec = loadYaml(pubspecFile.readAsStringSync()) as YamlMap;
241 242 243 244
    // If the pubspec file is empty, this will be null.
    if (pubspec == null) {
      return null;
    }
245
    return pubspec['builders'] as YamlMap;
246
  }
247 248

  /// Whether there are any builders used by this package.
249 250
  bool get hasBuilders {
    final YamlMap result = builders;
251 252
    return result != null && result.isNotEmpty;
  }
253 254
}

255 256 257 258 259 260 261 262 263 264
/// 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();
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
/// 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);

291 292 293 294 295 296
  /// 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;

297 298 299 300 301 302 303 304 305
  /// The CocoaPods 'Podfile'.
  File get podfile;

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

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

306 307
  /// Directory containing symlinks to pub cache plugins source generated on `pod install`.
  Directory get symlinks;
308 309
}

310 311 312
/// Represents the iOS sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `ios/` sub-folder of
313
/// Flutter applications and the `.ios/` sub-folder of Flutter module projects.
314
class IosProject extends FlutterProjectPlatform implements XcodeBasedProject {
315
  IosProject.fromFlutter(this.parent);
316

317
  @override
318 319
  final FlutterProject parent;

320 321 322
  @override
  String get pluginConfigKey => IOSPlugin.kConfigKey;

323
  static final RegExp _productBundleIdPattern = RegExp(r'''^\s*PRODUCT_BUNDLE_IDENTIFIER\s*=\s*(["']?)(.*?)\1;\s*$''');
324
  static const String _productBundleIdVariable = r'$(PRODUCT_BUNDLE_IDENTIFIER)';
325
  static const String _hostAppBundleName = 'Runner';
326

327
  Directory get ephemeralDirectory => parent.directory.childDirectory('.ios');
328 329 330 331
  Directory get _editableDirectory => parent.directory.childDirectory('ios');

  /// This parent folder of `Runner.xcodeproj`.
  Directory get hostAppRoot {
332
    if (!isModule || _editableDirectory.existsSync()) {
333
      return _editableDirectory;
334
    }
335
    return ephemeralDirectory;
336 337 338 339 340 341 342
  }

  /// 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
343
  /// a Flutter module with an editable host app.
344
  Directory get _flutterLibRoot => isModule ? ephemeralDirectory : _editableDirectory;
345

346 347 348
  /// The bundle name of the host app, `Runner.app`.
  String get hostAppBundleName => '$_hostAppBundleName.app';

349 350
  /// True, if the parent Flutter project is a module project.
  bool get isModule => parent.isModule;
351

352 353 354
  /// Whether the flutter application has an iOS project.
  bool get exists => hostAppRoot.existsSync();

355
  @override
356
  File xcodeConfigFor(String mode) => _flutterLibRoot.childDirectory('Flutter').childFile('$mode.xcconfig');
357

358 359 360
  @override
  File get generatedEnvironmentVariableExportScript => _flutterLibRoot.childDirectory('Flutter').childFile('flutter_export_environment.sh');

361
  @override
362
  File get podfile => hostAppRoot.childFile('Podfile');
363

364
  @override
365
  File get podfileLock => hostAppRoot.childFile('Podfile.lock');
366

367
  @override
368
  File get podManifestLock => hostAppRoot.childDirectory('Pods').childFile('Manifest.lock');
369

370 371
  /// The default 'Info.plist' file of the host app. The developer can change this location in Xcode.
  File get defaultHostInfoPlist => hostAppRoot.childDirectory(_hostAppBundleName).childFile('Info.plist');
372

373 374 375
  @override
  Directory get symlinks => _flutterLibRoot.childDirectory('.symlinks');

376
  @override
377
  Directory get xcodeProject => hostAppRoot.childDirectory('$_hostAppBundleName.xcodeproj');
378

379
  @override
380 381
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

382
  @override
383
  Directory get xcodeWorkspace => hostAppRoot.childDirectory('$_hostAppBundleName.xcworkspace');
384 385 386 387 388 389 390

  /// 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');

391
  @override
392 393 394 395
  bool existsSync()  {
    return parent.isModule || _editableDirectory.existsSync();
  }

396 397
  /// The product bundle identifier of the host app, or null if not set or if
  /// iOS tooling needed to read it is not installed.
398
  Future<String> get productBundleIdentifier async {
399
    String fromPlist;
400 401 402 403 404
    final File defaultInfoPlist = defaultHostInfoPlist;
    // Users can change the location of the Info.plist.
    // Try parsing the default, first.
    if (defaultInfoPlist.existsSync()) {
      try {
405
        fromPlist = globals.plistParser.getValueFromFile(
406 407 408 409 410 411
          defaultHostInfoPlist.path,
          PlistParser.kCFBundleIdentifierKey,
        );
      } on FileNotFoundException {
        // iOS tooling not found; likely not running OSX; let [fromPlist] be null
      }
412
      if (fromPlist != null && !fromPlist.contains(r'$')) {
413 414 415
        // Info.plist has no build variables in product bundle ID.
        return fromPlist;
      }
416
    }
417 418 419 420 421 422 423
    final Map<String, String> allBuildSettings = await buildSettings;
    if (allBuildSettings != null) {
      if (fromPlist != null) {
        // Perform variable substitution using build settings.
        return xcode.substituteXcodeVariables(fromPlist, allBuildSettings);
      }
      return allBuildSettings['PRODUCT_BUNDLE_IDENTIFIER'];
424
    }
425 426 427 428 429 430 431

    // 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.
432
    final String fromPbxproj = _firstMatchInFile(xcodeProjectInfoFile, _productBundleIdPattern)?.group(2);
433 434 435
    if (fromPbxproj != null && (fromPlist == null || fromPlist == _productBundleIdVariable)) {
      return fromPbxproj;
    }
436

437
    return null;
438 439 440
  }

  /// The build settings for the host app of this project, as a detached map.
441 442
  ///
  /// Returns null, if iOS tooling is unavailable.
443
  Future<Map<String, String>> get buildSettings async {
444
    if (!xcode.xcodeProjectInterpreter.isInstalled) {
445
      return null;
446
    }
447 448
    Map<String, String> buildSettings = _buildSettings;
    buildSettings ??= await xcode.xcodeProjectInterpreter.getBuildSettings(
449
      xcodeProject.path,
450
      _hostAppBundleName,
451
    );
452 453 454 455 456 457
    if (buildSettings != null && buildSettings.isNotEmpty) {
      // No timeouts, flakes, or errors.
      _buildSettings = buildSettings;
      return buildSettings;
    }
    return null;
458
  }
459

460 461
  Map<String, String> _buildSettings;

462
  Future<void> ensureReadyForPlatformSpecificTooling() async {
463
    _regenerateFromTemplateIfNeeded();
464
    if (!_flutterLibRoot.existsSync()) {
465
      return;
466
    }
467 468 469 470
    await _updateGeneratedXcodeConfigIfNeeded();
  }

  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
471
    if (globals.cache.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
472 473 474 475 476 477
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        targetOverride: bundle.defaultMainPath,
      );
    }
478 479
  }

480
  void _regenerateFromTemplateIfNeeded() {
481
    if (!isModule) {
482
      return;
483
    }
484
    final bool pubspecChanged = globals.fsUtils.isOlderThanReference(
485 486 487
      entity: ephemeralDirectory,
      referenceFile: parent.pubspecFile,
    );
488
    final bool toolingChanged = globals.cache.isOlderThanToolsStamp(ephemeralDirectory);
489
    if (!pubspecChanged && !toolingChanged) {
490
      return;
491
    }
492

493
    _deleteIfExistsSync(ephemeralDirectory);
494 495 496 497
    _overwriteFromTemplate(
      globals.fs.path.join('module', 'ios', 'library'),
      ephemeralDirectory,
    );
498 499
    // Add ephemeral host app, if a editable host app does not already exist.
    if (!_editableDirectory.existsSync()) {
500 501 502 503
      _overwriteFromTemplate(
        globals.fs.path.join('module', 'ios', 'host_app_ephemeral'),
        ephemeralDirectory,
      );
504
      if (hasPlugins(parent)) {
505 506 507 508
        _overwriteFromTemplate(
          globals.fs.path.join('module', 'ios', 'host_app_ephemeral_cocoapods'),
          ephemeralDirectory,
        );
509
      }
510 511 512 513 514 515 516 517 518 519 520 521
      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,
522
        platform: TargetPlatform.ios,
523 524 525 526 527 528 529 530
        mode: mode,
      )
    );
    if (framework.existsSync()) {
      final Directory engineDest = ephemeralDirectory
          .childDirectory('Flutter')
          .childDirectory('engine');
      final File podspec = framework.parent.childFile('Flutter.podspec');
531
      globals.fsUtils.copyDirectorySync(
532 533 534 535
        framework,
        engineDest.childDirectory('Flutter.framework'),
      );
      podspec.copySync(engineDest.childFile('Flutter.podspec').path);
536
    }
537 538
  }

539
  Future<void> makeHostAppEditable() async {
540
    assert(isModule);
541
    if (_editableDirectory.existsSync()) {
542
      throwToolExit('iOS host app is already editable. To start fresh, delete the ios/ folder.');
543
    }
544
    _deleteIfExistsSync(ephemeralDirectory);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    _overwriteFromTemplate(
      globals.fs.path.join('module', 'ios', 'library'),
      ephemeralDirectory,
    );
    _overwriteFromTemplate(
      globals.fs.path.join('module', 'ios', 'host_app_ephemeral'),
      _editableDirectory,
    );
    _overwriteFromTemplate(
      globals.fs.path.join('module', 'ios', 'host_app_ephemeral_cocoapods'),
      _editableDirectory,
    );
    _overwriteFromTemplate(
      globals.fs.path.join('module', 'ios', 'host_app_editable_cocoapods'),
      _editableDirectory,
    );
561 562
    await _updateGeneratedXcodeConfigIfNeeded();
    await injectPlugins(parent);
563
  }
564

565
  @override
566 567 568
  File get generatedXcodePropertiesFile => _flutterLibRoot
    .childDirectory('Flutter')
    .childFile('Generated.xcconfig');
569

570 571 572 573
  Directory get compiledDartFramework => _flutterLibRoot
      .childDirectory('Flutter')
      .childDirectory('App.framework');

574
  Directory get pluginRegistrantHost {
575
    return isModule
576 577 578
        ? _flutterLibRoot
            .childDirectory('Flutter')
            .childDirectory('FlutterPluginRegistrant')
579
        : hostAppRoot.childDirectory(_hostAppBundleName);
580 581 582
  }

  void _overwriteFromTemplate(String path, Directory target) {
583
    final Template template = Template.fromName(path);
584 585 586 587
    template.render(
      target,
      <String, dynamic>{
        'projectName': parent.manifest.appName,
588
        'iosIdentifier': parent.manifest.iosBundleIdentifier,
589 590 591 592
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
593
  }
594 595
}

596 597 598
/// Represents the Android sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `android/` sub-folder of
599
/// Flutter applications and the `.android/` sub-folder of Flutter module projects.
600
class AndroidProject extends FlutterProjectPlatform {
601 602 603 604 605
  AndroidProject._(this.parent);

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

606 607 608
  @override
  String get pluginConfigKey => AndroidPlugin.kConfigKey;

609
  static final RegExp _applicationIdPattern = RegExp('^\\s*applicationId\\s+[\'"](.*)[\'"]\\s*\$');
610
  static final RegExp _kotlinPluginPattern = RegExp('^\\s*apply plugin\\:\\s+[\'"]kotlin-android[\'"]\\s*\$');
611
  static final RegExp _groupPattern = RegExp('^\\s*group\\s+[\'"](.*)[\'"]\\s*\$');
612

613 614 615 616
  /// 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 {
617
    if (!isModule || _editableHostAppDirectory.existsSync()) {
618
      return _editableHostAppDirectory;
619
    }
620
    return ephemeralDirectory;
621 622 623 624
  }

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

628
  Directory get ephemeralDirectory => parent.directory.childDirectory('.android');
629
  Directory get _editableHostAppDirectory => parent.directory.childDirectory('android');
630

631 632
  /// True if the parent Flutter project is a module.
  bool get isModule => parent.isModule;
633

634 635 636
  /// True if the Flutter project is using the AndroidX support library
  bool get usesAndroidX => parent.usesAndroidX;

637 638 639 640 641 642
  /// 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;
  }

643
  File get appManifestFile {
644
    return isUsingGradle
645
        ? globals.fs.file(globals.fs.path.join(hostAppGradleRoot.path, 'app', 'src', 'main', 'AndroidManifest.xml'))
646
        : hostAppGradleRoot.childFile('AndroidManifest.xml');
647 648
  }

649
  File get gradleAppOutV1File => gradleAppOutV1Directory.childFile('app-debug.apk');
650 651

  Directory get gradleAppOutV1Directory {
652
    return globals.fs.directory(globals.fs.path.join(hostAppGradleRoot.path, 'app', 'build', 'outputs', 'apk'));
653 654
  }

655
  /// Whether the current flutter project has an Android sub-project.
656
  @override
657 658 659 660
  bool existsSync() {
    return parent.isModule || _editableHostAppDirectory.existsSync();
  }

661
  bool get isUsingGradle {
662
    return hostAppGradleRoot.childFile('build.gradle').existsSync();
663
  }
664

665
  String get applicationId {
666
    final File gradleFile = hostAppGradleRoot.childDirectory('app').childFile('build.gradle');
667
    return _firstMatchInFile(gradleFile, _applicationIdPattern)?.group(1);
668 669
  }

670
  String get group {
671
    final File gradleFile = hostAppGradleRoot.childFile('build.gradle');
672
    return _firstMatchInFile(gradleFile, _groupPattern)?.group(1);
673
  }
674

675 676 677 678 679
  /// The build directory where the Android artifacts are placed.
  Directory get buildDirectory {
    return parent.directory.childDirectory('build');
  }

680
  Future<void> ensureReadyForPlatformSpecificTooling() async {
681
    if (isModule && _shouldRegenerateFromTemplate()) {
682
      _regenerateLibrary();
683 684
      // Add ephemeral host app, if an editable host app does not already exist.
      if (!_editableHostAppDirectory.existsSync()) {
685 686
        _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'host_app_common'), ephemeralDirectory);
        _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'host_app_ephemeral'), ephemeralDirectory);
687
      }
688
    }
689
    if (!hostAppGradleRoot.existsSync()) {
690
      return;
691 692
    }
    gradle.updateLocalProperties(project: parent, requireAndroidSdk: false);
693 694
  }

695
  bool _shouldRegenerateFromTemplate() {
696
    return globals.fsUtils.isOlderThanReference(
697 698 699
      entity: ephemeralDirectory,
      referenceFile: parent.pubspecFile,
    ) || globals.cache.isOlderThanToolsStamp(ephemeralDirectory);
700
  }
701

702
  Future<void> makeHostAppEditable() async {
703
    assert(isModule);
704
    if (_editableHostAppDirectory.existsSync()) {
705
      throwToolExit('Android host app is already editable. To start fresh, delete the android/ folder.');
706
    }
707
    _regenerateLibrary();
708 709 710
    _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'host_app_common'), _editableHostAppDirectory);
    _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'host_app_editable'), _editableHostAppDirectory);
    _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'gradle'), _editableHostAppDirectory);
711
    gradle.gradleUtils.injectGradleWrapperIfNeeded(_editableHostAppDirectory);
712
    gradle.writeLocalProperties(_editableHostAppDirectory.childFile('local.properties'));
713 714 715 716 717
    await injectPlugins(parent);
  }

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

718
  Directory get pluginRegistrantHost => _flutterLibGradleRoot.childDirectory(isModule ? 'Flutter' : 'app');
719 720

  void _regenerateLibrary() {
721
    _deleteIfExistsSync(ephemeralDirectory);
722
    _overwriteFromTemplate(globals.fs.path.join(
723 724
      'module',
      'android',
725
      featureFlags.isAndroidEmbeddingV2Enabled ? 'library_new_embedding' : 'library',
726
    ), ephemeralDirectory);
727
    _overwriteFromTemplate(globals.fs.path.join('module', 'android', 'gradle'), ephemeralDirectory);
728
    gradle.gradleUtils.injectGradleWrapperIfNeeded(ephemeralDirectory);
729
  }
730

731
  void _overwriteFromTemplate(String path, Directory target) {
732
    final Template template = Template.fromName(path);
733 734 735 736 737
    template.render(
      target,
      <String, dynamic>{
        'projectName': parent.manifest.appName,
        'androidIdentifier': parent.manifest.androidPackage,
738
        'androidX': usesAndroidX,
739
        'useAndroidEmbeddingV2': featureFlags.isAndroidEmbeddingV2Enabled,
740 741 742 743 744
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
  }
745 746

  AndroidEmbeddingVersion getEmbeddingVersion() {
747 748 749 750 751
    if (isModule) {
      // A module type's Android project is used in add-to-app scenarios and
      // only supports the V2 embedding.
      return AndroidEmbeddingVersion.v2;
    }
752 753 754 755 756 757 758 759 760 761 762 763 764
    if (appManifestFile == null || !appManifestFile.existsSync()) {
      return AndroidEmbeddingVersion.v1;
    }
    xml.XmlDocument document;
    try {
      document = xml.parse(appManifestFile.readAsStringSync());
    } on xml.XmlParserException {
      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.');
    }
765
    for (final xml.XmlElement metaData in document.findAllElements('meta-data')) {
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
      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,
787 788
}

789
/// Represents the web sub-project of a Flutter project.
790
class WebProject extends FlutterProjectPlatform {
791 792 793 794
  WebProject._(this.parent);

  final FlutterProject parent;

795 796 797
  @override
  String get pluginConfigKey => WebPlugin.kConfigKey;

798
  /// Whether this flutter project has a web sub-project.
799
  @override
800
  bool existsSync() {
801 802
    return parent.directory.childDirectory('web').existsSync()
      && indexFile.existsSync();
803
  }
804

805 806 807
  /// The 'lib' directory for the application.
  Directory get libDirectory => parent.directory.childDirectory('lib');

808 809 810
  /// The directory containing additional files for the application.
  Directory get directory => parent.directory.childDirectory('web');

811
  /// The html file used to host the flutter web application.
812 813 814
  File get indexFile => parent.directory
      .childDirectory('web')
      .childFile('index.html');
815

816
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
817 818
}

819 820
/// Deletes [directory] with all content.
void _deleteIfExistsSync(Directory directory) {
821
  if (directory.existsSync()) {
822
    directory.deleteSync(recursive: true);
823
  }
824 825 826 827
}


/// Returns the first line-based match for [regExp] in [file].
828 829
///
/// Assumes UTF8 encoding.
830 831
Match _firstMatchInFile(File file, RegExp regExp) {
  if (!file.existsSync()) {
832 833
    return null;
  }
834
  for (final String line in file.readAsLinesSync()) {
835 836 837 838 839 840
    final Match match = regExp.firstMatch(line);
    if (match != null) {
      return match;
    }
  }
  return null;
841
}
842 843

/// The macOS sub project.
844
class MacOSProject extends FlutterProjectPlatform implements XcodeBasedProject {
845
  MacOSProject._(this.parent);
846

847 848
  @override
  final FlutterProject parent;
849

850 851 852
  @override
  String get pluginConfigKey => MacOSPlugin.kConfigKey;

853
  static const String _hostAppBundleName = 'Runner';
854

855
  @override
856
  bool existsSync() => _macOSDirectory.existsSync();
857

858
  Directory get _macOSDirectory => parent.directory.childDirectory('macos');
859

860 861 862 863 864 865 866 867 868
  /// 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');
869

870 871 872 873 874 875 876 877
  /// 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');

878
  @override
879 880
  File get generatedXcodePropertiesFile => ephemeralDirectory.childFile('Flutter-Generated.xcconfig');

881
  @override
882
  File xcodeConfigFor(String mode) => managedDirectory.childFile('Flutter-$mode.xcconfig');
883

884
  @override
885
  File get generatedEnvironmentVariableExportScript => ephemeralDirectory.childFile('flutter_export_environment.sh');
886

887 888 889 890 891 892 893 894 895 896
  @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
897
  Directory get xcodeProject => _macOSDirectory.childDirectory('$_hostAppBundleName.xcodeproj');
898

899 900 901 902
  @override
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

  @override
903
  Directory get xcodeWorkspace => _macOSDirectory.childDirectory('$_hostAppBundleName.xcworkspace');
904

905 906 907
  @override
  Directory get symlinks => ephemeralDirectory.childDirectory('.symlinks');

908 909
  /// The file where the Xcode build will write the name of the built app.
  ///
Chris Bracken's avatar
Chris Bracken committed
910
  /// Ideally this will be replaced in the future with inspection of the Runner
911
  /// scheme's target.
912
  File get nameFile => ephemeralDirectory.childFile('.app_filename');
913 914 915 916 917 918 919

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

  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
920
    if (globals.cache.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
921 922 923 924 925 926 927 928
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        useMacOSConfig: true,
        setSymroot: false,
      );
    }
  }
929 930 931
}

/// The Windows sub project
932
class WindowsProject extends FlutterProjectPlatform {
933 934 935 936
  WindowsProject._(this.project);

  final FlutterProject project;

937 938 939 940
  @override
  String get pluginConfigKey => WindowsPlugin.kConfigKey;

  @override
941
  bool existsSync() => _editableDirectory.existsSync();
942

943 944
  Directory get _editableDirectory => project.directory.childDirectory('windows');

945 946 947 948 949 950 951 952 953
  /// 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');
954

955 956
  /// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
  /// the build.
957
  File get generatedPropertySheetFile => ephemeralDirectory.childFile('Generated.props');
958

959 960 961
  /// Contains configuration to add plugins to the build.
  File get generatedPluginPropertySheetFile => managedDirectory.childFile('GeneratedPlugins.props');

962 963
  // The MSBuild project file.
  File get vcprojFile => _editableDirectory.childFile('Runner.vcxproj');
964

965 966 967
  // The MSBuild solution file.
  File get solutionFile => _editableDirectory.childFile('Runner.sln');

968 969 970
  /// The file where the VS build will write the name of the built app.
  ///
  /// Ideally this will be replaced in the future with inspection of the project.
971
  File get nameFile => ephemeralDirectory.childFile('exe_filename');
972

973 974 975
  /// The directory to write plugin symlinks.
  Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');

976
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
977 978 979
}

/// The Linux sub project.
980
class LinuxProject extends FlutterProjectPlatform {
981 982 983 984
  LinuxProject._(this.project);

  final FlutterProject project;

985 986 987
  @override
  String get pluginConfigKey => LinuxPlugin.kConfigKey;

988
  Directory get _editableDirectory => project.directory.childDirectory('linux');
989

990 991 992 993
  /// 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');
994

995 996 997 998 999
  /// 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');

1000
  @override
1001
  bool existsSync() => _editableDirectory.existsSync();
1002

1003
  /// The Linux project makefile.
1004 1005 1006 1007 1008
  File get makeFile => _editableDirectory.childFile('Makefile');

  /// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
  /// the build.
  File get generatedMakeConfigFile => ephemeralDirectory.childFile('generated_config.mk');
1009

1010 1011 1012
  /// Makefile with rules and variables for plugin builds.
  File get generatedPluginMakeFile => managedDirectory.childFile('generated_plugins.mk');

1013 1014 1015
  /// The directory to write plugin symlinks.
  Directory get pluginSymlinkDirectory => ephemeralDirectory.childDirectory('.plugin_symlinks');

1016
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
1017
}
1018

1019
/// The Fuchsia sub project
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
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');
}