daemon_client.dart 1.57 KB
Newer Older
Devon Carew's avatar
Devon Carew committed
1 2 3 4 5 6 7 8 9
// Copyright 2015 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.

import 'dart:convert';
import 'dart:io';

Process daemon;

10 11 12 13 14 15 16
// To use, start from the console and enter:
//   version: print version
//   shutdown: terminate the server
//   start: start an app
//   stopAll: stop any running app
//   devices: list devices

Devon Carew's avatar
Devon Carew committed
17
main() async {
18
  daemon = await Process.start('flutter', ['daemon']);
Devon Carew's avatar
Devon Carew committed
19 20 21 22 23 24 25 26 27 28 29
  print('daemon process started, pid: ${daemon.pid}');

  daemon.stdout
    .transform(UTF8.decoder)
    .transform(const LineSplitter())
    .listen((String line) => print('<== ${line}'));
  daemon.stderr.listen((data) => stderr.add(data));

  stdout.write('> ');
  stdin.transform(UTF8.decoder).transform(const LineSplitter()).listen((String line) {
    if (line == 'version' || line == 'v') {
30
      _send({'method': 'daemon.version'});
Devon Carew's avatar
Devon Carew committed
31
    } else if (line == 'shutdown' || line == 'q') {
32
      _send({'method': 'daemon.shutdown'});
Devon Carew's avatar
Devon Carew committed
33
    } else if (line == 'start') {
34
      _send({'method': 'app.start'});
Devon Carew's avatar
Devon Carew committed
35
    } else if (line == 'stopAll') {
36 37 38
      _send({'method': 'app.stopAll'});
    } else if (line == 'devices') {
      _send({'method': 'device.getDevices'});
Devon Carew's avatar
Devon Carew committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    } else {
      print('command not understood: ${line}');
    }
    stdout.write('> ');
  });

  daemon.exitCode.then((int code) {
    print('daemon exiting (${code})');
    exit(code);
  });
}

int id = 0;

void _send(Map map) {
  map['id'] = id++;
  String str = '[${JSON.encode(map)}]';
  daemon.stdin.writeln(str);
  print('==> ${str}');
}