8
8
import { window , commands , QuickPickItem , Uri , workspace , ExtensionContext , debug , DebugConfiguration , extensions , ProgressLocation , DebugSession , Disposable } from 'vscode' ;
9
9
import { ChildProcess , exec } from 'child_process' ;
10
10
import { isURL } from 'validator' ;
11
+ import { Subject } from 'rxjs' ;
11
12
import OpenShiftItem , { selectTargetApplication , selectTargetComponent } from './openshiftItem' ;
12
13
import { OpenShiftObject , ContextType } from '../odo' ;
13
14
import { Command } from "../odo/command" ;
@@ -30,9 +31,17 @@ import treeKill = require('tree-kill');
30
31
31
32
const waitPort = require ( 'wait-port' ) ;
32
33
34
+ export class ComponentEvent {
35
+ readonly type : 'watchStarted' | 'watchTerminated' ;
36
+ readonly component : OpenShiftObject ;
37
+ readonly process ?: ChildProcess ;
38
+ }
39
+
33
40
export class Component extends OpenShiftItem {
34
41
public static extensionContext : ExtensionContext ;
35
42
public static debugSessions : Map < string , DebugSession > = new Map ( ) ;
43
+ public static watchSessions : Map < string , ChildProcess > = new Map ( ) ;
44
+ public static readonly watchSubject : Subject < ComponentEvent > = new Subject < ComponentEvent > ( ) ;
36
45
37
46
public static init ( context : ExtensionContext ) : Disposable [ ] {
38
47
Component . extensionContext = context ;
@@ -57,6 +66,13 @@ export class Component extends OpenShiftItem {
57
66
}
58
67
}
59
68
69
+ static stopWatchSession ( component : OpenShiftObject ) : void {
70
+ const ws = Component . watchSessions . get ( component . contextPath . fsPath ) ;
71
+ if ( ws ) {
72
+ treeKill ( ws . pid ) ;
73
+ }
74
+ }
75
+
60
76
static async getOpenshiftData ( context : OpenShiftObject ) : Promise < OpenShiftObject > {
61
77
return Component . getOpenShiftCmdData ( context ,
62
78
"In which Project you want to create a Component" ,
@@ -119,6 +135,7 @@ export class Component extends OpenShiftItem {
119
135
await Component . unlinkAllComponents ( component ) ;
120
136
}
121
137
Component . stopDebugSession ( component ) ;
138
+ Component . stopWatchSession ( component ) ;
122
139
await Component . odo . deleteComponent ( component ) ;
123
140
124
141
} ) . then ( ( ) => `Component '${ name } ' successfully deleted` )
@@ -140,6 +157,7 @@ export class Component extends OpenShiftItem {
140
157
if ( value === 'Yes' ) {
141
158
return Progress . execFunctionWithProgress ( `Undeploying the Component '${ component . getName ( ) } '` , async ( ) => {
142
159
Component . stopDebugSession ( component ) ;
160
+ Component . stopWatchSession ( component ) ;
143
161
await Component . odo . undeployComponent ( component ) ;
144
162
} ) . then ( ( ) => `Component '${ name } ' successfully undeployed` )
145
163
. catch ( ( err ) => Promise . reject ( new VsCommandError ( `Failed to undeploy Component with error '${ err } '` ) ) ) ;
@@ -451,16 +469,48 @@ export class Component extends OpenShiftItem {
451
469
}
452
470
}
453
471
472
+ static addWatchSession ( component : OpenShiftObject , process : ChildProcess ) : void {
473
+ Component . watchSessions . set ( component . contextPath . fsPath , process ) ;
474
+ Component . watchSubject . next ( {
475
+ type : 'watchStarted' ,
476
+ component,
477
+ process
478
+ } ) ;
479
+ }
480
+
481
+ static removeWatchSession ( component : OpenShiftObject ) : void {
482
+ Component . watchSessions . delete ( component . contextPath . fsPath ) ;
483
+ Component . watchSubject . next ( {
484
+ type : 'watchTerminated' ,
485
+ component
486
+ } ) ;
487
+ }
488
+
454
489
@vsCommand ( 'openshift.component.watch' , true )
455
490
@selectTargetComponent (
456
491
'Select a Project' ,
457
492
'Select an Application' ,
458
493
'Select a Component you want to watch' ,
459
494
( target ) => target . contextValue === ContextType . COMPONENT_PUSHED
460
495
)
461
- static watch ( component : OpenShiftObject ) : Promise < void > {
496
+ static async watch ( component : OpenShiftObject ) : Promise < void > {
462
497
if ( ! component ) return null ;
463
- Component . odo . executeInTerminal ( Command . watchComponent ( component . getParent ( ) . getParent ( ) . getName ( ) , component . getParent ( ) . getName ( ) , component . getName ( ) ) , component . contextPath . fsPath , `OpenShift: Watch '${ component . getName ( ) } ' Component` ) ;
498
+ if ( component . compType !== SourceType . LOCAL && component . compType !== SourceType . BINARY ) {
499
+ window . showInformationMessage ( `Watch is supported only for Components with local or binary source type.` )
500
+ return null ;
501
+ }
502
+ if ( Component . watchSessions . get ( component . contextPath . fsPath ) ) {
503
+ const sel = await window . showInformationMessage ( `Watch process is already running for '${ component . getName ( ) } '` , 'Show Log' ) ;
504
+ if ( sel === 'Show Log' ) {
505
+ commands . executeCommand ( 'openshift.component.watch.showLog' , component . contextPath . fsPath ) ;
506
+ }
507
+ } else {
508
+ const process : ChildProcess = await Component . odo . spawn ( Command . watchComponent ( component . getParent ( ) . getParent ( ) . getName ( ) , component . getParent ( ) . getName ( ) , component . getName ( ) ) , component . contextPath . fsPath ) ;
509
+ Component . addWatchSession ( component , process ) ;
510
+ process . on ( 'exit' , ( ) => {
511
+ Component . removeWatchSession ( component ) ;
512
+ } ) ;
513
+ }
464
514
}
465
515
466
516
@vsCommand ( 'openshift.component.openUrl' , true )
0 commit comments