import * as vscode from 'vscode' ; import IEnvironment from './utils/IEnvironment' ; import IDictionary from './utils/IDictionary' ; export enum Context { Server = "Server" , Service = "Service" , Environment = "Environment" , ServerConnected = "ServerConnected" , ServiceConnected = "ServiceConnected" , EnvironmentConnected = "EnvironmentConnected" } export class ServerView { constructor ( public readonly serverName ? , public readonly serverIP ? , public readonly services ? , public isConnected ? ) { } public AddService ( service ) { return this . services . push ( service ) ; } } export class ServiceView { constructor ( public readonly serviceName ? , public readonly servicePort ? , public readonly environments ? , public readonly serverVersion ? , public readonly passwordCipher ? , public readonly includeList ? , public readonly user ? , public readonly smartClientPath ? , public isConnected ? , public parent ? ) { } public AddEnvironment ( environment ) { return this . environments . push ( environment ) ; } } export class EnvironmentView { constructor ( public readonly environmentLabel ? , public readonly environmentName ? , public readonly environment ? , public isConnected ? , public parent ? ) { } } export class ServerManagement { private _config : vscode . WorkspaceConfiguration ; private _environments : Array < IEnvironment > ; private _dictionary : Array < IDictionary > ; private _selectedEnvironment : string ; private _servers : ServerView [ ] ; public get servers ( ) : ServerView [ ] { return this . _servers ; } constructor ( onlyEnabled ) { this . _config = vscode . workspace . getConfiguration ( "advpl" ) ; this . _environments = this . _config . get < Array < IEnvironment > > ( "environments" ) ; this . _dictionary = this . _config . get < Array < IDictionary > > ( "dictionary" ) ; this . _selectedEnvironment = this . _config . get < string > ( "selectedEnvironment" ) ; this . _servers = this . GetServers ( onlyEnabled ) ; } private GetServers ( onlyEnabled ) { let servers = Array < ServerView > ( ) ; if ( onlyEnabled ) { this . _environments = this . _environments . filter ( env => env . enable !== false ) ; } this . _environments . forEach ( environment => { let positionServer = servers . findIndex ( server => server . serverIP === environment . server ) ; if ( positionServer < 0 ) { let serverLabel = this . _dictionary . find ( dic => dic . name . trim ( ) === environment . server . trim ( ) ) ; positionServer = servers . push ( new ServerView ( serverLabel ? serverLabel . label : environment . server , environment . server , [ ] ) ) - 1 ; } let positionService = servers [ positionServer ] . services . findIndex ( service => service . servicePort === environment . port ) ; if ( positionService < 0 ) { let serviceLabel = this . _dictionary . find ( dic => validParent ( dic ) && dic . name . trim ( ) === environment . port . toString ( ) ) ; /**
                 * Valida o atributo parent dessa forma para evitar erros
                 * caso essa propriedade seja null.
                 */ function validParent ( dic ) { if ( dic . parent ) return dic . parent . trim ( ) === environment . server . trim ( ) ; else return true ; } positionService = servers [ positionServer ] . AddService ( new ServiceView ( serviceLabel ? serviceLabel . label : environment . port . toString ( ) , environment . port , [ ] , environment . serverVersion , environment . passwordCipher , environment . includeList , environment . user , environment . smartClientPath , false , servers [ positionServer ] ) ) - 1 ; } let positionEnvironment = servers [ positionServer ] . services [ positionService ] . environments . findIndex ( env => env . environment === environment . environment ) ; if ( positionEnvironment < 0 ) { let environmentLabel = environment . name ? environment . name : environment . environment ; positionEnvironment = servers [ positionServer ] . services [ positionService ] . AddEnvironment ( new EnvironmentView ( environmentLabel , environment . name , environment . environment , environmentLabel === this . _selectedEnvironment . trim ( ) , servers [ positionServer ] . services [ positionService ] ) ) - 1 ; } if ( positionEnvironment >= 0 ) { if ( servers [ positionServer ] . services [ positionService ] . environments [ positionEnvironment ] . isConnected ) { servers [ positionServer ] . isConnected = true ; servers [ positionServer ] . services [ positionService ] . isConnected = true ; } } } ) ; return servers ; } ; public AddEnvironment ( environment ) { this . _environments . push ( environment ) ; this . _config . update ( "environments" , this . _environments ) ; } }