linux_device.dart 3.58 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
  })  : _operatingSystemUtils = operatingSystemUtils,
28
        _logger = logger,
29 30 31 32 33 34 35 36 37 38 39
        super(
          'linux',
          platformType: PlatformType.linux,
          ephemeral: false,
          logger: logger,
          processManager: processManager,
          fileSystem: fileSystem,
          operatingSystemUtils: operatingSystemUtils,
        );

  final OperatingSystemUtils _operatingSystemUtils;
40
  final Logger _logger;
41

42 43 44 45
  @override
  bool isSupported() => true;

  @override
46
  String get name => 'Linux';
47 48

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

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

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

  @override
76 77
  String executablePathForDevice(covariant LinuxApp package, BuildInfo buildInfo) {
    return package.executable(buildInfo.mode);
78
  }
79 80 81
}

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

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

  @override
108
  bool get supportsPlatform => _platform.isLinux;
109 110

  @override
111
  bool get canListAnything => _linuxWorkflow.canListDevices;
112 113

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

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

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