project.dart 31.1 KB
Newer Older
1 2 3 4 5
// Copyright 2018 The Chromium Authors. All rights reserved.
// 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:yaml/yaml.dart';
9

10
import 'android/gradle.dart' as gradle;
11
import 'base/common.dart';
12
import 'base/context.dart';
13
import 'base/file_system.dart';
14
import 'build_info.dart';
15
import 'bundle.dart' as bundle;
16
import 'cache.dart';
17
import 'features.dart';
18
import 'flutter_manifest.dart';
19
import 'globals.dart';
20
import 'ios/plist_parser.dart';
21
import 'ios/xcodeproj.dart' as xcode;
22
import 'plugins.dart';
23
import 'template.dart';
24

25
FlutterProjectFactory get projectFactory => context.get<FlutterProjectFactory>() ?? FlutterProjectFactory();
26 27

class FlutterProjectFactory {
28 29 30 31
  FlutterProjectFactory();

  final Map<String, FlutterProject> _projects =
      <String, FlutterProject>{};
32 33 34 35 36

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

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

67 68 69
  /// 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);
70

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

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

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

82
  /// The manifest of this project.
83 84
  final FlutterManifest manifest;

85
  /// The manifest of the example sub-project of this project.
86
  final FlutterManifest _exampleManifest;
87

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

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

  /// The iOS sub project of this project.
112 113
  IosProject _ios;
  IosProject get ios => _ios ??= IosProject.fromFlutter(this);
114 115

  /// The Android sub project of this project.
116 117
  AndroidProject _android;
  AndroidProject get android => _android ??= AndroidProject._(this);
118

119
  /// The web sub project of this project.
120 121
  WebProject _web;
  WebProject get web => _web ??= WebProject._(this);
122

123 124 125
  /// The MacOS sub project of this project.
  MacOSProject _macos;
  MacOSProject get macos => _macos ??= MacOSProject._(this);
126

127 128 129
  /// The Linux sub project of this project.
  LinuxProject _linux;
  LinuxProject get linux => _linux ??= LinuxProject._(this);
130

131 132 133 134 135 136 137
  /// 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);
138

139 140 141 142 143 144 145
  /// 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.
146 147
  File get flutterPluginsFile => directory.childFile('.flutter-plugins');

148 149 150
  /// The `.dart-tool` directory of this project.
  Directory get dartTool => directory.childDirectory('.dart_tool');

151 152
  /// The directory containing the generated code for this project.
  Directory get generated => directory
153
    .absolute
154 155 156 157 158
    .childDirectory('.dart_tool')
    .childDirectory('build')
    .childDirectory('generated')
    .childDirectory(manifest.appName);

159
  /// The example sub-project of this project.
160
  FlutterProject get example => FlutterProject(
161 162 163 164 165
    _exampleDirectory(directory),
    _exampleManifest,
    FlutterManifest.empty(),
  );

166 167
  /// True if this project is a Flutter module project.
  bool get isModule => manifest.isModule;
168

169 170 171
  /// True if the Flutter project is using the AndroidX support library
  bool get usesAndroidX => manifest.usesAndroidX;

172
  /// True if this project has an example application.
173
  bool get hasExampleApp => _exampleDirectory(directory).existsSync();
174 175

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

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

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

  /// Return the set of builders used by this package.
229 230 231 232 233
  YamlMap get builders {
    if (!pubspecFile.existsSync()) {
      return null;
    }
    final YamlMap pubspec = loadYaml(pubspecFile.readAsStringSync());
234 235 236 237
    // If the pubspec file is empty, this will be null.
    if (pubspec == null) {
      return null;
    }
238
    return pubspec['builders'];
239
  }
240 241

  /// Whether there are any builders used by this package.
242 243
  bool get hasBuilders {
    final YamlMap result = builders;
244 245
    return result != null && result.isNotEmpty;
  }
246 247
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/// 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);

274 275 276 277 278 279
  /// 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;

280 281 282 283 284 285 286 287 288 289
  /// The CocoaPods 'Podfile'.
  File get podfile;

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

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

  /// True if the host app project is using Swift.
290
  Future<bool> get isSwift;
291 292 293

  /// Directory containing symlinks to pub cache plugins source generated on `pod install`.
  Directory get symlinks;
294 295
}

