@@ -4,39 +4,31 @@ import * as path from 'path';
44import { IotResult , StatusResult } from '../IotResult' ;
55import { IYamlValidator } from './IYamlValidator' ;
66import { IoTHelper } from '../Helper/IoTHelper' ;
7+ import YAML from 'yaml' ;
78
89export class YamlRedHatServiceSingleton {
910 private static instance : YamlRedHatServiceSingleton ;
1011 //
1112 private readonly _identifierExtension = "redhat.vscode-yaml" ;
12- private readonly _SCHEMA = "myschema" ;
1313 private readonly _timeout = 60 ; // 30 sec
14+ private readonly _SCHEMA = "myschema" ;
1415 private _yamlExtension :vscode . Extension < any > | undefined ;
1516 private _yamlExtensionAPI :any ;
1617 private _schemas :Map < string , string > = new Map < string , string > ( ) ;
1718 private _currentYamlFile :vscode . Uri = vscode . Uri . file ( "c://non" ) ;
1819 private _validationErrors :Array < string > = [ ] ;
1920 private _responseReceivedFlag = false ;
20- /**
21- * Конструктор Одиночки всегда должен быть скрытым, чтобы предотвратить
22- * создание объекта через оператор new.
23- */
21+
2422 private constructor ( ) { }
2523
26- /**
27- * Статический метод, управляющий доступом к экземпляру одиночки.
28- *
29- * Эта реализация позволяет вам расширять класс Одиночки, сохраняя повсюду
30- * только один экземпляр каждого подкласса.
31- */
3224 public static getInstance ( ) : YamlRedHatServiceSingleton {
3325 if ( ! YamlRedHatServiceSingleton . instance ) {
3426 YamlRedHatServiceSingleton . instance = new YamlRedHatServiceSingleton ( ) ;
3527 }
3628 return YamlRedHatServiceSingleton . instance ;
3729 }
3830
39- private async InstallExtension ( ) : Promise < IotResult > {
31+ private async InstallExtensionAsync ( ) : Promise < IotResult > {
4032 let result :IotResult ;
4133 let msg :string ;
4234 try {
@@ -47,13 +39,16 @@ export class YamlRedHatServiceSingleton {
4739 let statusBarBackground = vscode . window . createStatusBarItem (
4840 vscode . StatusBarAlignment . Right , 1000 ) ;
4941 msg = `Install Red Hat's YAML extension` ;
42+ vscode . window . showWarningMessage ( msg ) ;
5043 statusBarBackground . text = `$(loading~spin) ${ msg } ` ;
5144 statusBarBackground . tooltip = msg ;
45+ statusBarBackground . show ( ) ;
5246 //Install
5347 await vscode . commands . executeCommand (
5448 `workbench.extensions.installExtension` ,
5549 `${ this . _identifierExtension } `
5650 ) ;
51+ await vscode . commands . executeCommand ( "workbench.action.reloadWindow" ) ;
5752 //
5853 statusBarBackground . hide ( ) ;
5954 statusBarBackground . dispose ( ) ;
@@ -72,60 +67,67 @@ export class YamlRedHatServiceSingleton {
7267 //result
7368 msg = `Red Hat's YAML extension failed to install` ;
7469 vscode . window . showErrorMessage ( msg ) ;
75- result = new IotResult ( StatusResult . Error , msg ) ;
70+ result = new IotResult ( StatusResult . Error , msg , err ) ;
7671 }
7772 return Promise . resolve ( result ) ;
7873 }
7974
80- private onRequestSchemaURI ( resource : string ) : string | undefined {
81- if ( resource . endsWith ( '.yaml' ) ) {
82- const filename = path . parse ( resource ) . base ;
83- //ex: "myschema://schema/template.fastiot"
84- return `${ this . _SCHEMA } ://schema/${ filename } ` ;
85- }
86- return undefined ;
87- }
88-
89- private onRequestSchemaContent ( schemaUri : string ) : string | undefined {
90- const parsedUri = vscode . Uri . parse ( schemaUri ) ;
91- if ( parsedUri . scheme !== this . _SCHEMA ) {
92- return undefined ;
93- }
94- if ( ! parsedUri . path || ! parsedUri . path . startsWith ( '/' ) ) {
95- return undefined ;
96- }
97- //get schema
98- const schema = this . _schemas . get ( schemaUri ) ;
99- //ex: "file:///d:/Anton/GitHub/vscode-extension-dotnet-fastiot/schemas/template.fastiot.schema.yaml"
100- return schema ;
101- }
102-
10375 /**
10476 * redhat.vscode-yaml extension activation
10577 */
106- public async Activate ( ) : Promise < IotResult > {
78+ public async ActivateAsync ( ) : Promise < IotResult > {
79+ let result :IotResult ;
10780 //check
10881 if ( this . _yamlExtension && this . _yamlExtension . isActive && this . _yamlExtensionAPI ) {
10982 //ok
110- return Promise . resolve ( new IotResult ( StatusResult . Ok , "Red Hat's YAML extension is active" ) ) ;
83+ result = new IotResult ( StatusResult . Ok , "Red Hat's YAML extension is active" ) ;
84+ return Promise . resolve ( result ) ;
11185 }
112- let result :IotResult ;
113- let msg :string ;
11486 try {
11587 this . _yamlExtension = vscode . extensions . getExtension ( this . _identifierExtension ) ;
11688 if ( ! this . _yamlExtension ) {
11789 //need install
118- result = await this . InstallExtension ( ) ;
90+ result = await this . InstallExtensionAsync ( ) ;
11991 if ( result . Status == StatusResult . Error ) return Promise . resolve ( result ) ;
12092 }
12193 //activate
12294 this . _yamlExtensionAPI = await this . _yamlExtension ?. activate ( ) ;
95+ //func
96+ const onRequestSchemaURI = ( resource : string ) : string | undefined => {
97+ if ( resource . endsWith ( '.fastiot.yaml' ) ) {
98+ const filename = path . parse ( resource ) . base ;
99+ //ex: "myschema://schema/template.fastiot"
100+ const schema = `${ this . _SCHEMA } ://schema/${ filename } ` ;
101+ return schema ;
102+ }
103+ return undefined ;
104+ } ;
105+
106+ const onRequestSchemaContent = ( schemaUri : string ) : string | undefined => {
107+ //ex: template.fastiot.yaml
108+ const parsedUri = vscode . Uri . parse ( schemaUri ) ;
109+ if ( parsedUri . scheme !== this . _SCHEMA ) {
110+ return undefined ;
111+ }
112+ if ( ! parsedUri . path || ! parsedUri . path . startsWith ( '/' ) ) {
113+ return undefined ;
114+ }
115+ if ( parsedUri . path . length < 5 ) {
116+ return undefined ;
117+ }
118+ //get schema
119+ schemaUri = parsedUri . path . substring ( 1 , parsedUri . path . length - 5 ) ;
120+ const file = this . _schemas . get ( schemaUri ) ?? "c:\\file.yaml" ;
121+ //ex: "file:///d:/Anton/GitHub/vscode-extension-dotnet-fastiot/schemas/template.fastiot.schema.yaml"
122+ const fileData = fs . readFileSync ( file , 'utf8' ) ;
123+ return fileData ;
124+ } ;
123125 //register
124- this . _yamlExtensionAPI . registerContributor ( this . _SCHEMA , this . onRequestSchemaURI , this . onRequestSchemaContent ) ;
126+ this . _yamlExtensionAPI . registerContributor ( this . _SCHEMA , onRequestSchemaURI , onRequestSchemaContent ) ;
125127 //Diagnostics
126128 vscode . languages . onDidChangeDiagnostics ( e => {
127129 e . uris . forEach ( fileUri => {
128- if ( this . _currentYamlFile == fileUri ) {
130+ if ( this . _currentYamlFile . fsPath == fileUri . fsPath ) {
129131 let diagnostics = vscode . languages . getDiagnostics ( fileUri ) ; // returns an array
130132 this . _validationErrors = [ ] ;
131133 diagnostics . forEach ( item => {
@@ -134,7 +136,7 @@ export class YamlRedHatServiceSingleton {
134136 //
135137 this . _responseReceivedFlag = true ;
136138 //close file
137- this . closeFileIfOpen ( fileUri ) ;
139+ this . closeFileIfOpenAsync ( fileUri ) ;
138140 }
139141 } ) ;
140142 } ) ;
@@ -151,16 +153,15 @@ export class YamlRedHatServiceSingleton {
151153 private AddSchema ( schemaFilePath :string ) {
152154 //template.fastiot.schema.yaml
153155 const filename = path . parse ( schemaFilePath ) . base ;
154- let key = filename . substring ( 0 , filename . length - 12 ) ;
155- //ex: "myschema://schema/template.fastiot"
156- key = `${ this . _SCHEMA } ://schema/${ key } ` ;
156+ //ex: "template.fastiot"
157+ const key = filename . substring ( 0 , filename . length - 31 ) ;
157158 //exist
158159 if ( this . _schemas . has ( key ) ) return ;
159160 //add
160161 this . _schemas . set ( key , schemaFilePath ) ;
161162 }
162163
163- public async ValidateSchema ( yamlFilePath :string , schemaFilePath :string ) :Promise < IotResult > {
164+ public async ValidateSchemaAsync ( yamlFilePath :string , schemaFilePath :string ) :Promise < IotResult > {
164165 let result :IotResult ;
165166 try {
166167 //Add schema
@@ -169,17 +170,17 @@ export class YamlRedHatServiceSingleton {
169170 this . _currentYamlFile = vscode . Uri . file ( yamlFilePath ) ;
170171 //adding a file to processing
171172 this . _responseReceivedFlag = false ;
172- let ymldoc = await vscode . workspace . openTextDocument ( vscode . Uri . file ( yamlFilePath ) ) ;
173+ await vscode . workspace . openTextDocument ( vscode . Uri . file ( yamlFilePath ) ) ;
173174 //waiting response
174175 for ( let i = 0 ; i < this . _timeout ; i ++ ) {
175- IoTHelper . Sleep ( 500 ) ;
176+ await IoTHelper . Sleep ( 500 ) ;
176177 if ( this . _responseReceivedFlag ) i = this . _timeout ;
177178 }
178179 //response
179180 if ( this . _responseReceivedFlag ) {
180181 //ok
181182 const msg = `Response received from Red Hat's YAML extension` ;
182- result = new IotResult ( StatusResult . Error , msg ) ;
183+ result = new IotResult ( StatusResult . Ok , msg ) ;
183184 if ( this . _validationErrors . length > 0 ) {
184185 result . returnObject = this . _validationErrors ;
185186 }
@@ -196,14 +197,11 @@ export class YamlRedHatServiceSingleton {
196197 return Promise . resolve ( result ) ;
197198 }
198199
199- private async closeFileIfOpen ( file :vscode . Uri ) : Promise < void > {
200+ private async closeFileIfOpenAsync ( file :vscode . Uri ) : Promise < void > {
200201 const tabs : vscode . Tab [ ] = vscode . window . tabGroups . all . map ( tg => tg . tabs ) . flat ( ) ;
201202 const index = tabs . findIndex ( tab => tab . input instanceof vscode . TabInputText && tab . input . uri . path === file . path ) ;
202203 if ( index !== - 1 ) {
203204 await vscode . window . tabGroups . close ( tabs [ index ] ) ;
204205 }
205206 }
206207}
207-
208-
209-
0 commit comments