microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
48ca0c6243e4cda833290b57fec530369e205179

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/debuggingConfiguration/reactNativeDebugDynamicConfigProvider.ts

175lines · modeblame

e7a2c40dRedMickey4 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as vscode from "vscode";
5import {
6debugConfigurations,
7DEBUG_CONFIGURATION_NAMES,
8DebugScenarioType,
9DebugConfigurationState,
10} from "./debugConfigTypesAndConstants";
11import { SettingsHelper } from "../settingsHelper";
12import { TelemetryHelper } from "../../common/telemetryHelper";
13import { Telemetry } from "../../common/telemetry";
14import { ProjectVersionHelper } from "../../common/projectVersionHelper";
15import { ReactNativeProjectHelper } from "../../common/reactNativeProjectHelper";
16import { PlatformType } from "../launchArgs";
17import { MultiStepInput, InputStep } from "./multiStepInput";
18import { ILaunchRequestArgs } from "../../debugger/debugSessionBase";
19import { ConfigurationProviderHelper } from "../../common/configurationProviderHelper";
20
21export class ReactNativeDebugDynamicConfigProvider implements vscode.DebugConfigurationProvider {
22public async provideDebugConfigurations(
23folder: vscode.WorkspaceFolder | undefined,
24token?: vscode.CancellationToken, // eslint-disable-line @typescript-eslint/no-unused-vars
25): Promise<vscode.DebugConfiguration[]> {
26let debugConfigurationsToShow = Object.assign({}, debugConfigurations);
27
28if (folder) {
29let rootPath = folder.uri.fsPath;
30let projectRootPath = SettingsHelper.getReactNativeProjectRoot(rootPath);
31const versions = await ProjectVersionHelper.tryToGetRNSemverValidVersionsFromProjectPackage(
32projectRootPath,
33ProjectVersionHelper.generateAllAdditionalPackages(),
34projectRootPath,
35);
36
37let macOSHermesEnabled = false;
38let windowsHermesEnabled = false;
39let androidHermesEnabled = ReactNativeProjectHelper.isAndroidHermesEnabled(rootPath);
40let iOSHermesEnabled = ReactNativeProjectHelper.isIOSHermesEnabled(rootPath);
41
42if (ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
43delete debugConfigurationsToShow[DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS];
44delete debugConfigurationsToShow[
45DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS_HERMES_EXPERIMENTAL
46];
47} else {
48windowsHermesEnabled = ReactNativeProjectHelper.isWindowsHermesEnabled(rootPath);
49if (!windowsHermesEnabled) {
50delete debugConfigurationsToShow[
51DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS_HERMES_EXPERIMENTAL
52];
53}
54}
55
56if (ProjectVersionHelper.isVersionError(versions.reactNativeMacOSVersion)) {
57delete debugConfigurationsToShow[DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS];
58delete debugConfigurationsToShow[
59DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS_HERMES_EXPERIMENTAL
60];
61} else {
62macOSHermesEnabled = ReactNativeProjectHelper.isMacOSHermesEnabled(rootPath);
63if (!macOSHermesEnabled) {
64delete debugConfigurationsToShow[
65DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS_HERMES_EXPERIMENTAL
66];
67}
68}
69
70if (!androidHermesEnabled) {
71delete debugConfigurationsToShow[
72DEBUG_CONFIGURATION_NAMES.DEBUG_ANDROID_HERMES_EXPERIMENTAL
73];
74delete debugConfigurationsToShow[
75DEBUG_CONFIGURATION_NAMES.RUN_ANDROID_HERMES_EXPERIMENTAL
76];
77}
78
79if (!iOSHermesEnabled) {
80delete debugConfigurationsToShow[
81DEBUG_CONFIGURATION_NAMES.DEBUG_IOS_HERMES_EXPERIMENTAL
82];
83delete debugConfigurationsToShow[
84DEBUG_CONFIGURATION_NAMES.RUN_IOS_HERMES_EXPERIMENTAL
85];
86}
87
88if (
89!androidHermesEnabled &&
90!iOSHermesEnabled &&
91!macOSHermesEnabled &&
92!windowsHermesEnabled
93) {
94delete debugConfigurationsToShow[
95DEBUG_CONFIGURATION_NAMES.ATTACH_TO_HERMES_APPLICATION_EXPERIMENTAL
96];
97}
98}
99
100const debugConfigurationsToShowList = Object.values(debugConfigurationsToShow);
101debugConfigurationsToShowList.forEach(config => {
102config.isDynamic = true;
103});
104
105return debugConfigurationsToShowList;
106}
107
108public async resolveDebugConfiguration(
109folder: vscode.WorkspaceFolder | undefined,
110config: vscode.DebugConfiguration,
111token?: vscode.CancellationToken,
112): Promise<vscode.ProviderResult<vscode.DebugConfiguration>> {
113if (config.isDynamic) {
114const chosenConfigsEvent = TelemetryHelper.createTelemetryEvent(
115"chosenDynamicDebugConfiguration",
116{
117selectedConfiguration: config.name,
118},
119);
120Telemetry.send(chosenConfigsEvent);
121if (config.request === "attach") {
122await this.configureAttachScenario(folder, config, token);
123}
124if (config.platform === PlatformType.Exponent) {
125await this.configureExpoScenario(folder, config, token);
126}
127}
128
129return config;
130}
131
132private async configureExpoScenario(
133folder: vscode.WorkspaceFolder | undefined,
134config: vscode.DebugConfiguration,
135token?: vscode.CancellationToken,
136): Promise<vscode.DebugConfiguration> {
137const state = { config, scenarioType: DebugScenarioType.DebugApp, folder, token };
138const picker = new MultiStepInput<DebugConfigurationState>();
139const configurationProviderHelper = new ConfigurationProviderHelper();
140await picker.run(async (input, s) => {
141await configurationProviderHelper.selectExpoHostType(input, s.config, 1, 1);
142}, state);
143
144return config;
145}
146
147private async configureAttachScenario(
148folder: vscode.WorkspaceFolder | undefined,
149config: vscode.DebugConfiguration,
150token?: vscode.CancellationToken,
151): Promise<vscode.DebugConfiguration> {
152const state = { config, scenarioType: DebugScenarioType.AttachApp, folder, token };
153const picker = new MultiStepInput<DebugConfigurationState>();
154const configurationProviderHelper = new ConfigurationProviderHelper();
155
156const configurePort = async (
157input: MultiStepInput<DebugConfigurationState>,
158config: Partial<ILaunchRequestArgs>,
159): Promise<InputStep<DebugConfigurationState> | void> => {
160await configurationProviderHelper.configurePort(input, config, 2, 2);
161};
162
163const configureAddress = async (
164input: MultiStepInput<DebugConfigurationState>,
165config: Partial<ILaunchRequestArgs>,
166): Promise<InputStep<DebugConfigurationState> | void> => {
167await configurationProviderHelper.configureAddress(input, config, 1, 2, "localhost");
168return () => configurePort(input, config);
169};
170
171await picker.run((input, s) => configureAddress(input, s.config), state);
172
173return config;
174}
175}