Skip to content

Commit

Permalink
[INTERNAL] generateFlexChangesBundle: Follow-up adjustments (#319)
Browse files Browse the repository at this point in the history
- Ensure existence of sap.ui5 and sap.ui5/dependencies sections
- Format modified manifest.json with tabs
- Modify manifest json once after writing processed resources
- Refactoring
  • Loading branch information
matz3 authored Aug 28, 2019
1 parent a8edff4 commit 7bb71a2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 30 deletions.
49 changes: 21 additions & 28 deletions lib/tasks/bundlers/generateFlexChangesBundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const log = require("@ui5/logger").getLogger("builder:tasks:bundlers:generateFlexChangesBundle");
const flexChangesBundler = require("../../processors/bundlers/flexChangesBundler");
const resourceFactory = require("@ui5/fs").resourceFactory;

/**
* Task to create changesBundle.json file containing all changes stored in the /changes folder for easier consumption
Expand All @@ -26,40 +25,30 @@ module.exports = function({workspace, options}) {

function updateJson(data) {
// ensure the existence of the libs section in the dependencies
data["sap.ui5"].dependencies.libs = data["sap.ui5"].dependencies.libs || {};
const mLibs = data["sap.ui5"].dependencies.libs;
data["sap.ui5"] = data["sap.ui5"] || {};
data["sap.ui5"].dependencies = data["sap.ui5"].dependencies || {};
const mLibs = data["sap.ui5"].dependencies.libs = data["sap.ui5"].dependencies.libs || {};

if ("sap.ui.fl" in mLibs) {
if (mLibs["sap.ui.fl"]) {
log.verbose("sap.ui.fl found in manifest.json");
if ("lazy" in mLibs["sap.ui.fl"]) {
if (mLibs["sap.ui.fl"].lazy) {
log.verbose("sap.ui.fl 'lazy' attribute found in manifest.json, setting it to false...");
if (mLibs["sap.ui.fl"].lazy === true) {
mLibs["sap.ui.fl"].lazy = false;
}
mLibs["sap.ui.fl"].lazy = false;
}
} else {
log.verbose("sap.ui.fl not found in manifest.json, inserting it...");
mLibs["sap.ui.fl"] = {};
}

return data;
}

function updateFLdependency() {
return workspace.byPath(`${pathPrefix}/manifest.json`)
.then((manifestData) => {
return manifestData.getBuffer().then((buffer) => {
return JSON.parse(buffer.toString());
});
})
.then((manifestContent) => {
let updatedContent = updateJson(manifestContent);
updatedContent = JSON.stringify(updatedContent);
return workspace.write(resourceFactory.createResource({
path: `${pathPrefix}/manifest.json`,
string: updatedContent
}));
});
async function updateFLdependency() {
const manifestResource = await workspace.byPath(`${pathPrefix}/manifest.json`);
const manifestContent = JSON.parse(await manifestResource.getString());

updateJson(manifestContent);
manifestResource.setString(JSON.stringify(manifestContent, null, "\t"));

await workspace.write(manifestResource);
}

log.verbose("Collecting flexibility changes");
Expand All @@ -74,10 +63,14 @@ module.exports = function({workspace, options}) {
});
})
.then((processedResources) => {
return Promise.all(processedResources.map(async (resource) => {
return Promise.all(processedResources.map((resource) => {
log.verbose("Writing flexibility changes bundle");
await updateFLdependency();
return workspace.write(resource);
}));
})).then(async () => {
// Add the sap.ui.fl dependency if a bundle has been created
if (processedResources.length > 0) {
await updateFLdependency();
}
});
});
};
26 changes: 25 additions & 1 deletion test/expected/build/application.i/dest/manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
{"_version":"1.1.0","sap.app":{"_version":"1.1.0","id":"application.i","type":"application","applicationVersion":{"version":"1.2.2"},"embeds":["embedded"],"title":"{{title}}"},"sap.ui5":{"dependencies":{"libs":{"sap.ui.layout":{},"sap.ui.core":{},"sap.m":{},"sap.ui.fl":{}}}}}
{
"_version": "1.1.0",
"sap.app": {
"_version": "1.1.0",
"id": "application.i",
"type": "application",
"applicationVersion": {
"version": "1.2.2"
},
"embeds": [
"embedded"
],
"title": "{{title}}"
},
"sap.ui5": {
"dependencies": {
"libs": {
"sap.ui.layout": {},
"sap.ui.core": {},
"sap.m": {},
"sap.ui.fl": {}
}
}
}
}
28 changes: 27 additions & 1 deletion test/expected/build/application.j/dest/manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
{"_version":"1.1.0","sap.app":{"_version":"1.1.0","id":"application.j","type":"application","applicationVersion":{"version":"1.2.2"},"embeds":["embedded"],"title":"{{title}}"},"sap.ui5":{"dependencies":{"libs":{"sap.ui.layout":{},"sap.ui.core":{},"sap.m":{},"sap.ui.fl":{"lazy":false}}}}}
{
"_version": "1.1.0",
"sap.app": {
"_version": "1.1.0",
"id": "application.j",
"type": "application",
"applicationVersion": {
"version": "1.2.2"
},
"embeds": [
"embedded"
],
"title": "{{title}}"
},
"sap.ui5": {
"dependencies": {
"libs": {
"sap.ui.layout": {},
"sap.ui.core": {},
"sap.m": {},
"sap.ui.fl": {
"lazy": false
}
}
}
}
}

0 comments on commit 7bb71a2

Please sign in to comment.