application_package.dart 4.3 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:io';
6 7

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

10
import 'build_info.dart';
11
import 'ios/plist_utils.dart';
12

13
abstract class ApplicationPackage {
14 15
  /// Path to the package's root folder.
  final String rootPath;
16 17

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

20 21
  ApplicationPackage({this.rootPath, this.id}) {
    assert(rootPath != null);
22 23
    assert(id != null);
  }
24

25 26
  String get name;

27 28
  String get displayName => name;

29
  @override
30
  String toString() => displayName;
31 32 33
}

class AndroidApk extends ApplicationPackage {
34 35 36
  /// Path to the actual apk file.
  final String apkPath;

37
  /// The path to the activity that should be launched.
38 39 40
  final String launchActivity;

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

  /// Creates a new AndroidApk based on the information in the Android manifest.
51
  factory AndroidApk.fromCurrentDirectory() {
Adam Barth's avatar
Adam Barth committed
52 53
    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;

75 76 77 78 79 80
    return new AndroidApk(
      buildDir: 'build',
      id: id,
      apkPath: path.join('build', 'app.apk'),
      launchActivity: launchActivity
    );
81
  }
82 83 84

  @override
  String get name => path.basename(apkPath);
85 86
}

87
class IOSApp extends ApplicationPackage {
88 89
  static final String kBundleName = 'Runner.app';

90
  IOSApp({
91 92 93
    String projectDir,
    String projectBundleId
  }) : super(rootPath: projectDir, id: projectBundleId);
94

95 96
  factory IOSApp.fromCurrentDirectory() {
    if (getCurrentHostPlatform() != HostPlatform.darwin_x64)
97 98
      return null;

Adam Barth's avatar
Adam Barth committed
99
    String plistPath = path.join('ios', 'Info.plist');
100 101
    String value = getValueFromFile(plistPath, kCFBundleIdentifierKey);
    if (value == null)
102 103
      return null;

104 105 106 107
    return new IOSApp(
      projectDir: path.join('ios', '.generated'),
      projectBundleId: value
    );
108
  }
109

110 111 112
  @override
  String get name => kBundleName;

113
  @override
114
  String get displayName => id;
115 116 117 118 119 120 121 122

  String get simulatorBundlePath => _buildAppPath('iphonesimulator');

  String get deviceBundlePath => _buildAppPath('iphoneos');

  String _buildAppPath(String type) {
    return path.join(rootPath, 'build', 'Release-$type', kBundleName);
  }
123 124
}

125 126 127 128
ApplicationPackage getApplicationPackageForPlatform(TargetPlatform platform) {
  switch (platform) {
    case TargetPlatform.android_arm:
    case TargetPlatform.android_x64:
129
    case TargetPlatform.android_x86:
130 131 132 133 134 135 136 137 138
      return new AndroidApk.fromCurrentDirectory();
    case TargetPlatform.ios:
      return new IOSApp.fromCurrentDirectory();
    case TargetPlatform.darwin_x64:
    case TargetPlatform.linux_x64:
      return null;
  }
}

139
class ApplicationPackageStore {
140 141
  AndroidApk android;
  IOSApp iOS;
142

143
  ApplicationPackageStore({ this.android, this.iOS });
144

145
  ApplicationPackage getPackageForPlatform(TargetPlatform platform) {
146
    switch (platform) {
147
      case TargetPlatform.android_arm:
148
      case TargetPlatform.android_x64:
149
      case TargetPlatform.android_x86:
150
        android ??= new AndroidApk.fromCurrentDirectory();
151
        return android;
152
      case TargetPlatform.ios:
153
        iOS ??= new IOSApp.fromCurrentDirectory();
154
        return iOS;
155 156
      case TargetPlatform.darwin_x64:
      case TargetPlatform.linux_x64:
157 158
        return null;
    }
159 160
  }
}