desktop.dart 1.95 KB
Newer Older
1
// Copyright 2019 The Chromium 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
import 'dart:async';

7 8
import 'base/io.dart';
import 'base/process_manager.dart';
9 10
import 'convert.dart';
import 'device.dart';
11 12 13

/// Kills a process on linux or macOS.
Future<bool> killProcess(String executable) async {
14 15 16
  if (executable == null) {
    return false;
  }
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  final RegExp whitespace = RegExp(r'\s+');
  bool succeeded = true;
  try {
    final ProcessResult result = await processManager.run(<String>[
      'ps', 'aux',
    ]);
    if (result.exitCode != 0) {
      return false;
    }
    final List<String> lines = result.stdout.split('\n');
    for (String line in lines) {
      if (!line.contains(executable)) {
        continue;
      }
      final List<String> values = line.split(whitespace);
      if (values.length < 2) {
        continue;
      }
35 36 37 38 39 40 41
      final String processPid = values[1];
      final String currentRunningProcessPid = pid.toString();
      // Don't kill the flutter tool process
      if (processPid == currentRunningProcessPid) {
        continue;
      }

42
      final ProcessResult killResult = await processManager.run(<String>[
43
        'kill', processPid,
44 45 46 47 48 49 50 51 52
      ]);
      succeeded &= killResult.exitCode == 0;
    }
    return true;
  } on ArgumentError {
    succeeded = false;
  }
  return succeeded;
}
53 54

class DesktopLogReader extends DeviceLogReader {
55
  final StreamController<List<int>> _inputController = StreamController<List<int>>.broadcast();
56 57

  void initializeProcess(Process process) {
58 59 60 61 62
    process.stdout.listen(_inputController.add);
    process.stderr.listen(_inputController.add);
    process.exitCode.then((int result) {
      _inputController.close();
    });
63 64 65 66
  }

  @override
  Stream<String> get logLines {
67 68 69
    return _inputController.stream
      .transform(utf8.decoder)
      .transform(const LineSplitter());
70 71 72 73 74
  }

  @override
  String get name => 'desktop';
}