microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/debuggingConfiguration/reactNativeDebugConfigProvider.ts

259lines · modecode

1// 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 * as nls from "vscode-nls";
6import { TelemetryHelper } from "../../common/telemetryHelper";
7import { Telemetry } from "../../common/telemetry";
8import { ILaunchRequestArgs } from "../../debugger/debugSessionBase";
9import {
10 debugConfigurations,
11 DEBUG_CONFIGURATION_NAMES,
12 DEBUG_TYPES,
13 DebugScenarioType,
14 DebugConfigurationQuickPickItem,
15 DebugConfigurationState,
16} from "./debugConfigTypesAndConstants";
17import { DebugScenarioNameGenerator } from "./debugScenarioNameGenerator";
18
19import { MultiStepInput, IMultiStepInput, InputStep, IQuickPickParameters } from "./multiStepInput";
20import { ConfigProviderFactory } from "./configurationProviders/configProviderFactory";
21
22nls.config({
23 messageFormat: nls.MessageFormat.bundle,
24 bundleFormat: nls.BundleFormat.standalone,
25})();
26const localize = nls.loadMessageBundle();
27
28export class ReactNativeDebugConfigProvider implements vscode.DebugConfigurationProvider {
29 private initialPickConfig: ReadonlyArray<vscode.QuickPickItem> = [
30 {
31 label: DEBUG_CONFIGURATION_NAMES.DEBUG_ANDROID,
32 description: localize("DebugAndroidConfigDesc", "Run and debug Android application"),
33 },
34 {
35 label: DEBUG_CONFIGURATION_NAMES.RUN_ANDROID,
36 description: localize("RunAndroidConfigDesc", "Run Android application"),
37 },
38 {
39 label: DEBUG_CONFIGURATION_NAMES.DEBUG_IOS,
40 description: localize("DebugiOSConfigDesc", "Run and debug iOS application"),
41 },
42 {
43 label: DEBUG_CONFIGURATION_NAMES.RUN_IOS,
44 description: localize("RuniOSConfigDesc", "Run iOS application"),
45 },
46 {
47 label: DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS,
48 description: localize("DebugWindowsConfigDesc", "Run and debug Windows application"),
49 },
50 {
51 label: DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS,
52 description: localize("DebugmacOSConfigDesc", "Run and debug macOS application"),
53 },
54 {
55 label: DEBUG_CONFIGURATION_NAMES.ATTACH_TO_PACKAGER,
56 description: localize(
57 "AttachToPackagerConfigDesc",
58 "Attach to already working application packager",
59 ),
60 },
61 {
62 label: DEBUG_CONFIGURATION_NAMES.DEBUG_IN_EXPONENT,
63 description: localize(
64 "DebugExpoConfigDesc",
65 "Debug Expo application or React Native application in Expo",
66 ),
67 },
68 {
69 label: DEBUG_CONFIGURATION_NAMES.DEBUG_ANDROID_HERMES_EXPERIMENTAL,
70 description: localize(
71 "DebugAndroidHermesConfigDesc",
72 "Run and debug Android Hermes application",
73 ),
74 },
75 {
76 label: DEBUG_CONFIGURATION_NAMES.RUN_ANDROID_HERMES_EXPERIMENTAL,
77 description: localize("RunAndroidHermesConfigDesc", "Run Android Hermes application"),
78 },
79 {
80 label: DEBUG_CONFIGURATION_NAMES.DEBUG_IOS_HERMES_EXPERIMENTAL,
81 description: localize(
82 "DebugIosHermesConfigDesc",
83 "Run and debug iOS Hermes application",
84 ),
85 },
86 {
87 label: DEBUG_CONFIGURATION_NAMES.RUN_IOS_HERMES_EXPERIMENTAL,
88 description: localize("RunIosHermesConfigDesc", "Run iOS Hermes application"),
89 },
90 {
91 label: DEBUG_CONFIGURATION_NAMES.DEBUG_MACOS_HERMES_EXPERIMENTAL,
92 description: localize(
93 "DebugMacOSHermesConfigDesc",
94 "Run and debug macOS Hermes application",
95 ),
96 },
97 {
98 label: DEBUG_CONFIGURATION_NAMES.DEBUG_WINDOWS_HERMES_EXPERIMENTAL,
99 description: localize(
100 "DebugWindowsHermesConfigDesc",
101 "Run and debug Windows Hermes application",
102 ),
103 },
104 {
105 label: DEBUG_CONFIGURATION_NAMES.ATTACH_TO_HERMES_APPLICATION_EXPERIMENTAL,
106 description: localize(
107 "AttachToPackagerHermesConfigDesc",
108 "Attach to already working React Native Hermes application on Android directly",
109 ),
110 },
111 {
112 label: DEBUG_CONFIGURATION_NAMES.DEBUG_DIRECT_IOS_EXPERIMENTAL,
113 description: localize(
114 "DebugDirectiOSConfigDesc",
115 "Run and debug iOS application directly",
116 ),
117 },
118 {
119 label: DEBUG_CONFIGURATION_NAMES.RUN_DIRECT_IOS_EXPERIMENTAL,
120 description: localize(
121 "RunDirectiOSConfigDesc",
122 "Run iOS application with direct debugging support",
123 ),
124 },
125 {
126 label: DEBUG_CONFIGURATION_NAMES.ATTACH_TO_DIRECT_IOS_EXPERIMENTAL,
127 description: localize(
128 "AttachToPackageriOSConfigDesc",
129 "Attach to already working React Native iOS application directly",
130 ),
131 },
132 ];
133
134 private sequentialPickConfig: ReadonlyArray<DebugConfigurationQuickPickItem> = [
135 {
136 label: "Run application",
137 description: localize(
138 "RunApplicationScenario",
139 "Run React Native application without debugging",
140 ),
141 type: DebugScenarioType.RunApp,
142 },
143 {
144 label: "Debug application",
145 description: localize("DebugApplicationScenario", "Debug React Native application"),
146 type: DebugScenarioType.DebugApp,
147 },
148 {
149 label: "Attach to application",
150 description: localize(
151 "AttachApplicationScenario",
152 "Attach to running React Native application",
153 ),
154 type: DebugScenarioType.AttachApp,
155 },
156 ];
157
158 public async provideDebugConfigurations(
159 folder: vscode.WorkspaceFolder | undefined, // eslint-disable-line @typescript-eslint/no-unused-vars
160 token?: vscode.CancellationToken, // eslint-disable-line @typescript-eslint/no-unused-vars
161 ): Promise<vscode.DebugConfiguration[]> {
162 return new Promise<vscode.DebugConfiguration[]>(resolve => {
163 const configPicker = this.prepareDebugConfigPicker();
164 const disposables: vscode.Disposable[] = [];
165 const pickHandler = () => {
166 const chosenConfigsEvent = TelemetryHelper.createTelemetryEvent(
167 "chosenDebugConfigurations",
168 );
169 const selected: string[] = configPicker.selectedItems.map(element => element.label);
170 chosenConfigsEvent.properties.selectedItems = selected;
171 Telemetry.send(chosenConfigsEvent);
172 const launchConfig = this.gatherDebugScenarios(selected);
173 disposables.forEach(d => d.dispose());
174 resolve(launchConfig);
175 };
176
177 disposables.push(
178 configPicker.onDidAccept(pickHandler),
179 configPicker.onDidHide(pickHandler),
180 configPicker,
181 );
182
183 configPicker.show();
184 });
185 }
186
187 public async provideDebugConfigurationSequentially(
188 folder: vscode.WorkspaceFolder | undefined,
189 token?: vscode.CancellationToken,
190 ): Promise<vscode.DebugConfiguration | undefined> {
191 const config: Partial<ILaunchRequestArgs> = {};
192 const state = { config, scenarioType: DebugScenarioType.DebugApp, folder, token };
193
194 const multiStep = new MultiStepInput<DebugConfigurationState>();
195 await multiStep.run((input, s) => this.pickDebugConfiguration(input, s), state);
196
197 if (Object.keys(state.config).length === 0) {
198 return;
199 }
200 if (state.config.type === DEBUG_TYPES.REACT_NATIVE_DIRECT) {
201 state.config.name = DebugScenarioNameGenerator.createScenarioName(
202 state.scenarioType,
203 state.config.type,
204 state.config.platform,
205 state.config.useHermesEngine !== false,
206 true,
207 );
208 } else {
209 state.config.name = DebugScenarioNameGenerator.createScenarioName(
210 state.scenarioType,
211 state.config.type || DEBUG_TYPES.REACT_NATIVE,
212 state.config.platform,
213 );
214 }
215 return state.config as vscode.DebugConfiguration;
216 }
217
218 private async pickDebugConfiguration(
219 input: IMultiStepInput<DebugConfigurationState>,
220 state: DebugConfigurationState,
221 ): Promise<InputStep<DebugConfigurationState> | void> {
222 state.config = {};
223 const pick = await input.showQuickPick<
224 DebugConfigurationQuickPickItem,
225 IQuickPickParameters<DebugConfigurationQuickPickItem>
226 >({
227 title: localize("DebugConfigQuickPickSequentialLabel", "Select a debug configuration"),
228 placeholder: "Debug Configuration",
229 activeItem: this.sequentialPickConfig[0],
230 items: this.sequentialPickConfig,
231 });
232 if (pick) {
233 const provider = ConfigProviderFactory.create(pick.type);
234 return provider.buildConfiguration.bind(provider);
235 }
236 }
237
238 private gatherDebugScenarios(selectedItems: string[]): vscode.DebugConfiguration[] {
239 const launchConfig: vscode.DebugConfiguration[] = selectedItems.map(
240 element => debugConfigurations[element],
241 );
242 return launchConfig;
243 }
244
245 private prepareDebugConfigPicker(): vscode.QuickPick<vscode.QuickPickItem> {
246 const debugConfigPicker = vscode.window.createQuickPick();
247 debugConfigPicker.canSelectMany = true;
248 debugConfigPicker.ignoreFocusOut = true;
249 debugConfigPicker.title = localize(
250 "DebugConfigQuickPickLabel",
251 "Pick debug configurations",
252 );
253 debugConfigPicker.items = this.initialPickConfig;
254 // QuickPickItem property `picked` doesn't work, so this line will check first item in the list
255 // which is supposed to be Debug Android
256 debugConfigPicker.selectedItems = [this.initialPickConfig[0]];
257 return debugConfigPicker;
258 }
259}
260