Add iOS module template (#18830) (#18903)

Add iOS module template

This will enable integration of flutter-views into existing iOS project.
This commit is contained in:
Sigurd Meldgaard
2018-06-28 14:35:00 +02:00
committed by GitHub
parent 266a720209
commit 1e8ef602c3
16 changed files with 385 additions and 114 deletions

View File

@@ -169,6 +169,19 @@ class Cache {
return fs.file(fs.path.join(getRoot().path, '$artifactName.stamp'));
}
/// Returns `true` if either [file] is older than the tools stamp or if
/// [file] doesn't exist.
bool fileOlderThanToolsStamp(File file) {
if (!file.existsSync()) {
return true;
}
final File flutterToolsStamp = getStampFileFor('flutter_tools');
return flutterToolsStamp.existsSync() &&
flutterToolsStamp
.lastModifiedSync()
.isAfter(file.lastModifiedSync());
}
bool isUpToDate() => _artifacts.every((CachedArtifact artifact) => artifact.isUpToDate());
Future<String> getThirdPartyFile(String urlStr, String serviceName) async {

View File

@@ -233,20 +233,20 @@ To edit platform code in an IDE see https://flutter.io/developing-packages/#edit
}
}
Future<int> _generateModule(String dirPath, Map<String, dynamic> templateContext) async {
Future<int> _generateModule(String path, Map<String, dynamic> templateContext) async {
int generatedCount = 0;
final String description = argResults.wasParsed('description')
? argResults['description']
: 'A new flutter module project.';
templateContext['description'] = description;
generatedCount += _renderTemplate(fs.path.join('module', 'common'), dirPath, templateContext);
generatedCount += _renderTemplate(fs.path.join('module', 'common'), path, templateContext);
if (argResults['pub']) {
await pubGet(
context: PubContext.create,
directory: dirPath,
directory: path,
offline: argResults['offline'],
);
final FlutterProject project = new FlutterProject(fs.directory(dirPath));
final FlutterProject project = new FlutterProject.fromPath(path);
await project.ensureReadyForPlatformSpecificTooling();
}
return generatedCount;
@@ -303,23 +303,23 @@ To edit platform code in an IDE see https://flutter.io/developing-packages/#edit
return generatedCount;
}
Future<int> _generateApp(String appPath, Map<String, dynamic> templateContext) async {
Future<int> _generateApp(String projectPath, Map<String, dynamic> templateContext) async {
int generatedCount = 0;
generatedCount += _renderTemplate('create', appPath, templateContext);
generatedCount += _injectGradleWrapper(appPath);
generatedCount += _renderTemplate('create', projectPath, templateContext);
generatedCount += _injectGradleWrapper(projectPath);
if (argResults['with-driver-test']) {
final String testPath = fs.path.join(appPath, 'test_driver');
final String testPath = fs.path.join(projectPath, 'test_driver');
generatedCount += _renderTemplate('driver', testPath, templateContext);
}
if (argResults['pub']) {
await pubGet(context: PubContext.create, directory: appPath, offline: argResults['offline']);
await new FlutterProject(fs.directory(appPath)).ensureReadyForPlatformSpecificTooling();
await pubGet(context: PubContext.create, directory: projectPath, offline: argResults['offline']);
await new FlutterProject.fromPath(projectPath).ensureReadyForPlatformSpecificTooling();
}
if (android_sdk.androidSdk != null)
await gradle.updateLocalProperties(projectPath: appPath);
await gradle.updateLocalProperties(projectPath: projectPath);
return generatedCount;
}

View File

@@ -4,6 +4,8 @@
import 'dart:async';
import '../base/file_system.dart';
import '../flutter_manifest.dart';
import '../globals.dart';
import '../plugins.dart';
import '../runner/flutter_command.dart';
@@ -24,7 +26,9 @@ class InjectPluginsCommand extends FlutterCommand {
@override
Future<Null> runCommand() async {
injectPlugins();
final String projectPath = fs.currentDirectory.path;
final FlutterManifest manifest = await FlutterManifest.createFromPath(projectPath);
injectPlugins(projectPath: projectPath, manifest: manifest);
final bool result = hasPlugins();
if (result) {
printStatus('GeneratedPluginRegistrants successfully written.');

View File

@@ -5,7 +5,6 @@
import 'dart:async';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/os.dart';
import '../dart/pub.dart';
import '../project.dart';
@@ -81,7 +80,7 @@ class PackagesGetCommand extends FlutterCommand {
}
await _runPubGet(target);
final FlutterProject rootProject = new FlutterProject(fs.directory(target));
final FlutterProject rootProject = new FlutterProject.fromPath(target);
await rootProject.ensureReadyForPlatformSpecificTooling();
// Get/upgrade packages in example app as well

View File

@@ -15,6 +15,7 @@ import '../base/process.dart';
import '../base/process_manager.dart';
import '../base/version.dart';
import '../cache.dart';
import '../flutter_manifest.dart';
import '../globals.dart';
import 'xcodeproj.dart';
@@ -150,11 +151,14 @@ class CocoaPods {
/// Ensures the `ios` sub-project of the Flutter project at [appDirectory]
/// contains a suitable `Podfile` and that its `Flutter/Xxx.xcconfig` files
/// include pods configuration.
void setupPodfile(String appDirectory) {
void setupPodfile(String appDirectory, FlutterManifest manifest) {
if (!xcodeProjectInterpreter.isInstalled) {
// Don't do anything for iOS when host platform doesn't support it.
return;
}
if (!fs.directory(fs.path.join(appDirectory, 'ios')).existsSync()) {
return;
}
final String podfilePath = fs.path.join(appDirectory, 'ios', 'Podfile');
if (!fs.file(podfilePath).existsSync()) {
final bool isSwift = xcodeProjectInterpreter.getBuildSettings(
@@ -171,6 +175,7 @@ class CocoaPods {
));
podfileTemplate.copySync(podfilePath);
}
_addPodsDependencyToFlutterXcconfig(appDirectory, 'Debug');
_addPodsDependencyToFlutterXcconfig(appDirectory, 'Release');
}

View File

@@ -20,6 +20,7 @@ import '../base/process.dart';
import '../base/process_manager.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../flutter_manifest.dart';
import '../globals.dart';
import '../plugins.dart';
import '../services.dart';
@@ -242,11 +243,15 @@ Future<XcodeBuildResult> buildXcodeProject({
final Directory appDirectory = fs.directory(app.appDirectory);
await _addServicesToBundle(appDirectory);
await updateGeneratedXcodeProperties(
final FlutterManifest manifest = await FlutterManifest.createFromPath(
fs.currentDirectory.childFile('pubspec.yaml').path,
);
updateGeneratedXcodeProperties(
projectPath: fs.currentDirectory.path,
buildInfo: buildInfo,
targetOverride: targetOverride,
previewDart2: buildInfo.previewDart2,
manifest: manifest,
);
if (hasPlugins()) {

View File

@@ -2,12 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:meta/meta.dart';
import '../artifacts.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/io.dart';
@@ -28,21 +25,30 @@ String flutterFrameworkDir(BuildMode mode) {
return fs.path.normalize(fs.path.dirname(artifacts.getArtifactPath(Artifact.flutterFramework, TargetPlatform.ios, mode)));
}
String _generatedXcodePropertiesPath(String projectPath) {
return fs.path.join(projectPath, 'ios', 'Flutter', 'Generated.xcconfig');
String _generatedXcodePropertiesPath({@required String projectPath, @required FlutterManifest manifest}) {
if (manifest.isModule) {
return fs.path.join(projectPath, '.ios', 'Flutter', 'Generated.xcconfig');
} else {
return fs.path.join(projectPath, 'ios', 'Flutter', 'Generated.xcconfig');
}
}
/// Writes default Xcode properties files in the Flutter project at [projectPath],
/// if project is an iOS project and such files do not already exist.
Future<void> generateXcodeProperties(String projectPath) async {
if (fs.isDirectorySync(fs.path.join(projectPath, 'ios'))) {
if (fs.file(_generatedXcodePropertiesPath(projectPath)).existsSync())
/// if project is an iOS project and such files are out of date or do not
/// already exist.
void generateXcodeProperties({String projectPath, FlutterManifest manifest}) {
if (manifest.isModule || fs.isDirectorySync(fs.path.join(projectPath, 'ios'))) {
final File propertiesFile = fs.file(_generatedXcodePropertiesPath(projectPath: projectPath, manifest: manifest));
if (!Cache.instance.fileOlderThanToolsStamp(propertiesFile)) {
return;
await updateGeneratedXcodeProperties(
}
updateGeneratedXcodeProperties(
projectPath: projectPath,
manifest: manifest,
buildInfo: BuildInfo.debug,
targetOverride: bundle.defaultMainPath,
previewDart2: false,
previewDart2: true,
);
}
}
@@ -51,14 +57,13 @@ Future<void> generateXcodeProperties(String projectPath) async {
///
/// targetOverride: Optional parameter, if null or unspecified the default value
/// from xcode_backend.sh is used 'lib/main.dart'.
///
/// Returns the number of files written.
Future<void> updateGeneratedXcodeProperties({
void updateGeneratedXcodeProperties({
@required String projectPath,
@required FlutterManifest manifest,
@required BuildInfo buildInfo,
String targetOverride,
@required bool previewDart2,
}) async {
}) {
final StringBuffer localsBuffer = new StringBuffer();
localsBuffer.writeln('// This is a generated file; do not edit or check into version control.');
@@ -81,14 +86,11 @@ Future<void> updateGeneratedXcodeProperties({
localsBuffer.writeln('SYMROOT=\${SOURCE_ROOT}/../${getIosBuildDirectory()}');
localsBuffer.writeln('FLUTTER_FRAMEWORK_DIR=${flutterFrameworkDir(buildInfo.mode)}');
final String flutterManifest = fs.path.join(projectPath, bundle.defaultManifestPath);
FlutterManifest manifest;
try {
manifest = await FlutterManifest.createFromPath(flutterManifest);
} catch (error) {
throwToolExit('Failed to load pubspec.yaml: $error');
if (!manifest.isModule) {
// For module projects we do not want to write the FLUTTER_FRAMEWORK_DIR
// explicitly. Rather we rely on the xcode backend script and the Podfile
// logic to derive it from FLUTTER_ROOT and FLUTTER_BUILD_MODE.
localsBuffer.writeln('FLUTTER_FRAMEWORK_DIR=${flutterFrameworkDir(buildInfo.mode)}');
}
final String buildName = buildInfo?.buildName ?? manifest.buildName;
@@ -123,7 +125,7 @@ Future<void> updateGeneratedXcodeProperties({
localsBuffer.writeln('TRACK_WIDGET_CREATION=true');
}
final File localsFile = fs.file(_generatedXcodePropertiesPath(projectPath));
final File localsFile = fs.file(_generatedXcodePropertiesPath(projectPath: projectPath, manifest: manifest));
localsFile.createSync(recursive: true);
localsFile.writeAsStringSync(localsBuffer.toString());
}

View File

@@ -8,9 +8,18 @@ import 'package:yaml/yaml.dart';
import 'base/file_system.dart';
import 'dart/package_map.dart';
import 'flutter_manifest.dart';
import 'globals.dart';
import 'ios/cocoapods.dart';
void _renderTemplateToFile(String template, dynamic context, String filePath) {
final String renderedTemplate =
new mustache.Template(template).renderString(context);
final File file = fs.file(filePath);
file.createSync(recursive: true);
file.writeAsStringSync(renderedTemplate);
}
class Plugin {
final String name;
final String path;
@@ -60,7 +69,7 @@ Plugin _pluginFromPubspec(String name, Uri packageRoot) {
return new Plugin.fromYaml(name, packageRootPath, flutterConfig['plugin']);
}
List<Plugin> _findPlugins(String directory) {
List<Plugin> findPlugins(String directory) {
final List<Plugin> plugins = <Plugin>[];
Map<String, Uri> packages;
try {
@@ -146,14 +155,9 @@ void _writeAndroidPluginRegistrant(String directory, List<Plugin> plugins) {
'plugins': androidPlugins,
};
final String pluginRegistry =
new mustache.Template(_androidPluginRegistryTemplate).renderString(context);
final String javaSourcePath = fs.path.join(directory, 'src', 'main', 'java');
final Directory registryDirectory =
fs.directory(fs.path.join(javaSourcePath, 'io', 'flutter', 'plugins'));
registryDirectory.createSync(recursive: true);
final File registryFile = registryDirectory.childFile('GeneratedPluginRegistrant.java');
registryFile.writeAsStringSync(pluginRegistry);
final String registryPath = fs.path.join(javaSourcePath, 'io', 'flutter', 'plugins', 'GeneratedPluginRegistrant.java');
_renderTemplateToFile(_androidPluginRegistryTemplate, context, registryPath);
}
const String _iosPluginRegistryHeaderTemplate = '''//
@@ -192,7 +196,32 @@ const String _iosPluginRegistryImplementationTemplate = '''//
@end
''';
void _writeIOSPluginRegistrant(String directory, List<Plugin> plugins) {
const String _iosPluginRegistrantPodspecTemplate = '''
#
# Generated file, do not edit.
#
Pod::Spec.new do |s|
s.name = 'FlutterPluginRegistrant'
s.version = '0.0.1'
s.summary = 'Registers plugins with your flutter app'
s.description = <<-DESC
Depends on all your plugins, and provides a function to register them.
DESC
s.homepage = 'https://flutter.io'
s.license = { :type => 'BSD' }
s.author = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' }
s.ios.deployment_target = '7.0'
s.source_files = "Classes", "Classes/**/*.{h,m}"
s.source = { :path => '.' }
s.public_header_files = './Classes/**/*.h'
{{#plugins}}
s.dependency '{{name}}'
{{/plugins}}
end
''';
void _writeIOSPluginRegistrant(String directory, FlutterManifest manifest, List<Plugin> plugins) {
final List<Map<String, dynamic>> iosPlugins = plugins
.where((Plugin p) => p.pluginClass != null)
.map((Plugin p) => <String, dynamic>{
@@ -205,16 +234,41 @@ void _writeIOSPluginRegistrant(String directory, List<Plugin> plugins) {
'plugins': iosPlugins,
};
final String pluginRegistryHeader =
new mustache.Template(_iosPluginRegistryHeaderTemplate).renderString(context);
final String pluginRegistryImplementation =
new mustache.Template(_iosPluginRegistryImplementationTemplate).renderString(context);
final Directory registryDirectory = fs.directory(fs.path.join(directory, 'ios', 'Runner'));
registryDirectory.createSync(recursive: true);
final File registryHeaderFile = registryDirectory.childFile('GeneratedPluginRegistrant.h');
registryHeaderFile.writeAsStringSync(pluginRegistryHeader);
final File registryImplementationFile = registryDirectory.childFile('GeneratedPluginRegistrant.m');
registryImplementationFile.writeAsStringSync(pluginRegistryImplementation);
if (manifest.isModule) {
// In a module create the GeneratedPluginRegistrant as a pod to be included
// from a hosting app.
final String registryDirectory = fs.path.join(directory, 'FlutterPluginRegistrant');
final String registryClassesDirectory = fs.path.join(registryDirectory, 'Classes');
_renderTemplateToFile(
_iosPluginRegistrantPodspecTemplate,
context,
fs.path.join(registryDirectory, 'FlutterPluginRegistrant.podspec'),
);
_renderTemplateToFile(
_iosPluginRegistryHeaderTemplate,
context,
fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.h'),
);
_renderTemplateToFile(
_iosPluginRegistryImplementationTemplate,
context,
fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.m'),
);
} else {
// For a non-module create the GeneratedPluginRegistrant as source files
// directly in the ios project.
final String runnerDirectory = fs.path.join(directory, 'Runner');
_renderTemplateToFile(
_iosPluginRegistryHeaderTemplate,
context,
fs.path.join(runnerDirectory, 'GeneratedPluginRegistrant.h'),
);
_renderTemplateToFile(
_iosPluginRegistryImplementationTemplate,
context,
fs.path.join(runnerDirectory, 'GeneratedPluginRegistrant.m'),
);
}
}
class InjectPluginsResult{
@@ -229,22 +283,23 @@ class InjectPluginsResult{
}
/// Injects plugins found in `pubspec.yaml` into the platform-specific projects.
void injectPlugins({String directory}) {
directory ??= fs.currentDirectory.path;
final List<Plugin> plugins = _findPlugins(directory);
final bool changed = _writeFlutterPluginsList(directory, plugins);
if (fs.isDirectorySync(fs.path.join(directory, '.android', 'Flutter'))) {
_writeAndroidPluginRegistrant(fs.path.join(directory, '.android', 'Flutter'), plugins);
} else if (fs.isDirectorySync(fs.path.join(directory, 'android', 'app'))) {
_writeAndroidPluginRegistrant(fs.path.join(directory, 'android', 'app'), plugins);
void injectPlugins({@required String projectPath, @required FlutterManifest manifest}) {
final List<Plugin> plugins = findPlugins(projectPath);
final bool changed = _writeFlutterPluginsList(projectPath, plugins);
if (manifest.isModule) {
_writeAndroidPluginRegistrant(fs.path.join(projectPath, '.android', 'Flutter'), plugins);
} else if (fs.isDirectorySync(fs.path.join(projectPath, 'android', 'app'))) {
_writeAndroidPluginRegistrant(fs.path.join(projectPath, 'android', 'app'), plugins);
}
if (fs.isDirectorySync(fs.path.join(directory, 'ios'))) {
_writeIOSPluginRegistrant(directory, plugins);
if (manifest.isModule) {
_writeIOSPluginRegistrant(fs.path.join(projectPath, '.ios'), manifest, plugins);
} else if (fs.isDirectorySync(fs.path.join(projectPath, 'ios'))) {
_writeIOSPluginRegistrant(fs.path.join(projectPath, 'ios'), manifest, plugins);
final CocoaPods cocoaPods = new CocoaPods();
if (plugins.isNotEmpty)
cocoaPods.setupPodfile(directory);
cocoaPods.setupPodfile(projectPath, manifest);
if (changed)
cocoaPods.invalidatePodInstallOutput(directory);
cocoaPods.invalidatePodInstallOutput(projectPath);
}
}

View File

@@ -8,6 +8,7 @@ import 'dart:convert';
import 'android/gradle.dart' as gradle;
import 'base/file_system.dart';
import 'bundle.dart' as bundle;
import 'cache.dart';
import 'flutter_manifest.dart';
import 'ios/xcodeproj.dart' as xcode;
@@ -16,11 +17,20 @@ import 'template.dart';
/// Represents the contents of a Flutter project at the specified [directory].
class FlutterProject {
FlutterProject(this.directory);
FlutterProject.fromPath(String projectPath) : directory = fs.directory(projectPath);
/// The location of this project.
final Directory directory;
Future<FlutterManifest> get manifest {
return _manifest ??= FlutterManifest.createFromPath(
directory.childFile(bundle.defaultManifestPath).path,
);
}
Future<FlutterManifest> _manifest;
/// Asynchronously returns the organization names found in this project as
/// part of iOS product bundle identifier, Android application ID, or
/// Gradle group ID.
@@ -54,11 +64,17 @@ class FlutterProject {
/// The generated AndroidModule sub project of this module project.
AndroidModuleProject get androidModule => new AndroidModuleProject(directory.childDirectory('.android'));
/// The generated IosModule sub project of this module project.
IosModuleProject get iosModule => new IosModuleProject(directory.childDirectory('.ios'));
/// Returns true if this project has an example application
bool get hasExampleApp => directory.childDirectory('example').childFile('pubspec.yaml').existsSync();
bool get hasExampleApp => _exampleDirectory.childFile('pubspec.yaml').existsSync();
/// The example sub project of this (package or plugin) project.
FlutterProject get example => new FlutterProject(directory.childDirectory('example'));
FlutterProject get example => new FlutterProject(_exampleDirectory);
/// The directory that will contain the example if an example exists.
Directory get _exampleDirectory => directory.childDirectory('example');
/// Generates project files necessary to make Gradle builds work on Android
/// and CocoaPods+Xcode work on iOS, for app and module projects only.
@@ -68,12 +84,13 @@ class FlutterProject {
if (!directory.existsSync() || hasExampleApp) {
return 0;
}
final FlutterManifest manifest = await FlutterManifest.createFromPath(directory.childFile('pubspec.yaml').path);
final FlutterManifest manifest = await this.manifest;
if (manifest.isModule) {
await androidModule.ensureReadyForPlatformSpecificTooling(manifest);
await iosModule.ensureReadyForPlatformSpecificTooling(manifest);
}
injectPlugins(directory: directory.path);
await xcode.generateXcodeProperties(directory.path);
xcode.generateXcodeProperties(projectPath: directory.path, manifest: manifest);
injectPlugins(projectPath: directory.path, manifest: manifest);
}
}
@@ -90,6 +107,25 @@ class IosProject {
}
}
/// Represents the contents of the .ios/ folder of a Flutter module
/// project.
class IosModuleProject {
IosModuleProject(this.directory);
final Directory directory;
Future<void> ensureReadyForPlatformSpecificTooling(FlutterManifest manifest) async {
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'));
}
}
/// Represents the contents of the android/ folder of a Flutter project.
class AndroidProject {
static final RegExp _applicationIdPattern = new RegExp('^\\s*applicationId\\s+[\'\"](.*)[\'\"]\\s*\$');
@@ -129,14 +165,7 @@ class AndroidModuleProject {
}
bool _shouldRegenerate() {
final File flutterToolsStamp = Cache.instance.getStampFileFor('flutter_tools');
final File buildDotGradleFile = directory.childFile('build.gradle');
if (!buildDotGradleFile.existsSync())
return true;
return flutterToolsStamp.existsSync() &&
flutterToolsStamp
.lastModifiedSync()
.isAfter(buildDotGradleFile.lastModifiedSync());
return Cache.instance.fileOlderThanToolsStamp(directory.childFile('build.gradle'));
}
}