From c11051533572a8fa82ef471211d76ad4ed749269 Mon Sep 17 00:00:00 2001 From: Merlin Beutlberger Date: Fri, 16 Nov 2018 13:00:09 +0100 Subject: [PATCH] [FEATURE] projectPreprocessor: Add handling for task extensions As per RFC0004: https://github.com/SAP/ui5-tooling/pull/54 Depends on https://github.com/SAP/ui5-builder/pull/106 --- lib/projectPreprocessor.js | 21 +++++++++ test/fixtures/application.a/task.a.js | 1 + test/lib/extensions.js | 65 +++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 test/fixtures/application.a/task.a.js diff --git a/lib/projectPreprocessor.js b/lib/projectPreprocessor.js index fdcf7112e..835a6839b 100644 --- a/lib/projectPreprocessor.js +++ b/lib/projectPreprocessor.js @@ -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}`); } @@ -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) { @@ -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); + } } /** diff --git a/test/fixtures/application.a/task.a.js b/test/fixtures/application.a/task.a.js new file mode 100644 index 000000000..ea41b01de --- /dev/null +++ b/test/fixtures/application.a/task.a.js @@ -0,0 +1 @@ +module.exports = function () {}; diff --git a/test/lib/extensions.js b/test/lib/extensions.js index 3ce6c4d88..c3226c352 100644 --- a/test/lib/extensions.js +++ b/test/lib/extensions.js @@ -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"); +});