1
1
import vscode = require( 'vscode' ) ;
2
2
import fs = require( 'fs' ) ;
3
+ import path = require( 'path' ) ;
4
+ import glob = require( 'glob' ) ;
3
5
import { AtelierAPI } from '../api' ;
4
6
import { currentFile , CurrentFile , outputChannel } from '../utils' ;
5
7
import { documentContentProvider , config } from '../extension' ;
@@ -13,7 +15,7 @@ async function compileFlags(): Promise<string> {
13
15
} ) ;
14
16
}
15
17
16
- async function importFile ( file : CurrentFile , flags : string ) : Promise < any > {
18
+ async function importFile ( file : CurrentFile ) : Promise < any > {
17
19
const api = new AtelierAPI ( ) ;
18
20
return api
19
21
. putDoc (
@@ -23,13 +25,7 @@ async function importFile(file: CurrentFile, flags: string): Promise<any> {
23
25
content : file . content . split ( / \r ? \n / )
24
26
} ,
25
27
true
26
- )
27
- . then ( ( ) => compile ( file , flags ) )
28
- . catch ( ( error : Error ) => {
29
- outputChannel . appendLine ( error . message ) ;
30
- outputChannel . show ( true ) ;
31
- vscode . window . showErrorMessage ( error . message ) ;
32
- } ) ;
28
+ ) ;
33
29
}
34
30
35
31
function updateOthers ( others : string [ ] ) {
@@ -39,30 +35,42 @@ function updateOthers(others: string[]) {
39
35
} ) ;
40
36
}
41
37
42
- async function loadChanges ( file : CurrentFile ) : Promise < any > {
38
+ async function loadChanges ( files : CurrentFile [ ] ) : Promise < any > {
43
39
const api = new AtelierAPI ( ) ;
44
- return api . getDoc ( file . name ) . then ( data => {
45
- fs . writeFileSync ( file . fileName , ( data . result . content || [ ] ) . join ( '\n' ) ) ;
46
- api
47
- . actionIndex ( [ file . name ] )
48
- . then ( data => data . result . content [ 0 ] . others )
49
- . then ( updateOthers ) ;
50
- } ) ;
40
+ return Promise . all ( files . map ( file =>
41
+ api . getDoc ( file . name ) . then ( data => {
42
+ fs . writeFileSync ( file . fileName , ( data . result . content || [ ] ) . join ( '\n' ) ) ;
43
+ return api
44
+ . actionIndex ( [ file . name ] )
45
+ . then ( data => data . result . content [ 0 ] . others )
46
+ . then ( updateOthers ) ;
47
+ } )
48
+ ) ) ;
51
49
}
52
50
53
- async function compile ( file : CurrentFile , flags : string ) : Promise < any > {
51
+ async function compile ( docs : CurrentFile [ ] , flags ?: string ) : Promise < any > {
52
+ flags = flags || config ( ) . compileFlags ;
54
53
const api = new AtelierAPI ( ) ;
55
54
return api
56
- . actionCompile ( [ file . name ] , flags )
55
+ . actionCompile ( docs . map ( el => el . name ) , flags )
57
56
. then ( data => {
57
+ let info = docs . length > 1 ? '' : `${ docs [ 0 ] . name } : ` ;
58
58
if ( data . status && data . status . errors && data . status . errors . length ) {
59
- throw new Error ( `${ file . name } : Compile error` ) ;
59
+ throw new Error ( `${ info } Compile error` ) ;
60
60
} else {
61
- vscode . window . showInformationMessage ( `${ file . name } : Compile successed` ) ;
61
+ vscode . window . showInformationMessage ( `${ info } Compile successed` , 'Hide' ) ;
62
62
}
63
- return file ;
63
+ return docs ;
64
64
} )
65
- . then ( loadChanges ) ;
65
+ . then ( loadChanges )
66
+ . catch ( ( error : Error ) => {
67
+ outputChannel . appendLine ( error . message ) ;
68
+ outputChannel . show ( true ) ;
69
+ vscode . window . showErrorMessage ( error . message , 'Show details' )
70
+ . then ( data => {
71
+ outputChannel . show ( true ) ;
72
+ } ) ;
73
+ } ) ;
66
74
}
67
75
68
76
export async function importAndCompile ( askFLags = false ) : Promise < any > {
@@ -76,15 +84,15 @@ export async function importAndCompile(askFLags = false): Promise<any> {
76
84
77
85
const defaultFlags = config ( ) . compileFlags ;
78
86
const flags = askFLags ? await compileFlags ( ) : defaultFlags ;
79
- return importFile ( file , flags ) . catch ( error => {
87
+ return importFile ( file ) . catch ( error => {
80
88
console . error ( error ) ;
81
- } ) ;
89
+ } ) . then ( ( ) => compile ( [ file ] , flags ) ) ;
82
90
}
83
91
84
92
// Compiles all files types in the namespace
85
93
export async function namespaceCompile ( askFLags = false ) : Promise < any > {
86
94
const api = new AtelierAPI ( ) ;
87
- const fileTypes = [ " *.CLS" , " *.MAC" , " *.INC" , " *.BAS" ]
95
+ const fileTypes = [ ' *.CLS' , ' *.MAC' , ' *.INC' , ' *.BAS' ] ;
88
96
if ( ! config ( 'conn' ) . active ) {
89
97
throw new Error ( `No Active Connection` ) ;
90
98
}
@@ -94,21 +102,58 @@ export async function namespaceCompile(askFLags = false): Promise<any> {
94
102
// User cancelled
95
103
return ;
96
104
}
97
- vscode . window . withProgress ( {
98
- location : vscode . ProgressLocation . Notification ,
99
- title : `Compiling Namespace: ${ api . ns } ` ,
100
- cancellable : false
101
- } , async ( ) => {
102
- const data = await api
103
- . actionCompile ( fileTypes , flags ) ;
104
- if ( data . status && data . status . errors && data . status . errors . length ) {
105
- console . error ( data . status . summary ) ;
106
- throw new Error ( `Compiling Namespace: ${ api . ns } Error` ) ;
107
- }
108
- else {
109
- vscode . window . showInformationMessage ( `Compiling Namespace: ${ api . ns } Success` ) ;
105
+ vscode . window . withProgress (
106
+ {
107
+ location : vscode . ProgressLocation . Notification ,
108
+ title : `Compiling Namespace: ${ api . ns } ` ,
109
+ cancellable : false
110
+ } ,
111
+ async ( ) => {
112
+ const data = await api . actionCompile ( fileTypes , flags ) ;
113
+ if ( data . status && data . status . errors && data . status . errors . length ) {
114
+ console . error ( data . status . summary ) ;
115
+ throw new Error ( `Compiling Namespace: ${ api . ns } Error` ) ;
116
+ } else {
117
+ vscode . window . showInformationMessage ( `Compiling Namespace: ${ api . ns } Success` ) ;
118
+ }
119
+ const file = currentFile ( ) ;
120
+ return loadChanges ( [ file ] ) ;
110
121
}
111
- const file = currentFile ( ) ;
112
- return loadChanges ( file ) ;
113
- } ) ;
122
+ ) ;
123
+ }
124
+
125
+ function importFiles ( files ) {
126
+ return Promise . all < CurrentFile > (
127
+ files . map ( file =>
128
+ vscode . workspace
129
+ . openTextDocument ( file )
130
+ . then ( currentFile )
131
+ . then ( file =>
132
+ Promise . resolve ( file )
133
+ . then ( importFile )
134
+ . then ( data => {
135
+ outputChannel . appendLine ( 'Imported file: ' + file . fileName )
136
+ return file ;
137
+ } )
138
+ )
139
+ )
140
+ )
141
+ . then ( compile ) ;
142
+ }
143
+
144
+ export async function importFolder ( uri : vscode . Uri ) : Promise < any > {
145
+ let folder = uri . path ;
146
+ if ( fs . lstatSync ( folder ) . isFile ( ) ) {
147
+ return importFiles ( [ folder ] ) ;
148
+ }
149
+ glob (
150
+ '**/*.{cls,inc,mac,int}' ,
151
+ {
152
+ cwd : folder ,
153
+ nocase : true
154
+ } ,
155
+ ( error , files ) => importFiles (
156
+ files . map ( name => path . join ( folder , name ) )
157
+ )
158
+ ) ;
114
159
}
0 commit comments