@@ -25,12 +25,7 @@ function pathSeparator() {
25
25
}
26
26
}
27
27
28
- interface TasksObject {
29
- tasks : GenericTask [ ]
30
- }
31
-
32
- interface XSLTTask {
33
- type : string ,
28
+ interface XSLTTask extends vscode . TaskDefinition {
34
29
label : string ,
35
30
saxonJar : string ,
36
31
xsltFile : string ,
@@ -53,14 +48,11 @@ interface XSLTParameter {
53
48
value : string
54
49
}
55
50
56
- interface GenericTask {
57
- type : string ,
58
- group ?: object
59
- }
60
-
61
51
export class SaxonTaskProvider implements vscode . TaskProvider {
62
52
static SaxonBuildScriptType : string = 'xslt' ;
63
- private tasks : vscode . Task [ ] = [ ] ;
53
+ templateTaskLabel = 'Saxon Transform (New)' ;
54
+ templateTaskFound = false ;
55
+
64
56
65
57
constructor ( private workspaceRoot : string ) { }
66
58
@@ -71,7 +63,10 @@ export class SaxonTaskProvider implements vscode.TaskProvider {
71
63
72
64
if ( await exists ( tasksPath ) ) {
73
65
const tasksText = fs . readFileSync ( tasksPath ) . toString ( 'utf-8' ) ;
74
- const taskLines = tasksText . split ( "\n" ) ;
66
+ const commaFixedTasksText1 = tasksText . replace ( / , \s * } / , '}' ) ;
67
+ const commaFixedTasksText2 = commaFixedTasksText1 . replace ( / , \s * \] / , ']' ) ;
68
+
69
+ const taskLines = commaFixedTasksText2 . split ( "\n" ) ;
75
70
const jsonTaskLines : string [ ] = [ ] ;
76
71
taskLines . forEach ( ( taskLine ) => {
77
72
if ( taskLine . trimLeft ( ) . startsWith ( '//' ) ) {
@@ -81,140 +76,151 @@ export class SaxonTaskProvider implements vscode.TaskProvider {
81
76
}
82
77
} ) ;
83
78
const jsonString = jsonTaskLines . join ( '\n' ) ;
79
+ console . log ( jsonString ) ;
80
+
84
81
tasksObject = JSON . parse ( jsonString ) ;
85
82
86
83
} else {
87
84
tasksObject = { tasks : [ ] } ;
88
85
}
89
86
90
- return this . getTasks ( tasksObject ) ;
87
+ return this . getTasks ( tasksObject . tasks ) ;
91
88
}
92
89
93
90
public resolveTask ( _task : vscode . Task ) : vscode . Task | undefined {
94
- return undefined ;
91
+ return this . getTask ( _task . definition ) ;
95
92
}
96
93
97
94
private getProp ( obj : any , prop : string ) : string {
98
95
return obj [ prop ] ;
99
96
}
100
97
101
- private getTasks ( tasksObject : TasksObject ) : vscode . Task [ ] {
102
- this . tasks = [ ] ;
98
+ private getTasks ( tasks : XSLTTask [ ] ) {
99
+ let result : vscode . Task [ ] = [ ] ;
100
+ this . templateTaskFound = false ;
101
+ tasks . forEach ( ( task ) => {
102
+ let newTask = this . getTask ( task ) ;
103
+ if ( newTask ) {
104
+ result . push ( newTask ) ;
105
+ }
106
+ } )
107
+ if ( ! this . templateTaskFound ) {
108
+ let templateTask = this . addTemplateTask ( ) ;
109
+ if ( templateTask ) {
110
+ result . push ( templateTask ) ;
111
+ }
112
+ }
113
+
114
+ return result ;
115
+ }
103
116
104
- let newTaskLabel = 'Saxon Transform (New)' ;
117
+ private addTemplateTask ( ) {
105
118
let saxonJarDefault = '${config:XSLT.tasks.saxonJar}'
106
- let source = 'xslt' ;
107
119
let xmlSourceValue = '${file}' ;
108
120
let xsltFilePath = '${file}' ;
109
121
let resultPathValue = '${workspaceFolder}/xslt-out/result1.xml' ;
110
122
111
- let tasks : GenericTask [ ] = tasksObject . tasks ;
112
- let addNewTask = true ;
113
-
114
- for ( let i = 0 ; i < tasks . length + 1 ; i ++ ) {
115
- let genericTask : GenericTask ;
116
- if ( i === tasks . length ) {
117
- if ( addNewTask ) {
118
- let xsltTask : XSLTTask = {
119
- type : 'xslt' ,
120
- saxonJar : saxonJarDefault ,
121
- label : newTaskLabel ,
122
- xsltFile : xsltFilePath ,
123
- xmlSource : xmlSourceValue ,
124
- resultPath : resultPathValue
125
- } ;
126
- genericTask = xsltTask ;
127
- } else {
128
- genericTask = { type : 'ignore' } ;
129
- }
130
- } else {
131
- genericTask = tasks [ i ] ;
123
+ let xsltTask : XSLTTask = {
124
+ type : 'xslt' ,
125
+ saxonJar : saxonJarDefault ,
126
+ label : this . templateTaskLabel ,
127
+ xsltFile : xsltFilePath ,
128
+ xmlSource : xmlSourceValue ,
129
+ resultPath : resultPathValue ,
130
+ group : {
131
+ kind : "build"
132
132
}
133
- if ( genericTask . type === 'xslt' ) {
134
- let xsltTask : XSLTTask = < XSLTTask > genericTask ;
135
- if ( xsltTask . label === 'xslt: ' + newTaskLabel || xsltTask . label === newTaskLabel ) {
136
- // do not add a new task if there's already a task with the 'new' task label
137
- addNewTask = false ;
138
- }
139
- xsltTask . group = {
140
- kind : 'build'
141
- }
133
+ } ;
142
134
143
- let commandLineArgs : string [ ] = [ ] ;
135
+ return this . getTask ( xsltTask ) ;
136
+ }
144
137
145
- let xsltParameters : XSLTParameter [ ] = xsltTask . parameters ? xsltTask . parameters : [ ] ;
146
- let xsltParametersCommand : string [ ] = [ ]
147
- for ( const param of xsltParameters ) {
148
- xsltParametersCommand . push ( '"' + param . name + '=' + param . value + '"' ) ;
149
- }
150
- let classPaths : string [ ] = [ xsltTask . saxonJar ] ;
151
- if ( xsltTask . classPathEntries ) {
152
- classPaths = classPaths . concat ( xsltTask . classPathEntries ) ;
153
- }
138
+ private getTask ( genericTask : vscode . TaskDefinition ) : vscode . Task | undefined {
154
139
155
- for ( const propName in xsltTask ) {
156
- let propValue = this . getProp ( xsltTask , propName ) ;
157
- switch ( propName ) {
158
- case 'xsltFile' :
159
- commandLineArgs . push ( '-xsl:' + propValue ) ;
160
- break
161
- case 'xmlSource' :
162
- commandLineArgs . push ( '-s:' + propValue ) ;
163
- break ;
164
- case 'resultPath' :
165
- commandLineArgs . push ( '-o:' + propValue ) ;
166
- break
167
- case 'initialTemplate' :
168
- commandLineArgs . push ( '-it:' + propValue ) ;
169
- break ;
170
- case 'initialMode' :
171
- commandLineArgs . push ( '-im:' + propValue ) ;
172
- break ;
173
- case 'catalogFilenames' :
174
- commandLineArgs . push ( '-catalog:' + propValue ) ;
175
- break ;
176
- case 'configFilename' :
177
- commandLineArgs . push ( '-config:' + propValue ) ;
178
- break ;
179
- case 'dtd' :
180
- commandLineArgs . push ( '-dtd:' + propValue ) ;
181
- break ;
182
- case 'enableAssertions' :
183
- commandLineArgs . push ( '-ea' + propValue ) ;
184
- break ;
185
- case 'expandValues' :
186
- commandLineArgs . push ( '-expand:' + propValue ) ;
187
- break ;
188
- case 'explainFilename' :
189
- commandLineArgs . push ( '-explain:' + propValue ) ;
190
- break ;
191
- case 'exportFilename' :
192
- commandLineArgs . push ( '-export:' + propValue ) ;
193
- break ;
194
- case 'traceOutFilename' :
195
- commandLineArgs . push ( '-traceOut:' + propValue ) ;
196
- break ;
197
- case 'timing' :
198
- commandLineArgs . push ( '-t' ) ;
199
- break ;
200
- }
201
- }
140
+ let source = 'xslt' ;
141
+
142
+ if ( genericTask . type === 'xslt' ) {
143
+ let xsltTask : XSLTTask = < XSLTTask > genericTask ;
144
+ if ( xsltTask . label === 'xslt: ' + this . templateTaskLabel ) {
145
+ this . templateTaskFound = true ;
146
+ }
202
147
203
- if ( xsltParametersCommand . length > 0 ) {
204
- commandLineArgs . push ( xsltParametersCommand . join ( ' ' ) ) ;
148
+ let commandLineArgs : string [ ] = [ ] ;
149
+
150
+ let xsltParameters : XSLTParameter [ ] = xsltTask . parameters ? xsltTask . parameters : [ ] ;
151
+ let xsltParametersCommand : string [ ] = [ ]
152
+ for ( const param of xsltParameters ) {
153
+ xsltParametersCommand . push ( '"' + param . name + '=' + param . value + '"' ) ;
154
+ }
155
+ let classPaths : string [ ] = [ xsltTask . saxonJar ] ;
156
+ if ( xsltTask . classPathEntries ) {
157
+ classPaths = classPaths . concat ( xsltTask . classPathEntries ) ;
158
+ }
159
+
160
+ for ( const propName in xsltTask ) {
161
+ let propValue = this . getProp ( xsltTask , propName ) ;
162
+ switch ( propName ) {
163
+ case 'xsltFile' :
164
+ commandLineArgs . push ( '-xsl:' + propValue ) ;
165
+ break
166
+ case 'xmlSource' :
167
+ commandLineArgs . push ( '-s:' + propValue ) ;
168
+ break ;
169
+ case 'resultPath' :
170
+ commandLineArgs . push ( '-o:' + propValue ) ;
171
+ break
172
+ case 'initialTemplate' :
173
+ commandLineArgs . push ( '-it:' + propValue ) ;
174
+ break ;
175
+ case 'initialMode' :
176
+ commandLineArgs . push ( '-im:' + propValue ) ;
177
+ break ;
178
+ case 'catalogFilenames' :
179
+ commandLineArgs . push ( '-catalog:' + propValue ) ;
180
+ break ;
181
+ case 'configFilename' :
182
+ commandLineArgs . push ( '-config:' + propValue ) ;
183
+ break ;
184
+ case 'dtd' :
185
+ commandLineArgs . push ( '-dtd:' + propValue ) ;
186
+ break ;
187
+ case 'enableAssertions' :
188
+ commandLineArgs . push ( '-ea' + propValue ) ;
189
+ break ;
190
+ case 'expandValues' :
191
+ commandLineArgs . push ( '-expand:' + propValue ) ;
192
+ break ;
193
+ case 'explainFilename' :
194
+ commandLineArgs . push ( '-explain:' + propValue ) ;
195
+ break ;
196
+ case 'exportFilename' :
197
+ commandLineArgs . push ( '-export:' + propValue ) ;
198
+ break ;
199
+ case 'traceOutFilename' :
200
+ commandLineArgs . push ( '-traceOut:' + propValue ) ;
201
+ break ;
202
+ case 'timing' :
203
+ commandLineArgs . push ( '-t' ) ;
204
+ break ;
205
205
}
206
+ }
206
207
207
- let classPathString = classPaths . join ( pathSeparator ( ) ) ;
208
- let resolvedCommandLine = commandLineArgs . join ( ' ' ) ;
209
- // this is overriden if problemMatcher is set in the tasks.json file
210
- let problemMatcher = "$saxon-xslt" ;
211
- let commandline = `java -cp ${ classPathString } net.sf.saxon.Transform ${ resolvedCommandLine } ` ;
212
- let newTask = new vscode . Task ( xsltTask , xsltTask . label , source , new vscode . ShellExecution ( commandline ) , problemMatcher ) ;
213
- this . tasks . push ( newTask ) ;
208
+ if ( xsltParametersCommand . length > 0 ) {
209
+ commandLineArgs . push ( xsltParametersCommand . join ( ' ' ) ) ;
214
210
}
211
+
212
+ let classPathString = classPaths . join ( pathSeparator ( ) ) ;
213
+ let resolvedCommandLine = commandLineArgs . join ( ' ' ) ;
214
+ // this is overriden if problemMatcher is set in the tasks.json file
215
+ let problemMatcher = "$saxon-xslt" ;
216
+ let commandline = `java -cp ${ classPathString } net.sf.saxon.Transform ${ resolvedCommandLine } ` ;
217
+ let newTask = new vscode . Task ( xsltTask , xsltTask . label , source , new vscode . ShellExecution ( commandline ) , problemMatcher ) ;
218
+ return newTask ;
219
+ } else {
220
+ return undefined ;
215
221
}
222
+
216
223
217
- return this . tasks ;
218
224
219
225
}
220
226
}
0 commit comments