project.dart 17.9 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 8
import 'package:meta/meta.dart';

9
import 'android/gradle.dart' as gradle;
10
import 'base/common.dart';
11
import 'base/file_system.dart';
12
import 'build_info.dart';
13
import 'bundle.dart' as bundle;
14 15
import 'cache.dart';
import 'flutter_manifest.dart';
16 17
import 'ios/ios_workflow.dart';
import 'ios/plist_utils.dart' as plist;
18
import 'ios/xcodeproj.dart' as xcode;
19
import 'plugins.dart';
20
import 'template.dart';
21 22

/// Represents the contents of a Flutter project at the specified [directory].
23
///
24 25 26 27 28 29 30
/// [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.
31
class FlutterProject {
32
  @visibleForTesting
33 34 35 36
  FlutterProject(this.directory, this.manifest, this._exampleManifest)
      : assert(directory != null),
        assert(manifest != null),
        assert(_exampleManifest != null);
37

38 39
  /// Returns a future that completes with a [FlutterProject] view of the given directory
  /// or a ToolExit error, if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
40
  static Future<FlutterProject> fromDirectory(Directory directory) async {
41 42
    assert(directory != null);
    final FlutterManifest manifest = await _readManifest(
43 44
      directory.childFile(bundle.defaultManifestPath).path,
    );
45 46
    final FlutterManifest exampleManifest = await _readManifest(
      _exampleDirectory(directory).childFile(bundle.defaultManifestPath).path,
47
    );
48
    return FlutterProject(directory, manifest, exampleManifest);
49
  }
50

51 52
  /// Returns a future that completes with a [FlutterProject] view of the current directory.
  /// or a ToolExit error, if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
53 54
  static Future<FlutterProject> current() => fromDirectory(fs.currentDirectory);

55 56
  /// Returns a future that completes with a [FlutterProject] view of the given directory.
  /// or a ToolExit error, if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
57
  static Future<FlutterProject> fromPath(String path) => fromDirectory(fs.directory(path));
58 59 60 61

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

62
  /// The manifest of this project.
63 64
  final FlutterManifest manifest;

65
  /// The manifest of the example sub-project of this project.
66
  final FlutterManifest _exampleManifest;
67

68
  /// The set of organization names found in this project as
69 70
  /// part of iOS product bundle identifier, Android application ID, or
  /// Gradle group ID.
71 72 73 74 75 76 77 78
  Set<String> get organizationNames {
    final List<String> candidates = <String>[
      ios.productBundleIdentifier,
      android.applicationId,
      android.group,
      example.android.applicationId,
      example.ios.productBundleIdentifier,
    ];
79
    return Set<String>.from(candidates
80
        .map<String>(_organizationNameFromPackageName)
81
        .where((String name) => name != null));
82 83 84 85 86 87 88 89 90 91
  }

  String _organizationNameFromPackageName(String packageName) {
    if (packageName != null && 0 <= packageName.lastIndexOf('.'))
      return packageName.substring(0, packageName.lastIndexOf('.'));
    else
      return null;
  }

  /// The iOS sub project of this project.
92
  IosProject get ios => IosProject._(this);
93 94

  /// The Android sub project of this project.
95
  AndroidProject get android => AndroidProject._(this);
96

97 98 99 100 101 102 103
  /// 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.
104 105 106
  File get flutterPluginsFile => directory.childFile('.flutter-plugins');

  /// The example sub-project of this project.
107
  FlutterProject get example => FlutterProject(
108 109 110 111 112
    _exampleDirectory(directory),
    _exampleManifest,
    FlutterManifest.empty(),
  );

113 114
  /// True if this project is a Flutter module project.
  bool get isModule => manifest.isModule;
115

116
  /// True if this project has an example application.
117
  bool get hasExampleApp => _exampleDirectory(directory).existsSync();
118 119

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

122 123 124 125 126 127 128 129 130 131 132 133
  /// 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.
  static Future<FlutterManifest> _readManifest(String path) async {
    final FlutterManifest manifest = await FlutterManifest.createFromPath(path);
    if (manifest == null)
      throwToolExit('Please correct the pubspec.yaml file at $path');
    return manifest;
  }

134
  /// Generates project files necessary to make Gradle builds work on Android
135
  /// and CocoaPods+Xcode work on iOS, for app and module projects only.
136
  Future<void> ensureReadyForPlatformSpecificTooling() async {
137
    if (!directory.existsSync() || hasExampleApp)
138
      return;
139
    refreshPluginsList(this);
140 141
    await android.ensureReadyForPlatformSpecificTooling();
    await ios.ensureReadyForPlatformSpecificTooling();
142
    await injectPlugins(this);
143
  }
144 145
}