296 297 298
/// Represents the iOS sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `ios/` sub-folder of
299
/// Flutter applications and the `.ios/` sub-folder of Flutter module projects.
300
class IosProject implements XcodeBasedProject {
301
  IosProject.fromFlutter(this.parent);
302

303
  @override
304 305
  final FlutterProject parent;

306
  static final RegExp _productBundleIdPattern = RegExp(r'''^\s*PRODUCT_BUNDLE_IDENTIFIER\s*=\s*(["']?)(.*?)\1;\s*$''');
307 308 309
  static const String _productBundleIdVariable = r'$(PRODUCT_BUNDLE_IDENTIFIER)';
  static const String _hostAppBundleName = 'Runner';

310
  Directory get ephemeralDirectory => parent.directory.childDirectory('.ios');
311 312 313 314
  Directory get _editableDirectory => parent.directory.childDirectory('ios');

  /// This parent folder of `Runner.xcodeproj`.
  Directory get hostAppRoot {
315
    if (!isModule || _editableDirectory.existsSync()) {
316
      return _editableDirectory;
317
    }
318
    return ephemeralDirectory;
319 320 321 322 323 324 325
  }

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

329
  /// The bundle name of the host app, `Runner.app`.
330 331
  String get hostAppBundleName => '$_hostAppBundleName.app';

332 333
  /// True, if the parent Flutter project is a module project.
  bool get isModule => parent.isModule;
334

335 336 337
  /// Whether the flutter application has an iOS project.
  bool get exists => hostAppRoot.existsSync();

338
  @override
339
  File xcodeConfigFor(String mode) => _flutterLibRoot.childDirectory('Flutter').childFile('$mode.xcconfig');
340

341 342 343
  @override
  File get generatedEnvironmentVariableExportScript => _flutterLibRoot.childDirectory('Flutter').childFile('flutter_export_environment.sh');

344
  @override
345
  File get podfile => hostAppRoot.childFile('Podfile');
346

347
  @override
348
  File get podfileLock => hostAppRoot.childFile('Podfile.lock');
349

350
  @override
351
  File get podManifestLock => hostAppRoot.childDirectory('Pods').childFile('Manifest.lock');
352

353
  /// The 'Info.plist' file of the host app.
354
  File get hostInfoPlist => hostAppRoot.childDirectory(_hostAppBundleName).childFile('Info.plist');
355

356 357 358
  @override
  Directory get symlinks => _flutterLibRoot.childDirectory('.symlinks');

359
  @override
360
  Directory get xcodeProject => hostAppRoot.childDirectory('$_hostAppBundleName.xcodeproj');
361

362
  @override
363 364
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

365
  @override
366
  Directory get xcodeWorkspace => hostAppRoot.childDirectory('$_hostAppBundleName.xcworkspace');
367 368 369 370 371 372 373

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

374
  @override
375 376 377 378
  bool existsSync()  {
    return parent.isModule || _editableDirectory.existsSync();
  }

379 380
  /// The product bundle identifier of the host app, or null if not set or if
  /// iOS tooling needed to read it is not installed.
381
  Future<String> get productBundleIdentifier async {
382 383 384 385 386 387 388 389 390
    String fromPlist;
    try {
      fromPlist = PlistParser.instance.getValueFromFile(
        hostInfoPlist.path,
        PlistParser.kCFBundleIdentifierKey,
      );
    } on FileNotFoundException {
      // iOS tooling not found; likely not running OSX; let [fromPlist] be null
    }
391 392 393 394
    if (fromPlist != null && !fromPlist.contains('\$')) {
      // Info.plist has no build variables in product bundle ID.
      return fromPlist;
    }
395
    final String fromPbxproj = _firstMatchInFile(xcodeProjectInfoFile, _productBundleIdPattern)?.group(2);
396 397 398 399 400 401
    if (fromPbxproj != null && (fromPlist == null || fromPlist == _productBundleIdVariable)) {
      // Common case. Avoids parsing build settings.
      return fromPbxproj;
    }
    if (fromPlist != null && xcode.xcodeProjectInterpreter.isInstalled) {
      // General case: perform variable substitution using build settings.
402
      return xcode.substituteXcodeVariables(fromPlist, await buildSettings);
403 404
    }
    return null;
405 406
  }

407
  @override
408 409
  Future<bool> get isSwift async =>
    (await buildSettings)?.containsKey('SWIFT_VERSION') ?? false;
410 411

