application_package.dart 5.03 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2019 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 'package:meta/meta.dart';

import '../application_package.dart';
import '../base/file_system.dart';
9
import '../build_info.dart';
10 11
import '../globals.dart';
import '../ios/plist_utils.dart' as plist;
12
import '../project.dart';
13

14
/// Tests whether a [FileSystemEntity] is an macOS bundle directory
15 16 17 18 19 20
bool _isBundleDirectory(FileSystemEntity entity) =>
    entity is Directory && entity.path.endsWith('.app');

abstract class MacOSApp extends ApplicationPackage {
  MacOSApp({@required String projectBundleId}) : super(id: projectBundleId);

21 22 23 24 25
  /// Creates a new [MacOSApp] from a macOS project directory.
  factory MacOSApp.fromMacOSProject(MacOSProject project) {
    return BuildableMacOSApp(project);
  }

26 27 28 29 30 31 32
  /// Creates a new [MacOSApp] from an existing app bundle.
  ///
  /// `applicationBinary` is the path to the framework directory created by an
  /// Xcode build. By default, this is located under
  /// "~/Library/Developer/Xcode/DerivedData/" and contains an executable
  /// which is expected to start the application and send the observatory
  /// port over stdout.
33
  factory MacOSApp.fromPrebuiltApp(FileSystemEntity applicationBinary) {
34 35 36 37 38 39 40 41 42 43 44 45 46
    final _ExecutableAndId executableAndId = _executableFromBundle(applicationBinary);
    final Directory applicationBundle = fs.directory(applicationBinary);
    return PrebuiltMacOSApp(
      bundleDir: applicationBundle,
      bundleName: applicationBundle.path,
      projectBundleId: executableAndId.id,
      executable: executableAndId.executable,
    );
  }

  /// Look up the executable name for a macOS application bundle.
  static _ExecutableAndId _executableFromBundle(Directory applicationBundle) {
    final FileSystemEntityType entityType = fs.typeSync(applicationBundle.path);
47
    if (entityType == FileSystemEntityType.notFound) {
48
      printError('File "${applicationBundle.path}" does not exist.');
49 50 51 52
      return null;
    }
    Directory bundleDir;
    if (entityType == FileSystemEntityType.directory) {
53
      final Directory directory = fs.directory(applicationBundle);
54
      if (!_isBundleDirectory(directory)) {
55
        printError('Folder "${applicationBundle.path}" is not an app bundle.');
56 57
        return null;
      }
58
      bundleDir = fs.directory(applicationBundle);
59
    } else {
60
      printError('Folder "${applicationBundle.path}" is not an app bundle.');
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      return null;
    }
    final String plistPath = fs.path.join(bundleDir.path, 'Contents', 'Info.plist');
    if (!fs.file(plistPath).existsSync()) {
      printError('Invalid prebuilt macOS app. Does not contain Info.plist.');
      return null;
    }
    final String id = plist.getValueFromFile(plistPath, plist.kCFBundleIdentifierKey);
    final String executableName = plist.getValueFromFile(plistPath, plist.kCFBundleExecutable);
    if (id == null) {
      printError('Invalid prebuilt macOS app. Info.plist does not contain bundle identifier');
      return null;
    }
    final String executable = fs.path.join(bundleDir.path, 'Contents', 'MacOS', executableName);
    if (!fs.file(executable).existsSync()) {
      printError('Could not find macOS binary at $executable');
    }
78
    return _ExecutableAndId(executable, id);
79 80 81 82 83
  }

  @override
  String get displayName => id;

84 85 86
  String applicationBundle(BuildMode buildMode);

  String executable(BuildMode buildMode);
87 88
}

89
class PrebuiltMacOSApp extends MacOSApp {
90 91 92 93
  PrebuiltMacOSApp({
    @required this.bundleDir,
    @required this.bundleName,
    @required this.projectBundleId,
94 95 96
    @required String executable,
  }) : _executable = executable,
       super(projectBundleId: projectBundleId);
97 98 99 100 101

  final Directory bundleDir;
  final String bundleName;
  final String projectBundleId;

102
  final String _executable;
103 104 105

  @override
  String get name => bundleName;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  @override
  String applicationBundle(BuildMode buildMode) => bundleDir.path;

  @override
  String executable(BuildMode buildMode) => _executable;
}

class BuildableMacOSApp extends MacOSApp {
  BuildableMacOSApp(this.project);

  final MacOSProject project;

  @override
  String get name => 'macOS';

  @override
  String applicationBundle(BuildMode buildMode) {
124 125 126 127 128 129 130 131 132 133 134
    final File appBundleNameFile = project.nameFile;
    if (!appBundleNameFile.existsSync()) {
      printError('Unable to find app name. ${appBundleNameFile.path} does not exist');
      return null;
    }
    return fs.path.join(
        getMacOSBuildDirectory(),
        'Build',
        'Products',
        buildMode == BuildMode.debug ? 'Debug' : 'Release',
        appBundleNameFile.readAsStringSync().trim());
135 136 137 138
  }

  @override
  String executable(BuildMode buildMode) {
139 140 141 142
    final String directory = applicationBundle(buildMode);
    if (directory == null) {
      return null;
    }
143 144 145 146 147 148 149 150 151 152
    final _ExecutableAndId executableAndId = MacOSApp._executableFromBundle(fs.directory(directory));
    return executableAndId.executable;
  }
}

class _ExecutableAndId {
  _ExecutableAndId(this.executable, this.id);

  final String executable;
  final String id;
153
}