146 147 148
/// Represents the iOS sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `ios/` sub-folder of
149
/// Flutter applications and the `.ios/` sub-folder of Flutter module projects.
150
class IosProject {
151 152 153 154 155
  IosProject._(this.parent);

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

156 157 158 159
  static final RegExp _productBundleIdPattern = RegExp(r'^\s*PRODUCT_BUNDLE_IDENTIFIER\s*=\s*(.*);\s*$');
  static const String _productBundleIdVariable = r'$(PRODUCT_BUNDLE_IDENTIFIER)';
  static const String _hostAppBundleName = 'Runner';

160 161 162 163 164
  Directory get _ephemeralDirectory => parent.directory.childDirectory('.ios');
  Directory get _editableDirectory => parent.directory.childDirectory('ios');

  /// This parent folder of `Runner.xcodeproj`.
  Directory get hostAppRoot {
165
    if (!isModule || _editableDirectory.existsSync())
166 167 168 169 170 171 172 173 174
      return _editableDirectory;
    return _ephemeralDirectory;
  }

  /// 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
175 176
  /// a Flutter module with an editable host app.
  Directory get _flutterLibRoot => isModule ? _ephemeralDirectory : _editableDirectory;
177

178
  /// The bundle name of the host app, `Runner.app`.
179 180
  String get hostAppBundleName => '$_hostAppBundleName.app';

181 182
  /// True, if the parent Flutter project is a module project.
  bool get isModule => parent.isModule;
183

184
  /// The xcode config file for [mode].
185
  File xcodeConfigFor(String mode) => _flutterLibRoot.childDirectory('Flutter').childFile('$mode.xcconfig');
186 187

  /// The 'Podfile'.
188
  File get podfile => hostAppRoot.childFile('Podfile');
189 190

  /// The 'Podfile.lock'.
191
  File get podfileLock => hostAppRoot.childFile('Podfile.lock');
192 193

  /// The 'Manifest.lock'.
194
  File get podManifestLock => hostAppRoot.childDirectory('Pods').childFile('Manifest.lock');
195

196
  /// The 'Info.plist' file of the host app.
197
  File get hostInfoPlist => hostAppRoot.childDirectory(_hostAppBundleName).childFile('Info.plist');
198

199
  /// '.xcodeproj' folder of the host app.
200
  Directory get xcodeProject => hostAppRoot.childDirectory('$_hostAppBundleName.xcodeproj');
201 202 203 204

  /// The '.pbxproj' file of the host app.
  File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

205
  /// Xcode workspace directory of the host app.
206
  Directory get xcodeWorkspace => hostAppRoot.childDirectory('$_hostAppBundleName.xcworkspace');
207 208 209 210 211 212 213

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

214 215
  /// The product bundle identifier of the host app, or null if not set or if
  /// iOS tooling needed to read it is not installed.
216
  String get productBundleIdentifier {
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    final String fromPlist = iosWorkflow.getPlistValueFromFile(
      hostInfoPlist.path,
      plist.kCFBundleIdentifierKey,
    );
    if (fromPlist != null && !fromPlist.contains('\$')) {
      // Info.plist has no build variables in product bundle ID.
      return fromPlist;
    }
    final String fromPbxproj = _firstMatchInFile(xcodeProjectInfoFile, _productBundleIdPattern)?.group(1);
    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.
      return xcode.substituteXcodeVariables(fromPlist, buildSettings);
    }
    return null;
235 236 237 238 239 240
  }

  /// True, if the host app project is using Swift.
  bool get isSwift => buildSettings?.containsKey('SWIFT_VERSION');

  /// The build settings for the host app of this project, as a detached map.
241 242
  ///
  /// Returns null, if iOS tooling is unavailable.
243
  Map<String, String> get buildSettings {
244 245
    if (!xcode.xcodeProjectInterpreter.isInstalled)
      return null;
246
    return xcode.xcodeProjectInterpreter.getBuildSettings(xcodeProject.path, _hostAppBundleName);
247
  }
