custom_device_config.dart 6.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2014 The Flutter 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 'package:meta/meta.dart';

/// A single configured custom device.
///
/// In the custom devices config file on disk, there may be multiple custom
/// devices configured.
@immutable
class CustomDeviceConfig {
  const CustomDeviceConfig({
    required this.id,
    required this.label,
    required this.sdkNameAndVersion,
    required this.disabled,
    required this.pingCommand,
    this.pingSuccessRegex,
    required this.postBuildCommand,
    required this.installCommand,
    required this.uninstallCommand,
    required this.runDebugCommand,
    this.forwardPortCommand,
25 26
    this.forwardPortSuccessRegex,
    this.screenshotCommand
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }) : assert(forwardPortCommand == null || forwardPortSuccessRegex != null);

  factory CustomDeviceConfig.fromJson(dynamic json) {
    final Map<String, Object> typedMap = (json as Map<dynamic, dynamic>).cast<String, Object>();

    return CustomDeviceConfig(
      id: typedMap[_kId]! as String,
      label: typedMap[_kLabel]! as String,
      sdkNameAndVersion: typedMap[_kSdkNameAndVersion]! as String,
      disabled: typedMap[_kDisabled]! as bool,
      pingCommand: _castStringList(typedMap[_kPingCommand]!),
      pingSuccessRegex: _convertToRegexOrNull(typedMap[_kPingSuccessRegex]),
      postBuildCommand: _castStringListOrNull(typedMap[_kPostBuildCommand]),
      installCommand: _castStringList(typedMap[_kInstallCommand]!),
      uninstallCommand: _castStringList(typedMap[_kUninstallCommand]!),
      runDebugCommand: _castStringList(typedMap[_kRunDebugCommand]!),
      forwardPortCommand: _castStringListOrNull(typedMap[_kForwardPortCommand]),
44 45
      forwardPortSuccessRegex: _convertToRegexOrNull(typedMap[_kForwardPortSuccessRegex]),
      screenshotCommand: _castStringListOrNull(typedMap[_kScreenshotCommand])
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    );
  }

  static const String _kId = 'id';
  static const String _kLabel = 'label';
  static const String _kSdkNameAndVersion = 'sdkNameAndVersion';
  static const String _kDisabled = 'disabled';
  static const String _kPingCommand = 'ping';
  static const String _kPingSuccessRegex = 'pingSuccessRegex';
  static const String _kPostBuildCommand = 'postBuild';
  static const String _kInstallCommand = 'install';
  static const String _kUninstallCommand = 'uninstall';
  static const String _kRunDebugCommand = 'runDebug';
  static const String _kForwardPortCommand = 'forwardPort';
  static const String _kForwardPortSuccessRegex = 'forwardPortSuccessRegex';
61
  static const String _kScreenshotCommand = 'screenshot';
62 63 64 65 66 67 68 69 70 71 72 73 74 75

  /// An example device config used for creating the default config file.
  static final CustomDeviceConfig example = CustomDeviceConfig(
    id: 'test1',
    label: 'Test Device',
    sdkNameAndVersion: 'Test Device 4 Model B+',
    disabled: true,
    pingCommand: const <String>['ping', '-w', '500', '-n', '1', 'raspberrypi'],
    pingSuccessRegex: RegExp('ms TTL='),
    postBuildCommand: null,
    installCommand: const <String>['scp', '-r', r'${localPath}', r'pi@raspberrypi:/tmp/${appName}'],
    uninstallCommand: const <String>['ssh', 'pi@raspberrypi', r'rm -rf "/tmp/${appName}"'],
    runDebugCommand: const <String>['ssh', 'pi@raspberrypi', r'flutter-pi "/tmp/${appName}"'],
    forwardPortCommand: const <String>['ssh', '-o', 'ExitOnForwardFailure=yes', '-L', r'127.0.0.1:${hostPort}:127.0.0.1:${devicePort}', 'pi@raspberrypi'],
76 77
    forwardPortSuccessRegex: RegExp('Linux'),
    screenshotCommand: const <String>['ssh', 'pi@raspberrypi', r"fbgrab /tmp/screenshot.png && cat /tmp/screenshot.png | base64 | tr -d ' \n\t'"]
78 79 80 81 82 83 84 85 86 87 88 89 90 91
  );

