linux_device.dart 3.53 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
import 'package:process/process.dart';
6

7
import '../base/file_system.dart';
8
import '../base/logger.dart';
9
import '../base/os.dart';
10
import '../base/platform.dart';
11
import '../build_info.dart';
12
import '../desktop_device.dart';
13
import '../device.dart';
14
import '../features.dart';
15 16 17
import '../project.dart';
import 'application_package.dart';
import 'build_linux.dart';
18 19 20
import 'linux_workflow.dart';

/// A device that represents a desktop Linux target.
21
class LinuxDevice extends DesktopDevice {
22
  LinuxDevice({
23 24 25 26
    required ProcessManager processManager,
    required Logger logger,
    required FileSystem fileSystem,
    required OperatingSystemUtils operatingSystemUtils,
27 28 29 30 31 32 33 34 35 36 37 38 39
  })  : _operatingSystemUtils = operatingSystemUtils,
        super(
          'linux',
          platformType: PlatformType.linux,
          ephemeral: false,
          logger: logger,
          processManager: processManager,
          fileSystem: fileSystem,
          operatingSystemUtils: operatingSystemUtils,
        );

  final OperatingSystemUtils _operatingSystemUtils;

40 41 42 43
  @override
  bool isSupported() => true;

  @override
44
  String get name => 'Linux';
45 46

  @override
47 48 49
  late final Future<TargetPlatform> targetPlatform = () async {
    if (_operatingSystemUtils.hostPlatform == HostPlatform.linux_x64) {
      return TargetPlatform.linux_x64;
50
    }
51 52
    return TargetPlatform.linux_arm64;
  }();
53 54

  @override
55 56 57
  bool isSupportedForProject(FlutterProject flutterProject) {
    return flutterProject.linux.existsSync();
  }
58 59

  @override
60
  Future<void> buildForDevice(
61
    covariant LinuxApp package, {
62 63
    String? mainPath,
    required BuildInfo buildInfo,
64
  }) async {
65 66 67 68
    await buildLinux(
      FlutterProject.current().linux,
      buildInfo,
      target: mainPath,
69
      targetPlatform: await targetPlatform,
70
    );
71 72 73
  }

  @override
74 75
  String executablePathForDevice(covariant LinuxApp package, BuildMode buildMode) {
    return package.executable(buildMode);
76
  }
77 78 79
}

class LinuxDevices extends PollingDeviceDiscovery {
80
  LinuxDevices({
81 82 83 84 85 86
    required Platform platform,
    required FeatureFlags featureFlags,
    required OperatingSystemUtils operatingSystemUtils,
    required FileSystem fileSystem,
    required ProcessManager processManager,
    required Logger logger,
87
  }) : _platform = platform,
88 89 90 91
       _linuxWorkflow = LinuxWorkflow(
          platform: platform,
          featureFlags: featureFlags,
       ),
92
       _fileSystem = fileSystem,
93
       _logger = logger,
94 95
       _processManager = processManager,
       _operatingSystemUtils = operatingSystemUtils,
96 97 98 99
       super('linux devices');

  final Platform _platform;
  final LinuxWorkflow _linuxWorkflow;
100 101
  final ProcessManager _processManager;
  final Logger _logger;
102
  final FileSystem _fileSystem;
103
  final OperatingSystemUtils _operatingSystemUtils;
104 105

  @override
106
  bool get supportsPlatform => _platform.isLinux;
107 108

  @override
109
  bool get canListAnything => _linuxWorkflow.canListDevices;
110 111

  @override
112
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
113 114 115 116
    if (!canListAnything) {
      return const <Device>[];
    }
    return <Device>[
117 118 119
      LinuxDevice(
        logger: _logger,
        processManager: _processManager,
120
        fileSystem: _fileSystem,
121
        operatingSystemUtils: _operatingSystemUtils,
122
      ),
123 124 125 126 127
    ];
  }

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

  @override
  List<String> get wellKnownIds => const <String>['linux'];
131
}