project.dart 9.53 KB
Newer Older
1 2 3 4 5 6
// 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';
import 'dart:convert';
7

8
import 'android/gradle.dart' as gradle;
9
import 'base/file_system.dart';
10
import 'bundle.dart' as bundle;
11 12 13
import 'cache.dart';
import 'flutter_manifest.dart';
import 'ios/xcodeproj.dart' as xcode;
14
import 'plugins.dart';
15
import 'template.dart';
16 17 18

/// Represents the contents of a Flutter project at the specified [directory].
class FlutterProject {
19

20
  FlutterProject(this.directory);
21
  FlutterProject.fromPath(String projectPath) : directory = fs.directory(projectPath);
22 23 24 25

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

26 27 28 29 30 31 32
  Future<FlutterManifest> get manifest {
    return _manifest ??= FlutterManifest.createFromPath(
      directory.childFile(bundle.defaultManifestPath).path,
    );
  }
  Future<FlutterManifest> _manifest;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  /// Asynchronously returns the organization names found in this project as
  /// part of iOS product bundle identifier, Android application ID, or
  /// Gradle group ID.
  Future<Set<String>> organizationNames() async {
    final List<String> candidates = await Future.wait(<Future<String>>[
      ios.productBundleIdentifier(),
      android.applicationId(),
      android.group(),
      example.android.applicationId(),
      example.ios.productBundleIdentifier(),
    ]);
    return new Set<String>.from(
      candidates.map(_organizationNameFromPackageName)
                .where((String name) => name != null)
    );
  }

  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.
  IosProject get ios => new IosProject(directory.childDirectory('ios'));

  /// The Android sub project of this project.
  AndroidProject get android => new AndroidProject(directory.childDirectory('android'));

63 64 65
  /// The generated AndroidModule sub project of this module project.
  AndroidModuleProject get androidModule => new AndroidModuleProject(directory.childDirectory('.android'));

66 67 68
  /// The generated IosModule sub project of this module project.
  IosModuleProject get iosModule => new IosModuleProject(directory.childDirectory('.ios'));

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  Future<File> get androidLocalPropertiesFile {
    return _androidLocalPropertiesFile ??= manifest.then<File>((FlutterManifest manifest) {
      return directory.childDirectory(manifest.isModule ? '.android' : 'android')
          .childFile('local.properties');
    });
  }
  Future<File> _androidLocalPropertiesFile;

  Future<File> get generatedXcodePropertiesFile {
    return _generatedXcodeProperties ??= manifest.then<File>((FlutterManifest manifest) {
      return directory.childDirectory(manifest.isModule ? '.ios' : 'ios')
          .childDirectory('Flutter')
          .childFile('Generated.xcconfig');
    });
  }
  Future<File> _generatedXcodeProperties;

  File get flutterPluginsFile {
    return _flutterPluginsFile ??= directory.childFile('.flutter-plugins');
  }
  File _flutterPluginsFile;

  Future<Directory> get androidPluginRegistrantHost async {
    return _androidPluginRegistrantHost ??= manifest.then((FlutterManifest manifest) {
      if (manifest.isModule) {
        return directory.childDirectory('.android').childDirectory('Flutter');
      } else {
        return directory.childDirectory('android').childDirectory('app');
      }
    });
  }
  Future<Directory> _androidPluginRegistrantHost;

  Future<Directory> get iosPluginRegistrantHost async {
    return _iosPluginRegistrantHost ??= manifest.then((FlutterManifest manifest) {
      if (manifest.isModule) {
        // In a module create the GeneratedPluginRegistrant as a pod to be included
        // from a hosting app.
        return directory
            .childDirectory('.ios')
            .childDirectory('Flutter')
            .childDirectory('FlutterPluginRegistrant');
      } else {
        // For a non-module create the GeneratedPluginRegistrant as source files
        // directly in the iOS project.
        return directory.childDirectory('ios').childDirectory('Runner');
      }
    });
  }
  Future<Directory> _iosPluginRegistrantHost;

120
  /// Returns true if this project has an example application
121
  bool get hasExampleApp => _exampleDirectory.childFile('pubspec.yaml').existsSync();
122

123
  /// The example sub project of this (package or plugin) project.
124 125 126 127
  FlutterProject get example => new FlutterProject(_exampleDirectory);

  /// The directory that will contain the example if an example exists.
  Directory get _exampleDirectory => directory.childDirectory('example');
128 129

  /// Generates project files necessary to make Gradle builds work on Android
130 131 132
  /// and CocoaPods+Xcode work on iOS, for app and module projects only.
  ///
  /// Returns the number of files written.
133
  Future<void> ensureReadyForPlatformSpecificTooling() async {
134
    if (!directory.existsSync() || hasExampleApp) {
135 136
      return 0;
    }
137
    final FlutterManifest manifest = await this.manifest;
138
    if (manifest.isModule) {
139 140
      await androidModule.ensureReadyForPlatformSpecificTooling(this);
      await iosModule.ensureReadyForPlatformSpecificTooling();
141
    }
142 143
    await xcode.generateXcodeProperties(project: this);
    await injectPlugins(this);
144
  }
145 146 147 148 149 150 151 152 153
}

