fuchsia_dev_finder.dart 3.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
import 'package:meta/meta.dart';
import 'package:process/process.dart';

8
import '../base/common.dart';
9
import '../base/logger.dart';
10 11 12
import '../base/process.dart';
import 'fuchsia_sdk.dart';

13
// Usage: device-finder <flags> <subcommand> <subcommand args>
14 15 16 17 18 19 20 21 22
//
// Subcommands:
//   commands         list all command names
//   flags            describe all known top-level flags
//   help             describe subcommands and their syntax
//   list             lists all Fuchsia devices on the network
//   resolve          attempts to resolve all passed Fuchsia domain names on the
//                    network

23
/// A simple wrapper for the Fuchsia SDK's 'device-finder' tool.
24
class FuchsiaDevFinder {
25 26 27 28 29 30 31 32 33 34 35 36 37 38
  FuchsiaDevFinder({
    @required FuchsiaArtifacts fuchsiaArtifacts,
    @required Logger logger,
    @required ProcessManager processManager,
  })
    : _fuchsiaArtifacts = fuchsiaArtifacts,
      _logger = logger,
      _processUtils = ProcessUtils(logger: logger, processManager: processManager);


  final FuchsiaArtifacts _fuchsiaArtifacts;
  final Logger _logger;
  final ProcessUtils _processUtils;

39 40
  /// Returns a list of attached devices as a list of strings with entries
  /// formatted as follows:
41 42
  ///
  ///     192.168.42.172 scare-cable-skip-joy
43
  Future<List<String>> list({ Duration timeout }) async {
44 45
    if (_fuchsiaArtifacts.devFinder == null ||
        !_fuchsiaArtifacts.devFinder.existsSync()) {
46
      throwToolExit('Fuchsia device-finder tool not found.');
47 48
    }
    final List<String> command = <String>[
49
      _fuchsiaArtifacts.devFinder.path,
50
      'list',
51
      '-full',
52 53
      if (timeout != null)
        ...<String>['-timeout', '${timeout.inMilliseconds}ms']
54
    ];
55
    final RunResult result = await _processUtils.run(command);
56
    if (result.exitCode != 0) {
57 58 59 60 61
      // No devices returns error code 1.
      // https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=48563
      if (!result.stderr.contains('no devices found')) {
        _logger.printError('device-finder failed: ${result.stderr}');
      }
62 63 64
      return null;
    }
    return result.stdout.split('\n');
65 66
  }

67 68 69 70
  /// Returns the address of the named device.
  ///
  /// If local is true, then gives the address by which the device reaches the
  /// host.
71 72 73
  ///
  /// The string [deviceName] should be the name of the device from the
  /// 'list' command, e.g. 'scare-cable-skip-joy'.
74
  Future<String> resolve(String deviceName, {bool local = false}) async {
75 76
    if (_fuchsiaArtifacts.devFinder == null ||
        !_fuchsiaArtifacts.devFinder.existsSync()) {
77
      throwToolExit('Fuchsia device-finder tool not found.');
78 79
    }
    final List<String> command = <String>[
80
      _fuchsiaArtifacts.devFinder.path,
81
      'resolve',
82
      if (local) '-local',
83
      '-device-limit', '1',
84
      deviceName,
85
    ];
86
    final RunResult result = await _processUtils.run(command);
87
    if (result.exitCode != 0) {
88
      _logger.printError('device-finder failed: ${result.stderr}');
89 90 91
      return null;
    }
    return result.stdout.trim();
92 93
  }
}