macos_device.dart 2.49 KB
Newer Older
1 2 3 4
// 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.

5
import '../base/io.dart';
6
import '../base/platform.dart';
7
import '../base/process_manager.dart';
8
import '../build_info.dart';
9
import '../desktop_device.dart';
10
import '../device.dart';
11
import '../macos/application_package.dart';
12 13
import '../project.dart';
import 'build_macos.dart';
14 15 16
import 'macos_workflow.dart';

/// A device that represents a desktop MacOS target.
17
class MacOSDevice extends DesktopDevice {
18 19 20 21 22
  MacOSDevice() : super(
      'macOS',
      platformType: PlatformType.macos,
      ephemeral: false,
  );
23 24 25 26 27

  @override
  bool isSupported() => true;

  @override
28
  String get name => 'macOS';
29 30

  @override
31
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.darwin_x64;
32 33

  @override
34 35 36
  bool isSupportedForProject(FlutterProject flutterProject) {
    return flutterProject.macos.existsSync();
  }
37 38

  @override
39
  Future<void> buildForDevice(
40
    covariant MacOSApp package, {
41
    String mainPath,
42
    BuildInfo buildInfo,
43
  }) async {
44 45 46 47 48
    await buildMacOS(
      flutterProject: FlutterProject.current(),
      buildInfo: buildInfo,
      targetOverride: mainPath,
    );
49 50
  }

51
  @override
52 53
  String executablePathForDevice(covariant MacOSApp package, BuildMode buildMode) {
    return package.executable(buildMode);
54 55 56
  }

  @override
57 58 59 60 61 62 63 64 65 66 67 68
  void onAttached(covariant MacOSApp package, BuildMode buildMode, Process process) {
    // Bring app to foreground. Ideally this would be done post-launch rather
    // than post-attach, since this won't run for release builds, but there's
    // no general-purpose way of knowing when a process is far enoug along in
    // the launch process for 'open' to foreground it.
    processManager.run(<String>[
      'open', package.applicationBundle(buildMode),
    ]).then((ProcessResult result) {
      if (result.exitCode != 0) {
        print('Failed to foreground app; open returned ${result.exitCode}');
      }
    });
69
  }
70 71 72
}

class MacOSDevices extends PollingDeviceDiscovery {
73
  MacOSDevices() : super('macOS devices');
74 75 76 77 78 79 80 81 82 83 84 85 86

  @override
  bool get supportsPlatform => platform.isMacOS;

  @override
  bool get canListAnything => macOSWorkflow.canListDevices;

  @override
  Future<List<Device>> pollingGetDevices() async {
    if (!canListAnything) {
      return const <Device>[];
    }
    return <Device>[
87
      MacOSDevice(),
88 89 90 91 92 93
    ];
  }

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