Skip to content

Commit

Permalink
[FEAT]: enable build with CSS variables
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Mar 24, 2022
1 parent 81285b3 commit ba54f72
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/builder/BuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@ const ProjectBuildContext = require("./ProjectBuildContext");
* @memberof module:@ui5/builder.builder
*/
class BuildContext {
constructor({rootProject}) {
constructor({rootProject, options = {}}) {
if (!rootProject) {
throw new Error(`Missing parameter 'rootProject'`);
}
this.rootProject = rootProject;
this.projectBuildContexts = [];
this.options = options;
}

getRootProject() {
return this.rootProject;
}

getOptions() {
return this.options;
}

getOption(key) {
return this.options[key];
}

createProjectContext({project, resources}) {
const projectBuildContext = new ProjectBuildContext({
buildContext: this,
Expand Down
8 changes: 8 additions & 0 deletions lib/builder/ProjectBuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class ProjectBuildContext {
return this._project === this._buildContext.getRootProject();
}

getOptions() {
this._buildContext.getOptions();
}

getOption(key) {
this._buildContext.getOption(key);
}

registerCleanupTask(callback) {
this.queues.cleanup.push(callback);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/builder/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ module.exports = {
* @param {boolean} [parameters.dev=false]
* Decides whether a development build should be activated (skips non-essential and time-intensive tasks)
* @param {boolean} [parameters.selfContained=false] Flag to activate self contained build
* @param {boolean} [parameters.cssVariables=false] Flag to activate CSS variables generation
* @param {boolean} [parameters.jsdoc=false] Flag to activate JSDoc build
* @param {Array.<string>} [parameters.includedTasks=[]] List of tasks to be included
* @param {Array.<string>} [parameters.excludedTasks=[]] List of tasks to be excluded.
Expand All @@ -235,7 +236,7 @@ module.exports = {
async build({
tree, destPath, cleanDest = false,
buildDependencies = false, includedDependencies = [], excludedDependencies = [],
dev = false, selfContained = false, jsdoc = false,
dev = false, selfContained = false, cssVariables = false, jsdoc = false,
includedTasks = [], excludedTasks = [], devExcludeProject = []
}) {
const startTime = process.hrtime();
Expand All @@ -250,7 +251,12 @@ module.exports = {
virBasePath: "/"
});

const buildContext = new BuildContext({rootProject: tree});
const buildContext = new BuildContext({
rootProject: tree,
options: {
cssVariables: cssVariables
}
});
const cleanupSigHooks = registerCleanupSigHooks(buildContext);

const projects = {}; // Unique project index to prevent building the same project multiple times
Expand Down
22 changes: 22 additions & 0 deletions lib/tasks/TaskUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ class TaskUtil {
return this._projectBuildContext.isRootProject();
}

/**
* Retrieves all build options. If not a single option is stored, an empty object is returned.
*
* @returns {object} The built options
* @public
*/
getBuildOptions() {
return this._projectBuildContext.getOptions();
}

/**
* Retrieves a build option defined by it's <code>key</code.
* If no option with the given <code>key</code> is stored, <code>undefined</code> is returned.
*
* @param {string} key The option key
* @returns {object} The built options
* @public
*/
getBuildOption(key) {
return this._projectBuildContext.getOption(key);
}

/**
* Register a function that must be executed once the build is finished. This can be used to, for example,
* clean up files temporarily created on the file system. If the callback returns a Promise, it will be waited for.
Expand Down
3 changes: 2 additions & 1 deletion lib/types/library/LibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ class LibraryBuilder extends AbstractBuilder {
projectName: project.metadata.name,
librariesPattern: !taskUtil.isRootProject() ? "/resources/**/(*.library|library.js)" : undefined,
themesPattern: !taskUtil.isRootProject() ? "/resources/sap/ui/core/themes/*" : undefined,
inputPattern
inputPattern,
cssVariables: taskUtil.getBuildOption("cssVariables")
}
});
});
Expand Down
3 changes: 2 additions & 1 deletion lib/types/themeLibrary/ThemeLibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class ThemeLibraryBuilder extends AbstractBuilder {
projectName: project.metadata.name,
librariesPattern: !taskUtil.isRootProject() ? "/resources/**/(*.library|library.js)" : undefined,
themesPattern: !taskUtil.isRootProject() ? "/resources/sap/ui/core/themes/*" : undefined,
inputPattern: "/resources/**/themes/*/library.source.less"
inputPattern: "/resources/**/themes/*/library.source.less",
cssVariables: taskUtil.getBuildOption("cssVariables")
}
});
});
Expand Down
36 changes: 36 additions & 0 deletions test/lib/builder/BuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,42 @@ test("getRootProject", (t) => {
t.is(buildContext.getRootProject(), "pony", "Returned correct value");
});

