Skip to content

Commit

Permalink
[FEATURE] flexChangesBundler: Merge existing with new flexibility-bun…
Browse files Browse the repository at this point in the history
…dle.json

Merge any existing level zero changes into the flexibility-bundle.json
of UI5 adaptation projects

#974
  • Loading branch information
alexeng06 authored and RandomByte committed Jan 18, 2024
1 parent b84d3e9 commit fd070ab
Show file tree
Hide file tree
Showing 4 changed files with 1,050 additions and 4 deletions.
30 changes: 27 additions & 3 deletions lib/processors/bundlers/flexChangesBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import {createResource} from "@ui5/fs/resourceFactory";
* @param {string} parameters.options.pathPrefix Prefix for bundle path
* @param {string} parameters.options.hasFlexBundleVersion true if minUI5Version >= 1.73 than
* create flexibility-bundle.json
* @param {object} [parameters.existingFlexBundle={}] Object with existing flexibility-bundle.json
* to merge with new changes
* @returns {Promise<@ui5/fs/Resource[]>} Promise resolving with flex changes bundle resources
*/
export default function({resources, options: {pathPrefix, hasFlexBundleVersion}}) {
export default function({resources, options: {pathPrefix, hasFlexBundleVersion}, existingFlexBundle = {}}) {
let bundleName = "changes-bundle.json";

function sortByTimeStamp(a, b) {
Expand Down Expand Up @@ -86,19 +88,41 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion}}
return JSON.stringify(changes);
} else {
bundleName = "flexibility-bundle.json";
const newChangeFormat = {
let newChangeFormat = {
changes,
compVariants,
variants,
variantChanges,
variantDependentControlChanges,
variantManagementChanges
};

if (Object.keys(existingFlexBundle).length > 0) {
newChangeFormat = mergeFlexChangeBundles(newChangeFormat);
}
return JSON.stringify(newChangeFormat);
}
}

/**
* Merge new and existing bundles
*
* @param {object} newFlexBundle Object with new content of flexibility-bundle.json
* @returns {object} Object with merged content of new and existing flexibility-bundle.json
*/
function mergeFlexChangeBundles(newFlexBundle) {
const result = {};

Object.keys(newFlexBundle).forEach((key) => {
if (existingFlexBundle[key] && Array.isArray(existingFlexBundle[key])) {
result[key] = existingFlexBundle[key].concat(newFlexBundle[key]);
} else {
result[key] = newFlexBundle[key];
}
});

return result;
}

return Promise.all(resources.map((resource) => {
return resource.getBuffer().then((buffer) => {
return JSON.parse(buffer.toString());
Expand Down
8 changes: 7 additions & 1 deletion lib/tasks/bundlers/generateFlexChangesBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ export default async function({workspace, taskUtil, options = {}}) {
if (allResources.length > 0) {
const version = semver.coerce(await readManifestMinUI5Version());
let hasFlexBundleVersion = false;
let flexBundle = {};
if (semver.compare(version, "1.73.0") >= 0) {
hasFlexBundleVersion = true;
const flexBundleResource = await workspace.byPath(`${pathPrefix}/changes/flexibility-bundle.json`);
if (flexBundleResource) {
flexBundle = JSON.parse(await flexBundleResource.getString());
}
}
const processedResources = await flexChangesBundler({
resources: allResources,
options: {
pathPrefix,
hasFlexBundleVersion
}
},
existingFlexBundle: flexBundle
});
await Promise.all(processedResources.map((resource) => {
log.verbose("Writing flexibility changes bundle");
Expand Down
Loading

0 comments on commit fd070ab

Please sign in to comment.