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

import '../base/common.dart';
import '../base/process.dart';
7
import '../globals.dart' as globals;
8 9
import 'fuchsia_sdk.dart';

10
// Usage: device-finder <flags> <subcommand> <subcommand args>
11 12 13 14 15 16 17 18 19
//
// 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

20
/// A simple wrapper for the Fuchsia SDK's 'device-finder' tool.
21 22 23 24 25
class FuchsiaDevFinder {
  /// Returns a list of attached devices as a list of strings with entries
  /// formatted as follows:
  /// 192.168.42.172 scare-cable-skip-joy
  Future<List<String>> list() async {
26 27
    if (fuchsiaArtifacts.devFinder == null ||
        !fuchsiaArtifacts.devFinder.existsSync()) {
28
      throwToolExit('Fuchsia device-finder tool not found.');
29 30 31 32
    }
    final List<String> command = <String>[
      fuchsiaArtifacts.devFinder.path,
      'list',
33
      '-full',
34
    ];
35
    final RunResult result = await processUtils.run(command);
36
    if (result.exitCode != 0) {
37
      globals.printError('device-finder failed: ${result.stderr}');
38 39 40
      return null;
    }
    return result.stdout.split('\n');
41 42
  }

43 44 45 46
  /// Returns the address of the named device.
  ///
  /// If local is true, then gives the address by which the device reaches the
  /// host.
47 48 49
  ///
  /// The string [deviceName] should be the name of the device from the
  /// 'list' command, e.g. 'scare-cable-skip-joy'.
50
  Future<String> resolve(String deviceName, {bool local = false}) async {
51 52
    if (fuchsiaArtifacts.devFinder == null ||
        !fuchsiaArtifacts.devFinder.existsSync()) {
53
      throwToolExit('Fuchsia device-finder tool not found.');
54 55 56 57
    }
    final List<String> command = <String>[
      fuchsiaArtifacts.devFinder.path,
      'resolve',
58
      if (local) '-local',
59
      '-device-limit', '1',
60
      deviceName,
61
    ];
62
    final RunResult result = await processUtils.run(command);
63
    if (result.exitCode != 0) {
64
      globals.printError('device-finder failed: ${result.stderr}');
65 66 67
      return null;
    }
    return result.stdout.trim();
68 69
  }
}