Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] builder: add cssVariables option #728

Merged
merged 6 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/builder/BuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ const GLOBAL_TAGS = Object.freeze({
* @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._resourceTagCollection = new ResourceTagCollection({
allowedTags: Object.values(GLOBAL_TAGS)
});
this.options = options;
}

getRootProject() {
return this.rootProject;
}

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

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

getOption(key) {
flovogt marked this conversation as resolved.
Show resolved Hide resolved
return 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 @@ -224,6 +224,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 @@ -234,7 +235,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 @@ -249,7 +250,12 @@ module.exports = {
virBasePath: "/"
});

const buildContext = new BuildContext({rootProject: tree});
const buildContext = new BuildContext({
rootProject: tree,
options: {
cssVariables: cssVariables
flovogt marked this conversation as resolved.
Show resolved Hide resolved
}
});
const cleanupSigHooks = registerCleanupSigHooks(buildContext);

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

/**
* Retrieves a build option defined by it's <code>key</code.
flovogt marked this conversation as resolved.
Show resolved Hide resolved
* If no option with the given <code>key</code> is stored, <code>undefined</code> is returned.
*
* @param {string} key The option key
* @returns {object|undefined} The build option (or undefined)
flovogt marked this conversation as resolved.
Show resolved Hide resolved
* @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 @@ -198,7 +198,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.someClass {
color: @someColor
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:root{--someColor:#000}
/* Inline theming parameters */
#sap-ui-theme-theme\.j{background-image:url('data:text/plain;utf-8,%7B%22someColor%22%3A%22%23000%22%7D')}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@someColor: #000000;

:root {
--someColor: @someColor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.someClass{color:#000}
/* Inline theming parameters */
#sap-ui-theme-theme\.j{background-image:url('data:text/plain;utf-8,%7B%22someColor%22%3A%22%23000%22%7D')}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"someColor":"#000"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.someClass{color:#000}
/* Inline theming parameters */
#sap-ui-theme-theme\.j{background-image:url('data:text/plain;utf-8,%7B%22someColor%22%3A%22%23000%22%7D')}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@someColor: black;
@import "Button.less";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.someClass{color:var(--someColor)}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.someClass{color:var(--someColor)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<theme xmlns="http://www.sap.com/sap.ui.library.xsd" >

<name>my_theme</name>
<vendor>me</vendor>
<copyright>Some fancy copyright</copyright>
<version>1.0.0</version>

</theme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:root{--mycolor:#00f}
/* Inline theming parameters */
#sap-ui-theme-theme\.library\.e{background-image:url('data:text/plain;utf-8,%7B%22mycolor%22%3A%22%2300f%22%7D')}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@mycolor: #0000ff;

:root {
--mycolor: @mycolor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*!
* Some fancy copyright
*/.sapUiBody{background-color:#00f}
/* Inline theming parameters */
#sap-ui-theme-theme\.library\.e{background-image:url('data:text/plain;utf-8,%7B%22mycolor%22%3A%22%2300f%22%7D')}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"mycolor":"#00f"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*!
* Some fancy copyright
*/.sapUiBody{background-color:#00f}
/* Inline theming parameters */
#sap-ui-theme-theme\.library\.e{background-image:url('data:text/plain;utf-8,%7B%22mycolor%22%3A%22%2300f%22%7D')}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*!
* Some fancy copyright
*/

@mycolor: blue;

.sapUiBody {
background-color: @mycolor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*!
* Some fancy copyright
*/.sapUiBody{background-color:var(--mycolor)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*!
* Some fancy copyright
*/.sapUiBody{background-color:var(--mycolor)}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"sEntity": "Theme",
"sId": "sap_belize",
"oExtends": "base",
"sVendor": "SAP",
"aBundled": ["sap_belize_plus"],
"mCssScopes": {
"library": {
"sBaseFile": "library",
"sEmbeddingMethod": "APPEND",
"aScopes": [
{
"sLabel": "Contrast",
"sSelector": "sapContrast",
"sEmbeddedFile": "sap_belize_plus.library",
"sEmbeddedCompareFile": "library",
"sThemeIdSuffix": "Contrast",
"sThemability": "PUBLIC",
"aThemabilityFilter": [
"Color"
],
"rExcludeSelector": "\\.sapContrastPlus\\W"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*!
* ${copyright}
*/

@mycolor: blue;

.sapUiBody {
background-color: @mycolor;
}
19 changes: 19 additions & 0 deletions test/lib/builder/BuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ 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.serial("createProjectContext", (t) => {
class DummyProjectContext {
constructor({buildContext, project, resources, globalTags}) {
Expand Down
14 changes: 14 additions & 0 deletions test/lib/builder/ProjectBuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ test("isRootProject: false", (t) => {
t.false(projectBuildContext.isRootProject(), "Correctly identified non-root project");
});

test("getBuildOption", (t) => {
const projectBuildContext = new ProjectBuildContext({
buildContext: {
getOption: () => "Pony",
getResourceTagCollection: () => t.context.resourceTagCollection
},
globalTags: {MyTag: "me:MyTag"},
project: "my project",
resources: "resources"
});

t.deepEqual(projectBuildContext.getOption("a"), "Pony", "Returned value is correct");
});

test("registerCleanupTask", (t) => {
const projectBuildContext = new ProjectBuildContext({
buildContext: {
Expand Down
Loading