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

5
import 'dart:async';
Devon Carew's avatar
Devon Carew committed
6
import 'dart:convert';
7
import 'dart:io';
Devon Carew's avatar
Devon Carew committed
8

9
late Process daemon;
Devon Carew's avatar
Devon Carew committed
10

11 12 13 14
// To use, start from the console and enter:
//   version: print version
//   shutdown: terminate the server
//   start: start an app
15
//   stop: stop a running app
16
//   devices: list devices
17
//   emulators: list emulators
18
//   emulator-launch: launch an emulator, append the word coldBoot to cold boot the emulator.
19

20
Future<void> main() async {
21
  daemon = await Process.start('dart', <String>['bin/flutter_tools.dart', 'daemon']);
Devon Carew's avatar
Devon Carew committed
22 23 24
  print('daemon process started, pid: ${daemon.pid}');

  daemon.stdout
25 26
    .transform<String>(utf8.decoder)
    .transform<String>(const LineSplitter())
27
    .listen((String line) => print('<== $line'));
28
  daemon.stderr.listen(stderr.add);
Devon Carew's avatar
Devon Carew committed
29 30

  stdout.write('> ');
31
  stdin.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen((String line) {
32
    final List<String> words = line.split(' ');
33

Devon Carew's avatar
Devon Carew committed
34
    if (line == 'version' || line == 'v') {
35
      _send(<String, dynamic>{'method': 'daemon.version'});
Devon Carew's avatar
Devon Carew committed
36
    } else if (line == 'shutdown' || line == 'q') {
37
      _send(<String, dynamic>{'method': 'daemon.shutdown'});
38 39 40
    } else if (words.first == 'start') {
      _send(<String, dynamic>{
        'method': 'app.start',
41
        'params': <String, dynamic>{
42
          'deviceId': words[1],
43 44
          'projectDirectory': words[2],
          'launchMode': words[3],
45
        },
46 47 48 49 50
      });
    } else if (words.first == 'stop') {
      if (words.length > 1) {
        _send(<String, dynamic>{
          'method': 'app.stop',
51
          'params': <String, dynamic>{'appId': words[1]},
52 53 54 55 56 57 58 59
        });
      } else {
        _send(<String, dynamic>{'method': 'app.stop'});
      }
    } else if (words.first == 'restart') {
      if (words.length > 1) {
        _send(<String, dynamic>{
          'method': 'app.restart',
60
          'params': <String, dynamic>{'appId': words[1]},
61 62 63 64
        });
      } else {
        _send(<String, dynamic>{'method': 'app.restart'});
      }
65
    } else if (line == 'devices') {
66
      _send(<String, dynamic>{'method': 'device.getDevices'});
67 68 69 70 71 72
    } else if (line == 'emulators') {
      _send(<String, dynamic>{'method': 'emulator.getEmulators'});
    } else if (words.first == 'emulator-launch') {
      _send(<String, dynamic>{
        'method': 'emulator.launch',
        'params': <String, dynamic>{
73
          'emulatorId': words[1],
74
          if (words.contains('coldBoot'))
75
            'coldBoot': true,
76
        },
77
      });
78 79
    } else if (line == 'enable') {
      _send(<String, dynamic>{'method': 'device.enable'});
Devon Carew's avatar
Devon Carew committed
80
    } else {
81
      _send(<String, dynamic>{'method': line.trim()});
Devon Carew's avatar
Devon Carew committed
82 83 84 85
    }
    stdout.write('> ');
  });

86
  // Print in the callback can't fail.
87
  unawaited(daemon.exitCode.then<int>((int code) {
88
    print('daemon exiting ($code)');
Devon Carew's avatar
Devon Carew committed
89
    exit(code);
90
  }));
Devon Carew's avatar
Devon Carew committed
91 92 93 94
}

int id = 0;

95
void _send(Map<String, dynamic> map) {
Devon Carew's avatar
Devon Carew committed
96
  map['id'] = id++;
97
  final String str = '[${json.encode(map)}]';
Devon Carew's avatar
Devon Carew committed
98
  daemon.stdin.writeln(str);
99
  print('==> $str');
Devon Carew's avatar
Devon Carew committed
100
}