Skip to content

Commit

Permalink
[FEATURE] projectPreprocessor: Add handling for task extensions
Browse files Browse the repository at this point in the history
As per RFC0004: SAP/ui5-tooling#54
Depends on SAP/ui5-builder#106
  • Loading branch information
RandomByte committed Nov 17, 2018
1 parent 0491c7f commit 0722865
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/projectPreprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ class ProjectPreprocessor {
case "project-shim":
this.handleShim(extension);
break;
case "task":
this.handleTask(extension);
break;
default:
throw new Error(`Unknown extension type '${extension.type}' for ${extension.id}`);
}
Expand All @@ -349,6 +352,9 @@ class ProjectPreprocessor {
}

handleShim(extension) {
if (!extension.shims) {
throw new Error(`Project shim extension ${extension.id} is missing 'shim' configuration`);
}
const {configurations, dependencies, collections} = extension.shims;

if (configurations) {
Expand Down Expand Up @@ -465,6 +471,21 @@ class ProjectPreprocessor {
}
}
}

handleTask(extension) {
if (!extension.metadata && !extension.metadata.name) {
throw new Error(`Task extension ${extension.id} is missing 'metadata.name' configuration`);
}
if (!extension.task) {
throw new Error(`Task extension ${extension.id} is missing 'task' configuration`);
}
const taskRepository = require("@ui5/builder").tasks.taskRepository;

const taskPath = path.join(extension.path, extension.task.path);
const task = require(taskPath); // throws if not found

taskRepository.addTask(extension.metadata.name, task);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/application.a/task.a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function () {};
65 changes: 65 additions & 0 deletions test/lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,68 @@ test("Project with unknown extension dependency inline configuration", (t) => {
return t.throws(projectPreprocessor.processTree(tree),
"Unknown extension type 'phony-pony' for extension.a", "Rejected with error");
});

test("Project with task extension dependency", (t) => {
// "project-type" extension handling not yet implemented => test currently checks for error
const tree = {
id: "application.a",
path: applicationAPath,
dependencies: [{
id: "ext.task.a",
path: applicationAPath,
dependencies: [],
version: "1.0.0",
specVersion: "0.1",
kind: "extension",
type: "task",
metadata: {
name: "task.a"
},
task: {
path: "task.a.js"
}
}],
version: "1.0.0",
specVersion: "0.1",
type: "application",
metadata: {
name: "xy"
}
};
return projectPreprocessor.processTree(tree).then((parsedTree) => {
t.deepEqual(parsedTree.dependencies.length, 0, "Application project has no dependencies");
const taskRepository = require("@ui5/builder").tasks.taskRepository;
t.truthy(taskRepository.getTask("task.a"), "task.a has been added to the task repository");
});
});

test("Project with task extension dependency - task module not found", async (t) => {
// "project-type" extension handling not yet implemented => test currently checks for error
const tree = {
id: "application.a",
path: applicationAPath,
dependencies: [{
id: "ext.task.a",
path: applicationAPath,
dependencies: [],
version: "1.0.0",
specVersion: "0.1",
kind: "extension",
type: "task",
metadata: {
name: "task.a"
},
task: {
path: "task.not.existing.js"
}
}],
version: "1.0.0",
specVersion: "0.1",
type: "application",
metadata: {
name: "xy"
}
};
const error = await t.throws(projectPreprocessor.processTree(tree));
t.regex(error.message, /^Cannot find module.*/, "Rejected with error");
});

0 comments on commit 0722865

Please sign in to comment.