application_package.dart 4.05 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
import 'build_configuration.dart';
12
import 'ios/plist_utils.dart';
13

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

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

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

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

  String get displayName => name;

34
  @override
35
  String toString() => displayName;
36 37 38
}

class AndroidApk extends ApplicationPackage {
39
  /// The path to the activity that should be launched.
40 41 42 43
  final String launchActivity;

  AndroidApk({
    String localPath,
Adam Barth's avatar
Adam Barth committed
44 45
    String id,
    this.launchActivity
46 47 48
  }) : super(localPath: localPath, id: id) {
    assert(launchActivity != null);
  }
49 50

  /// Creates a new AndroidApk based on the information in the Android manifest.
Adam Barth's avatar
Adam Barth committed
51 52 53
  factory AndroidApk.fromBuildConfiguration(BuildConfiguration config) {
    String manifestPath = path.join('android', 'AndroidManifest.xml');
    if (!FileSystemEntity.isFileSync(manifestPath))
54
      return null;
Adam Barth's avatar
Adam Barth committed
55
    String manifestString = new File(manifestPath).readAsStringSync();
56 57 58 59 60 61 62 63 64 65
    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') {
Hixie's avatar
Hixie committed
66
        xml.XmlElement activity = category.parent.parent;
67 68 69 70 71 72 73 74
        String activityName = activity.getAttribute('android:name');
        launchActivity = "$id/$activityName";
        break;
      }
    }
    if (id == null || launchActivity == null)
      return null;

Adam Barth's avatar
Adam Barth committed
75
    String localPath = path.join('build', 'app.apk');
76 77
    return new AndroidApk(localPath: localPath, id: id, launchActivity: launchActivity);
  }
78 79
}

80
class IOSApp extends ApplicationPackage {
81
  IOSApp({
82 83 84 85 86
    String iosProjectDir,
    String iosProjectBundleId
  }) : super(localPath: iosProjectDir, id: iosProjectBundleId);

  factory IOSApp.fromBuildConfiguration(BuildConfiguration config) {
87
    if (getCurrentHostPlatform() != HostPlatform.mac)
88 89
      return null;

Adam Barth's avatar
Adam Barth committed
90
    String plistPath = path.join('ios', 'Info.plist');
91 92
    String value = getValueFromFile(plistPath, kCFBundleIdentifierKey);
    if (value == null)
93 94
      return null;

Adam Barth's avatar
Adam Barth committed
95
    String projectDir = path.join('ios', '.generated');
96
    return new IOSApp(iosProjectDir: projectDir, iosProjectBundleId: value);
97
  }
98

99
  @override
100
  String get displayName => id;
101 102
}

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

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

109
  ApplicationPackage getPackageForPlatform(TargetPlatform platform) {
110
    switch (platform) {
111
      case TargetPlatform.android_arm:
112
        return android;
113
      case TargetPlatform.ios:
114
        return iOS;
115 116
      case TargetPlatform.darwin_x64:
      case TargetPlatform.linux_x64:
117 118
        return null;
    }
119 120
  }

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

125
    for (BuildConfiguration config in configs) {
126
      switch (config.targetPlatform) {
127
        case TargetPlatform.android_arm:
Devon Carew's avatar
Devon Carew committed
128
          android ??= new AndroidApk.fromBuildConfiguration(config);
129
          break;
130

131
        case TargetPlatform.ios:
Devon Carew's avatar
Devon Carew committed
132
          iOS ??= new IOSApp.fromBuildConfiguration(config);
133
          break;
134

135 136
        case TargetPlatform.darwin_x64:
        case TargetPlatform.linux_x64:
137
          break;
138 139 140
      }
    }

141
    return new ApplicationPackageStore(android: android, iOS: iOS);
142 143
  }
}