Skip to content

Commit

Permalink
[FEATURE] TaskUtil: Add resourceFactory API to v3 interface
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Nov 24, 2022
1 parent 665eecc commit 2e863cf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
58 changes: 58 additions & 0 deletions lib/build/helpers/TaskUtil.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import {
createReaderCollectionPrioritized,
createResource,
createFilterReader,
createLinkReader,
createFlatReader
} from "@ui5/fs/resourceFactory";

/**
* Convenience functions for UI5 tasks.
* An instance of this class is passed to every standard UI5 task that requires it.
Expand Down Expand Up @@ -220,6 +228,43 @@ class TaskUtil {
return this._projectBuildContext.getDependencies(projectName);
}

/**
* Specification Version-dependent set of [@ui5/fs/resourceFactory]{@link @ui5/fs/resourceFactory}
* functions provided to tasks.
* For details on individual functions, see [@ui5/fs/resourceFactory]{@link @ui5/fs/resourceFactory}
*
* @public
* @typedef {object} @ui5/project/build/helpers/TaskUtil~resourceFactory
* @property {Function} createResource Creates a [Resource]{@link @ui5/fs/Resource}.
* Accepts the same parameters as the [Resource]{@link @ui5/fs/Resource} constructor.
* @property {Function} createReaderCollectionPrioritized Creates a prioritized reader collection:
* [ReaderCollectionPrioritized]{@link @ui5/fs/ReaderCollectionPrioritized}
* @property {Function} createFilterReader
* Create a [Filter-Reader]{@link @ui5/fs/readers/Filter} with the given reader.
* @property {Function} createLinkReader
* Create a [Link-Reader]{@link @ui5/fs/readers/Filter} with the given reader.
* @property {Function} createFlatReader Create a [Link-Reader]{@link @ui5/fs/readers/Link}
* where all requests are prefixed with <code>/resources/<namespace></code>.
*/

/**
* Provides limited access to [@ui5/fs/resourceFactory]{@link @ui5/fs/resourceFactory} functions
*
* </br></br>
* This attribute is only available to custom task extensions defining
* <b>Specification Version 3.0 and above</b>.
*
* @type {@ui5/project/build/helpers/TaskUtil~resourceFactory}
* @public
*/
resourceFactory = {
createResource,
createReaderCollectionPrioritized,
createFilterReader,
createLinkReader,
createFlatReader,
};

/**
* Get an interface to an instance of this class that only provides those functions
* that are supported by the given custom task extension specification version.
Expand All @@ -246,6 +291,7 @@ class TaskUtil {
case "2.6":
return baseInterface;
case "3.0":
// getProject function, returning an interfaced project instance
baseInterface.getProject = (projectName) => {
const project = this.getProject(projectName);
const baseProjectInterface = {};
Expand All @@ -258,9 +304,21 @@ class TaskUtil {
return baseProjectInterface;
}
};
// getDependencies function, returning an array of project names
baseInterface.getDependencies = (projectName) => {
return this.getDependencies(projectName);
};

baseInterface.resourceFactory = Object.create(null);
[
// Once new functions get added, extract this array into a variable
// and enhance based on spec version once new functions get added
"createResource", "createReaderCollectionPrioritized",
"createFilterReader", "createLinkReader", "createFlatReader",
].forEach((factoryFunction) => {
baseInterface.resourceFactory[factoryFunction] = this.resourceFactory[factoryFunction];
});

return baseInterface;
default:
throw new Error(`TaskUtil: Unknown or unsupported Specification Version ${specVersion}`);
Expand Down
19 changes: 18 additions & 1 deletion test/lib/build/helpers/TaskUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ test("getInterface: specVersion 3.0", (t) => {
getFrameworkVersion: () => "frameworkVersion", // Should not be exposed
});
const getDependenciesStub = sinon.stub().returns(["dep a", "dep b"]);

const taskUtil = new TaskUtil({
projectBuildContext: {
getProject: getProjectStub,
Expand All @@ -359,7 +360,8 @@ test("getInterface: specVersion 3.0", (t) => {
"isRootProject",
"registerCleanupTask",
"getProject",
"getDependencies"
"getDependencies",
"resourceFactory",
], "Correct methods are provided");

t.deepEqual(interfacedTaskUtil.STANDARD_TAGS, STANDARD_TAGS, "attribute STANDARD_TAGS is provided");
Expand All @@ -370,6 +372,7 @@ test("getInterface: specVersion 3.0", (t) => {
t.is(typeof interfacedTaskUtil.registerCleanupTask, "function", "function registerCleanupTask is provided");
t.is(typeof interfacedTaskUtil.getProject, "function", "function registerCleanupTask is provided");

// getProject
const interfacedProject = interfacedTaskUtil.getProject("pony");
t.deepEqual(Object.keys(interfacedProject), [
"getSpecVersion",
Expand All @@ -395,8 +398,22 @@ test("getInterface: specVersion 3.0", (t) => {
t.is(interfacedProject.isFrameworkProject(), "isFrameworkProject",
"isFrameworkProject function is bound correctly");

// getDependencies
t.deepEqual(interfacedTaskUtil.getDependencies("pony"), ["dep a", "dep b"],
"getDependencies function is available and bound correctly");

// resourceFactory
const resourceFactory = interfacedTaskUtil.resourceFactory;
t.is(typeof resourceFactory.createResource, "function",
"resourceFactory function createResource is available");
t.is(typeof resourceFactory.createReaderCollectionPrioritized, "function",
"resourceFactory function createReaderCollectionPrioritized is available");
t.is(typeof resourceFactory.createFilterReader, "function",
"resourceFactory function createFilterReader is available");
t.is(typeof resourceFactory.createLinkReader, "function",
"resourceFactory function createLinkReader is available");
t.is(typeof resourceFactory.createFlatReader, "function",
"resourceFactory function createFlatReader is available");
});

test("getInterface: specVersion undefined", (t) => {
Expand Down

0 comments on commit 2e863cf

Please sign in to comment.