248

249
  Future<void> ensureReadyForPlatformSpecificTooling() async {
250
    _regenerateFromTemplateIfNeeded();
251
    if (!_flutterLibRoot.existsSync())
252
      return;
253 254 255 256
    await _updateGeneratedXcodeConfigIfNeeded();
  }

  Future<void> _updateGeneratedXcodeConfigIfNeeded() async {
257
    if (Cache.instance.isOlderThanToolsStamp(generatedXcodePropertiesFile)) {
258 259 260 261 262 263
      await xcode.updateGeneratedXcodeProperties(
        project: parent,
        buildInfo: BuildInfo.debug,
        targetOverride: bundle.defaultMainPath,
      );
    }
264 265
  }

266
  void _regenerateFromTemplateIfNeeded() {
267
    if (!isModule)
268
      return;
269 270
    final bool pubspecChanged = isOlderThanReference(entity: _ephemeralDirectory, referenceFile: parent.pubspecFile);
    final bool toolingChanged = Cache.instance.isOlderThanToolsStamp(_ephemeralDirectory);
271 272
    if (!pubspecChanged && !toolingChanged)
      return;
273
    _deleteIfExistsSync(_ephemeralDirectory);
274
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'library'), _ephemeralDirectory);
275 276
    // Add ephemeral host app, if a editable host app does not already exist.
    if (!_editableDirectory.existsSync()) {
277
      _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_ephemeral'), _ephemeralDirectory);
278
      if (hasPlugins(parent)) {
279
        _overwriteFromTemplate(fs.path.join('module', 'ios', 'host_app_ephemeral_cocoapods'), _ephemeralDirectory);
280
      }
281
    }
282 283
  }

284
  Future<void> makeHostAppEditable() async {
285
    assert(isModule);
286 287 288
    if (_editableDirectory.existsSync())
      throwToolExit('iOS host app is already editable. To start fresh, delete the ios/ folder.');
    _deleteIfExistsSync(_ephemeralDirectory);
289 290 291 292
    _overwriteFromTemplate(fs.path.join('module', 'ios', 'library'), _ephemeralDirectory);
    _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);
293 294
    await _updateGeneratedXcodeConfigIfNeeded();
    await injectPlugins(parent);
295
  }
296

297
  File get generatedXcodePropertiesFile => _flutterLibRoot.childDirectory('Flutter').childFile('Generated.xcconfig');
