@@ -9,15 +9,11 @@ import * as path from 'path';
9
9
import * as fs from 'fs' ;
10
10
11
11
function exists ( file : string ) : Promise < boolean > {
12
- return new Promise < boolean > ( ( resolve , _reject ) => {
13
- fs . exists ( file , ( value ) => {
14
- resolve ( value ) ;
15
- } ) ;
16
- } ) ;
17
- }
18
-
19
- interface TasksObject {
20
- tasks : GenericTask [ ]
12
+ return new Promise < boolean > ( ( resolve , _reject ) => {
13
+ fs . exists ( file , ( value ) => {
14
+ resolve ( value ) ;
15
+ } ) ;
16
+ } ) ;
21
17
}
22
18
23
19
interface XSLTJSTask {
@@ -44,17 +40,16 @@ interface XSLTParameter {
44
40
value : string
45
41
}
46
42
47
- interface GenericTask {
48
- type : string ,
49
- group ?: object
50
- }
51
-
52
43
export class SaxonJsTaskProvider implements vscode . TaskProvider {
53
- static SaxonBuildScriptType : string = 'xslt-js' ;
54
- private tasks : vscode . Task [ ] = [ ] ;
44
+ static SaxonBuildScriptType : string = 'xslt-js' ;
45
+ templateTaskLabel = 'Saxon-JS Transform (New)' ;
46
+ templateTaskFound = false ;
55
47
56
- public async provideTasks ( ) : Promise < vscode . Task [ ] > {
57
- let rootPath = vscode . workspace . rootPath ? vscode . workspace . rootPath : '/' ;
48
+
49
+ constructor ( private workspaceRoot : string ) { }
50
+
51
+ public async provideTasks ( ) : Promise < vscode . Task [ ] > {
52
+ let rootPath = vscode . workspace . rootPath ? vscode . workspace . rootPath : '/' ;
58
53
let tasksPath = path . join ( rootPath , '.vscode' , 'tasks.json' ) ;
59
54
let tasksObject = undefined ;
60
55
@@ -65,121 +60,130 @@ export class SaxonJsTaskProvider implements vscode.TaskProvider {
65
60
66
61
const taskLines = commaFixedTasksText2 . split ( "\n" ) ;
67
62
const jsonTaskLines : string [ ] = [ ] ;
68
- taskLines . forEach ( ( taskLine ) => {
63
+ taskLines . forEach ( ( taskLine ) => {
69
64
if ( taskLine . trimLeft ( ) . startsWith ( '//' ) ) {
70
65
// don't add comment
71
66
} else {
72
67
jsonTaskLines . push ( taskLine ) ;
73
68
}
74
69
} ) ;
75
70
const jsonString = jsonTaskLines . join ( '\n' ) ;
71
+ console . log ( jsonString ) ;
72
+
76
73
tasksObject = JSON . parse ( jsonString ) ;
77
74
78
75
} else {
79
- tasksObject = { tasks : [ ] } ;
76
+ tasksObject = { tasks : [ ] } ;
80
77
}
81
78
82
- return this . getTasks ( tasksObject ) ;
83
- }
79
+ return this . getTasks ( tasksObject . tasks ) ;
80
+ }
84
81
85
- public resolveTask ( _task : vscode . Task ) : vscode . Task | undefined {
86
- return undefined ;
82
+ public resolveTask ( _task : vscode . Task ) : vscode . Task | undefined {
83
+ return this . getTask ( _task . definition ) ;
87
84
}
88
-
85
+
89
86
private getProp ( obj : any , prop : string ) : string {
90
87
return obj [ prop ] ;
91
88
}
92
89
93
- private getTasks ( tasksObject : TasksObject ) : vscode . Task [ ] {
94
- this . tasks = [ ] ;
90
+ private getTasks ( tasks : XSLTJSTask [ ] ) {
91
+ let result : vscode . Task [ ] = [ ] ;
92
+ this . templateTaskFound = false ;
93
+ tasks . forEach ( ( task ) => {
94
+ let newTask = this . getTask ( task ) ;
95
+ if ( newTask ) {
96
+ result . push ( newTask ) ;
97
+ }
98
+ } )
99
+ if ( ! this . templateTaskFound ) {
100
+ let templateTask = this . addTemplateTask ( ) ;
101
+ if ( templateTask ) {
102
+ result . push ( templateTask ) ;
103
+ }
104
+ }
105
+
106
+ return result ;
107
+ }
95
108
96
- let newTaskLabel = 'Saxon-JS Transform (New)' ;
109
+ private addTemplateTask ( ) {
97
110
let nodeModulesDefault = '${workspaceFolder}/node_modules'
98
- let source = 'xslt-js' ;
99
111
let xmlSourceValue = '${file}' ;
100
112
let xsltFilePath = '${file}' ;
101
113
let resultPathValue = '${workspaceFolder}/xslt-out/result1.xml' ;
102
114
103
- let tasks : GenericTask [ ] = tasksObject . tasks ;
104
- let addNewTask = true ;
105
-
106
- for ( let i = 0 ; i < tasks . length + 1 ; i ++ ) {
107
- let genericTask : GenericTask ;
108
- if ( i === tasks . length ) {
109
- if ( addNewTask ) {
110
- let xsltTask : XSLTJSTask = {
111
- type : 'xslt-js' ,
112
- nodeModulesFolder : nodeModulesDefault ,
113
- label : newTaskLabel ,
114
- xsltFile : xsltFilePath ,
115
- xmlSource : xmlSourceValue ,
116
- resultPath : resultPathValue ,
117
- group : {
118
- kind : "build"
119
- }
120
- } ;
121
- genericTask = xsltTask ;
122
- } else {
123
- genericTask = { type : 'ignore' } ;
124
- }
125
- } else {
126
- genericTask = tasks [ i ] ;
115
+ let xsltTask : XSLTJSTask = {
116
+ type : 'xslt-js' ,
117
+ nodeModulesFolder : nodeModulesDefault ,
118
+ label : this . templateTaskLabel ,
119
+ xsltFile : xsltFilePath ,
120
+ xmlSource : xmlSourceValue ,
121
+ resultPath : resultPathValue ,
122
+ group : {
123
+ kind : "build"
127
124
}
128
- if ( genericTask . type === 'xslt-js' ) {
129
- let xsltTask : XSLTJSTask = < XSLTJSTask > genericTask ;
130
- if ( xsltTask . label === 'xslt-js: ' + newTaskLabel || xsltTask . label === newTaskLabel ) {
131
- // do not add a new task if there's already a task with the 'new' task label
132
- addNewTask = false ;
133
- }
125
+ } ;
134
126
135
- let commandLineArgs : string [ ] = [ ] ;
127
+ return this . getTask ( xsltTask ) ;
128
+ }
136
129
137
- let xsltParameters : XSLTParameter [ ] = xsltTask . parameters ? xsltTask . parameters : [ ] ;
138
- let xsltParametersCommand : string [ ] = [ ]
139
- for ( const param of xsltParameters ) {
140
- xsltParametersCommand . push ( '"' + param . name + '=' + param . value + '"' ) ;
141
- }
142
-
143
- for ( const propName in xsltTask ) {
144
- let propValue = this . getProp ( xsltTask , propName ) ;
145
- switch ( propName ) {
146
- case 'xsltFile' :
147
- commandLineArgs . push ( '-xsl:' + propValue ) ;
148
- break
149
- case 'xmlSource' :
150
- commandLineArgs . push ( '-s:' + propValue ) ;
151
- break ;
152
- case 'resultPath' :
153
- commandLineArgs . push ( '-o:' + propValue ) ;
154
- break
155
- case 'initialTemplate' :
156
- commandLineArgs . push ( '-it:' + propValue ) ;
157
- break ;
158
- case 'initialMode' :
159
- commandLineArgs . push ( '-im:' + propValue ) ;
160
- break ;
161
- case 'export' :
162
- commandLineArgs . push ( '-export:' + propValue ) ;
163
- break ;
164
- }
165
- }
130
+ private getTask ( genericTask : vscode . TaskDefinition ) : vscode . Task | undefined {
166
131
167
- let nodeModulesPath = xsltTask . nodeModulesFolder + path . sep + '.bin' + path . sep ;
132
+ let source = 'xslt-js' ;
168
133
169
- if ( xsltParametersCommand . length > 0 ) {
170
- commandLineArgs . push ( xsltParametersCommand . join ( ' ' ) ) ;
171
- }
134
+ if ( genericTask . type === 'xslt-js' ) {
135
+ let xsltTask : XSLTJSTask = < XSLTJSTask > genericTask ;
136
+ if ( xsltTask . label === 'xslt-js: ' + this . templateTaskLabel ) {
137
+ this . templateTaskFound = true ;
138
+ }
172
139
173
- let resolvedCommandLine = commandLineArgs . join ( ' ' ) ;
174
- // this is overriden if problemMatcher is set in the tasks.json file
175
- let problemMatcher = "$saxon-xslt-js" ;
176
- let commandline = `${ nodeModulesPath } xslt3 ${ resolvedCommandLine } ` ;
177
- let newTask = new vscode . Task ( xsltTask , xsltTask . label , source , new vscode . ShellExecution ( commandline ) , problemMatcher ) ;
178
- this . tasks . push ( newTask ) ;
140
+ let commandLineArgs : string [ ] = [ ] ;
141
+
142
+ let xsltParameters : XSLTParameter [ ] = xsltTask . parameters ? xsltTask . parameters : [ ] ;
143
+ let xsltParametersCommand : string [ ] = [ ]
144
+ for ( const param of xsltParameters ) {
145
+ xsltParametersCommand . push ( '"' + param . name + '=' + param . value + '"' ) ;
146
+ }
147
+
148
+ for ( const propName in xsltTask ) {
149
+ let propValue = this . getProp ( xsltTask , propName ) ;
150
+ switch ( propName ) {
151
+ case 'xsltFile' :
152
+ commandLineArgs . push ( '-xsl:' + propValue ) ;
153
+ break
154
+ case 'xmlSource' :
155
+ commandLineArgs . push ( '-s:' + propValue ) ;
156
+ break ;
157
+ case 'resultPath' :
158
+ commandLineArgs . push ( '-o:' + propValue ) ;
159
+ break
160
+ case 'initialTemplate' :
161
+ commandLineArgs . push ( '-it:' + propValue ) ;
162
+ break ;
163
+ case 'initialMode' :
164
+ commandLineArgs . push ( '-im:' + propValue ) ;
165
+ break ;
166
+ case 'export' :
167
+ commandLineArgs . push ( '-export:' + propValue ) ;
168
+ break ;
169
+ }
179
170
}
180
- }
181
171
182
- return this . tasks ;
172
+ let nodeModulesPath = xsltTask . nodeModulesFolder + path . sep + '.bin' + path . sep ;
173
+
174
+ if ( xsltParametersCommand . length > 0 ) {
175
+ commandLineArgs . push ( xsltParametersCommand . join ( ' ' ) ) ;
176
+ }
183
177
184
- }
178
+ let resolvedCommandLine = commandLineArgs . join ( ' ' ) ;
179
+ // this is overriden if problemMatcher is set in the tasks.json file
180
+ let problemMatcher = "$saxon-xslt-js" ;
181
+ let commandline = `${ nodeModulesPath } xslt3 ${ resolvedCommandLine } ` ;
182
+ let newTask = new vscode . Task ( xsltTask , xsltTask . label , source , new vscode . ShellExecution ( commandline ) , problemMatcher ) ;
183
+ return newTask ;
184
+ } else {
185
+ return undefined ;
186
+ }
187
+
188
+ }
185
189
}
0 commit comments