macos_device.dart 4.95 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 '../cache.dart';
12
import '../desktop.dart';
13
import '../device.dart';
14 15
import '../globals.dart';
import '../macos/application_package.dart';
16
import '../project.dart';
17
import '../protocol_discovery.dart';
18
import 'build_macos.dart';
19 20 21 22
import 'macos_workflow.dart';

/// A device that represents a desktop MacOS target.
class MacOSDevice extends Device {
23 24 25 26 27 28
  MacOSDevice() : super(
      'macOS',
      category: Category.desktop,
      platformType: PlatformType.macos,
      ephemeral: false,
  );
29 30

  @override
31
  void clearLogs() { }
32 33

  @override
34 35 36 37
  DeviceLogReader getLogReader({ ApplicationPackage app }) {
    return _deviceLogReader;
  }
  final DesktopLogReader _deviceLogReader = DesktopLogReader();
38

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

44 45
  // Since the host and target devices are the same, no work needs to be done
  // to install the application.
46
  @override
47
  Future<bool> isAppInstalled(ApplicationPackage app) async => true;
48

49 50
  // Since the host and target devices are the same, no work needs to be done
  // to install the application.
51
  @override
52
  Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => true;
53 54 55 56

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

57 58 59
  @override
  Future<String> get emulatorId async => null;

60 61 62 63
  @override
  bool isSupported() => true;

  @override
64
  String get name => 'macOS';
65 66 67 68 69 70 71 72

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

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

  @override
73 74
  Future<LaunchResult> startApp(
    covariant MacOSApp package, {
75 76 77 78 79 80
    String mainPath,
    String route,
    DebuggingOptions debuggingOptions,
    Map<String, dynamic> platformArgs,
    bool prebuiltApplication = false,
    bool ipv6 = false,
81
  }) async {
82
    // Stop any running applications with the same executable.
83 84 85
    if (!prebuiltApplication) {
      Cache.releaseLockEarly();
      await buildMacOS(
86 87 88 89
        flutterProject: FlutterProject.current(),
        buildInfo: debuggingOptions?.buildInfo,
        targetOverride: mainPath,
      );
90
    }
91 92

    // Ensure that the executable is locatable.
93 94
    final String executable = package.executable(debuggingOptions?.buildInfo?.mode);
    if (executable == null) {
95 96 97 98
      printError('Unable to find executable to run');
      return LaunchResult.failed();
    }

99
    // Make sure to call stop app after we've built.
100
    _lastBuiltMode = debuggingOptions?.buildInfo?.mode;
101
    await stopApp(package);
102
    final Process process = await processManager.start(<String>[
103
      executable
104 105 106 107
    ]);
    if (debuggingOptions?.buildInfo?.isRelease == true) {
      return LaunchResult.succeeded();
    }
108 109
    _deviceLogReader.initializeProcess(process);
    final ProtocolDiscovery observatoryDiscovery = ProtocolDiscovery.observatory(_deviceLogReader);
110 111
    try {
      final Uri observatoryUri = await observatoryDiscovery.uri;
112 113
      // Bring app to foreground.
      await processManager.run(<String>[
114
        'open', package.applicationBundle(debuggingOptions?.buildInfo?.mode),
115
      ]);
116 117 118 119 120 121 122
      return LaunchResult.succeeded(observatoryUri: observatoryUri);
    } catch (error) {
      printError('Error waiting for a debug connection: $error');
      return LaunchResult.failed();
    } finally {
      await observatoryDiscovery.cancel();
    }
123 124
  }

125 126 127
  // TODO(jonahwilliams): implement using process manager.
  // currently we rely on killing the isolate taking down the application.
  @override
128
  Future<bool> stopApp(covariant MacOSApp app) async {
129
    return killProcess(app.executable(_lastBuiltMode));
130 131 132 133 134
  }

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

135 136
  // Since the host and target devices are the same, no work needs to be done
  // to uninstall the application.
137
  @override
138
  Future<bool> uninstallApp(ApplicationPackage app) async => true;
139 140 141 142 143

  @override
  bool isSupportedForProject(FlutterProject flutterProject) {
    return flutterProject.macos.existsSync();
  }
144 145 146

  // Track the last built mode from startApp.
  BuildMode _lastBuiltMode;
147 148 149
}

class MacOSDevices extends PollingDeviceDiscovery {
150
  MacOSDevices() : super('macOS devices');
151 152 153 154 155 156 157 158 159 160 161 162 163

  @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>[
164
      MacOSDevice(),
165 166 167 168 169 170
    ];
  }

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