Skip to content

Commit 4bf19c7

Browse files
committed
update saxonJSTaskProvider to be more robust
1 parent 902cd11 commit 4bf19c7

File tree

1 file changed

+106
-102
lines changed

1 file changed

+106
-102
lines changed

src/saxonJsTaskProvider.ts

Lines changed: 106 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ import * as path from 'path';
99
import * as fs from 'fs';
1010

1111
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+
});
2117
}
2218

2319
interface XSLTJSTask {
@@ -44,17 +40,16 @@ interface XSLTParameter {
4440
value: string
4541
}
4642

47-
interface GenericTask {
48-
type: string,
49-
group?: object
50-
}
51-
5243
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;
5547

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 : '/';
5853
let tasksPath = path.join(rootPath, '.vscode', 'tasks.json');
5954
let tasksObject = undefined;
6055

@@ -65,121 +60,130 @@ export class SaxonJsTaskProvider implements vscode.TaskProvider {
6560

6661
const taskLines = commaFixedTasksText2.split("\n");
6762
const jsonTaskLines: string[] = [];
68-
taskLines.forEach((taskLine) => {
63+
taskLines.forEach((taskLine) => {
6964
if (taskLine.trimLeft().startsWith('//')) {
7065
// don't add comment
7166
} else {
7267
jsonTaskLines.push(taskLine);
7368
}
7469
});
7570
const jsonString = jsonTaskLines.join('\n');
71+
console.log(jsonString);
72+
7673
tasksObject = JSON.parse(jsonString);
7774

7875
} else {
79-
tasksObject = {tasks: []};
76+
tasksObject = { tasks: [] };
8077
}
8178

82-
return this.getTasks(tasksObject);
83-
}
79+
return this.getTasks(tasksObject.tasks);
80+
}
8481

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);
8784
}
88-
85+
8986
private getProp(obj: any, prop: string): string {
9087
return obj[prop];
9188
}
9289

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

96-
let newTaskLabel = 'Saxon-JS Transform (New)';
109+
private addTemplateTask() {
97110
let nodeModulesDefault = '${workspaceFolder}/node_modules'
98-
let source = 'xslt-js';
99111
let xmlSourceValue = '${file}';
100112
let xsltFilePath = '${file}';
101113
let resultPathValue = '${workspaceFolder}/xslt-out/result1.xml';
102114

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"
127124
}
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+
};
134126

135-
let commandLineArgs: string[] = [];
127+
return this.getTask(xsltTask);
128+
}
136129

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

167-
let nodeModulesPath = xsltTask.nodeModulesFolder + path.sep + '.bin' + path.sep;
132+
let source = 'xslt-js';
168133

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

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+
}
179170
}
180-
}
181171

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

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

0 commit comments

Comments
 (0)