flutterRunsOn function

Future<List<({String dir, int pid})>> flutterRunsOn(
  1. String deviceId, {
  2. Set<int> except = const {},
})

Other flutter run processes debugging deviceId, with their project folders; a phone runs one Xcode debug session at a time.

Implementation

Future<List<({int pid, String dir})>> flutterRunsOn(String deviceId, {Set<int> except = const {}}) async {
  final ps = await runBounded('ps', ['-Ao', 'pid=,command='], timeout: const Duration(seconds: 5), phase: 'ps');
  final runs = <({int pid, String dir})>[];
  for (final line in '${ps.stdout}'.split('\n')) {
    final m = RegExp(r'^\s*(\d+)\s+.*flutter_tools\.snapshot (?:run|attach) .*-d ' + RegExp.escape(deviceId))
        .firstMatch(line);
    if (m == null) continue;
    final pid = int.parse(m.group(1)!);
    if (except.contains(pid)) continue;
    final cwd = await runBounded('lsof', ['-a', '-p', '$pid', '-d', 'cwd', '-Fn'],
        timeout: const Duration(seconds: 5), phase: 'lsof');
    final dir =
        '${cwd.stdout}'.split('\n').where((l) => l.startsWith('n')).map((l) => l.substring(1)).firstOrNull ?? '?';
    runs.add((pid: pid, dir: dir));
  }
  return runs;
}