  /// The build settings for the host app of this project, as a detached map.
412 413
  ///
  /// Returns null, if iOS tooling is unavailable.
414
  Future<Map<String, String>> get buildSettings async {
415
    if (!xcode.xcodeProjectInterpreter.isInstalled) {
416
      return null;
417
    }
418 419 420 421
    _buildSettings ??= await xcode.xcodeProjectInterpreter.getBuildSettings(
      xcodeProject.path,
      _hostAppBundleName,
    );
422
    return _buildSettings;
423
  }
424

425 426
  Map<String, String> _buildSettings;

427
  Future<void> ensureReadyForPlatformSpecificTooling() async {
428
    _regenerateFromTemplateIfNeeded();
429
    if (!_flutterLibRoot.existsSync()) {
430
      return;
431
    }
432 433 434 435
    await _updateGeneratedXcodeConfigIfNeeded();
  }

  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
436
    if (Cache.instance.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
437 438 439 440 441 442
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        targetOverride: bundle.defaultMainPath,
      );
    }
443 444
  }

445
  void _regenerateFromTemplateIfNeeded() {
446
    if (!isModule) {
447
      return;
448
    }
449 450
    final bool pubspecChanged = isOlderThanReference(entity: ephemeralDirectory, referenceFile: parent.pubspecFile);
    final bool toolingChanged = Cache.instance.isOlderThanToolsStamp(ephemeralDirectory);
451
    if (!pubspecChanged && !toolingChanged) {
452
      return;
453
    }
454 455
    _deleteIfExistsSync(ephemeralDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'library'), ephemeralDirectory);
456 457
    // Add ephemeral host app, if a editable host app does not already exist.
    if (!_editableDirectory.existsSync()) {
458
      _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_ephemeral'), ephemeralDirectory);
459
      if (hasPlugins(parent)) {
460
        _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_ephemeral_cocoapods'), ephemeralDirectory);
461
      }
462
    }
463 464
  }

465
  Future<void> makeHostAppEditable() async {
466
    assert(isModule);
467
    if (_editableDirectory.existsSync()) {
468
      throwToolExit('iOS host app is already editable. To start fresh, delete the ios/ folder.');
469
    }
470 471
    _deleteIfExistsSync(ephemeralDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'library'), ephemeralDirectory);
472 473 474
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_ephemeral'), _editableDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_ephemeral_cocoapods'), _editableDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_editable_cocoapods'), _editableDirectory);
475 476
    await _updateGeneratedXcodeConfigIfNeeded();
    await injectPlugins(parent);
477
  }
478

479
  @override
480
  File get generatedXcodePropertiesFile => _flutterLibRoot.childDirectory('Flutter').childFile('Generated.xcconfig');
481 482

  Directory get pluginRegistrantHost {
483
    return isModule
484 485
        ? _flutterLibRoot.childDirectory('Flutter').childDirectory('FlutterPluginRegistrant')
        : hostAppRoot.childDirectory(_hostAppBundleName);
486 487 488
  }

  void _overwriteFromTemplate(String path, Directory target) {
489
    final Template template = Template.fromName(path);
490 491 492 493
    template.render(
      target,
      <String, dynamic>{
        'projectName': parent.manifest.appName,
494
        'iosIdentifier': parent.manifest.iosBundleIdentifier,
495 496 497 498
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
499
  }
500 501
}

502 503 504
/// Represents the Android sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `android/` sub-folder of
505
/// Flutter applications and the `.android/` sub-folder of Flutter module projects.
506
class AndroidProject {
507 508 509 510 511
  AndroidProject._(this.parent);

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

512
  static final RegExp _applicationIdPattern = RegExp('^\\s*applicationId\\s+[\'\"](.*)[\'\"]\\s*\$');
513
  static final RegExp _kotlinPluginPattern = RegExp('^\\s*apply plugin\:\\s+[\'\"]kotlin-android[\'\"]\\s*\$');
514 515
  static final RegExp _groupPattern = RegExp('^\\s*group\\s+[\'\"](.*)[\'\"]\\s*\$');

516 517 518 519
  /// 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 {
520
    if (!isModule || _editableHostAppDirectory.existsSync()) {
521
      return _editableHostAppDirectory;
522
    }
523
    return ephemeralDirectory;
524 525 526 527
  }

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

531
  Directory get ephemeralDirectory => parent.directory.childDirectory('.android');
532
  Directory get _editableHostAppDirectory => parent.directory.childDirectory('android');
533

534 535
  /// True if the parent Flutter project is a module.
  bool get isModule => parent.isModule;
536

537 538 539
  /// True if the Flutter project is using the AndroidX support library
  bool get usesAndroidX => parent.usesAndroidX;

540 541 542 543 544 545
  /// 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;
  }

546
  File get appManifestFile {
547
    return isUsingGradle
548 549
        ? fs.file(fs.path.join(hostAppGradleRoot.path, 'app', 'src', 'main', 'AndroidManifest.xml'))
        : hostAppGradleRoot.childFile('AndroidManifest.xml');
550 551
  }

