Create Podfile dynamically part 1 (#11101)

* start

* with create

* refactor cocoapod code, add tests

* fix tests

* throw when cocoapod missing

* obj-c projects don’t use use_framework!
This commit is contained in:
xster
2017-07-12 18:35:08 -07:00
committed by GitHub
parent 0adb39ae9b
commit 6a49419be4
9 changed files with 365 additions and 102 deletions

View File

@@ -224,6 +224,9 @@ class BuildableIOSApp extends IOSApp {
@override
String get deviceBundlePath => _buildAppPath('iphoneos');
/// True if the app is built from a Swift project. Null if unknown.
bool get isSwift => buildSettings?.containsKey('SWIFT_VERSION');
String _buildAppPath(String type) {
return fs.path.join(getIosBuildDirectory(), 'Release-$type', kBundleName);
}

View File

@@ -0,0 +1,136 @@
// Copyright 2017 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 'package:meta/meta.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../base/process_manager.dart';
import '../base/version.dart';
import '../cache.dart';
import '../globals.dart';
final String noCocoaPodsConsequence = '''
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins''';
final String cocoaPodsInstallInstructions = '''
brew update
brew install cocoapods
pod setup''';
final String cocoaPodsUpgradeInstructions = '''
brew update
brew upgrade cocoapods
pod setup''';
CocoaPods get cocoaPods => context.putIfAbsent(CocoaPods, () => const CocoaPods());
class CocoaPods {
const CocoaPods();
Future<bool> get hasCocoaPods => exitsHappyAsync(<String>['pod', '--version']);
String get cocoaPodsMinimumVersion => '1.0.0';
Future<String> get cocoaPodsVersionText async => (await runAsync(<String>['pod', '--version'])).processResult.stdout.trim();
Future<bool> get isCocoaPodsInstalledAndMeetsVersionCheck async {
if (!await hasCocoaPods)
return false;
try {
final Version installedVersion = new Version.parse(await cocoaPodsVersionText);
return installedVersion >= new Version.parse(cocoaPodsMinimumVersion);
} on FormatException {
return false;
}
}
/// Whether CocoaPods ran 'pod setup' once where the costly pods' specs are cloned.
Future<bool> get isCocoaPodsInitialized => fs.isDirectory(fs.path.join(homeDirPath, '.cocoapods', 'repos', 'master'));
Future<Null> processPods({
@required Directory appIosDir,
@required String iosEngineDir,
bool isSwift: false,
}) async {
if (await _checkPodCondition()) {
if (!fs.file(fs.path.join(appIosDir.path, 'Podfile')).existsSync()) {
await _createPodfile(appIosDir, isSwift);
} // TODO(xster): Add more logic for handling merge conflicts.
await _runPodInstall(appIosDir, iosEngineDir);
} else {
throwToolExit('CocoaPods not available for project using Flutter plugins');
}
}
Future<bool> _checkPodCondition() async {
if (!await isCocoaPodsInstalledAndMeetsVersionCheck) {
final String minimumVersion = cocoaPodsMinimumVersion;
printError(
'Warning: CocoaPods version $minimumVersion or greater not installed. Skipping pod install.\n'
'$noCocoaPodsConsequence\n'
'To install:\n'
'$cocoaPodsInstallInstructions\n',
emphasis: true,
);
return false;
}
if (!await isCocoaPodsInitialized) {
printError(
'Warning: CocoaPods installed but not initialized. Skipping pod install.\n'
'$noCocoaPodsConsequence\n'
'To initialize CocoaPods, run:\n'
' pod setup\n'
'once to finalize CocoaPods\' installation.',
emphasis: true,
);
return false;
}
return true;
}
Future<Null> _createPodfile(Directory bundle, bool isSwift) async {
final File podfileTemplate = fs.file(fs.path.join(
Cache.flutterRoot,
'packages',
'flutter_tools',
'templates',
'cocoapods',
isSwift ? 'Podfile-swift' : 'Podfile-objc',
));
podfileTemplate.copySync(fs.path.join(bundle.path, 'Podfile'));
}
Future<Null> _runPodInstall(Directory bundle, String engineDirectory) async {
final Status status = logger.startProgress('Running pod install...', expectSlowOperation: true);
final ProcessResult result = await processManager.run(
<String>['pod', 'install', '--verbose'],
workingDirectory: bundle.path,
environment: <String, String>{'FLUTTER_FRAMEWORK_DIR': engineDirectory},
);
status.stop();
if (logger.isVerbose || result.exitCode != 0) {
if (result.stdout.isNotEmpty) {
printStatus('CocoaPods\' output:\n↳');
printStatus(result.stdout, indent: 4);
}
if (result.stderr.isNotEmpty) {
printStatus('Error output from CocoaPods:\n↳');
printStatus(result.stderr, indent: 4);
}
}
if (result.exitCode != 0)
throwToolExit('Error running pod install');
}
}

View File

@@ -4,14 +4,13 @@
import 'dart:async';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/os.dart';
import '../base/platform.dart';
import '../base/process.dart';
import '../base/version.dart';
import '../doctor.dart';
import 'cocoapods.dart';
import 'mac.dart';
IOSWorkflow get iosWorkflow => context.putIfAbsent(IOSWorkflow, () => new IOSWorkflow());
@@ -43,12 +42,6 @@ class IOSWorkflow extends DoctorValidator implements Workflow {
bool get hasPythonSixModule => kPythonSix.isInstalled;
Future<bool> get hasCocoaPods => exitsHappyAsync(<String>['pod', '--version']);
String get cocoaPodsMinimumVersion => '1.0.0';
Future<String> get cocoaPodsVersionText async => (await runAsync(<String>['pod', '--version'])).processResult.stdout.trim();
Future<String> get macDevMode async => (await runAsync(<String>['DevToolsSecurity', '-status'])).processResult.stdout;
Future<bool> get _iosDeployIsInstalledAndMeetsVersionCheck async {
@@ -62,20 +55,6 @@ class IOSWorkflow extends DoctorValidator implements Workflow {
}
}
Future<bool> get isCocoaPodsInstalledAndMeetsVersionCheck async {
if (!await hasCocoaPods)
return false;
try {
final Version installedVersion = new Version.parse(await cocoaPodsVersionText);
return installedVersion >= new Version.parse(cocoaPodsMinimumVersion);
} on FormatException {
return false;
}
}
/// Whether CocoaPods ran 'pod setup' once where the costly pods' specs are cloned.
Future<bool> get isCocoaPodsInitialized => fs.isDirectory(fs.path.join(homeDirPath, '.cocoapods', 'repos', 'master'));
@override
Future<ValidationResult> validate() async {
final List<ValidationMessage> messages = <ValidationMessage>[];
@@ -187,9 +166,9 @@ class IOSWorkflow extends DoctorValidator implements Workflow {
}
}
if (await isCocoaPodsInstalledAndMeetsVersionCheck) {
if (await isCocoaPodsInitialized) {
messages.add(new ValidationMessage('CocoaPods version ${await cocoaPodsVersionText}'));
if (await cocoaPods.isCocoaPodsInstalledAndMeetsVersionCheck) {
if (await cocoaPods.isCocoaPodsInitialized) {
messages.add(new ValidationMessage('CocoaPods version ${await cocoaPods.cocoaPodsVersionText}'));
} else {
brewStatus = ValidationType.partial;
messages.add(new ValidationMessage.error(
@@ -202,7 +181,7 @@ class IOSWorkflow extends DoctorValidator implements Workflow {
}
} else {
brewStatus = ValidationType.partial;
if (!await hasCocoaPods) {
if (!await cocoaPods.hasCocoaPods) {
messages.add(new ValidationMessage.error(
'CocoaPods not installed.\n'
'$noCocoaPodsConsequence\n'
@@ -211,7 +190,7 @@ class IOSWorkflow extends DoctorValidator implements Workflow {
));
} else {
messages.add(new ValidationMessage.error(
'CocoaPods out of date ($cocoaPodsMinimumVersion is required).\n'
'CocoaPods out of date ($cocoaPods.cocoaPodsMinimumVersion is required).\n'
'$noCocoaPodsConsequence\n'
'To upgrade:\n'
'$cocoaPodsUpgradeInstructions'

View File

@@ -21,8 +21,8 @@ import '../flx.dart' as flx;
import '../globals.dart';
import '../plugins.dart';
import '../services.dart';
import 'cocoapods.dart';
import 'code_signing.dart';
import 'ios_workflow.dart';
import 'xcodeproj.dart';
const int kXcodeRequiredVersionMajor = 8;
@@ -215,7 +215,11 @@ Future<XcodeBuildResult> buildXcodeProject({
final bool hasFlutterPlugins = injectPlugins();
if (hasFlutterPlugins)
await _runPodInstall(appDirectory, flutterFrameworkDir(mode));
await cocoaPods.processPods(
appIosDir: appDirectory,
iosEngineDir: flutterFrameworkDir(mode),
isSwift: app.isSwift,
);
updateXcodeGeneratedProperties(
projectPath: fs.currentDirectory.path,
@@ -396,67 +400,6 @@ bool _checkXcodeVersion() {
return true;
}
final String noCocoaPodsConsequence = '''
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins''';
final String cocoaPodsInstallInstructions = '''
brew update
brew install cocoapods
pod setup''';
final String cocoaPodsUpgradeInstructions = '''
brew update
brew upgrade cocoapods
pod setup''';
Future<Null> _runPodInstall(Directory bundle, String engineDirectory) async {
if (fs.file(fs.path.join(bundle.path, 'Podfile')).existsSync()) {
if (!await iosWorkflow.isCocoaPodsInstalledAndMeetsVersionCheck) {
final String minimumVersion = iosWorkflow.cocoaPodsMinimumVersion;
printError(
'Warning: CocoaPods version $minimumVersion or greater not installed. Skipping pod install.\n'
'$noCocoaPodsConsequence\n'
'To install:\n'
'$cocoaPodsInstallInstructions\n',
emphasis: true,
);
return;
}
if (!await iosWorkflow.isCocoaPodsInitialized) {
printError(
'Warning: CocoaPods installed but not initialized. Skipping pod install.\n'
'$noCocoaPodsConsequence\n'
'To initialize CocoaPods, run:\n'
' pod setup\n'
'once to finalize CocoaPods\' installation.',
emphasis: true,
);
return;
}
final Status status = logger.startProgress('Running pod install...', expectSlowOperation: true);
final ProcessResult result = await processManager.run(
<String>['pod', 'install', '--verbose'],
workingDirectory: bundle.path,
environment: <String, String>{'FLUTTER_FRAMEWORK_DIR': engineDirectory},
);
status.stop();
if (logger.isVerbose || result.exitCode != 0) {
if (result.stdout.isNotEmpty) {
printStatus('CocoaPods\' output:\n↳');
printStatus(result.stdout, indent: 4);
}
if (result.stderr.isNotEmpty) {
printStatus('Error output from CocoaPods:\n↳');
printStatus(result.stderr, indent: 4);
}
}
if (result.exitCode != 0)
throwToolExit('Error running pod install');
}
}
Future<Null> _addServicesToBundle(Directory bundle) async {
final List<Map<String, String>> services = <Map<String, String>>[];
printTrace("Trying to resolve native pub services.");