application_package.dart 5.07 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'dart:async';
6
import 'dart:io';
7 8

import 'package:path/path.dart' as path;
9
import 'package:xml/xml.dart' as xml;
10

11 12
import 'artifacts.dart';
import 'build_configuration.dart';
13
import 'ios/plist_utils.dart';
14

15 16
abstract class ApplicationPackage {
  /// Path to the actual apk or bundle.
17
  final String localPath;
18 19

  /// Package ID from the Android Manifest or equivalent.
20
  final String id;
21 22

  /// File name of the apk or bundle.
23 24 25 26 27 28 29 30 31
  final String name;

  ApplicationPackage({
    String localPath,
    this.id
  }) : localPath = localPath, name = path.basename(localPath) {
    assert(localPath != null);
    assert(id != null);
  }
32 33 34
}

class AndroidApk extends ApplicationPackage {
35 36 37
  static const String _defaultName = 'SkyShell.apk';
  static const String _defaultId = 'org.domokit.sky.shell';
  static const String _defaultLaunchActivity = '$_defaultId/$_defaultId.SkyActivity';
38
  static const String _defaultManifestPath = 'android/AndroidManifest.xml';
39
  static const String _defaultOutputPath = 'build/app.apk';
40

41 42
  /// The path to the activity that should be launched.
  /// Defaults to 'org.domokit.sky.shell/org.domokit.sky.shell.SkyActivity'
43 44 45 46 47 48 49 50 51
  final String launchActivity;

  AndroidApk({
    String localPath,
    String id: _defaultId,
    this.launchActivity: _defaultLaunchActivity
  }) : super(localPath: localPath, id: id) {
    assert(launchActivity != null);
  }
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  /// Creates a new AndroidApk based on the information in the Android manifest.
  static AndroidApk getCustomApk({
    String localPath: _defaultOutputPath,
    String manifest: _defaultManifestPath
  }) {
    if (!FileSystemEntity.isFileSync(manifest))
      return null;
    String manifestString = new File(manifest).readAsStringSync();
    xml.XmlDocument document = xml.parse(manifestString);

    Iterable<xml.XmlElement> manifests = document.findElements('manifest');
    if (manifests.isEmpty)
      return null;
    String id = manifests.first.getAttribute('package');

    String launchActivity;
    for (xml.XmlElement category in document.findAllElements('category')) {
      if (category.getAttribute('android:name') == 'android.intent.category.LAUNCHER') {
        xml.XmlElement activity = category.parent.parent as xml.XmlElement;
        String activityName = activity.getAttribute('android:name');
        launchActivity = "$id/$activityName";
        break;
      }
    }
    if (id == null || launchActivity == null)
      return null;

    return new AndroidApk(localPath: localPath, id: id, launchActivity: launchActivity);
  }
82 83
}

84
class IOSApp extends ApplicationPackage {
85
  IOSApp({
86 87 88 89 90
    String iosProjectDir,
    String iosProjectBundleId
  }) : super(localPath: iosProjectDir, id: iosProjectBundleId);

  factory IOSApp.fromBuildConfiguration(BuildConfiguration config) {
91
    if (getCurrentHostPlatform() != HostPlatform.mac)
92 93 94
      return null;

    String plistPath = path.join("ios", "Info.plist");
95 96
    String value = getValueFromFile(plistPath, kCFBundleIdentifierKey);
    if (value == null)
97 98 99
      return null;

    String projectDir = path.join("ios", ".generated");
100
    return new IOSApp(iosProjectDir: projectDir, iosProjectBundleId: value);
101
  }
102 103
}

104 105 106 107
class ApplicationPackageStore {
  final AndroidApk android;
  final IOSApp iOS;

108
  ApplicationPackageStore({ this.android, this.iOS });
109

110
  ApplicationPackage getPackageForPlatform(TargetPlatform platform) {
111
    switch (platform) {
112
      case TargetPlatform.android:
113
        return android;
114 115
      case TargetPlatform.iOS:
      case TargetPlatform.iOSSimulator:
116
        return iOS;
Ian Hickson's avatar
Ian Hickson committed
117
      case TargetPlatform.mac:
118
      case TargetPlatform.linux:
119 120
        return null;
    }
121 122
  }

123 124 125
  static Future<ApplicationPackageStore> forConfigs(List<BuildConfiguration> configs) async {
    AndroidApk android;
    IOSApp iOS;
126

127
    for (BuildConfiguration config in configs) {
128 129
      switch (config.targetPlatform) {
        case TargetPlatform.android:
130
          assert(android == null);
131 132 133 134 135 136 137
          android = AndroidApk.getCustomApk();
          // Fall back to the prebuilt or engine-provided apk if we can't build
          // a custom one.
          // TODO(mpcomplete): we should remove both these fallbacks.
          if (android != null) {
            break;
          } else if (config.type != BuildType.prebuilt) {
138 139 140 141 142 143 144
            String localPath = path.join(config.buildDir, 'apks', AndroidApk._defaultName);
            android = new AndroidApk(localPath: localPath);
          } else {
            Artifact artifact = ArtifactStore.getArtifact(
              type: ArtifactType.shell, targetPlatform: TargetPlatform.android);
            android = new AndroidApk(localPath: await ArtifactStore.getPath(artifact));
          }
145
          break;
146

147
        case TargetPlatform.iOS:
148
        case TargetPlatform.iOSSimulator:
Devon Carew's avatar
Devon Carew committed
149
          iOS ??= new IOSApp.fromBuildConfiguration(config);
150
          break;
151

Ian Hickson's avatar
Ian Hickson committed
152
        case TargetPlatform.mac:
153
        case TargetPlatform.linux:
154
          break;
155 156 157
      }
    }

158
    return new ApplicationPackageStore(android: android, iOS: iOS);
159 160
  }
}