552
  File get gradleAppOutV1File => gradleAppOutV1Directory.childFile('app-debug.apk');
553 554

  Directory get gradleAppOutV1Directory {
555
    return fs.directory(fs.path.join(hostAppGradleRoot.path, 'app', 'build', 'outputs', 'apk'));
556 557
  }

558 559 560 561 562
  /// Whether the current flutter project has an Android sub-project.
  bool existsSync() {
    return parent.isModule || _editableHostAppDirectory.existsSync();
  }

563
  bool get isUsingGradle {
564
    return hostAppGradleRoot.childFile('build.gradle').existsSync();
565
  }
566

567
  String get applicationId {
568
    final File gradleFile = hostAppGradleRoot.childDirectory('app').childFile('build.gradle');
569
    return _firstMatchInFile(gradleFile, _applicationIdPattern)?.group(1);
570 571
  }

572
  String get group {
573
    final File gradleFile = hostAppGradleRoot.childFile('build.gradle');
574
    return _firstMatchInFile(gradleFile, _groupPattern)?.group(1);
575
  }
576

577
  Future<void> ensureReadyForPlatformSpecificTooling() async {
578
    if (isModule && _shouldRegenerateFromTemplate()) {
579
      _regenerateLibrary();
580 581
      // Add ephemeral host app, if an editable host app does not already exist.
      if (!_editableHostAppDirectory.existsSync()) {
582 583
        _overwriteFromTemplate(fs.path.join('module', 'android', 'host_app_common'), ephemeralDirectory);
        _overwriteFromTemplate(fs.path.join('module', 'android', 'host_app_ephemeral'), ephemeralDirectory);
584
      }
585
    }
586
    if (!hostAppGradleRoot.existsSync()) {
587
      return;
588 589
    }
    gradle.updateLocalProperties(project: parent, requireAndroidSdk: false);
590 591
  }

592
  bool _shouldRegenerateFromTemplate() {
593 594
    return isOlderThanReference(entity: ephemeralDirectory, referenceFile: parent.pubspecFile)
        || Cache.instance.isOlderThanToolsStamp(ephemeralDirectory);
595
  }
596

597
  Future<void> makeHostAppEditable() async {
598
    assert(isModule);
599
    if (_editableHostAppDirectory.existsSync()) {
600
      throwToolExit('Android host app is already editable. To start fresh, delete the android/ folder.');
601
    }
602
    _regenerateLibrary();
603 604 605
    _overwriteFromTemplate(fs.path.join('module', 'android', 'host_app_common'), _editableHostAppDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'android', 'host_app_editable'), _editableHostAppDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'android', 'gradle'), _editableHostAppDirectory);
606
    gradle.injectGradleWrapperIfNeeded(_editableHostAppDirectory);
607
    gradle.writeLocalProperties(_editableHostAppDirectory.childFile('local.properties'));
608 609 610 611 612
    await injectPlugins(parent);
  }

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

613
  Directory get pluginRegistrantHost => _flutterLibGradleRoot.childDirectory(isModule ? 'Flutter' : 'app');
614 615

  void _regenerateLibrary() {
616
    _deleteIfExistsSync(ephemeralDirectory);
617 618 619
    _overwriteFromTemplate(fs.path.join(
      'module',
      'android',
620
      featureFlags.isAndroidEmbeddingV2Enabled ? 'library_new_embedding' : 'library',
621
    ), ephemeralDirectory);
622
    _overwriteFromTemplate(fs.path.join('module', 'android', 'gradle'), ephemeralDirectory);
623
    gradle.injectGradleWrapperIfNeeded(ephemeralDirectory);
624
  }
625

