[flutter_tools] Support zipped application bundles for macOS (#68854)

* [flutter_tools] Support zipped application bundles for macOS

It is not possible to directly produce a directory (.app) in some build systems
but rather it must be zip'ed before being passed to the tool for
running. This adds support for attempting to extract an application
bundle from a zip file if the bundle is not already a directory. This
uses very similar code from lib/src/application_package.dart which is
used for extracting an ipa for iOS.

This introduces tests for the macos/application_package.dart behavior which did not exist before. These tests cover the changes in the PR and some of the existing behavior, but do not cover everything in that file.
This commit is contained in:
Andy Weiss
2020-11-02 08:58:33 -08:00
committed by GitHub
parent 8328b56ef3
commit c4ceea397a
2 changed files with 268 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ import 'package:meta/meta.dart';
import '../application_package.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../globals.dart' as globals;
@@ -32,18 +33,21 @@ abstract class MacOSApp extends ApplicationPackage {
/// which is expected to start the application and send the observatory
/// port over stdout.
factory MacOSApp.fromPrebuiltApp(FileSystemEntity applicationBinary) {
final _ExecutableAndId executableAndId = _executableFromBundle(applicationBinary);
final Directory applicationBundle = globals.fs.directory(applicationBinary);
final _BundleInfo bundleInfo = _executableFromBundle(applicationBinary);
if (bundleInfo == null) {
return null;
}
return PrebuiltMacOSApp(
bundleDir: applicationBundle,
bundleName: applicationBundle.path,
projectBundleId: executableAndId.id,
executable: executableAndId.executable,
bundleDir: bundleInfo.bundle,
bundleName: bundleInfo.bundle.path,
projectBundleId: bundleInfo.id,
executable: bundleInfo.executable,
);
}
/// Look up the executable name for a macOS application bundle.
static _ExecutableAndId _executableFromBundle(FileSystemEntity applicationBundle) {
static _BundleInfo _executableFromBundle(FileSystemEntity applicationBundle) {
final FileSystemEntityType entityType = globals.fs.typeSync(applicationBundle.path);
if (entityType == FileSystemEntityType.notFound) {
globals.printError('File "${applicationBundle.path}" does not exist.');
@@ -58,8 +62,23 @@ abstract class MacOSApp extends ApplicationPackage {
}
bundleDir = globals.fs.directory(applicationBundle);
} else {
globals.printError('Folder "${applicationBundle.path}" is not an app bundle.');
return null;
// Try to unpack as a zip.
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_app.');
try {
globals.os.unzip(globals.fs.file(applicationBundle), tempDir);
} on ProcessException {
globals.printError('Invalid prebuilt macOS app. Unable to extract bundle from archive.');
return null;
}
try {
bundleDir = tempDir
.listSync()
.whereType<Directory>()
.singleWhere(_isBundleDirectory);
} on StateError {
globals.printError('Archive "${applicationBundle.path}" does not contain a single app bundle.');
return null;
}
}
final String plistPath = globals.fs.path.join(bundleDir.path, 'Contents', 'Info.plist');
if (!globals.fs.file(plistPath).existsSync()) {
@@ -73,11 +92,15 @@ abstract class MacOSApp extends ApplicationPackage {
globals.printError('Invalid prebuilt macOS app. Info.plist does not contain bundle identifier');
return null;
}
if (executableName == null) {
globals.printError('Invalid prebuilt macOS app. Info.plist does not contain bundle executable');
return null;
}
final String executable = globals.fs.path.join(bundleDir.path, 'Contents', 'MacOS', executableName);
if (!globals.fs.file(executable).existsSync()) {
globals.printError('Could not find macOS binary at $executable');
}
return _ExecutableAndId(executable, id);
return _BundleInfo(executable, id, bundleDir);
}
@override
@@ -142,14 +165,15 @@ class BuildableMacOSApp extends MacOSApp {
if (directory == null) {
return null;
}
final _ExecutableAndId executableAndId = MacOSApp._executableFromBundle(globals.fs.directory(directory));
return executableAndId?.executable;
final _BundleInfo bundleInfo = MacOSApp._executableFromBundle(globals.fs.directory(directory));
return bundleInfo?.executable;
}
}
class _ExecutableAndId {
_ExecutableAndId(this.executable, this.id);
class _BundleInfo {
_BundleInfo(this.executable, this.id, this.bundle);
final Directory bundle;
final String executable;
final String id;
}