Unverified Commit 04657354 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Build IPA command (#67781)

* Build IPA command

* xcarchive -> ipa
parent 91478d96
...@@ -385,6 +385,13 @@ class BuildableIOSApp extends IOSApp { ...@@ -385,6 +385,13 @@ class BuildableIOSApp extends IOSApp {
String get archiveBundlePath String get archiveBundlePath
=> globals.fs.path.join(getIosBuildDirectory(), 'archive', globals.fs.path.withoutExtension(_hostAppBundleName)); => globals.fs.path.join(getIosBuildDirectory(), 'archive', globals.fs.path.withoutExtension(_hostAppBundleName));
// The output xcarchive bundle path `build/ios/archive/Runner.xcarchive`.
String get archiveBundleOutputPath =>
globals.fs.path.setExtension(archiveBundlePath, '.xcarchive');
String get ipaOutputPath =>
globals.fs.path.join(getIosBuildDirectory(), 'ipa');
String _buildAppPath(String type) { String _buildAppPath(String type) {
return globals.fs.path.join(getIosBuildDirectory(), type, _hostAppBundleName); return globals.fs.path.join(getIosBuildDirectory(), type, _hostAppBundleName);
} }
......
...@@ -8,17 +8,18 @@ import 'package:meta/meta.dart'; ...@@ -8,17 +8,18 @@ import 'package:meta/meta.dart';
import '../application_package.dart'; import '../application_package.dart';
import '../base/analyze_size.dart'; import '../base/analyze_size.dart';
import '../base/common.dart'; import '../base/common.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../base/utils.dart'; import '../base/utils.dart';
import '../build_info.dart'; import '../build_info.dart';
import '../convert.dart'; import '../convert.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
import '../ios/mac.dart'; import '../ios/mac.dart';
import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult; import '../runner/flutter_command.dart';
import 'build.dart'; import 'build.dart';
/// Builds an .app for an iOS app to be used for local testing on an iOS device /// Builds an .app for an iOS app to be used for local testing on an iOS device
/// or simulator. Can only be run on a macOS host. For producing deployment /// or simulator. Can only be run on a macOS host.
/// .ipas, see https://flutter.dev/docs/deployment/ios.
class BuildIOSCommand extends _BuildIOSSubCommand { class BuildIOSCommand extends _BuildIOSSubCommand {
BuildIOSCommand({ @required bool verboseHelp }) : super(verboseHelp: verboseHelp) { BuildIOSCommand({ @required bool verboseHelp }) : super(verboseHelp: verboseHelp) {
argParser argParser
...@@ -56,14 +57,27 @@ class BuildIOSCommand extends _BuildIOSSubCommand { ...@@ -56,14 +57,27 @@ class BuildIOSCommand extends _BuildIOSSubCommand {
bool get shouldCodesign => boolArg('codesign'); bool get shouldCodesign => boolArg('codesign');
} }
/// Builds an .xcarchive for an iOS app to be generated for App Store submission. /// Builds an .xcarchive and optionally .ipa for an iOS app to be generated for
/// App Store submission.
///
/// Can only be run on a macOS host. /// Can only be run on a macOS host.
/// For producing deployment .ipas, see https://flutter.dev/docs/deployment/ios.
class BuildIOSArchiveCommand extends _BuildIOSSubCommand { class BuildIOSArchiveCommand extends _BuildIOSSubCommand {
BuildIOSArchiveCommand({ @required bool verboseHelp }) : super(verboseHelp: verboseHelp); BuildIOSArchiveCommand({@required bool verboseHelp})
: super(verboseHelp: verboseHelp) {
argParser.addOption(
'export-options-plist',
valueHelp: 'ExportOptions.plist',
// TODO(jmagman): Update help text with link to Flutter docs.
help:
'Optionally export an IPA with these options. See "xcodebuild -h" for available exportOptionsPlist keys.',
);
}
@override @override
final String name = 'xcarchive'; final String name = 'ipa';
@override
final List<String> aliases = <String>['xcarchive'];
@override @override
final String description = 'Build an iOS archive bundle (Mac OS X host only).'; final String description = 'Build an iOS archive bundle (Mac OS X host only).';
...@@ -79,6 +93,76 @@ class BuildIOSArchiveCommand extends _BuildIOSSubCommand { ...@@ -79,6 +93,76 @@ class BuildIOSArchiveCommand extends _BuildIOSSubCommand {
@override @override
final bool shouldCodesign = true; final bool shouldCodesign = true;
String get exportOptionsPlist => stringArg('export-options-plist');
@override
Future<FlutterCommandResult> runCommand() async {
if (exportOptionsPlist != null) {
final FileSystemEntityType type = globals.fs.typeSync(exportOptionsPlist);
if (type == FileSystemEntityType.notFound) {
throwToolExit(
'"$exportOptionsPlist" property list does not exist. See "man xcodebuild" for available keys.');
} else if (type != FileSystemEntityType.file) {
throwToolExit(
'"$exportOptionsPlist" is not a file. See "xcodebuild -h" for available keys.');
}
}
final FlutterCommandResult xcarchiveResult = await super.runCommand();
if (exportOptionsPlist == null) {
return xcarchiveResult;
}
// xcarchive failed or not at expected location.
if (xcarchiveResult.exitStatus != ExitStatus.success) {
globals.logger.printStatus('Skipping IPA');
return xcarchiveResult;
}
// Build IPA from generated xcarchive.
final BuildableIOSApp app = await buildableIOSApp;
Status status;
RunResult result;
final String outputPath = globals.fs.path.absolute(app.ipaOutputPath);
try {
status = globals.logger.startProgress('Building IPA...');
result = await globals.processUtils.run(
<String>[
...globals.xcode.xcrunCommand(),
'xcodebuild',
'-exportArchive',
'-archivePath',
globals.fs.path.absolute(app.archiveBundleOutputPath),
'-exportPath',
outputPath,
'-exportOptionsPlist',
globals.fs.path.absolute(exportOptionsPlist),
],
);
} finally {
status.stop();
}
if (result.exitCode != 0) {
final StringBuffer errorMessage = StringBuffer();
// "error:" prefixed lines are the nicely formatted error message, the
// rest is the same message but printed as a IDEFoundationErrorDomain.
// Example:
// error: exportArchive: exportOptionsPlist error for key 'method': expected one of {app-store, ad-hoc, enterprise, development, validation}, but found developmentasdasd
// Error Domain=IDEFoundationErrorDomain Code=1 "exportOptionsPlist error for key 'method': expected one of {app-store, ad-hoc, enterprise, development, validation}, but found developmentasdasd" ...
LineSplitter.split(result.stderr)
.where((String line) => line.contains('error: '))
.forEach(errorMessage.writeln);
throwToolExit('Encountered error while building IPA:\n$errorMessage');
}
globals.logger.printStatus('Built IPA to $outputPath.');
return FlutterCommandResult.success();
}
} }
abstract class _BuildIOSSubCommand extends BuildSubCommand { abstract class _BuildIOSSubCommand extends BuildSubCommand {
...@@ -111,10 +195,26 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand { ...@@ -111,10 +195,26 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand {
bool get configOnly; bool get configOnly;
bool get shouldCodesign; bool get shouldCodesign;
BuildInfo get buildInfo {
_buildInfo ??= getBuildInfo();
return _buildInfo;
}
BuildInfo _buildInfo;
Future<BuildableIOSApp> get buildableIOSApp async {
_buildableIOSApp ??= await applicationPackages.getPackageForPlatform(
TargetPlatform.ios,
buildInfo: buildInfo,
) as BuildableIOSApp;
return _buildableIOSApp;
}
BuildableIOSApp _buildableIOSApp;
@override @override
Future<FlutterCommandResult> runCommand() async { Future<FlutterCommandResult> runCommand() async {
defaultBuildMode = forSimulator ? BuildMode.debug : BuildMode.release; defaultBuildMode = forSimulator ? BuildMode.debug : BuildMode.release;
final BuildInfo buildInfo = getBuildInfo();
if (!globals.platform.isMacOS) { if (!globals.platform.isMacOS) {
throwToolExit('Building for iOS is only supported on macOS.'); throwToolExit('Building for iOS is only supported on macOS.');
...@@ -132,10 +232,7 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand { ...@@ -132,10 +232,7 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand {
); );
} }
final BuildableIOSApp app = await applicationPackages.getPackageForPlatform( final BuildableIOSApp app = await buildableIOSApp;
TargetPlatform.ios,
buildInfo: buildInfo,
) as BuildableIOSApp;
if (app == null) { if (app == null) {
throwToolExit('Application not configured for iOS'); throwToolExit('Application not configured for iOS');
...@@ -204,8 +301,10 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand { ...@@ -204,8 +301,10 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand {
if (result.output != null) { if (result.output != null) {
globals.printStatus('Built ${result.output}.'); globals.printStatus('Built ${result.output}.');
return FlutterCommandResult.success();
} }
return FlutterCommandResult.success(); return FlutterCommandResult.fail();
} }
} }
...@@ -438,7 +438,7 @@ Future<XcodeBuildResult> buildXcodeProject({ ...@@ -438,7 +438,7 @@ Future<XcodeBuildResult> buildXcodeProject({
globals.printError('Build succeeded but the expected app at $expectedOutputDirectory not found'); globals.printError('Build succeeded but the expected app at $expectedOutputDirectory not found');
} }
} else { } else {
outputDir = '${globals.fs.path.absolute(app.archiveBundlePath)}.xcarchive'; outputDir = globals.fs.path.absolute(app.archiveBundleOutputPath);
if (!globals.fs.isDirectorySync(outputDir)) { if (!globals.fs.isDirectorySync(outputDir)) {
globals.printError('Archive succeeded but the expected xcarchive at $outputDir not found'); globals.printError('Archive succeeded but the expected xcarchive at $outputDir not found');
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/build.dart'; import 'package:flutter_tools/src/commands/build.dart';
...@@ -45,6 +46,7 @@ final Platform notMacosPlatform = FakePlatform( ...@@ -45,6 +46,7 @@ final Platform notMacosPlatform = FakePlatform(
void main() { void main() {
FileSystem fileSystem; FileSystem fileSystem;
Usage usage; Usage usage;
BufferLogger logger;
setUpAll(() { setUpAll(() {
Cache.disableLocking(); Cache.disableLocking();
...@@ -53,6 +55,7 @@ void main() { ...@@ -53,6 +55,7 @@ void main() {
setUp(() { setUp(() {
fileSystem = MemoryFileSystem.test(); fileSystem = MemoryFileSystem.test();
usage = Usage.test(); usage = Usage.test();
logger = BufferLogger.test();
}); });
// Sets up the minimal mock project files necessary to look like a Flutter project. // Sets up the minimal mock project files necessary to look like a Flutter project.
...@@ -110,12 +113,26 @@ void main() { ...@@ -110,12 +113,26 @@ void main() {
); );
} }
testUsingContext('xcarchive build fails when there is no ios project', () async { const FakeCommand exportArchiveCommand = FakeCommand(
command: <String>[
'xcrun',
'xcodebuild',
'-exportArchive',
'-archivePath',
'/build/ios/archive/Runner.xcarchive',
'-exportPath',
'/build/ios/ipa',
'-exportOptionsPlist',
'/ExportOptions.plist'
],
);
testUsingContext('ipa build fails when there is no ios project', () async {
final BuildCommand command = BuildCommand(); final BuildCommand command = BuildCommand();
createCoreMockProjectFiles(); createCoreMockProjectFiles();
expect(createTestCommandRunner(command).run( expect(createTestCommandRunner(command).run(
const <String>['build', 'xcarchive', '--no-pub'] const <String>['build', 'ipa', '--no-pub']
), throwsToolExit(message: 'Application not configured for iOS')); ), throwsToolExit(message: 'Application not configured for iOS'));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => macosPlatform, Platform: () => macosPlatform,
...@@ -124,7 +141,7 @@ void main() { ...@@ -124,7 +141,7 @@ void main() {
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(), XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(),
}); });
testUsingContext('xcarchive build fails on non-macOS platform', () async { testUsingContext('ipa build fails on non-macOS platform', () async {
final BuildCommand command = BuildCommand(); final BuildCommand command = BuildCommand();
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('.packages').createSync(); fileSystem.file('.packages').createSync();
...@@ -132,7 +149,7 @@ void main() { ...@@ -132,7 +149,7 @@ void main() {
.createSync(recursive: true); .createSync(recursive: true);
expect(createTestCommandRunner(command).run( expect(createTestCommandRunner(command).run(
const <String>['build', 'xcarchive', '--no-pub'] const <String>['build', 'ipa', '--no-pub']
), throwsToolExit()); ), throwsToolExit());
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => notMacosPlatform, Platform: () => notMacosPlatform,
...@@ -141,12 +158,58 @@ void main() { ...@@ -141,12 +158,58 @@ void main() {
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(), XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(),
}); });
testUsingContext('xcarchive build invokes xcode build', () async { testUsingContext('ipa build fails when export plist does not exist',
() async {
final BuildCommand command = BuildCommand();
createMinimalMockProjectFiles();
await expectToolExitLater(
createTestCommandRunner(command).run(<String>[
'build',
'ipa',
'--export-options-plist',
'bogus.plist',
'--no-pub',
]),
contains('property list does not exist'),
);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
Platform: () => macosPlatform,
XcodeProjectInterpreter: () =>
FakeXcodeProjectInterpreterWithBuildSettings(),
});
testUsingContext('ipa build fails when export plist is not a file', () async {
final Directory bogus = fileSystem.directory('bogus')..createSync();
final BuildCommand command = BuildCommand();
createMinimalMockProjectFiles();
await expectToolExitLater(
createTestCommandRunner(command).run(<String>[
'build',
'ipa',
'--export-options-plist',
bogus.path,
'--no-pub',
]),
contains('is not a file.'),
);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
Platform: () => macosPlatform,
XcodeProjectInterpreter: () =>
FakeXcodeProjectInterpreterWithBuildSettings(),
});
testUsingContext('ipa build invokes xcode build', () async {
final BuildCommand command = BuildCommand(); final BuildCommand command = BuildCommand();
createMinimalMockProjectFiles(); createMinimalMockProjectFiles();
await createTestCommandRunner(command).run( await createTestCommandRunner(command).run(
const <String>['build', 'xcarchive', '--no-pub'] const <String>['build', 'ipa', '--no-pub']
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
...@@ -160,12 +223,12 @@ void main() { ...@@ -160,12 +223,12 @@ void main() {
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(), XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(),
}); });
testUsingContext('xcarchive build invokes xcode build with verbosity', () async { testUsingContext('ipa build invokes xcode build with verbosity', () async {
final BuildCommand command = BuildCommand(); final BuildCommand command = BuildCommand();
createMinimalMockProjectFiles(); createMinimalMockProjectFiles();
await createTestCommandRunner(command).run( await createTestCommandRunner(command).run(
const <String>['build', 'xcarchive', '--no-pub', '-v'] const <String>['build', 'ipa', '--no-pub', '-v']
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
...@@ -190,7 +253,7 @@ void main() { ...@@ -190,7 +253,7 @@ void main() {
// Capture Usage.test() events. // Capture Usage.test() events.
final StringBuffer buffer = await capturedConsolePrint(() => final StringBuffer buffer = await capturedConsolePrint(() =>
createTestCommandRunner(command).run( createTestCommandRunner(command).run(
const <String>['build', 'xcarchive', '--no-pub', '--analyze-size'] const <String>['build', 'ipa', '--no-pub', '--analyze-size']
) )
); );
...@@ -224,4 +287,38 @@ void main() { ...@@ -224,4 +287,38 @@ void main() {
Usage: () => usage, Usage: () => usage,
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(), XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(),
}); });
testUsingContext('ipa build invokes xcode build export archive', () async {
final String outputPath =
fileSystem.path.absolute(fileSystem.path.join('build', 'ios', 'ipa'));
final File exportOptions = fileSystem.file('ExportOptions.plist')
..createSync();
final BuildCommand command = BuildCommand();
createMinimalMockProjectFiles();
await createTestCommandRunner(command).run(
<String>[
'build',
'ipa',
'--no-pub',
'--export-options-plist',
exportOptions.path,
],
);
expect(logger.statusText, contains('Built IPA to $outputPath.'));
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
xattrCommand,
armCheckCommand,
setUpMockXcodeBuildHandler(),
setUpMockXcodeBuildHandler(showBuildSettings: true),
exportArchiveCommand,
]),
Platform: () => macosPlatform,
Logger: () => logger,
XcodeProjectInterpreter: () =>
FakeXcodeProjectInterpreterWithBuildSettings(),
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment