Skip to content

Commit 902cd11

Browse files
committed
update taskProviders for saxon java+js to be more robust in parsing etc.
1 parent 7afecb5 commit 902cd11

File tree

3 files changed

+140
-121
lines changed

3 files changed

+140
-121
lines changed

.vscode/tasks.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"presentation": {
1212
"reveal": "never"
1313
},
14+
"group": "build",
15+
"label": "npm: watch",
16+
"detail": "tsc -watch -p ./"
17+
},
18+
{
19+
"type": "npm",
20+
"script": "lint",
21+
"problemMatcher": [],
22+
"label": "npm: lint",
23+
"detail": "tslint -p ./",
1424
"group": {
1525
"kind": "build",
1626
"isDefault": true

src/saxonJsTaskProvider.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export class SaxonJsTaskProvider implements vscode.TaskProvider {
6060

6161
if (await exists(tasksPath)) {
6262
const tasksText = fs.readFileSync(tasksPath).toString('utf-8');
63-
const taskLines = tasksText.split("\n");
63+
const commaFixedTasksText1 = tasksText.replace(/,\s*}/, '}');
64+
const commaFixedTasksText2 = commaFixedTasksText1.replace(/,\s*\]/, ']');
65+
66+
const taskLines = commaFixedTasksText2.split("\n");
6467
const jsonTaskLines: string[] = [];
6568
taskLines.forEach((taskLine) => {
6669
if (taskLine.trimLeft().startsWith('//')) {
@@ -110,7 +113,10 @@ export class SaxonJsTaskProvider implements vscode.TaskProvider {
110113
label: newTaskLabel,
111114
xsltFile: xsltFilePath,
112115
xmlSource: xmlSourceValue,
113-
resultPath: resultPathValue
116+
resultPath: resultPathValue,
117+
group: {
118+
kind: "build"
119+
}
114120
};
115121
genericTask = xsltTask;
116122
} else {
@@ -125,9 +131,6 @@ export class SaxonJsTaskProvider implements vscode.TaskProvider {
125131
// do not add a new task if there's already a task with the 'new' task label
126132
addNewTask = false;
127133
}
128-
xsltTask.group = {
129-
kind: 'build'
130-
}
131134

132135
let commandLineArgs: string[] = [];
133136

src/saxonTaskProvider.ts

Lines changed: 122 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ function pathSeparator() {
2525
}
2626
}
2727

28-
interface TasksObject {
29-
tasks: GenericTask[]
30-
}
31-
32-
interface XSLTTask {
33-
type: string,
28+
interface XSLTTask extends vscode.TaskDefinition {
3429
label: string,
3530
saxonJar: string,
3631
xsltFile: string,
@@ -53,14 +48,11 @@ interface XSLTParameter {
5348
value: string
5449
}
5550

56-
interface GenericTask {
57-
type: string,
58-
group?: object
59-
}
60-
6151
export class SaxonTaskProvider implements vscode.TaskProvider {
6252
static SaxonBuildScriptType: string = 'xslt';
63-
private tasks: vscode.Task[] = [];
53+
templateTaskLabel = 'Saxon Transform (New)';
54+
templateTaskFound = false;
55+
6456

6557
constructor(private workspaceRoot: string) { }
6658

@@ -71,7 +63,10 @@ export class SaxonTaskProvider implements vscode.TaskProvider {
7163

7264
if (await exists(tasksPath)) {
7365
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");
7570
const jsonTaskLines: string[] = [];
7671
taskLines.forEach((taskLine) => {
7772
if (taskLine.trimLeft().startsWith('//')) {
@@ -81,140 +76,151 @@ export class SaxonTaskProvider implements vscode.TaskProvider {
8176
}
8277
});
8378
const jsonString = jsonTaskLines.join('\n');
79+
console.log(jsonString);
80+
8481
tasksObject = JSON.parse(jsonString);
8582

8683
} else {
8784
tasksObject = { tasks: [] };
8885
}
8986

90-
return this.getTasks(tasksObject);
87+
return this.getTasks(tasksObject.tasks);
9188
}
9289

9390
public resolveTask(_task: vscode.Task): vscode.Task | undefined {
94-
return undefined;
91+
return this.getTask(_task.definition);
9592
}
9693

9794
private getProp(obj: any, prop: string): string {
9895
return obj[prop];
9996
}
10097

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+
}
103116

104-
let newTaskLabel = 'Saxon Transform (New)';
117+
private addTemplateTask() {
105118
let saxonJarDefault = '${config:XSLT.tasks.saxonJar}'
106-
let source = 'xslt';
107119
let xmlSourceValue = '${file}';
108120
let xsltFilePath = '${file}';
109121
let resultPathValue = '${workspaceFolder}/xslt-out/result1.xml';
110122

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"
132132
}
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+
};
142134

143-
let commandLineArgs: string[] = [];
135+
return this.getTask(xsltTask);
136+
}
144137

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 {
154139

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+
}
202147

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;
205205
}
206+
}
206207

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(' '));
214210
}
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;
215221
}
222+
216223

217-
return this.tasks;
218224

219225
}
220226
}

0 commit comments

Comments
 (0)