application_package.dart 4.92 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 13
import 'artifacts.dart';
import 'build_configuration.dart';

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
}

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

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

  AndroidApk({
    String localPath,
    String id: _defaultId,
    this.launchActivity: _defaultLaunchActivity
  }) : super(localPath: localPath, id: id) {
    assert(launchActivity != null);
  }
51 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

  /// 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);
  }
81 82
}

83
class IOSApp extends ApplicationPackage {
84
  static const String _defaultId = 'io.flutter.runner.Runner';
Todd Volkert's avatar
Todd Volkert committed
85
  static const String _defaultPath = 'ios/.generated';
86

87
  IOSApp({
88
    String localPath: _defaultPath,
89 90
    String id: _defaultId
  }) : super(localPath: localPath, id: id);
91 92
}

93 94 95 96 97 98 99
class ApplicationPackageStore {
  final AndroidApk android;
  final IOSApp iOS;
  final IOSApp iOSSimulator;

  ApplicationPackageStore({ this.android, this.iOS, this.iOSSimulator });

100
  ApplicationPackage getPackageForPlatform(TargetPlatform platform) {
101
    switch (platform) {
102
      case TargetPlatform.android:
103
        return android;
104
      case TargetPlatform.iOS:
105
        return iOS;
106
      case TargetPlatform.iOSSimulator:
107
        return iOSSimulator;
Ian Hickson's avatar
Ian Hickson committed
108
      case TargetPlatform.mac:
109
      case TargetPlatform.linux:
110 111
        return null;
    }
112 113
  }

114 115 116 117
  static Future<ApplicationPackageStore> forConfigs(List<BuildConfiguration> configs) async {
    AndroidApk android;
    IOSApp iOS;
    IOSApp iOSSimulator;
118

119
    for (BuildConfiguration config in configs) {
120 121
      switch (config.targetPlatform) {
        case TargetPlatform.android:
122
          assert(android == null);
123 124 125 126 127 128 129
          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) {
130 131 132 133 134 135 136
            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));
          }
137
          break;
138

139
        case TargetPlatform.iOS:
140
          assert(iOS == null);
141
          iOS = new IOSApp();
142
          break;
143

144
        case TargetPlatform.iOSSimulator:
145
          assert(iOSSimulator == null);
146
          iOSSimulator = new IOSApp();
147
          break;
148

Ian Hickson's avatar
Ian Hickson committed
149
        case TargetPlatform.mac:
150
        case TargetPlatform.linux:
151
          break;
152 153 154
      }
    }

155
    return new ApplicationPackageStore(android: android, iOS: iOS, iOSSimulator: iOSSimulator);
156 157
  }
}