/// Represents the contents of the ios/ folder of a Flutter project.
class IosProject {
  static final RegExp _productBundleIdPattern = new RegExp(r'^\s*PRODUCT_BUNDLE_IDENTIFIER\s*=\s*(.*);\s*$');
  IosProject(this.directory);

  final Directory directory;

154 155 156 157 158 159 160 161 162 163 164 165 166 167
  /// The xcode config file for [mode].
  File xcodeConfigFor(String mode) {
    return directory.childDirectory('Flutter').childFile('$mode.xcconfig');
  }

  /// The 'Podfile'.
  File get podfile => directory.childFile('Podfile');

  /// The 'Podfile.lock'.
  File get podfileLock => directory.childFile('Podfile.lock');

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

168 169 170 171 172 173
  Future<String> productBundleIdentifier() {
    final File projectFile = directory.childDirectory('Runner.xcodeproj').childFile('project.pbxproj');
    return _firstMatchInFile(projectFile, _productBundleIdPattern).then((Match match) => match?.group(1));
  }
}

174 175 176 177 178 179 180
/// Represents the contents of the .ios/ folder of a Flutter module
/// project.
class IosModuleProject {
  IosModuleProject(this.directory);

  final Directory directory;

181
  Future<void> ensureReadyForPlatformSpecificTooling() async {
182 183 184 185 186 187 188 189 190 191 192
    if (_shouldRegenerate()) {
      final Template template = new Template.fromName(fs.path.join('module', 'ios'));
      template.render(directory, <String, dynamic>{}, printStatusWhenWriting: false);
    }
  }

  bool _shouldRegenerate() {
    return Cache.instance.fileOlderThanToolsStamp(directory.childFile('podhelper.rb'));
  }
}

193 194 195 196 197 198 199
/// Represents the contents of the android/ folder of a Flutter project.
class AndroidProject {
  static final RegExp _applicationIdPattern = new RegExp('^\\s*applicationId\\s+[\'\"](.*)[\'\"]\\s*\$');
  static final RegExp _groupPattern = new RegExp('^\\s*group\\s+[\'\"](.*)[\'\"]\\s*\$');

  AndroidProject(this.directory);

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  File get gradleManifestFile {
    return _gradleManifestFile ??= isUsingGradle()
        ? fs.file(fs.path.join(directory.path, 'app', 'src', 'main', 'AndroidManifest.xml'))
        : directory.childFile('AndroidManifest.xml');
  }
  File _gradleManifestFile;


  File get gradleAppOutV1File {
    return _gradleAppOutV1File ??= gradleAppOutV1Directory.childFile('app-debug.apk');
  }
  File _gradleAppOutV1File;

  Directory get gradleAppOutV1Directory {
    return _gradleAppOutV1Directory ??= fs.directory(fs.path.join(directory.path, 'app', 'build', 'outputs', 'apk'));
  }
  Directory _gradleAppOutV1Directory;


  bool isUsingGradle() {
    return directory.childFile('build.gradle').existsSync();
  }
222

223 224 225 226 227 228 229 230 231 232 233 234 235
  final Directory directory;

  Future<String> applicationId() {
    final File gradleFile = directory.childDirectory('app').childFile('build.gradle');
    return _firstMatchInFile(gradleFile, _applicationIdPattern).then((Match match) => match?.group(1));
  }

  Future<String> group() {
    final File gradleFile = directory.childFile('build.gradle');
    return _firstMatchInFile(gradleFile, _groupPattern).then((Match match) => match?.group(1));
  }
}

236 237 238 239 240 241 242
/// Represents the contents of the .android-generated/ folder of a Flutter module
/// project.
class AndroidModuleProject {
  AndroidModuleProject(this.directory);

  final Directory directory;

243
  Future<void> ensureReadyForPlatformSpecificTooling(FlutterProject project) async {
244 245 246
    if (_shouldRegenerate()) {
      final Template template = new Template.fromName(fs.path.join('module', 'android'));
      template.render(directory, <String, dynamic>{
247
        'androidIdentifier': (await project.manifest).moduleDescriptor['androidPackage'],
248 249 250
      }, printStatusWhenWriting: false);
      gradle.injectGradleWrapper(directory);
    }
251
    await gradle.updateLocalProperties(project: project, requireAndroidSdk: false);
252 253 254
  }

  bool _shouldRegenerate() {
255
    return Cache.instance.fileOlderThanToolsStamp(directory.childFile('build.gradle'));
256 257 258
  }
}

259 260 261 262 263 264 265 266 267
/// Asynchronously returns the first line-based match for [regExp] in [file].
///
/// Assumes UTF8 encoding.
Future<Match> _firstMatchInFile(File file, RegExp regExp) async {
  if (!await file.exists()) {
    return null;
  }
  return file
      .openRead()
268
      .transform(utf8.decoder)
269 270
      .transform(const LineSplitter())
      .map(regExp.firstMatch)
271
      .firstWhere((Match match) => match != null, orElse: () => null);
272
}