forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.ts
More file actions
612 lines (565 loc) · 29.6 KB
/
component.ts
File metadata and controls
612 lines (565 loc) · 29.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { platform } from 'os';
import * as path from 'path';
import { which } from 'shelljs';
import { commands, debug, DebugConfiguration, DebugSession, Disposable, EventEmitter, extensions, ProgressLocation, Uri, window, workspace } from 'vscode';
import { Oc } from '../oc/ocWrapper';
import { Command } from '../odo/command';
import { CommandProvider } from '../odo/componentTypeDescription';
import { Odo } from '../odo/odoWrapper';
import { ComponentWorkspaceFolder } from '../odo/workspace';
import { ChildProcessUtil, CliExitData } from '../util/childProcessUtil';
import { Progress } from '../util/progress';
import * as fs from '../util/utils';
import { vsCommand, VsCommandError } from '../vscommand';
import CreateComponentLoader from '../webview/create-component/createComponentLoader';
import { OpenShiftTerminalApi, OpenShiftTerminalManager } from '../webview/openshift-terminal/openShiftTerminal';
import OpenShiftItem, { clusterRequired, projectRequired } from './openshiftItem';
function createStartDebuggerResult(language: string, message = '') {
const result: any = new String(message);
result.properties = {
language
}
return result;
}
export enum ComponentContextState {
DEV = 'dev-nrn',
DEV_STARTING = 'dev-str',
DEV_RUNNING = 'dev-run',
DEV_STOPPING = 'dev-stp',
DEB = 'deb-nrn',
DEB_RUNNING = 'deb-run',
DEP = 'dep-nrn',
DEP_RUNNING = 'dep-run',
}
export class ComponentStateRegex {
public static readonly COMPONENT_DEV_STARTING = /openshift\.component\.dev-str.*/;
public static readonly COMPONENT_DEV_RUNNING = /openshift\.component\.dev-run.*/;
public static readonly COMPONENT_DEV_STOPPING = /openshift\.component\.dev-stp.*/;
public static readonly COMPONENT_DEB_STARTING = /openshift\.component.*\.deb-str.*/;
public static readonly COMPONENT_DEB_RUNNING = /openshift\.component.*\.deb-run.*/;
public static readonly COMPONENT_DEB_STOPPING = /openshift\.component.*\.deb-stp.*/;
public static readonly COMPONENT_DEP_STARTING = /openshift\.component.*\.dep-str.*/;
public static readonly COMPONENT_DEP_RUNNING = /openshift\.component.*\.dep-run.*/;
public static readonly COMPONENT_DEP_STOPPING = /openshift\.component.*\.dep-stp.*/;
}
interface ComponentDevState {
// dev state
devTerminal?: OpenShiftTerminalApi;
devStatus?: ComponentContextState;
contextValue?: string;
devProcessStopRequest?: DevProcessStopRequest;
// debug state
debugStatus?: string;
// deploy state
deployStatus?: string;
runOn?: undefined | 'podman';
}
interface DevProcessStopRequest extends Disposable {
isSigabrtSent: () => boolean;
sendSigabrt: () => void;
}
export class Component extends OpenShiftItem {
private static debugSessions = new Map<string, DebugSession>();
private static stateChanged = new EventEmitter<string>();
public static onDidStateChanged(listener: (context: string) => any) {
Component.stateChanged.event(listener);
}
public static init(): Disposable[] {
return [
debug.onDidStartDebugSession((session) => {
if (session.configuration.contextPath) {
Component.debugSessions.set(session.configuration.contextPath, session);
}
}),
debug.onDidTerminateDebugSession((session) => {
if (session.configuration.contextPath) {
Component.debugSessions.delete(session.configuration.contextPath);
}
})
];
}
private static readonly componentStates = new Map<string, ComponentDevState>();
static getComponentDevState(folder: ComponentWorkspaceFolder): ComponentDevState {
let state = Component.componentStates.get(folder.contextPath);
if (!state) {
state = {
devStatus: folder.component?.devfileData?.supportedOdoFeatures?.dev ? ComponentContextState.DEV : undefined,
debugStatus: folder.component?.devfileData?.supportedOdoFeatures?.debug ? ComponentContextState.DEB : undefined,
deployStatus: folder.component?.devfileData?.supportedOdoFeatures?.deploy ? ComponentContextState.DEP : undefined,
};
this.componentStates.set(folder.contextPath, state);
}
return state;
}
public static generateContextStateSuffixValue(folder: ComponentWorkspaceFolder): string {
const state = Component.componentStates.get(folder.contextPath);
let contextSuffix = '';
if (state.devStatus) {
contextSuffix = contextSuffix.concat('.').concat(state.devStatus);
}
if (state.debugStatus) {
contextSuffix = contextSuffix.concat('.').concat(state.debugStatus);
}
if (state.deployStatus) {
contextSuffix = contextSuffix.concat('.').concat(state.deployStatus);
}
return contextSuffix;
}
public static generateContextValue(folder: ComponentWorkspaceFolder): string {
return `openshift.component${this.generateContextStateSuffixValue(folder)}`;
}
public static renderLabel(folder: ComponentWorkspaceFolder) {
return `${folder.component.devfileData.devfile.metadata.name}${Component.renderStateLabel(folder)}`
};
public static renderStateLabel(folder: ComponentWorkspaceFolder) {
let label = '';
const state = Component.getComponentDevState(folder);
let runningOnSuffix = '';
if (state.runOn) {
runningOnSuffix = ` on ${state.runOn}`;
}
if (state.devStatus === ComponentContextState.DEV_STARTING) {
label = ` (dev starting${runningOnSuffix})`;
} else if(state.devStatus === ComponentContextState.DEV_RUNNING) {
label = ` (dev running${runningOnSuffix})`;
} else if(state.devStatus === ComponentContextState.DEV_STOPPING) {
label = ` (dev stopping${runningOnSuffix})`;
}
return label;
}
@vsCommand('openshift.component.showDevTerminal')
static showDevTerminal(context: ComponentWorkspaceFolder) {
Component.componentStates.get(context.contextPath)?.devTerminal.focusTerminal();
}
static devModeExitTimeout(): number {
return workspace
.getConfiguration('openshiftToolkit')
.get<number>('stopDevModeTimeout');
}
private static exitDevelopmentMode(componentContextPath: string) : DevProcessStopRequest {
const componentState = Component.componentStates.get(componentContextPath);
if (componentState) {
let sigAbortSent = false;
let devCleaningTimeout = setTimeout( () => {
void window.showWarningMessage('Exiting development mode is taking too long.', 'Keep waiting', 'Force exit')
.then((action) => {
if (!devCleaningTimeout) {
void window.showInformationMessage('The warning message has expired and requested action cannot be executed.');
} else {
if (action === 'Keep waiting') {
devCleaningTimeout.refresh();
} else if (action === 'Force exit') {
sigAbortSent = true;
componentState.devTerminal.forceKill();
}
}
});
}, Component.devModeExitTimeout());
return {
dispose: () => {
clearTimeout(devCleaningTimeout);
devCleaningTimeout = undefined;
},
// test devProcess.signalCode approach and switch back to Disposable
isSigabrtSent: () => sigAbortSent,
sendSigabrt: () => {
sigAbortSent = true;
componentState.devTerminal.forceKill();
}
}
}
}
@vsCommand('openshift.component.dev.onPodman')
static async devOnPodman(component: ComponentWorkspaceFolder) {
if (await Component.checkForPodman()) {
return Component.devRunOn(component, 'podman');
}
}
@vsCommand('openshift.component.dev.onPodman.manual')
static async devOnPodmanManualRebuild(component: ComponentWorkspaceFolder) {
if (await Component.checkForPodman()) {
return Component.devRunOn(component, 'podman', true);
}
}
private static async checkForPodman(): Promise<boolean> {
const podmanPath = which('podman');
if (podmanPath) {
if (platform() === 'linux') {
return true;
}
try {
const resultRaw: CliExitData = await ChildProcessUtil.Instance.execute(`"${podmanPath}" machine list --format json`);
const resultObj: { Running: boolean }[] = JSON.parse(resultRaw.stdout);
if (resultObj.length === 1 && resultObj[0].Running) {
return true;
}
} catch {
// do nothing; something is wrong with the podman setup
}
const SETUP_INSTRUCTIONS = 'Open setup instructions';
void window.showErrorMessage('Podman is present on the system, but is not fully set up yet.', SETUP_INSTRUCTIONS)
.then(result => {
if (result === SETUP_INSTRUCTIONS) {
void commands.executeCommand('vscode.open', Uri.parse('https://podman.io/docs/installation'));
}
});
} else {
void window.showErrorMessage('Podman is not present in the system, please install podman on your machine and try again.', 'Install podman')
.then(async (result) => {
if (result === 'Install podman') {
await commands.executeCommand('vscode.open', Uri.parse('https://podman.io/'));
}
});
}
return false;
}
@vsCommand('openshift.component.dev')
@clusterRequired()
@projectRequired()
static async dev(component: ComponentWorkspaceFolder) {
return Component.devRunOn(component, undefined);
}
@vsCommand('openshift.component.dev.manual')
@clusterRequired()
@projectRequired()
static async devManual(component: ComponentWorkspaceFolder): Promise<void> {
await Component.devRunOn(component, undefined, true);
}
static async devRunOn(component: ComponentWorkspaceFolder, runOn?: 'podman', manualRebuild: boolean = false) {
const cs = Component.getComponentDevState(component);
cs.devStatus = ComponentContextState.DEV_STARTING;
cs.runOn = runOn;
Component.stateChanged.fire(component.contextPath)
if (!runOn) {
try {
await Oc.Instance.deleteDeploymentByComponentLabel(component.component.devfileData.devfile.metadata.name);
} catch {
// do nothing, it probably was already deleted
}
}
try {
cs.devTerminal = await OpenShiftTerminalManager.getInstance().createTerminal(
Command.dev(component.component.devfileData.supportedOdoFeatures.debug, runOn, manualRebuild),
`odo dev: ${component.component.devfileData.devfile.metadata.name}`,
component.contextPath,
process.env,
{
onExit() {
if (cs.devProcessStopRequest) {
cs.devProcessStopRequest.dispose();
cs.devProcessStopRequest = undefined;
}
cs.devStatus = ComponentContextState.DEV;
Component.stateChanged.fire(component.contextPath);
},
onText(text: string) {
if (cs.devStatus === ComponentContextState.DEV_STARTING && text.includes('[p]')) {
Component.stateChanged.fire(component.contextPath)
cs.devStatus = ComponentContextState.DEV_RUNNING;
}
if (text.includes('^C')) {
cs.devStatus = ComponentContextState.DEV_STOPPING;
cs.devProcessStopRequest = Component.exitDevelopmentMode(component.contextPath);
Component.stateChanged.fire(component.contextPath);
}
}
},
);
} catch (err) {
void window.showErrorMessage(err.toString());
}
}
@vsCommand('openshift.component.exitDevMode')
static exitDevMode(component: ComponentWorkspaceFolder): Promise<void> {
const componentState = Component.componentStates.get(component.contextPath)
if (componentState) {
componentState.devTerminal.focusTerminal();
componentState.devTerminal.kill();
}
return;
}
@vsCommand('openshift.component.forceExitDevMode')
static forceExitDevMode(component: ComponentWorkspaceFolder): Promise<void> {
const componentState = Component.componentStates.get(component.contextPath)
if (componentState && componentState.devTerminal) {
componentState.devTerminal.focusTerminal();
componentState.devTerminal.forceKill();
Component.stateChanged.fire(component.contextPath)
}
return;
}
@vsCommand('openshift.component.openInBrowser')
@clusterRequired()
static async openInBrowser(component: ComponentWorkspaceFolder): Promise<string | null | undefined> {
const componentDescription = await Odo.Instance.describeComponent(component.contextPath, !!Component.getComponentDevState(component).runOn);
if (componentDescription.devForwardedPorts?.length === 1) {
const fp = componentDescription.devForwardedPorts[0];
await commands.executeCommand('vscode.open', Uri.parse(`http://${fp.localAddress}:${fp.localPort}`));
return;
} else if (componentDescription.devForwardedPorts?.length > 1) {
const ports = componentDescription.devForwardedPorts.map((fp) => ({
label: `${fp.localAddress}:${fp.localPort}`,
description: `Forwards to ${fp.containerName}:${fp.containerPort}`,
}));
const port = await window.showQuickPick(ports, {placeHolder: 'Select a URL to open in default browser'});
if(port) {
await commands.executeCommand('vscode.open', Uri.parse(`http://${port.label}`));
return;
}
return null;
}
return 'No forwarded ports available for component yet. Pleas wait and try again.';
}
static createExperimentalEnv(componentFolder: ComponentWorkspaceFolder) {
return Component.getComponentDevState(componentFolder).runOn ? {ODO_EXPERIMENTAL_MODE: 'true'} : {};
}
static getDevPlatform(componentFolder: ComponentWorkspaceFolder): string {
return Component.getComponentDevState(componentFolder).runOn;
}
@vsCommand('openshift.component.describe', true)
static async describe(componentFolder: ComponentWorkspaceFolder): Promise<void> {
const command = Command.describeComponent();
await OpenShiftTerminalManager.getInstance().executeInTerminal(
command,
componentFolder.contextPath,
`Describe '${componentFolder.component.devfileData.devfile.metadata.name}' Component`);
}
@vsCommand('openshift.component.openCreateComponent')
static async createComponent(): Promise<void> {
await CreateComponentLoader.loadView('Create Component');
}
/**
* Create a component from the root folder in workspace through command palette
*
*/
@vsCommand('openshift.component.createFromRootWorkspaceFolder')
static async createFromRootWorkspaceFolderPalette(context: { fsPath: string}): Promise<void> {
const devFileLocation = path.join(context.fsPath, 'devfile.yaml');
try {
await fs.access(devFileLocation);
await window.showErrorMessage('The selected folder already contains a devfile.');
return;
} catch {
// do nothing
}
await CreateComponentLoader.loadView('Create Component', context.fsPath);
}
@vsCommand('openshift.component.debug', true)
static async debug(component: ComponentWorkspaceFolder): Promise<string | null> {
if (!component) return null;
if (Component.debugSessions.get(component.contextPath)) return Component.startDebugger(component);
return Progress.execFunctionWithProgress(`Starting debugger session for the component '${component.component.devfileData.devfile.metadata.name}'.`, () => Component.startDebugger(component));
}
static async startDebugger(component: ComponentWorkspaceFolder): Promise<string | undefined> {
let result: undefined | string | PromiseLike<string> = null;
if (Component.debugSessions.get(component.contextPath)) {
const choice = await window.showWarningMessage(`Debugger session is already running for ${component.component.devfileData.devfile.metadata.name}.`, 'Show \'Run and Debug\' view');
if (choice) {
await commands.executeCommand('workbench.view.debug');
}
return result;
}
const isJava = component.component.devfileData.devfile.metadata.tags.includes('Java') ;
const isNode = component.component.devfileData.devfile.metadata.tags.includes('Node.js');
const isPython = component.component.devfileData.devfile.metadata.tags.includes('Python');
if (isJava || isNode || isPython) {
if (isJava) {
const JAVA_EXT = 'redhat.java';
const JAVA_DEBUG_EXT = 'vscjava.vscode-java-debug';
const jlsIsActive = extensions.getExtension(JAVA_EXT);
const jdIsActive = extensions.getExtension(JAVA_DEBUG_EXT);
if (!jlsIsActive || !jdIsActive) {
let warningMsg: string;
if (jlsIsActive && !jdIsActive) {
warningMsg = 'Debugger for Java (Publisher: Microsoft) extension is required to debug component';
} else if (!jlsIsActive && jdIsActive) {
warningMsg = 'Language support for Java ™ (Publisher: Red Hat) extension is required to support debugging.';
} else {
warningMsg = 'Language support for Java ™ and Debugger for Java extensions are required to debug component';
}
const response = await window.showWarningMessage(warningMsg, 'Install');
if (response === 'Install') {
await window.withProgress({ location: ProgressLocation.Notification }, async (progress) => {
progress.report({ message: 'Installing extensions required to debug Java Component ...' });
if (!jlsIsActive) await commands.executeCommand('workbench.extensions.installExtension', JAVA_EXT);
if (!jdIsActive) await commands.executeCommand('workbench.extensions.installExtension', JAVA_DEBUG_EXT);
});
await window.showInformationMessage('Please reload the window to activate installed extensions.', 'Reload');
await commands.executeCommand('workbench.action.reloadWindow');
}
}
if (jlsIsActive && jdIsActive) {
result = Component.startOdoAndConnectDebugger(component, {
name: `Attach to '${component.component.devfileData.devfile.metadata.name}' component.`,
type: 'java',
request: 'attach',
hostName: 'localhost',
projectName: path.basename(component.contextPath)
});
}
} else if (isPython) {
const PYTHON_EXT = 'ms-python.python';
const pythonExtIsInstalled = extensions.getExtension('ms-python.python');
if (!pythonExtIsInstalled) {
const response = await window.showWarningMessage('Python extension (Publisher: Microsoft) is required to support debugging.', 'Install');
if (response === 'Install') {
await window.withProgress({ location: ProgressLocation.Notification }, async (progress) => {
progress.report({ message: 'Installing extensions required to debug Python Component ...' });
await commands.executeCommand('workbench.extensions.installExtension', PYTHON_EXT);
});
await window.showInformationMessage('Please reload the window to activate installed extension.', 'Reload');
await commands.executeCommand('workbench.action.reloadWindow');
}
}
if (pythonExtIsInstalled) {
result = Component.startOdoAndConnectDebugger(component, {
name: `Attach to '${component.component.devfileData.devfile.metadata.name}' component.`,
type: 'python',
request: 'attach',
connect: {
host: 'localhost'
},
pathMappings: [{
localRoot: component.contextPath,
remoteRoot: '/projects'
}],
projectName: path.basename(component.contextPath)
});
}
} else {
result = Component.startOdoAndConnectDebugger(component, {
name: `Attach to '${component.component.devfileData.devfile.metadata.name}' component.`,
type: 'pwa-node',
request: 'attach',
address: 'localhost',
localRoot: component.contextPath,
remoteRoot: '/projects'
});
}
} else {
void window.showWarningMessage('Debug command currently supports local components with Java, Node.Js and Python component types.');
}
return result;
}
static async startOdoAndConnectDebugger(component: ComponentWorkspaceFolder, config: DebugConfiguration): Promise<string> {
const componentDescription = await Odo.Instance.describeComponent(component.contextPath, !!Component.getComponentDevState(component).runOn);
if (componentDescription.devForwardedPorts?.length > 0) {
// try to find debug port
const debugPortsCandidates:number[] = [];
componentDescription.devForwardedPorts.forEach((pf) => {
const devComponent = componentDescription.devfileData.devfile.components.find(item => item.name === pf.containerName);
if (devComponent?.container) {
const candidatePort = devComponent.container.endpoints.find(endpoint => endpoint.targetPort === pf.containerPort);
if (candidatePort.name.startsWith('debug')) {
debugPortsCandidates.push(candidatePort.targetPort);
}
}
});
const filteredForwardedPorts = debugPortsCandidates.length > 0
? componentDescription.devForwardedPorts.filter(fp => debugPortsCandidates.includes(fp.containerPort))
: componentDescription.devForwardedPorts;
const ports = filteredForwardedPorts.map((fp) => ({
label: `${fp.localAddress}:${fp.localPort}`,
description: `Forwards to ${fp.containerName}:${fp.containerPort}`,
fp
}));
const port = ports.length === 1 ? ports[0] : await window.showQuickPick(ports, {placeHolder: 'Select a port to start debugger session'});
if (!port) return null;
config.contextPath = component.contextPath;
if (config.type === 'python') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
config.connect.port = port.fp.localPort;
} else {
config.port = port.fp.localPort;
}
const result = await debug.startDebugging(workspace.getWorkspaceFolder(Uri.file(component.contextPath)), config);
if (!result) {
return Promise.reject(new VsCommandError('Debugger session failed to start.', undefined, undefined, {language: config.type}));
}
return createStartDebuggerResult(config.type, 'Debugger session has successfully started.');
}
return createStartDebuggerResult(config.type, 'Component has no ports forwarded.');
}
@vsCommand('openshift.component.revealContextInExplorer')
public static async revealContextInExplorer(context: ComponentWorkspaceFolder): Promise<void> {
await commands.executeCommand('workbench.view.explorer');
await commands.executeCommand('revealInExplorer', context.contextPath);
}
@vsCommand('openshift.component.deploy')
public static deploy(context: ComponentWorkspaceFolder) {
// TODO: Find out details for deployment workflow
// right now just let deploy and redeploy
// Undeploy is not provided
// --
// const cs = Component.getComponentDevState(context);
// cs.deployStatus = ComponentContextState.DEP_RUNNING;
// Component.stateChanged.fire(context.contextPath);
void OpenShiftTerminalManager.getInstance().executeInTerminal(
Command.deploy(),
context.contextPath,
`Deploying '${context.component.devfileData.devfile.metadata.name}' Component`);
}
@vsCommand('openshift.component.undeploy')
public static undeploy(context: ComponentWorkspaceFolder) {
// TODO: Find out details for deployment workflow
// right now just let deploy and redeploy
// Undeploy is not provided
// // --
// const cs = Component.getComponentDevState(context);
// cs.deployStatus = ComponentContextState.DEP;
// Component.stateChanged.fire(context.contextPath);
void OpenShiftTerminalManager.getInstance().executeInTerminal(
Command.undeploy(context.component.devfileData.devfile.metadata.name),
context.contextPath,
`Undeploying '${context.component.devfileData.devfile.metadata.name}' Component`);
}
@vsCommand('openshift.component.deleteConfigurationFiles')
public static async deleteConfigurationFiles(context: ComponentWorkspaceFolder): Promise<void> {
const DELETE_CONFIGURATION = 'Delete Configuration';
const CANCEL = 'Cancel';
const response = await window.showWarningMessage(`Are you sure you want to delete the configuration for the component ${context.contextPath}?\nOpenShift Toolkit will no longer recognize the project as a component.`, DELETE_CONFIGURATION, CANCEL);
if (response === DELETE_CONFIGURATION) {
await Odo.Instance.deleteComponentConfiguration(context.contextPath);
void commands.executeCommand('openshift.componentsView.refresh');
}
}
@vsCommand('openshift.component.deleteSourceFolder')
public static async deleteSourceFolder(context: ComponentWorkspaceFolder): Promise<void> {
const DELETE_SOURCE_FOLDER = 'Delete Source Folder';
const CANCEL = 'Cancel';
const response = await window.showWarningMessage(`Are you sure you want to delete the folder containing the source code for ${context.contextPath}?`, DELETE_SOURCE_FOLDER, CANCEL);
if (response === DELETE_SOURCE_FOLDER) {
await fs.rm(context.contextPath, { force: true, recursive: true });
let workspaceFolderToRmIndex = -1;
for (let i = 0; i < workspace.workspaceFolders.length; i++) {
if (workspace.workspaceFolders[i].uri.fsPath === context.contextPath) {
workspaceFolderToRmIndex = i;
break;
}
}
if (workspaceFolderToRmIndex !== -1) {
workspace.updateWorkspaceFolders(workspaceFolderToRmIndex, 1);
}
}
}
@vsCommand('openshift.component.commands.command.run', true)
static runComponentCommand(componentFolder: ComponentWorkspaceFolder): Promise<void> {
const componentName = componentFolder.component.devfileData.devfile.metadata.name;
if ('getCommand' in componentFolder) {
const componentCommand = (<CommandProvider>componentFolder).getCommand();
const command = Command.runComponentCommand(componentCommand.id);
void OpenShiftTerminalManager.getInstance().createTerminal(
command,
`Component ${componentName}: Run '${componentCommand.id}' Command`,
componentFolder.contextPath,
);
} else {
void window.showErrorMessage(`No Command found in Component '${componentName}`);
}
return;
}
}