298 299

  Directory get pluginRegistrantHost {
300
    return isModule
301 302
        ? _flutterLibRoot.childDirectory('Flutter').childDirectory('FlutterPluginRegistrant')
        : hostAppRoot.childDirectory(_hostAppBundleName);
303 304 305
  }

  void _overwriteFromTemplate(String path, Directory target) {
306
    final Template template = Template.fromName(path);
307 308 309 310 311 312 313 314 315
    template.render(
      target,
      <String, dynamic>{
        'projectName': parent.manifest.appName,
        'iosIdentifier': parent.manifest.iosBundleIdentifier
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
316
  }
317 318
}

319 320 321
/// Represents the Android sub-project of a Flutter project.
///
/// Instances will reflect the contents of the `android/` sub-folder of
322
/// Flutter applications and the `.android/` sub-folder of Flutter module projects.
323
class AndroidProject {
324 325 326 327 328
  AndroidProject._(this.parent);

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

329 330 331
  static final RegExp _applicationIdPattern = RegExp('^\\s*applicationId\\s+[\'\"](.*)[\'\"]\\s*\$');
  static final RegExp _groupPattern = RegExp('^\\s*group\\s+[\'\"](.*)[\'\"]\\s*\$');

332 333 334 335
  /// 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 {
336
    if (!isModule || _editableHostAppDirectory.existsSync())
337
      return _editableHostAppDirectory;
338 339 340 341 342
    return _ephemeralDirectory;
  }

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

  Directory get _ephemeralDirectory => parent.directory.childDirectory('.android');
347
  Directory get _editableHostAppDirectory => parent.directory.childDirectory('android');
348

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

352
  File get appManifestFile {
353
    return isUsingGradle
354 355
        ? fs.file(fs.path.join(hostAppGradleRoot.path, 'app', 'src', 'main', 'AndroidManifest.xml'))
        : hostAppGradleRoot.childFile('AndroidManifest.xml');
356 357
  }

358
  File get gradleAppOutV1File => gradleAppOutV1Directory.childFile('app-debug.apk');
359 360

  Directory get gradleAppOutV1Directory {
361
    return fs.directory(fs.path.join(hostAppGradleRoot.path, 'app', 'build', 'outputs', 'apk'));
362 363
  }

364
  bool get isUsingGradle {
365
    return hostAppGradleRoot.childFile('build.gradle').existsSync();
366
  }
367

368
  String get applicationId {
369
    final File gradleFile = hostAppGradleRoot.childDirectory('app').childFile('build.gradle');
370
    return _firstMatchInFile(gradleFile, _applicationIdPattern)?.group(1);
371 372
  }

373
  String get group {
374
    final File gradleFile = hostAppGradleRoot.childFile('build.gradle');
375
    return _firstMatchInFile(gradleFile, _groupPattern)?.group(1);
376
  }
377

378
  Future<void> ensureReadyForPlatformSpecificTooling() async {
379
    if (isModule && _shouldRegenerateFromTemplate()) {
380
      _regenerateLibrary();
381 382
      // Add ephemeral host app, if an editable host app does not already exist.
      if (!_editableHostAppDirectory.existsSync()) {
383 384
        _overwriteFromTemplate(fs.path.join('module', 'android', 'host_app_common'), _ephemeralDirectory);
        _overwriteFromTemplate(fs.path.join('module', 'android', 'host_app_ephemeral'), _ephemeralDirectory);
385
      }
386
    }
387
    if (!hostAppGradleRoot.existsSync()) {
388
      return;
389 390
    }
    gradle.updateLocalProperties(project: parent, requireAndroidSdk: false);
391 392
  }

393
  bool _shouldRegenerateFromTemplate() {
394 395
    return isOlderThanReference(entity: _ephemeralDirectory, referenceFile: parent.pubspecFile)
        || Cache.instance.isOlderThanToolsStamp(_ephemeralDirectory);
396
  }
397

398
  Future<void> makeHostAppEditable() async {
399
    assert(isModule);
400 401
    if (_editableHostAppDirectory.existsSync())
      throwToolExit('Android host app is already editable. To start fresh, delete the android/ folder.');
402
    _regenerateLibrary();
403 404 405
    _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);
406 407
    gradle.injectGradleWrapper(_editableHostAppDirectory);
    gradle.writeLocalProperties(_editableHostAppDirectory.childFile('local.properties'));
408 409 410 411 412
    await injectPlugins(parent);
  }

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

413
  Directory get pluginRegistrantHost => _flutterLibGradleRoot.childDirectory(isModule ? 'Flutter' : 'app');
414 415 416

  void _regenerateLibrary() {
    _deleteIfExistsSync(_ephemeralDirectory);
417 418
    _overwriteFromTemplate(fs.path.join('module', 'android', 'library'), _ephemeralDirectory);
    _overwriteFromTemplate(fs.path.join('module', 'android', 'gradle'), _ephemeralDirectory);
419 420
    gradle.injectGradleWrapper(_ephemeralDirectory);
  }
421

422
  void _overwriteFromTemplate(String path, Directory target) {
423
    final Template template = Template.fromName(path);
424 425 426 427 428 429 430 431 432 433
    template.render(
      target,
      <String, dynamic>{
        'projectName': parent.manifest.appName,
        'androidIdentifier': parent.manifest.androidPackage,
      },
      printStatusWhenWriting: false,
      overwriteExisting: true,
    );
  }
434 435
}

436 437 438 439 440 441 442 443
/// Deletes [directory] with all content.
void _deleteIfExistsSync(Directory directory) {
  if (directory.existsSync())
    directory.deleteSync(recursive: true);
}


/// Returns the first line-based match for [regExp] in [file].
444 445
///
/// Assumes UTF8 encoding.
446 447
Match _firstMatchInFile(File file, RegExp regExp) {
  if (!file.existsSync()) {
448 449
    return null;
  }
450 451 452 453 454 455 456
  for (String line in file.readAsLinesSync()) {
    final Match match = regExp.firstMatch(line);
    if (match != null) {
      return match;
    }
  }
  return null;
457
}