test("getBuildOption", (t) => {
const buildContext = new BuildContext({
rootProject: "root_project",
options: {
a: true,
b: "Pony",
c: 235,
d: {
d1: "Bee"
}
}
});

t.is(buildContext.getOption("a"), true, "Returned 'boolean' value is correct");
t.is(buildContext.getOption("b"), "Pony", "Returned 'String' value is correct");
t.is(buildContext.getOption("c"), 235, "Returned 'Number' value is correct");
t.deepEqual(buildContext.getOption("d"), {d1: "Bee"}, "Returned 'object' value is correct");
});

test("getBuildOptions", (t) => {
const options = {
a: true,
b: "Pony",
c: 235,
d: {
d1: "Bee"
}
};
const buildContext = new BuildContext({
rootProject: "root_project",
options: options
});

t.deepEqual(buildContext.getOptions(), options, "Returned value is correct");
});

test.serial("createProjectContext", (t) => {
class DummyProjectContext {
constructor({buildContext, project, resources}) {
Expand Down
20 changes: 20 additions & 0 deletions test/lib/builder/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ test.serial("Build", async (t) => {
getTag: getTagStub
});
const isRootProjectStub = sinon.stub().returns(true);
const getOptionsStub = sinon.stub().returns({friend: "Pony"});
const getOptionStub = sinon.stub().returns("Pony");
const dummyProjectContext = {
getResourceTagCollection: getResourceTagCollectionStub,
isRootProject: isRootProjectStub,
getOptions: getOptionsStub,
getOption: getOptionStub,
STANDARD_TAGS: {
OmitFromBuildResult: "👻"
}
Expand Down Expand Up @@ -888,6 +892,22 @@ test.serial("Build library.coreBuildtime: replaceBuildtime", (t) => {
});
});

// test.serial("Build library with theme configured for CSS variables", (t) => {
// return builder.build({
// cssVariables: true
// }).then(() => {
// assert.ok(false);
// });
// });

// test.serial("Build theme-library with CSS variables", (t) => {
// return builder.build({
// cssVariables: true
// }).then(() => {
// assert.ok(false);
// });
// });

test.serial("Cleanup", async (t) => {
const BuildContext = require("../../../lib/builder/BuildContext");
const createProjectContextStub = sinon.spy(BuildContext.prototype, "createProjectContext");
Expand Down
33 changes: 33 additions & 0 deletions test/lib/tasks/TaskUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ test("isRootProject", async (t) => {
t.is(res, true, "Correct result");
});

test("getBuildOption", (t) => {
const getOptionStub = sinon.stub().returns("Pony");
const taskUtil = new TaskUtil({
projectBuildContext: {
STANDARD_TAGS: ["some tag"],
getOption: getOptionStub
}
});

const res = taskUtil.getBuildOption("friend");

t.is(getOptionStub.callCount, 1, "ProjectBuildContext#getBuildOption got called once");
t.is(res, "Pony", "Correct result");
});

test("getBuildOptions", (t) => {
const expectedResult = {
friend: "Pony"
};
const getOptionsStub = sinon.stub().returns(expectedResult);
const taskUtil = new TaskUtil({
projectBuildContext: {
STANDARD_TAGS: ["some tag"],
getOptions: getOptionsStub
}
});

const res = taskUtil.getBuildOptions();

t.is(getOptionsStub.callCount, 1, "ProjectBuildContext#getBuildOptions got called once");
t.deepEqual(res, expectedResult, "Correct result");
});

test("registerCleanupTask", async (t) => {
const registerCleanupTaskStub = sinon.stub();
const taskUtil = new TaskUtil({
Expand Down
6 changes: 6 additions & 0 deletions test/lib/types/themeLibrary/ThemeLibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ test("tasks", async (t) => {
taskUtil: {
isRootProject: () => {
return true;
},
getBuildOptions: () => {
return {};
},
getBuildOption: (key) => {
return key;
}
}
});
Expand Down

0 comments on commit ba54f72

Please sign in to comment.