linux_device.dart 4.03 KB
Newer Older
1 2 3 4 5
// Copyright 2018 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 '../application_package.dart';
6
import '../base/io.dart';
7 8
import '../base/os.dart';
import '../base/platform.dart';
9
import '../base/process_manager.dart';
10
import '../build_info.dart';
11
import '../desktop.dart';
12
import '../device.dart';
13 14 15 16 17
import '../globals.dart';
import '../project.dart';
import '../protocol_discovery.dart';
import 'application_package.dart';
import 'build_linux.dart';
18 19 20 21 22 23 24
import 'linux_workflow.dart';

/// A device that represents a desktop Linux target.
class LinuxDevice extends Device {
  LinuxDevice() : super('Linux');

  @override
25
  void clearLogs() { }
26 27

  @override
28 29 30 31
  DeviceLogReader getLogReader({ ApplicationPackage app }) {
    return _logReader;
  }
  final DesktopLogReader _logReader = DesktopLogReader();
32

33 34
  // Since the host and target devices are the same, no work needs to be done
  // to install the application.
35
  @override
36
  Future<bool> installApp(ApplicationPackage app) async => true;
37

38 39
  // Since the host and target devices are the same, no work needs to be done
  // to install the application.
40
  @override
41
  Future<bool> isAppInstalled(ApplicationPackage app) async => true;
42

43 44
  // Since the host and target devices are the same, no work needs to be done
  // to install the application.
45
  @override
46
  Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => true;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  @override
  Future<bool> get isLocalEmulator async => false;

  @override
  bool isSupported() => true;

  @override
  String get name => 'Linux';

  @override
  DevicePortForwarder get portForwarder => const NoOpDevicePortForwarder();

  @override
  Future<String> get sdkNameAndVersion async => os.name;

  @override
64
  Future<LaunchResult> startApp(
65
    covariant LinuxApp package, {
66 67 68 69 70 71 72
    String mainPath,
    String route,
    DebuggingOptions debuggingOptions,
    Map<String, dynamic> platformArgs,
    bool prebuiltApplication = false,
    bool usesTerminalUi = true,
    bool ipv6 = false,
73
  }) async {
74
    _lastBuiltMode = debuggingOptions.buildInfo.mode;
75
    if (!prebuiltApplication) {
76
      await buildLinux(FlutterProject.current().linux, debuggingOptions.buildInfo);
77 78 79 80 81 82 83 84
    }
    await stopApp(package);
    final Process process = await processManager.start(<String>[
      package.executable(debuggingOptions?.buildInfo?.mode)
    ]);
    if (debuggingOptions?.buildInfo?.isRelease == true) {
      return LaunchResult.succeeded();
    }
85 86
    _logReader.initializeProcess(process);
    final ProtocolDiscovery observatoryDiscovery = ProtocolDiscovery.observatory(_logReader);
87 88 89 90 91 92 93 94 95
    try {
      final Uri observatoryUri = await observatoryDiscovery.uri;
      return LaunchResult.succeeded(observatoryUri: observatoryUri);
    } catch (error) {
      printError('Error waiting for a debug connection: $error');
      return LaunchResult.failed();
    } finally {
      await observatoryDiscovery.cancel();
    }
96 97 98
  }

  @override
99
  Future<bool> stopApp(covariant LinuxApp app) async {
100
    return killProcess(app.executable(_lastBuiltMode));
101 102 103 104 105
  }

  @override
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.linux_x64;

106 107
  // Since the host and target devices are the same, no work needs to be done
  // to uninstall the application.
108
  @override
109
  Future<bool> uninstallApp(ApplicationPackage app) async => true;
110

111 112 113 114 115
  @override
  bool isSupportedForProject(FlutterProject flutterProject) {
    return flutterProject.linux.existsSync();
  }

116 117
  // Track the last built mode from startApp.
  BuildMode _lastBuiltMode;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

class LinuxDevices extends PollingDeviceDiscovery {
  LinuxDevices() : super('linux devices');

  @override
  bool get supportsPlatform => platform.isLinux;

  @override
  bool get canListAnything => linuxWorkflow.canListDevices;

  @override
  Future<List<Device>> pollingGetDevices() async {
    if (!canListAnything) {
      return const <Device>[];
    }
    return <Device>[
135
      LinuxDevice(),
136 137 138 139 140 141
    ];
  }

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