12
12
* Broadcom, Inc. - initial API and implementation
13
13
*/
14
14
15
- import * as fs from "fs" ;
16
15
import * as path from "path" ;
17
16
import { Minimatch } from "minimatch" ;
18
17
import { SettingsUtils } from "./util/SettingsUtils" ;
@@ -29,11 +28,14 @@ import {
29
28
B4GTypeMetadata ,
30
29
decodeBridgeJson ,
31
30
loadBridgeJsonContent ,
32
- } from "./BridgeForGit" ;
33
-
34
- const PROCESSOR_GROUP_FOLDER = ".cobolplugin" ;
35
- const PROCESSOR_GROUP_PGM = "pgm_conf.json" ;
36
- const PROCESSOR_GROUP_PROC = "proc_grps.json" ;
31
+ } from "./BridgeForGitLoader" ;
32
+ import {
33
+ Preprocessor ,
34
+ ProcessorGroup ,
35
+ ProgramsConfig ,
36
+ readProcessorGroupsFileContent ,
37
+ readProgramConfigFileContent ,
38
+ } from "./ProcessorGroupsLoader" ;
37
39
38
40
export async function loadProcessorGroupCopybookPaths (
39
41
documentUri : string ,
@@ -128,51 +130,33 @@ export async function loadProcessorGroupDialectConfig(
128
130
readProgramConfigFileContent ( ) ,
129
131
decodeBridgeJson ( await loadBridgeJsonContent ( Uri . parse ( item . scopeUri ) ) ) ,
130
132
) ;
131
- if ( pgCfg === undefined ) {
133
+ if ( pgCfg === undefined || pgCfg . preprocessor == undefined ) {
132
134
return configObject ;
133
135
}
136
+
134
137
const dialects : Preprocessor [ ] = [ ] ;
135
138
136
- if ( Array . isArray ( pgCfg . preprocessor ) ) {
137
- for ( const pp of pgCfg . preprocessor as Preprocessor ) {
138
- if ( typeof pp === "object" && pp ) {
139
- dialects . push ( pp [ "name" ] ) ;
140
- }
141
- if ( typeof pp === "string" && pp ) {
142
- dialects . push ( pp ) ;
143
- }
139
+ const preprocessors = Array . isArray ( pgCfg . preprocessor )
140
+ ? pgCfg . preprocessor
141
+ : [ pgCfg . preprocessor ] ;
142
+ for ( const pp of preprocessors ) {
143
+ if ( typeof pp === "object" && pp ) {
144
+ dialects . push ( pp [ "name" ] ) ;
145
+ }
146
+ if ( typeof pp === "string" && pp ) {
147
+ dialects . push ( pp ) ;
144
148
}
145
- } else if ( pgCfg . preprocessor !== undefined ) {
146
- dialects . push ( pgCfg . preprocessor ) ;
147
149
}
148
150
149
151
// "SQL" is not a real dialect, we will use it only to set up sql backend for now
150
- return dialects . filter ( ( name ) => name != "SQL" ) || configObject ;
152
+ const result = dialects . filter ( ( name ) => name != "SQL" ) ;
153
+ return result . length > 0 ? result : configObject ;
151
154
} catch ( e ) {
152
155
console . error ( JSON . stringify ( e ) ) ;
153
156
return configObject ;
154
157
}
155
158
}
156
159
157
- type ProgramsConfig = {
158
- pgms : {
159
- program : string ;
160
- pgroup : string ;
161
- } [ ] ;
162
- } ;
163
-
164
- type Preprocessor = string | string [ ] ;
165
-
166
- type ProcessorConfig = {
167
- name : string ;
168
- libs ?: string [ ] ;
169
- preprocessor ?: Preprocessor [ ] ;
170
- } ;
171
-
172
- type ProcessorsConfig = {
173
- pgroups : ProcessorConfig [ ] ;
174
- } ;
175
-
176
160
function matchProcessorGroup (
177
161
pgmCfg : ProgramsConfig ,
178
162
documentPath : string ,
@@ -219,56 +203,12 @@ function pathMatches(program: string, documentPath: string) {
219
203
documentPath . toUpperCase ( ) ;
220
204
}
221
205
222
- const loadProcessorConfigurations = (
223
- processorsJsonContent : string ,
224
- ) : ProcessorConfig [ ] => {
225
- const procCfg : ProcessorsConfig = JSON . parse ( processorsJsonContent ) ;
226
- return procCfg . pgroups ;
227
- } ;
228
-
229
- export function readProcessorGroupsFileContent ( ) : ProcessorConfig [ ] {
230
- const ws = SettingsUtils . getWorkspaceFoldersPath ( true ) ;
231
- if ( ws . length < 1 ) {
232
- return [ ] ;
233
- }
234
- const cfgPath = path . join ( ws [ 0 ] , PROCESSOR_GROUP_FOLDER ) ;
235
- const procCfgPath = path . join ( cfgPath , PROCESSOR_GROUP_PROC ) ;
236
- if ( ! fs . existsSync ( procCfgPath ) ) {
237
- return [ ] ;
238
- }
239
- try {
240
- return JSON . parse ( fs . readFileSync ( procCfgPath ) . toString ( ) ) . pgroups ;
241
- } catch ( e ) {
242
- console . error ( e ) ;
243
- return [ ] ;
244
- }
245
- }
246
-
247
- export const readProgramConfigFileContent = ( ) : ProgramsConfig => {
248
- const EMPTY = { pgms : [ ] } ;
249
- const ws = SettingsUtils . getWorkspaceFoldersPath ( true ) ;
250
- if ( ws . length < 1 ) {
251
- return EMPTY ;
252
- }
253
- const cfgPath = path . join ( ws [ 0 ] , PROCESSOR_GROUP_FOLDER ) ;
254
- const pgmCfgPath = path . join ( cfgPath , PROCESSOR_GROUP_PGM ) ;
255
- if ( ! fs . existsSync ( pgmCfgPath ) ) {
256
- return EMPTY ;
257
- }
258
- try {
259
- return JSON . parse ( fs . readFileSync ( pgmCfgPath ) . toString ( ) ) ;
260
- } catch ( e ) {
261
- console . error ( e ) ;
262
- return EMPTY ;
263
- }
264
- } ;
265
-
266
206
export const loadProcessorsConfigForDocument = (
267
207
documentUri : string ,
268
- pgroups : ProcessorConfig [ ] ,
208
+ pgroups : ProcessorGroup [ ] ,
269
209
pgmCfg : ProgramsConfig ,
270
210
b4g : B4GTypeMetadata | undefined ,
271
- ) : ProcessorConfig | undefined => {
211
+ ) : ProcessorGroup | undefined => {
272
212
if ( pgroups . length === 0 ) {
273
213
return undefined ;
274
214
}
@@ -306,13 +246,19 @@ function selectProcessorGroup(
306
246
: b4g . elements [ selectedElement ] . processorGroup ;
307
247
}
308
248
309
- async function loadProcessorGroupSettings < T > (
249
+ async function loadProcessorGroupSettings < T extends string | string [ ] > (
310
250
documentUri : string ,
311
- atrtibute : string ,
251
+ atrtibute :
252
+ | "libs"
253
+ | "name"
254
+ | "target-sql-backend"
255
+ | "compiler-options"
256
+ | "copybook-file-encoding"
257
+ | "copybook-extensions" ,
312
258
configObject : T ,
313
259
dialect : string = "COBOL" ,
314
260
) : Promise < T > {
315
- const pgCfg : ProcessorConfig | undefined = loadProcessorsConfigForDocument (
261
+ const pgCfg : ProcessorGroup | undefined = loadProcessorsConfigForDocument (
316
262
documentUri ,
317
263
readProcessorGroupsFileContent ( ) ,
318
264
readProgramConfigFileContent ( ) ,
@@ -323,19 +269,19 @@ async function loadProcessorGroupSettings<T>(
323
269
}
324
270
try {
325
271
if ( dialect && dialect !== "COBOL" ) {
326
- for ( const pp of pgCfg . preprocessor as Preprocessor ) {
272
+ for ( const pp of pgCfg . preprocessor as Preprocessor [ ] ) {
327
273
if (
328
274
pp &&
329
275
typeof pp === "object" &&
330
276
pp [ "name" ] === dialect &&
331
- pp [ atrtibute ]
277
+ pp [ atrtibute ] !== undefined
332
278
) {
333
- return pp [ atrtibute ] ;
279
+ return pp [ atrtibute ] as T ;
334
280
}
335
281
}
336
282
} else {
337
- if ( pgCfg [ atrtibute as keyof ProcessorConfig ] ) {
338
- return pgCfg [ atrtibute as keyof ProcessorConfig ] as T ;
283
+ if ( pgCfg [ atrtibute ] !== undefined ) {
284
+ return pgCfg [ atrtibute ] as T ;
339
285
}
340
286
}
341
287
0 commit comments