fuchsia_sdk.dart 2.42 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'dart:async';

7
import '../base/context.dart';
8 9
import '../base/file_system.dart';
import '../base/io.dart';
10
import '../base/process.dart';
11
import '../base/process_manager.dart';
12
import '../convert.dart';
13
import '../globals.dart';
14 15 16 17

/// The [FuchsiaSdk] instance.
FuchsiaSdk get fuchsiaSdk => context[FuchsiaSdk];

18 19 20
/// The [FuchsiaArtifacts] instance.
FuchsiaArtifacts get fuchsiaArtifacts => context[FuchsiaArtifacts];

21 22 23 24 25
/// The Fuchsia SDK shell commands.
///
/// This workflow assumes development within the fuchsia source tree,
/// including a working fx command-line tool in the user's PATH.
class FuchsiaSdk {
26
  static const List<String> _syslogCommand = <String>['fx', 'syslog', '--clock', 'Local'];
27

28
  /// Example output:
29 30 31
  ///    $ dev_finder list -full
  ///    > 192.168.42.56 paper-pulp-bush-angel
  Future<String> listDevices() async {
32
    try {
33 34
      final String path = fuchsiaArtifacts.devFinder.absolute.path;
      final RunResult process = await runAsync(<String>[path, 'list', '-full']);
35
      return process.stdout.trim();
36 37
    } catch (exception) {
      printTrace('$exception');
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    }
    return null;
  }

  /// Returns the fuchsia system logs for an attached device.
  ///
  /// Does not currently support multiple attached devices.
  Stream<String> syslogs() {
    Process process;
    try {
      final StreamController<String> controller = StreamController<String>(onCancel: () {
        process.kill();
      });
      processManager.start(_syslogCommand).then((Process newProcess) {
        if (controller.isClosed) {
          return;
        }
        process = newProcess;
56
        process.exitCode.whenComplete(controller.close);
57 58 59
        controller.addStream(process.stdout.transform(utf8.decoder).transform(const LineSplitter()));
      });
      return controller.stream;
60 61
    } catch (exception) {
      printTrace('$exception');
62 63 64 65
    }
    return null;
  }
}
66 67 68 69

/// Fuchsia-specific artifacts used to interact with a device.
class FuchsiaArtifacts {
  /// Creates a new [FuchsiaArtifacts].
70
  FuchsiaArtifacts({this.sshConfig, this.devFinder});
71 72

  /// The location of the SSH configuration file used to interact with a
73 74 75 76 77 78
  /// Fuchsia device.
  final File sshConfig;

  /// The location of the dev finder tool used to locate connected
  /// Fuchsia devices.
  final File devFinder;
79
}