1
-
2
1
const vscode = require ( 'vscode' )
3
2
const workspace = vscode . workspace
4
3
const window = vscode . window
5
4
const API = require ( 'cos-api4node' )
6
5
const fs = require ( 'fs' )
6
+ const pkg = require ( './package.json' )
7
+
8
+ const COS_LANG_IDS = pkg [ 'contributes' ] [ 'languages' ] . map ( lang => lang . id )
7
9
8
10
const createBar = ( ) => {
9
11
@@ -23,20 +25,129 @@ const activate = context => {
23
25
24
26
const output = window . createOutputChannel ( 'cos' )
25
27
26
- const log = msg => output . appendLine ( msg )
28
+ const log = msg => {
29
+ output . appendLine ( msg )
30
+ return true
31
+ }
27
32
28
33
const conn = workspace . getConfiguration ( 'cos' ) . get ( 'conn' )
29
34
30
35
const api = API ( conn )
31
36
32
37
api . headServer ( ( err ) => {
33
38
34
- if ( ! ! err ) return log ( 'connect FAIL' )
35
- log ( 'connected ' + JSON . stringify ( conn ) )
39
+ const connectionParameters = JSON . stringify (
40
+ Object . assign ( { } , conn , {
41
+ password : "***"
42
+ } ) ,
43
+ null ,
44
+ 4
45
+ ) ;
46
+ if ( ! ! err ) return log ( 'Connection FAILED: ' + connectionParameters )
47
+ log ( 'Connected ' + connectionParameters )
36
48
bar . set ( conn )
37
49
38
50
} )
51
+
52
+ /**
53
+ * Import and compile current file.
54
+ */
55
+ const cosCompile = ( ) => {
56
+
57
+ if ( ! window . activeTextEditor )
58
+ return log ( 'No active editor, open one at first' )
59
+
60
+ const openedDoc = window . activeTextEditor . document
61
+
62
+ if ( ! openedDoc )
63
+ return log ( 'Open a Caché ObjectScript file first.' )
64
+
65
+ const fileName = openedDoc . fileName ;
66
+
67
+ if ( ! fileName || COS_LANG_IDS . indexOf ( openedDoc . languageId ) === - 1 )
68
+ return log ( `Document ${ fileName } cannot be compiled in Caché (type ${ openedDoc . languageId } unsupported)` )
69
+
70
+ log ( `Saving ${ fileName } ...` )
71
+
72
+ let cacheDocName ;
73
+ const fileBody = openedDoc . getText ( )
74
+ const isClass = / \. c l s $ / i. test ( fileName )
75
+ const content = fileBody . split ( / \r ? \n / g )
76
+
77
+ if ( isClass ) {
78
+ // Caché class files can be placed hierarchically (e.g. /src/Package/Class.cls),
79
+ // so we pick the class name from the class definition itself
80
+ cacheDocName = ( fileBody . replace ( / \/ \/ [ ^ \r \n ] * \r ? \n / g, '' ) . match ( / C l a s s ( [ ^ \s ] + ) / i ) || [ ] ) [ 1 ] || ""
81
+ const nameParts = cacheDocName . split ( / \. / g ) . filter ( s => ! ! s )
82
+ if ( nameParts . length < 2 )
83
+ return log ( `Unable to detect class name in source code of ${ fileName } .\n`
84
+ + `Is it a valid Caché ObjectScript class?` )
85
+ const matchingFileName = ( fileName . match ( / [ ^ \\ \/ ] + $ / ) || [ ] ) [ 0 ]
86
+ if ( ( cacheDocName . toLowerCase ( ) + '.cls' ) . indexOf ( matchingFileName . toLowerCase ( ) ) === - 1 )
87
+ return log ( `You tried to compile class named "${ cacheDocName } " in file "${ matchingFileName } ".\n`
88
+ + `Did you forget to rename the file/class to correspond to each other?` )
89
+ cacheDocName += '.cls'
90
+ } else {
91
+ // routine: cacheDocName = actual filename
92
+ cacheDocName = ( fileName . match ( / [ \\ \/ ] ( [ ^ \\ \/ ] + ) $ / ) || [ ] ) [ 1 ] || ""
93
+ }
94
+
95
+ const anyErrors = ( err , res , keyword ) => {
96
+
97
+ if ( err )
98
+ return log ( `Unable to ${ keyword } ${ cacheDocName } : ${ err . code ? err . code + ' ' + err . message : err } ` )
99
+
100
+ if ( ! res || ! res . status || ! ( res . status . errors instanceof Array ) )
101
+ return log ( `Unknown response from Atelier API while trying to ${
102
+ keyword } ${ cacheDocName } : ${ res } ` )
103
+
104
+ if ( res . result && res . result . status )
105
+ return log ( res . result . status )
106
+
107
+ if ( res . status . errors . length !== 0 )
108
+ return log ( `Unable to ${ keyword } ${ cacheDocName } : ${ res . status . errors . summary } \n\n${
109
+ res . console } \n\n${ res . status . errors . join ( '\n' ) } ` )
110
+
111
+ return false ;
112
+
113
+ }
114
+
115
+ const consoleOutput = ( output , defaultOutput = "" ) => {
116
+
117
+ let out = output instanceof Array
118
+ ? output . join ( '\n' )
119
+ : ( output || defaultOutput ) + '' ;
120
+ out = out . replace ( / ^ [ \s \r \n ] + / , '' ) ;
121
+
122
+ if ( out ) {
123
+ log ( out )
124
+ }
125
+
126
+ }
127
+
128
+ api . putDoc ( cacheDocName , { enc : false , content } , { ignoreConflict : true } , ( err , res ) => {
129
+
130
+ if ( anyErrors ( err , res , 'save' ) )
131
+ return ;
132
+
133
+ consoleOutput ( res . console )
134
+
135
+ api . compile ( cacheDocName , ( err , res ) => {
136
+
137
+ if ( anyErrors ( err , res , 'compile' ) )
138
+ return ;
139
+
140
+ consoleOutput ( res . console || "Done." )
141
+
142
+ } )
143
+
144
+ } )
145
+
146
+ }
39
147
148
+ /**
149
+ * Export all classes/routines in a namespace to working directory.
150
+ */
40
151
const cosExport = ( ) => {
41
152
42
153
const exportDoc = ( doc , cb ) => {
@@ -64,7 +175,7 @@ const activate = context => {
64
175
65
176
const load = ( doc , cb ) => {
66
177
67
- const loaded = ( err , data ) => {
178
+ const loaded = ( err , json ) => {
68
179
69
180
if ( ! ! err ) {
70
181
@@ -75,7 +186,6 @@ const activate = context => {
75
186
76
187
}
77
188
78
- const json = JSON . parse ( data )
79
189
const content = json . result
80
190
exportDoc ( content , cb )
81
191
@@ -96,18 +206,17 @@ const activate = context => {
96
206
}
97
207
98
208
99
- const onGetDocs = ( err , data ) => {
209
+ const onGetDocs = ( err , json ) => {
100
210
101
- if ( err ) return log ( 'getDocs ERROR' )
211
+ if ( err ) return log ( 'getDocs ERROR' )
102
212
103
- const json = JSON . parse ( data )
104
213
const list = json . result . content
105
214
log ( '' )
106
215
log ( 'list: ' + list . length )
107
216
const docs = list . filter ( doc => ( doc . cat !== 'CSP' ) && ( doc . name . substring ( 0 , 1 ) !== '%' ) && ( doc . name . substring ( 0 , 12 ) !== 'INFORMATION.' ) )
108
217
log ( 'without % and CSP and INFORMATION: ' + docs . length )
109
218
log ( '' )
110
- exportDocs ( docs , ( ) => {
219
+ exportDocs ( docs , ( ) => {
111
220
log ( '' )
112
221
log ( 'Export completed.' )
113
222
} )
@@ -119,8 +228,10 @@ const activate = context => {
119
228
}
120
229
121
230
// command 'cos.server' defined in statusBar
122
- const cmd = vscode . commands . registerCommand ( 'cos.export' , cosExport )
123
- context . subscriptions . push ( cmd )
231
+ context . subscriptions . push (
232
+ vscode . commands . registerCommand ( 'cos.export' , cosExport ) ,
233
+ vscode . commands . registerCommand ( 'cos.compile' , cosCompile )
234
+ )
124
235
125
236
}
126
237
0 commit comments