Skip to content

Commit dda3011

Browse files
authored
[FIX] generateThemeDesignerResources: Allow core .theming in sources (#1062)
This change respects an sap.ui.core library .theming file from the sources instead of simply overwriting the file. It allows the configuration to be put into the library and adjusted there instead of having to adjust it in the build task. The existing logic is kept to support older UI5 versions where the file is not available in the sources. JIRA: CPOUI5FOUNDATION-862
1 parent f52f895 commit dda3011

File tree

6 files changed

+488
-8
lines changed

6 files changed

+488
-8
lines changed

lib/tasks/generateThemeDesignerResources.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import posixPath from "node:path/posix";
22
import {getLogger} from "@ui5/logger";
33
const log = getLogger("builder:tasks:generateThemeDesignerResources");
44
import libraryLessGenerator from "../processors/libraryLessGenerator.js";
5+
import {updateLibraryDotTheming} from "./utils/dotTheming.js";
56
import ReaderCollectionPrioritized from "@ui5/fs/ReaderCollectionPrioritized";
67
import Resource from "@ui5/fs/Resource";
78
import fsInterface from "@ui5/fs/fsInterface";
@@ -44,6 +45,10 @@ function generateLibraryDotTheming({namespace, version, hasThemes}) {
4445
sVersion: version
4546
};
4647

48+
// Note that with sap.ui.core version 1.127.0 the .theming file has been put into
49+
// the library sources so that "aFiles" can be maintained from there.
50+
// The below configuration is still needed for older versions of sap.ui.core which do not
51+
// contain the file.
4752
if (namespace === "sap/ui/core") {
4853
dotTheming.aFiles = [
4954
"library",
@@ -241,12 +246,33 @@ export default async function({workspace, dependencies, options}) {
241246
// Only for type "library". Type "theme-library" does not provide a namespace
242247
// Also needs to be created in case a library does not have any themes (see bIgnore flag)
243248
if (namespace) {
244-
log.verbose(`Generating .theming for namespace ${namespace}`);
245-
const libraryDotThemingResource = generateLibraryDotTheming({
246-
namespace,
247-
version,
248-
hasThemes
249-
});
249+
let libraryDotThemingResource;
250+
251+
// Do not generate a .theming file for the sap.ui.core library
252+
if (namespace === "sap/ui/core") {
253+
// Check if the .theming file already exists
254+
libraryDotThemingResource = await workspace.byPath(`/resources/${namespace}/.theming`);
255+
if (libraryDotThemingResource) {
256+
// Update the existing .theming resource
257+
log.verbose(`Updating .theming for namespace ${namespace}`);
258+
await updateLibraryDotTheming({
259+
resource: libraryDotThemingResource,
260+
namespace,
261+
version,
262+
hasThemes
263+
});
264+
}
265+
}
266+
267+
if (!libraryDotThemingResource) {
268+
log.verbose(`Generating .theming for namespace ${namespace}`);
269+
libraryDotThemingResource = generateLibraryDotTheming({
270+
namespace,
271+
version,
272+
hasThemes
273+
});
274+
}
275+
250276
await workspace.write(libraryDotThemingResource);
251277
}
252278

lib/tasks/utils/dotTheming.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export async function updateLibraryDotTheming({resource, namespace, version, hasThemes}) {
2+
const dotTheming = JSON.parse(await resource.getString());
3+
4+
if (!dotTheming.sEntity) {
5+
throw new Error(`Missing 'sEntity' property in ${resource.getPath()}`);
6+
}
7+
8+
if (dotTheming.sEntity !== "Library") {
9+
throw new Error(
10+
`Incorrect 'sEntity' value '${dotTheming.sEntity}' in ${resource.getPath()}: ` +
11+
`Expected 'Library'`
12+
);
13+
}
14+
15+
if (!dotTheming.sId) {
16+
throw new Error(`Missing 'sId' property in ${resource.getPath()}`);
17+
}
18+
19+
if (dotTheming.sId !== namespace) {
20+
throw new Error(`Incorrect 'sId' value '${dotTheming.sId}' in ${resource.getPath()}: Expected '${namespace}'`);
21+
}
22+
23+
dotTheming.sVersion = version;
24+
25+
if (!hasThemes) {
26+
// Set ignore flag when there are no themes at all
27+
// This is important in case a library used to contain themes that have been removed
28+
// in a later version of the library.
29+
dotTheming.bIgnore = true;
30+
}
31+
32+
resource.setString(JSON.stringify(dotTheming, null, 2));
33+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"./processors/jsdoc/lib/*": null,
2323
"./tasks/*": "./lib/tasks/*.js",
2424
"./tasks/taskRepository": null,
25+
"./tasks/utils/*": null,
2526
"./tasks/bundlers/utils/*": null,
2627
"./package.json": "./package.json",
2728
"./internal/taskRepository": "./lib/tasks/taskRepository.js",

test/lib/package-exports.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ test("export of package.json", (t) => {
99
t.truthy(require("@ui5/builder/package.json").version);
1010
});
1111

12-
// Check number of definied exports
12+
// Check number of defined exports
1313
test("check number of exports", (t) => {
1414
const packageJson = require("@ui5/builder/package.json");
15-
t.is(Object.keys(packageJson.exports).length, 8);
15+
t.is(Object.keys(packageJson.exports).length, 9);
1616
});
1717

1818
// Public API contract (exported modules)
@@ -79,6 +79,12 @@ test("no export of processors/jsdoc/lib/ui5/plugin", async (t) => {
7979
});
8080
});
8181

82+
test("no export of tasks/utils/dotTheming", async (t) => {
83+
await t.throwsAsync(import("@ui5/builder/tasks/utils/dotTheming"), {
84+
code: "ERR_PACKAGE_PATH_NOT_EXPORTED"
85+
});
86+
});
87+
8288
test("no export of tasks/bundlers/utils/createModuleNameMapping", async (t) => {
8389
await t.throwsAsync(import("@ui5/builder/tasks/bundlers/utils/createModuleNameMapping"), {
8490
code: "ERR_PACKAGE_PATH_NOT_EXPORTED"

test/lib/tasks/generateThemeDesignerResources.js

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,234 @@ test.serial("generateThemeDesignerResources: Library sap.ui.core", async (t) =>
254254
"workspace.write should be called with libraryLessResource");
255255
});
256256

257+
test.serial("generateThemeDesignerResources: Library sap.ui.core with existing library .theming", async (t) => {
258+
const {sinon, generateThemeDesignerResources, libraryLessGeneratorStub, fsInterfaceStub, ResourceStub} = t.context;
259+
260+
const librarySourceLessResource = {
261+
getPath: sinon.stub().returns("/resources/sap/ui/core/themes/base/library.source.less")
262+
};
263+
264+
const coreLibraryDotThemingResource = {
265+
getString: async () => JSON.stringify({
266+
sEntity: "Library",
267+
sId: "sap/ui/core",
268+
aFiles: [
269+
"existing", "entries"
270+
]
271+
}, null, 2),
272+
setString: sinon.stub()
273+
};
274+
275+
const workspace = {
276+
byGlob: sinon.stub().callsFake(async (globPattern) => {
277+
if (globPattern === "/resources/sap/ui/core/themes/*/library.source.less") {
278+
return [librarySourceLessResource];
279+
} else {
280+
return [];
281+
}
282+
}),
283+
byPath: sinon.stub().callsFake(async (virPath) => {
284+
if (virPath === "/resources/sap/ui/core/themes/base/.theming") {
285+
return {};
286+
} else if (virPath === "/resources/sap/ui/core/.theming") {
287+
return coreLibraryDotThemingResource;
288+
} else {
289+
return null;
290+
}
291+
}),
292+
write: sinon.stub()
293+
};
294+
const dependencies = {};
295+
296+
const libraryLessResource = {};
297+
298+
libraryLessGeneratorStub.resolves([libraryLessResource]);
299+
300+
await generateThemeDesignerResources({
301+
workspace,
302+
dependencies,
303+
options: {
304+
projectName: "sap.ui.core",
305+
version: "1.2.3",
306+
namespace: "sap/ui/core"
307+
}
308+
});
309+
310+
t.is(t.context.ReaderCollectionPrioritizedStub.callCount, 1, "ReaderCollectionPrioritized should be created once");
311+
t.deepEqual(t.context.ReaderCollectionPrioritizedStub.getCall(0).args, [{
312+
name: `generateThemeDesignerResources - prioritize workspace over dependencies: sap.ui.core`,
313+
readers: [workspace, dependencies]
314+
}]);
315+
const combo = t.context.ReaderCollectionPrioritizedStub.getCall(0).returnValue;
316+
317+
t.is(fsInterfaceStub.callCount, 1, "fsInterface should be created once");
318+
t.deepEqual(fsInterfaceStub.getCall(0).args, [combo], "fsInterface should be created for 'combo'");
319+
const fs = fsInterfaceStub.getCall(0).returnValue;
320+
321+
t.is(libraryLessGeneratorStub.callCount, 1);
322+
323+
t.deepEqual(libraryLessGeneratorStub.getCall(0).args[0], {
324+
resources: [librarySourceLessResource],
325+
fs,
326+
}, "libraryLessGenerator processor should be called with expected arguments");
327+
328+
t.is(ResourceStub.callCount, 0, "No new resource should be created");
329+
330+
t.is(coreLibraryDotThemingResource.setString.callCount, 1);
331+
t.deepEqual(coreLibraryDotThemingResource.setString.getCall(0).args, [
332+
JSON.stringify({
333+
sEntity: "Library",
334+
sId: "sap/ui/core",
335+
aFiles: [
336+
"existing", "entries"
337+
],
338+
sVersion: "1.2.3",
339+
}, null, 2)
340+
]);
341+
342+
t.is(workspace.write.callCount, 2);
343+
t.is(workspace.write.getCall(0).args.length, 1,
344+
"workspace.write for coreLibraryDotThemingResource should be called with 1 argument");
345+
t.is(workspace.write.getCall(0).args[0], coreLibraryDotThemingResource,
346+
"workspace.write should be called with libraryDotTheming");
347+
t.is(workspace.write.getCall(1).args.length, 1,
348+
"workspace.write for libraryLessResource should be called with 1 argument");
349+
t.is(workspace.write.getCall(1).args[0], libraryLessResource,
350+
"workspace.write should be called with libraryLessResource");
351+
});
352+
353+
test.serial("generateThemeDesignerResources: Library sap.ui.core without themes, " +
354+
"with existing library .theming with version", async (t) => {
355+
// NOTE: This tests the case when sap.ui.core has no themes, which is not a likely scenario.
356+
// But as the underlying functionality might be used in other scenarios in future, it is tested here.
357+
358+
const {sinon, generateThemeDesignerResources, libraryLessGeneratorStub, fsInterfaceStub, ResourceStub} = t.context;
359+
360+
const coreLibraryDotThemingResource = {
361+
getString: async () => JSON.stringify({
362+
sEntity: "Library",
363+
sId: "sap/ui/core",
364+
sVersion: "0.0.0", // existing version should be ignored
365+
aFiles: [
366+
"existing", "entries"
367+
]
368+
}, null, 2),
369+
setString: sinon.stub()
370+
};
371+
372+
const workspace = {
373+
byGlob: sinon.stub().callsFake(async (globPattern) => {
374+
return [];
375+
}),
376+
byPath: sinon.stub().callsFake(async (virPath) => {
377+
if (virPath === "/resources/sap/ui/core/.theming") {
378+
return coreLibraryDotThemingResource;
379+
} else {
380+
return null;
381+
}
382+
}),
383+
write: sinon.stub()
384+
};
385+
const dependencies = {};
386+
387+
const libraryLessResource = {};
388+
389+
libraryLessGeneratorStub.resolves([libraryLessResource]);
390+
391+
await generateThemeDesignerResources({
392+
workspace,
393+
dependencies,
394+
options: {
395+
projectName: "sap.ui.core",
396+
version: "1.2.3",
397+
namespace: "sap/ui/core"
398+
}
399+
});
400+
401+
t.is(t.context.ReaderCollectionPrioritizedStub.callCount, 0, "ReaderCollectionPrioritized should not be created");
402+
403+
t.is(fsInterfaceStub.callCount, 0, "fsInterface should not be created");
404+
405+
t.is(libraryLessGeneratorStub.callCount, 0);
406+
407+
t.is(ResourceStub.callCount, 0, "No new resource should be created");
408+
409+
t.is(coreLibraryDotThemingResource.setString.callCount, 1);
410+
t.deepEqual(coreLibraryDotThemingResource.setString.getCall(0).args, [
411+
JSON.stringify({
412+
sEntity: "Library",
413+
sId: "sap/ui/core",
414+
sVersion: "1.2.3",
415+
aFiles: [
416+
"existing", "entries"
417+
],
418+
bIgnore: true
419+
}, null, 2)
420+
]);
421+
422+
t.is(workspace.write.callCount, 1);
423+
t.is(workspace.write.getCall(0).args.length, 1,
424+
"workspace.write for coreLibraryDotThemingResource should be called with 1 argument");
425+
t.is(workspace.write.getCall(0).args[0], coreLibraryDotThemingResource,
426+
"workspace.write should be called with libraryDotTheming");
427+
});
428+
429+
test.serial("generateThemeDesignerResources: Library sap.ui.core with existing invalid library .theming", async (t) => {
430+
const {sinon, generateThemeDesignerResources, libraryLessGeneratorStub, fsInterfaceStub, ResourceStub} = t.context;
431+
432+
const coreLibraryDotThemingResource = {
433+
getPath: () => "/resources/sap/ui/core/.theming",
434+
getString: async () => JSON.stringify({
435+
sEntity: "Library",
436+
sId: "sap/m"
437+
}, null, 2),
438+
setString: sinon.stub()
439+
};
440+
441+
const workspace = {
442+
byGlob: sinon.stub().callsFake(async (globPattern) => {
443+
return [];
444+
}),
445+
byPath: sinon.stub().callsFake(async (virPath) => {
446+
if (virPath === "/resources/sap/ui/core/.theming") {
447+
return coreLibraryDotThemingResource;
448+
} else {
449+
return null;
450+
}
451+
}),
452+
write: sinon.stub()
453+
};
454+
const dependencies = {};
455+
456+
const libraryLessResource = {};
457+
458+
libraryLessGeneratorStub.resolves([libraryLessResource]);
459+
460+
await t.throwsAsync(generateThemeDesignerResources({
461+
workspace,
462+
dependencies,
463+
options: {
464+
projectName: "sap.ui.core",
465+
version: "1.2.3",
466+
namespace: "sap/ui/core"
467+
}
468+
}), {
469+
message: "Incorrect 'sId' value 'sap/m' in /resources/sap/ui/core/.theming: Expected 'sap/ui/core'"
470+
});
471+
472+
t.is(t.context.ReaderCollectionPrioritizedStub.callCount, 0, "ReaderCollectionPrioritized should not be created");
473+
474+
t.is(fsInterfaceStub.callCount, 0, "fsInterface should not be created");
475+
476+
t.is(libraryLessGeneratorStub.callCount, 0);
477+
478+
t.is(ResourceStub.callCount, 0, "No new resource should be created");
479+
480+
t.is(coreLibraryDotThemingResource.setString.callCount, 0);
481+
482+
t.is(workspace.write.callCount, 0);
483+
});
484+
257485
test.serial("generateThemeDesignerResources: Library sap.ui.documentation is skipped", async (t) => {
258486
const {sinon, generateThemeDesignerResources, libraryLessGeneratorStub, fsInterfaceStub, ResourceStub} = t.context;
259487

0 commit comments

Comments
 (0)