  final String id;
  final String label;
  final String sdkNameAndVersion;
  final bool disabled;
  final List<String> pingCommand;
  final RegExp? pingSuccessRegex;
  final List<String>? postBuildCommand;
  final List<String> installCommand;
  final List<String> uninstallCommand;
  final List<String> runDebugCommand;
  final List<String>? forwardPortCommand;
  final RegExp? forwardPortSuccessRegex;
92
  final List<String>? screenshotCommand;
93 94 95

  bool get usesPortForwarding => forwardPortCommand != null;

96 97
  bool get supportsScreenshotting => screenshotCommand != null;

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  static List<String> _castStringList(Object object) {
    return (object as List<dynamic>).cast<String>();
  }

  static List<String>? _castStringListOrNull(Object? object) {
    return object == null ? null : _castStringList(object);
  }

  static RegExp? _convertToRegexOrNull(Object? object) {
    return object == null ? null : RegExp(object as String);
  }

  dynamic toJson() {
    return <String, Object?>{
      _kId: id,
      _kLabel: label,
      _kSdkNameAndVersion: sdkNameAndVersion,
      _kDisabled: disabled,
      _kPingCommand: pingCommand,
      _kPingSuccessRegex: pingSuccessRegex?.pattern,
      _kPostBuildCommand: postBuildCommand,
      _kInstallCommand: installCommand,
      _kUninstallCommand: uninstallCommand,
      _kRunDebugCommand: runDebugCommand,
      _kForwardPortCommand: forwardPortCommand,
123 124
      _kForwardPortSuccessRegex: forwardPortSuccessRegex?.pattern,
      _kScreenshotCommand: screenshotCommand,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    };
  }

  CustomDeviceConfig copyWith({
    String? id,
    String? label,
    String? sdkNameAndVersion,
    bool? disabled,
    List<String>? pingCommand,
    bool explicitPingSuccessRegex = false,
    RegExp? pingSuccessRegex,
    bool explicitPostBuildCommand = false,
    List<String>? postBuildCommand,
    List<String>? installCommand,
    List<String>? uninstallCommand,
    List<String>? runDebugCommand,
    bool explicitForwardPortCommand = false,
    List<String>? forwardPortCommand,
    bool explicitForwardPortSuccessRegex = false,
144 145 146
    RegExp? forwardPortSuccessRegex,
    bool explicitScreenshotCommand = false,
    List<String>? screenshotCommand
147 148 149 150 151 152 153 154 155 156 157 158 159
  }) {
    return CustomDeviceConfig(
      id: id ?? this.id,
      label: label ?? this.label,
      sdkNameAndVersion: sdkNameAndVersion ?? this.sdkNameAndVersion,
      disabled: disabled ?? this.disabled,
      pingCommand: pingCommand ?? this.pingCommand,
      pingSuccessRegex: explicitPingSuccessRegex ? pingSuccessRegex : (pingSuccessRegex ?? this.pingSuccessRegex),
      postBuildCommand: explicitPostBuildCommand ? postBuildCommand : (postBuildCommand ?? this.postBuildCommand),
      installCommand: installCommand ?? this.installCommand,
      uninstallCommand: uninstallCommand ?? this.uninstallCommand,
      runDebugCommand: runDebugCommand ?? this.runDebugCommand,
      forwardPortCommand: explicitForwardPortCommand ? forwardPortCommand : (forwardPortCommand ?? this.forwardPortCommand),
160 161
      forwardPortSuccessRegex: explicitForwardPortSuccessRegex ? forwardPortSuccessRegex : (forwardPortSuccessRegex ?? this.forwardPortSuccessRegex),
      screenshotCommand: explicitScreenshotCommand ? screenshotCommand : (screenshotCommand ?? this.screenshotCommand),
162 163 164
    );
  }
}