emulator.dart 4.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2018 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 'dart:async';
import 'dart:math' as math;

import 'android/android_emulator.dart';
import 'base/context.dart';
import 'globals.dart';
11
import 'ios/ios_emulators.dart';
12 13 14 15 16 17 18 19 20 21

EmulatorManager get emulatorManager => context[EmulatorManager];

/// A class to get all available emulators.
class EmulatorManager {
  /// Constructing EmulatorManager is cheap; they only do expensive work if some
  /// of their methods are called.
  EmulatorManager() {
    // Register the known discoverers.
    _emulatorDiscoverers.add(new AndroidEmulators());
22
    _emulatorDiscoverers.add(new IOSEmulators());
23 24 25 26
  }

  final List<EmulatorDiscovery> _emulatorDiscoverers = <EmulatorDiscovery>[];

27 28
  Future<List<Emulator>> getEmulatorsMatching(String searchText) async {
    final List<Emulator> emulators = await getAllAvailableEmulators();
29
    searchText = searchText.toLowerCase();
30
    bool exactlyMatchesEmulatorId(Emulator emulator) =>
31 32
        emulator.id?.toLowerCase() == searchText ||
        emulator.name?.toLowerCase() == searchText;
33
    bool startsWithEmulatorId(Emulator emulator) =>
34 35
        emulator.id?.toLowerCase()?.startsWith(searchText) == true ||
        emulator.name?.toLowerCase()?.startsWith(searchText) == true;
36 37 38 39

    final Emulator exactMatch = emulators.firstWhere(
        exactlyMatchesEmulatorId, orElse: () => null);
    if (exactMatch != null) {
40
      return <Emulator>[exactMatch];
41 42 43
    }

    // Match on a id or name starting with [emulatorId].
44
    return emulators.where(startsWithEmulatorId).toList();
45 46 47 48 49 50
  }

  Iterable<EmulatorDiscovery> get _platformDiscoverers {
    return _emulatorDiscoverers.where((EmulatorDiscovery discoverer) => discoverer.supportsPlatform);
  }

Danny Tuppeny's avatar
Danny Tuppeny committed
51
  /// Return the list of all available emulators.
52 53
  Future<List<Emulator>> getAllAvailableEmulators() async {
    final List<Emulator> emulators = <Emulator>[];
Danny Tuppeny's avatar
Danny Tuppeny committed
54
    await Future.forEach(_platformDiscoverers, (EmulatorDiscovery discoverer) async {
55 56 57
      emulators.addAll(await discoverer.emulators);
    });
    return emulators;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  /// Whether we're capable of listing any emulators given the current environment configuration.
  bool get canListAnything {
    return _platformDiscoverers.any((EmulatorDiscovery discoverer) => discoverer.canListAnything);
  }
}

/// An abstract class to discover and enumerate a specific type of emulators.
abstract class EmulatorDiscovery {
  bool get supportsPlatform;

  /// Whether this emulator discovery is capable of listing any emulators given the
  /// current environment configuration.
  bool get canListAnything;

  Future<List<Emulator>> get emulators;
}

abstract class Emulator {
78
  Emulator(this.id, this.hasConfig);
79 80

  final String id;
81 82 83 84
  final bool hasConfig;
  String get name;
  String get manufacturer;
  String get label;
85 86 87 88 89 90 91 92 93 94 95 96 97

  @override
  int get hashCode => id.hashCode;

  @override
  bool operator ==(dynamic other) {
    if (identical(this, other))
      return true;
    if (other is! Emulator)
      return false;
    return id == other.id;
  }

98
  Future<bool> launch();
99

100 101 102
  @override
  String toString() => name;

103
  static List<String> descriptions(List<Emulator> emulators) {
104
    if (emulators.isEmpty)
105
      return <String>[];
106 107 108 109 110

    // Extract emulators information
    final List<List<String>> table = <List<String>>[];
    for (Emulator emulator in emulators) {
      table.add(<String>[
Danny Tuppeny's avatar
Danny Tuppeny committed
111 112
        emulator.id ?? '',
        emulator.name ?? '',
113 114
        emulator.manufacturer ?? '',
        emulator.label ?? '',
115 116 117 118 119 120 121 122 123 124 125
      ]);
    }

    // Calculate column widths
    final List<int> indices = new List<int>.generate(table[0].length - 1, (int i) => i);
    List<int> widths = indices.map((int i) => 0).toList();
    for (List<String> row in table) {
      widths = indices.map((int i) => math.max(widths[i], row[i].length)).toList();
    }

    // Join columns into lines of text
126
    final RegExp whiteSpaceAndDots = new RegExp(r'[•\s]+$');
127 128 129
    return table.map((List<String> row) {
      return indices
        .map((int i) => row[i].padRight(widths[i]))
130 131 132 133
        .join(' • ') + ' • ${row.last}';
    })
        .map((String line) => line.replaceAll(whiteSpaceAndDots, ''))
        .toList();
134 135
  }

136 137
  static void printEmulators(List<Emulator> emulators) {
    descriptions(emulators).forEach(printStatus);
138 139
  }
}