626
  void _overwriteFromTemplate(String path, Directory target) {
627
    final Template template = Template.fromName(path);
628 629 630 631 632
    template.render(
      target,
      <String, dynamic>{
        'projectName': parent.manifest.appName,
        'androidIdentifier': parent.manifest.androidPackage,
633
        'androidX': usesAndroidX,
634
        'useAndroidEmbeddingV2': featureFlags.isAndroidEmbeddingV2Enabled,
635 636 637 638 639
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
  }
640 641
}

642 643 644 645 646 647
/// Represents the web sub-project of a Flutter project.
class WebProject {
  WebProject._(this.parent);

  final FlutterProject parent;

648 649
  /// Whether this flutter project has a web sub-project.
  bool existsSync() {
650 651
    return parent.directory.childDirectory('web').existsSync()
      && indexFile.existsSync();
652
  }
653

654 655 656
  /// The 'lib' directory for the application.
  Directory get libDirectory => parent.directory.childDirectory('lib');

657 658 659
  /// The directory containing additional files for the application.
  Directory get directory => parent.directory.childDirectory('web');

660
  /// The html file used to host the flutter web application.
661 662 663
  File get indexFile => parent.directory
      .childDirectory('web')
      .childFile('index.html');
664

665
  Future<void> ensureReadyForPlatformSpecificTooling() async {}
666 667
}

668 669
/// Deletes [directory] with all content.
void _deleteIfExistsSync(Directory directory) {
670
  if (directory.existsSync()) {
671
    directory.deleteSync(recursive: true);
672
  }
673 674 675 676
}


/// Returns the first line-based match for [regExp] in [file].
677 678
///
/// Assumes UTF8 encoding.
679 680
Match _firstMatchInFile(File file, RegExp regExp) {
  if (!file.existsSync()) {
681 682
    return null;
  }
683 684 685 686 687 688 689
  for (String line in file.readAsLinesSync()) {
    final Match match = regExp.firstMatch(line);
    if (match != null) {
      return match;
    }
  }
  return null;
690
}
691 692

/// The macOS sub project.
693 694
class MacOSProject implements XcodeBasedProject {
  MacOSProject._(this.parent);
695

696 697
  @override
  final FlutterProject parent;
698

699 700
  static const String _hostAppBundleName = 'Runner';

701
  @override
702
  bool existsSync() => _macOSDirectory.existsSync();
703

704
  Directory get _macOSDirectory => parent.directory.childDirectory('macos');
705

706 707 708 709 710 711 712 713 714
  /// 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');
715

716 717 718 719 720 721 722 723
  /// 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');

724
  @override
725 726
  File get generatedXcodePropertiesFile => ephemeralDirectory.childFile('Flutter-Generated.xcconfig');

727
  @override
728
  File xcodeConfigFor(String mode) => managedDirectory.childFile('Flutter-$mode.xcconfig');
729

730
  @override
731
  File get generatedEnvironmentVariableExportScript => ephemeralDirectory.childFile('flutter_export_environment.sh');
732

733 734 735 736 737 738 739 740 741 742
  @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
743
  Directory get xcodeProject => _macOSDirectory.childDirectory('$_hostAppBundleName.xcodeproj');
744

745 746 747 748
  @override
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

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

751 752 753
  @override
  Directory get symlinks => ephemeralDirectory.childDirectory('.symlinks');

754
  @override
755
  Future<bool> get isSwift async => true;
756

757 758
  /// The file where the Xcode build will write the name of the built app.
  ///
Chris Bracken's avatar
Chris Bracken committed
759
  /// Ideally this will be replaced in the future with inspection of the Runner
760
  /// scheme's target.
761
  File get nameFile => ephemeralDirectory.childFile('.app_filename');
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

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

  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
    if (Cache.instance.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        useMacOSConfig: true,
        setSymroot: false,
      );
    }
  }
778 779 780 781 782 783 784 785
}

/// The Windows sub project
class WindowsProject {
  WindowsProject._(this.project);

  final FlutterProject project;

786
  bool existsSync() => _editableDirectory.existsSync();
787

788 789
  Directory get _editableDirectory => project.directory.childDirectory('windows');

790 791 792 793 794 795 796 797 798
  /// 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');
799

800 801
  /// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
  /// the build.
802
  File get generatedPropertySheetFile => ephemeralDirectory.childFile('Generated.props');
803 804 805

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

807 808 809
  // The MSBuild solution file.
  File get solutionFile => _editableDirectory.childFile('Runner.sln');

810 811 812
  /// 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.
813
  File get nameFile => ephemeralDirectory.childFile('exe_filename');
814 815

  Future<void> ensureReadyForPlatformSpecificTooling() async {}
816 817 818 819 820 821 822 823
}

/// The Linux sub project.
class LinuxProject {
  LinuxProject._(this.project);

  final FlutterProject project;

824
  Directory get _editableDirectory => project.directory.childDirectory('linux');
825

826 827 828 829
  /// 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');
830

831 832 833 834 835 836
  /// 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');

  bool existsSync() => _editableDirectory.existsSync();
837

838
  /// The Linux project makefile.
839 840 841 842 843
  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');
844 845

  Future<void> ensureReadyForPlatformSpecificTooling() async {}
846
}
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863

/// The Fuchisa sub project
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');
}