@@ -4,6 +4,7 @@ import vscode = require('vscode');
44import cp = require( 'child_process' ) ;
55import { Logger } from '../src/logging' ;
66import { LanguageClient , LanguageClientOptions , ServerOptions } from 'vscode-languageclient' ;
7+ import { ConnectionConfiguration } from './configuration' ;
78import { setupPuppetCommands } from '../src/commands/puppetcommands' ;
89import { setupPDKCommands } from '../src/commands/pdkcommands' ;
910import { reporter } from './telemetry/telemetry' ;
@@ -39,6 +40,9 @@ export interface IConnectionConfiguration {
3940export interface IConnectionManager {
4041 status : ConnectionStatus ;
4142 languageClient : LanguageClient ;
43+ showConnectionMenu ( ) ;
44+ showLogger ( ) ;
45+ restartConnection ( connectionConfig ?: IConnectionConfiguration ) ;
4246}
4347
4448export class ConnectionManager implements IConnectionManager {
@@ -58,6 +62,9 @@ export class ConnectionManager implements IConnectionManager {
5862 public get languageClient ( ) : LanguageClient {
5963 return this . languageServerClient ;
6064 }
65+ public showLogger ( ) {
66+ this . logger . show ( )
67+ }
6168
6269 constructor ( context : vscode . ExtensionContext , logger : Logger ) {
6370 this . logger = logger ;
@@ -78,7 +85,7 @@ export class ConnectionManager implements IConnectionManager {
7885 this . terminal = vscode . window . createTerminal ( 'Puppet PDK' ) ;
7986 this . terminal . processId . then (
8087 pid => {
81- console . log ( "pdk shell started, pid: " + pid ) ;
88+ this . logger . debug ( "pdk shell started, pid: " + pid ) ;
8289 } ) ;
8390 setupPDKCommands ( langID , this , this . extensionContext , this . logger , this . terminal ) ;
8491 this . extensionContext . subscriptions . push ( this . terminal ) ;
@@ -325,9 +332,11 @@ export class ConnectionManager implements IConnectionManager {
325332 var languageServerClient = new LanguageClient ( title , serverOptions , clientOptions )
326333 languageServerClient . onReady ( ) . then ( ( ) => {
327334 langClient . logger . debug ( 'Language server client started, setting puppet version' )
328- languageServerClient . sendRequest ( messages . PuppetVersionRequest . type ) . then ( ( versionDetails ) => {
329- this . setConnectionStatus ( versionDetails . puppetVersion , ConnectionStatus . Running ) ;
330- if ( reporter ) {
335+ this . setConnectionStatus ( "Loading Puppet" , ConnectionStatus . Starting ) ;
336+ this . queryLanguageServerStatus ( ) ;
337+ // Send telemetry
338+ if ( reporter ) {
339+ languageServerClient . sendRequest ( messages . PuppetVersionRequest . type ) . then ( ( versionDetails ) => {
331340 reporter . sendTelemetryEvent ( 'puppetVersion' + versionDetails . puppetVersion ) ;
332341 reporter . sendTelemetryEvent ( 'facterVersion' + versionDetails . facterVersion ) ;
333342 reporter . sendTelemetryEvent ( 'languageServerVersion' + versionDetails . languageServerVersion ) ;
@@ -336,26 +345,66 @@ export class ConnectionManager implements IConnectionManager {
336345 facterVersion : versionDetails . facterVersion ,
337346 languageServerVersion : versionDetails . languageServerVersion ,
338347 } ) ;
339- }
340- } ) ;
348+ } ) ;
349+ }
341350 } , ( reason ) => {
342351 this . setSessionFailure ( "Could not start language service: " , reason ) ;
343352 } ) ;
344353
345354 return languageServerClient ;
346355 }
347356
348- private restartConnection ( connectionConfig ?: IConnectionConfiguration ) {
349- this . stop ( ) ;
350- this . start ( connectionConfig ) ;
357+ private queryLanguageServerStatus ( ) {
358+ let connectionManager = this ;
359+
360+ return new Promise ( ( resolve , reject ) => {
361+ let count = 0 ;
362+ let lastVersionResponse = null ;
363+ let handle = setInterval ( ( ) => {
364+ count ++ ;
365+
366+ // After 30 seonds timeout the progress
367+ if ( count >= 30 || connectionManager . languageClient == undefined ) {
368+ clearInterval ( handle ) ;
369+ connectionManager . setConnectionStatus ( lastVersionResponse . puppetVersion , ConnectionStatus . Running ) ;
370+ resolve ( ) ;
371+ }
372+
373+ connectionManager . languageClient . sendRequest ( messages . PuppetVersionRequest . type ) . then ( ( versionDetails ) => {
374+ lastVersionResponse = versionDetails
375+ if ( versionDetails . factsLoaded && versionDetails . functionsLoaded && versionDetails . typesLoaded ) {
376+ clearInterval ( handle ) ;
377+ connectionManager . setConnectionStatus ( lastVersionResponse . puppetVersion , ConnectionStatus . Running ) ;
378+ resolve ( ) ;
379+ } else {
380+ let progress = 0 ;
381+
382+ if ( versionDetails . factsLoaded ) { progress ++ ; }
383+ if ( versionDetails . functionsLoaded ) { progress ++ ; }
384+ if ( versionDetails . typesLoaded ) { progress ++ ; }
385+ progress = Math . round ( progress / 3.0 * 100 ) ;
386+
387+ this . setConnectionStatus ( "Loading Puppet (" + progress . toString ( ) + "%)" , ConnectionStatus . Starting ) ;
388+ }
389+ } ) ;
390+
391+ } , 1000 ) ;
392+ } ) ;
393+ }
394+
395+ public restartConnection ( connectionConfig ?: IConnectionConfiguration ) {
396+ if ( connectionConfig == undefined ) {
397+ connectionConfig = new ConnectionConfiguration ( this . extensionContext ) ;
398+ }
399+ this . stop ( ) ;
400+ this . start ( connectionConfig ) ;
351401 }
352402
353403 private createStatusBarItem ( ) {
354404 if ( this . statusBarItem === undefined ) {
355405 this . statusBarItem = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right , 1 ) ;
356406
357- // TODO: Add a command here to show the connection menu
358- // this.statusBarItem.command = this.ShowConnectionMenuCommandName;
407+ this . statusBarItem . command = messages . PuppetCommandStrings . PuppetShowConnectionMenuCommandId ;
359408 this . statusBarItem . show ( ) ;
360409 vscode . window . onDidChangeActiveTextEditor ( textEditor => {
361410 if ( textEditor === undefined || textEditor . document . languageId !== "puppet" ) {
@@ -368,6 +417,27 @@ export class ConnectionManager implements IConnectionManager {
368417 }
369418 }
370419
420+ public showConnectionMenu ( ) {
421+ var menuItems : ConnectionMenuItem [ ] = [ ] ;
422+
423+ menuItems . push (
424+ new ConnectionMenuItem (
425+ "Restart Current Puppet Session" ,
426+ ( ) => { vscode . commands . executeCommand ( messages . PuppetCommandStrings . PuppetRestartSessionCommandId ) ; } ) ,
427+ )
428+
429+ menuItems . push (
430+ new ConnectionMenuItem (
431+ "Show Puppet Session Logs" ,
432+ ( ) => { vscode . commands . executeCommand ( messages . PuppetCommandStrings . PuppetShowConnectionLogsCommandId ) ; } ) ,
433+ )
434+
435+ vscode
436+ . window
437+ . showQuickPick < ConnectionMenuItem > ( menuItems )
438+ . then ( ( selectedItem ) => { selectedItem . callback ( ) ; } ) ;
439+ }
440+
371441 private setConnectionStatus ( statusText : string , status : ConnectionStatus ) : void {
372442 // Set color and icon for 'Running' by default
373443 var statusIconText = "$(terminal) " ;
@@ -391,3 +461,11 @@ export class ConnectionManager implements IConnectionManager {
391461 this . setConnectionStatus ( "Starting Error" , ConnectionStatus . Failed ) ;
392462 }
393463}
464+
465+ class ConnectionMenuItem implements vscode . QuickPickItem {
466+ public description : string ;
467+
468+ constructor ( public readonly label : string , public readonly callback : ( ) => void = ( ) => { } )
469+ {
470+ }
471+ }
0 commit comments