windows_device.dart 2.94 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter 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 7
import 'package:meta/meta.dart';

import '../base/io.dart';
8
import '../base/process.dart';
9
import '../build_info.dart';
10
import '../desktop_device.dart';
11
import '../device.dart';
12
import '../globals.dart' as globals;
13 14 15
import '../project.dart';
import 'application_package.dart';
import 'build_windows.dart';
16 17 18
import 'windows_workflow.dart';

/// A device that represents a desktop Windows target.
19
class WindowsDevice extends DesktopDevice {
20
  WindowsDevice() : super(
21
      'windows',
22 23 24
      platformType: PlatformType.windows,
      ephemeral: false,
  );
25 26 27 28 29

  @override
  bool isSupported() => true;

  @override
30
  String get name => 'Windows';
31 32

  @override
33
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.windows_x64;
34 35

  @override
36 37 38
  bool isSupportedForProject(FlutterProject flutterProject) {
    return flutterProject.windows.existsSync();
  }
39 40

  @override
41
  Future<void> buildForDevice(
42
    covariant WindowsApp package, {
43
    String mainPath,
44
    BuildInfo buildInfo,
45
  }) async {
46 47 48 49
    await buildWindows(
      FlutterProject.current().windows,
      buildInfo,
      target: mainPath,
50
    );
51 52 53
  }

  @override
54 55
  String executablePathForDevice(covariant WindowsApp package, BuildMode buildMode) {
    return package.executable(buildMode);
56
  }
57 58 59 60 61 62
}

class WindowsDevices extends PollingDeviceDiscovery {
  WindowsDevices() : super('windows devices');

  @override
63
  bool get supportsPlatform => globals.platform.isWindows;
64 65 66 67 68

  @override
  bool get canListAnything => windowsWorkflow.canListDevices;

  @override
69
  Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
70 71 72 73
    if (!canListAnything) {
      return const <Device>[];
    }
    return <Device>[
74
      WindowsDevice(),
75 76 77 78 79 80
    ];
  }

  @override
  Future<List<String>> getDiagnostics() async => const <String>[];
}
81

82
final RegExp _whitespace = RegExp(r'\s+');
83 84 85 86 87 88 89

/// Returns the running process matching `process` name.
///
/// This list contains the process name and id.
@visibleForTesting
List<String> runningProcess(String processName) {
  // TODO(jonahwilliams): find a way to do this without powershell.
90
  final RunResult result = processUtils.runSync(
91 92
    <String>['powershell', '-script="Get-CimInstance Win32_Process"'],
  );
93 94 95
  if (result.exitCode != 0) {
    return null;
  }
96
  for (final String rawProcess in result.stdout.split('\n')) {
97 98 99 100 101
    final String process = rawProcess.trim();
    if (!process.contains(processName)) {
      continue;
    }
    final List<String> parts = process.split(_whitespace);
102 103 104 105 106 107 108

    final String processPid = parts[0];
    final String currentRunningProcessPid = pid.toString();
    // Don't kill the flutter tool process
    if (processPid == currentRunningProcessPid) {
      continue;
    }
109
    final List<String> data = <String>[
110
      processPid, // ID
111 112 113 114 115 116
      parts[1], // Name
    ];
    return data;
  }
  return null;
}