Skip to content

Commit

Permalink
Refactor workspace write to loop only once over the resources
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Apr 13, 2022
1 parent 2a585e6 commit 3a99539
Showing 1 changed file with 58 additions and 52 deletions.
110 changes: 58 additions & 52 deletions lib/tasks/generateThemeDesignerResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ function getPathToRoot(themeFolder) {
return "../".repeat(themeFolder.split("/").length - 2);
}

/**
* Generates an less import statement for the given <code>filePath</code>
*
* @param {string} filePath The path to the desired file
* @returns {string} The less import statement
*/
function lessImport(filePath) {
return `@import "${filePath}";\n`;
}

function generateLibraryDotTheming({namespace, version, hasThemes}) {
const dotTheming = {
sEntity: "Library",
Expand Down Expand Up @@ -51,58 +61,6 @@ function generateLibraryDotTheming({namespace, version, hasThemes}) {
});
}

function lessImport(filePath) {
return `@import "${filePath}";\n`;
}

async function createCssVariablesLessResource({workspace, combo, themeFolder}) {
const pathToRoot = getPathToRoot(themeFolder);
const cssVariablesSourceLessFile = "css_variables.source.less";
const cssVariablesLessFile = "css_variables.less";

// posix as it is a virtual path (separated with /)
const themeName = posixPath.basename(themeFolder);
// The "base" theme of the baseLib is called "baseTheme"
const baseLibThemeName = themeName === "base" ? "baseTheme" : themeName;

// Some themes do not have a base.less file (e.g. sap_hcb)
const hasBaseLess = !!(await combo.byPath(`/resources/sap/ui/core/themes/${themeName}/base.less`));

let cssVariablesLess =
`/* NOTE: This file was generated as an optimized version of "${cssVariablesSourceLessFile}" \
for the Theme Designer. */\n\n`;

if (themeName !== "base") {
const cssVariablesSourceLessResource = await workspace.byPath(
posixPath.join(themeFolder, cssVariablesSourceLessFile)
);

if (!cssVariablesSourceLessResource) {
throw new Error(`Could not find file "${cssVariablesSourceLessFile}" in theme "${themeFolder}"`);
}

const cssVariablesSourceLess = await cssVariablesSourceLessResource.getString();

cssVariablesLess += lessImport(`../base/${cssVariablesLessFile}`);
cssVariablesLess += `
/* START "${cssVariablesSourceLessFile}" */
${cssVariablesSourceLess}
/* END "${cssVariablesSourceLessFile}" */
`;
}

if (hasBaseLess) {
cssVariablesLess += lessImport(`${pathToRoot}../Base/baseLib/${baseLibThemeName}/base.less`);
}
cssVariablesLess += lessImport(`${pathToRoot}sap/ui/core/themes/${themeName}/global.less`);

return new Resource({
path: posixPath.join(themeFolder, cssVariablesLessFile),
string: cssVariablesLess
});
}

async function generateThemeDotTheming({workspace, combo, themeFolder}) {
const themeName = posixPath.basename(themeFolder);
const libraryMatchPattern = /^\/resources\/(.*)\/themes\/[^/]*$/i;
Expand Down Expand Up @@ -153,6 +111,54 @@ async function generateThemeDotTheming({workspace, combo, themeFolder}) {
return newDotThemingResource;
}

async function createCssVariablesLessResource({workspace, combo, themeFolder}) {
const pathToRoot = getPathToRoot(themeFolder);
const cssVariablesSourceLessFile = "css_variables.source.less";
const cssVariablesLessFile = "css_variables.less";

// posix as it is a virtual path (separated with /)
const themeName = posixPath.basename(themeFolder);
// The "base" theme of the baseLib is called "baseTheme"
const baseLibThemeName = themeName === "base" ? "baseTheme" : themeName;

// Some themes do not have a base.less file (e.g. sap_hcb)
const hasBaseLess = !!(await combo.byPath(`/resources/sap/ui/core/themes/${themeName}/base.less`));

let cssVariablesLess =
`/* NOTE: This file was generated as an optimized version of "${cssVariablesSourceLessFile}" \
for the Theme Designer. */\n\n`;

if (themeName !== "base") {
const cssVariablesSourceLessResource = await workspace.byPath(
posixPath.join(themeFolder, cssVariablesSourceLessFile)
);

if (!cssVariablesSourceLessResource) {
throw new Error(`Could not find file "${cssVariablesSourceLessFile}" in theme "${themeFolder}"`);
}

const cssVariablesSourceLess = await cssVariablesSourceLessResource.getString();

cssVariablesLess += lessImport(`../base/${cssVariablesLessFile}`);
cssVariablesLess += `
/* START "${cssVariablesSourceLessFile}" */
${cssVariablesSourceLess}
/* END "${cssVariablesSourceLessFile}" */
`;
}

if (hasBaseLess) {
cssVariablesLess += lessImport(`${pathToRoot}../Base/baseLib/${baseLibThemeName}/base.less`);
}
cssVariablesLess += lessImport(`${pathToRoot}sap/ui/core/themes/${themeName}/global.less`);

return new Resource({
path: posixPath.join(themeFolder, cssVariablesLessFile),
string: cssVariablesLess
});
}

async function generateCssVariablesLess({workspace, combo, namespace}) {
let cssVariablesSourceLessResourcePattern;
if (namespace) {
Expand Down

0 comments on commit 3a99539

Please sign in to comment.