fuchsia_dev_finder.dart 2.99 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
// @dart = 2.8

7
import 'package:meta/meta.dart';
8
import 'package:process/process.dart';
9

10
import '../base/common.dart';
11
import '../base/logger.dart';
12 13 14
import '../base/process.dart';
import 'fuchsia_sdk.dart';

15
// Usage: device-finder <flags> <subcommand> <subcommand args>
16 17 18 19 20 21 22 23 24
//
// 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

25
/// A simple wrapper for the Fuchsia SDK's 'device-finder' tool.
26
class FuchsiaDevFinder {
27 28 29 30 31 32 33 34 35 36 37 38 39 40
  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;

41 42
  /// Returns a list of attached devices as a list of strings with entries
  /// formatted as follows:
43 44
  ///
  ///     192.168.42.172 scare-cable-skip-joy
45
  Future<List<String>> list({ Duration timeout }) async {
46 47
    if (_fuchsiaArtifacts.devFinder == null ||
        !_fuchsiaArtifacts.devFinder.existsSync()) {
48
      throwToolExit('Fuchsia device-finder tool not found.');
49 50
    }
    final List<String> command = <String>[
51
      _fuchsiaArtifacts.devFinder.path,
52
      'list',
53
      '-full',
54 55
      if (timeout != null)
        ...<String>['-timeout', '${timeout.inMilliseconds}ms']
56
    ];
57
    final RunResult result = await _processUtils.run(command);
58
    if (result.exitCode != 0) {
59 60 61 62 63
      // 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}');
      }
64 65 66
      return null;
    }
    return result.stdout.split('\n');
67 68
  }

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