-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathversionInfoGenerator.js
More file actions
470 lines (419 loc) · 15.3 KB
/
versionInfoGenerator.js
File metadata and controls
470 lines (419 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import {getLogger} from "@ui5/logger";
const log = getLogger("builder:processors:versionInfoGenerator");
import {createResource} from "@ui5/fs/resourceFactory";
import posixPath from "node:path/posix";
/**
* @public
* @module @ui5/builder/processors/versionInfoGenerator
*/
function pad(v) {
return String(v).padStart(2, "0");
}
function getTimestamp() {
const date = new Date();
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
// yyyyMMddHHmm
return year + month + day + hours + minutes;
}
/**
* Manifest libraries as defined in the manifest.json file
*
* @typedef {Object<string, {lazy: boolean}>} ManifestLibraries
*
* sample:
* @example
* {
* "sap.chart": {
* "lazy": true
* },
* "sap.f": { }
* }
*/
/**
* Extracted information from a manifest's <code>sap.app</code> and <code>sap.ui5</code> sections.
*
* @typedef {object} ManifestInfo
*
* @property {string} id The library name, e.g. "lib.x"
* @property {string} embeddedBy the library this component is embedded in, e.g. "lib.x"
* @property {string[]} embeds the embedded component names, e.g. ["lib.x.sub"]
* @property {module:@ui5/builder/processors/versionInfoGenerator~ManifestLibraries} libs the dependencies, e.g.
* {"sap.chart":{"lazy": true}, "sap.f":{}}
*/
/**
* Processes manifest resource and extracts information.
*
* @param {@ui5/fs/Resource} manifestResource
* @returns {Promise<module:@ui5/builder/processors/versionInfoGenerator~ManifestInfo>}
*/
const processManifest = async (manifestResource) => {
const manifestContent = await manifestResource.getString();
const manifestObject = JSON.parse(manifestContent);
const manifestInfo = Object.create(null);
// sap.ui5/dependencies is used for the "manifestHints/libs"
if (manifestObject["sap.ui5"]) {
const manifestDependencies = manifestObject["sap.ui5"]["dependencies"];
if (manifestDependencies && manifestDependencies.libs) {
const libs = Object.create(null);
for (const [libKey, libValue] of Object.entries(manifestDependencies.libs)) {
libs[libKey] = Object.create(null);
if (libValue.lazy) {
libs[libKey].lazy = true;
}
}
manifestInfo.libs = libs;
}
}
// sap.app/embeds, sap.app/embeddedBy and sap.app/id is used for "components"
if (manifestObject["sap.app"]) {
const manifestEmbeds = manifestObject["sap.app"]["embeds"];
manifestInfo.embeds = manifestEmbeds;
const manifestEmbeddedBy = manifestObject["sap.app"]["embeddedBy"];
manifestInfo.embeddedBy = manifestEmbeddedBy;
const id = manifestObject["sap.app"]["id"];
manifestInfo.id = id;
}
return manifestInfo;
};
/**
* Checks if a component (componentPath) is bundled with the library (embeddedBy)
*
* @param {string} embeddedBy e.g. "../"
* @param {string} componentPath e.g. "lib/x/sub"
* @param {string} libraryPathPrefix e.g. "lib/x"
* @returns {boolean} whether or not this component is bundled with the library
*/
const isBundledWithLibrary = (embeddedBy, componentPath, libraryPathPrefix) => {
if (typeof embeddedBy === "undefined") {
log.verbose(" Component doesn't declare 'sap.app/embeddedBy', don't list it as 'embedded'");
return false;
}
if (typeof embeddedBy !== "string") {
log.error(
` Component '${componentPath}': property 'sap.app/embeddedBy' is of type '${typeof embeddedBy}' ` +
`(expected 'string'), it won't be listed as 'embedded'`);
return false;
}
if ( !embeddedBy.length ) {
log.error(
` Component '${componentPath}': property 'sap.app/embeddedBy' has an empty string value ` +
`(which is invalid), it won't be listed as 'embedded'`
);
return false;
}
let resolvedEmbeddedBy = posixPath.resolve(componentPath, embeddedBy);
if ( resolvedEmbeddedBy && !resolvedEmbeddedBy.endsWith("/") ) {
resolvedEmbeddedBy = resolvedEmbeddedBy + "/";
}
if ( libraryPathPrefix === resolvedEmbeddedBy ) {
log.verbose(" Component's 'sap.app/embeddedBy' property points to library, list it as 'embedded'");
return true;
} else {
log.verbose(
` Component's 'sap.app/embeddedBy' points to '${resolvedEmbeddedBy}', don't list it as 'embedded'`);
return false;
}
};
/**
* Retrieves the manifest path of a subcomponent
*
* @param {string} filePath path to the manifest, e.g. "lib/x/manifest.json"
* @param {string} subPath relative sub path, e.g. "sub"
* @returns {string} manifest path, e.g. "lib/x/sub/manifest.json"
*/
const getManifestPath = (filePath, subPath) => {
return posixPath.resolve(posixPath.dirname(filePath), subPath, "manifest.json");
};
/**
* Represents dependency information for a library.
* Dependencies can be retrieved using <code>#getResolvedLibraries</code>
* and with that are resolved recursively
*/
class DependencyInfo {
/**
*
* @param {module:@ui5/builder/processors/versionInfoGenerator~ManifestLibraries} libs
* @param {string} name library name, e.g. "lib.x"
*/
constructor(libs, name) {
this.libs = libs;
this.name = name;
}
/**
* Add library to libsResolved and if already present
* merge lazy property
*
* @param {string} libName library name, e.g. "lib.x"
* @param {boolean} lazy
* @returns {{lazy: boolean}} the added library
*/
addResolvedLibDependency(libName, lazy) {
let alreadyResolved = this._libsResolved[libName];
if (!alreadyResolved) {
alreadyResolved = Object.create(null);
if (lazy) {
alreadyResolved.lazy = true;
}
this._libsResolved[libName] = alreadyResolved;
} else {
// siblings if sibling is eager only if one other sibling eager
alreadyResolved.lazy = alreadyResolved.lazy && lazy;
}
return alreadyResolved;
}
/**
* Resolves dependencies recursively and retrieves them with
* - resolved siblings a lazy and a eager dependency becomes eager
* - resolved children become lazy if their parent is lazy
*
* @param {Map<string,DependencyInfo>} dependencyInfoMap
* @returns {module:@ui5/builder/processors/versionInfoGenerator~ManifestLibraries} resolved libraries
*/
getResolvedLibraries(dependencyInfoMap) {
if (!this._libsResolved) {
// early set if there is a potential cycle
this._libsResolved = Object.create(null);
if (!this.libs) {
return this._libsResolved;
}
for (const [libName, libValue] of Object.entries(this.libs)) {
const lazy = libValue.lazy;
const dependencyInfoObjectAdded = this.addResolvedLibDependency(libName, lazy);
const dependencyInfo = dependencyInfoMap.get(libName);
if (dependencyInfo) {
const childLibsResolved = dependencyInfo.getResolvedLibraries(dependencyInfoMap);
// children if parent is lazy children become lazy
for (const [resolvedLibName, resolvedLib] of Object.entries(childLibsResolved)) {
this.addResolvedLibDependency(resolvedLibName,
resolvedLib.lazy || dependencyInfoObjectAdded.lazy);
}
} else {
log.info(`Cannot find dependency '${libName}' `+
`defined in the manifest.json or .library file of project '${this.name}'. ` +
"This might prevent some UI5 runtime performance optimizations from taking effect. " +
"Please double check your project's dependency configuration.");
}
}
}
return this._libsResolved;
}
}
/**
* Sorts the keys of a given object
*
* @param {object} obj the object
* @returns {object} the object with sorted keys
*/
const sortObjectKeys = (obj) => {
const sortedObject = Object.create(null);
const keys = Object.keys(obj);
keys.sort();
keys.forEach((key) => {
sortedObject[key] = obj[key];
});
return sortedObject;
};
/**
* Builds the manifestHints object from the dependencyInfo
*
* @param {DependencyInfo} dependencyInfo
* @param {Map<string, DependencyInfo>} dependencyInfoMap
* @returns {{dependencies: {libs: ManifestLibraries}}} manifestHints
*/
const getManifestHints = (dependencyInfo, dependencyInfoMap) => {
if (dependencyInfo) {
const libsResolved = dependencyInfo.getResolvedLibraries(dependencyInfoMap);
if (libsResolved && Object.keys(libsResolved).length) {
return {
dependencies: {
libs: sortObjectKeys(libsResolved)
}
};
}
}
};
/**
* Common type for Library and Component
* embeds and bundled components make only sense for library
*
* @typedef {object} ArtifactInfo
* @property {string} componentName The library name, e.g. "lib.x"
* @property {Set<string>} bundledComponents The embedded components which have an embeddedBy reference to the library
* @property {DependencyInfo} dependencyInfo The dependency info object
* @property {module:@ui5/builder/processors/versionInfoGenerator~ArtifactInfo[]} embeds The embedded artifact infos
*/
/**
* Processes the manifest and creates a ManifestInfo and an ArtifactInfo.
*
* @param {@ui5/fs/Resource} libraryManifest
* @param {string} [name] library name, if not provided using the ManifestInfo's id
* @returns {Promise<{manifestInfo: ManifestInfo, libraryArtifactInfo: ArtifactInfo}>}
*/
async function processManifestAndGetArtifactInfo(libraryManifest, name) {
const manifestInfo = await processManifest(libraryManifest);
name = name || manifestInfo.id;
const libraryArtifactInfo = Object.create(null);
libraryArtifactInfo.componentName = name;
libraryArtifactInfo.dependencyInfo = new DependencyInfo(manifestInfo.libs, name);
return {manifestInfo, libraryArtifactInfo};
}
/**
* Processes the library info and fills the maps <code>dependencyInfoMap</code> and <code>embeddedInfoMap</code>.
*
* @param {module:@ui5/builder/processors/versionInfoGenerator~LibraryInfo} libraryInfo
* @returns {Promise<module:@ui5/builder/processors/versionInfoGenerator~ArtifactInfo|undefined>}
*/
const processLibraryInfo = async (libraryInfo) => {
if (!libraryInfo.libraryManifest) {
log.verbose(
`Cannot add meta information for library '${libraryInfo.name}'. The manifest.json file cannot be found`);
return;
}
const {manifestInfo, libraryArtifactInfo} =
await processManifestAndGetArtifactInfo(libraryInfo.libraryManifest, libraryInfo.name);
const bundledComponents = new Set();
libraryArtifactInfo.bundledComponents = bundledComponents;
const embeds = manifestInfo.embeds||[]; // e.g. ["sub"]
// filter only embedded manifests
const embeddedPaths = embeds.map((embed) => {
return getManifestPath(libraryInfo.libraryManifest.getPath(), embed);
});
// e.g. manifest resource with lib/x/sub/manifest.json
let embeddedManifests = libraryInfo.embeddedManifests || [];
embeddedManifests = embeddedManifests.filter((manifestResource) => {
return embeddedPaths.includes(manifestResource.getPath());
});
// get all embedded manifests
const embeddedManifestPromises = embeddedManifests.map(async (embeddedManifest) => {
const {manifestInfo: embeddedManifestInfo, libraryArtifactInfo: embeddedArtifactInfo} =
await processManifestAndGetArtifactInfo(embeddedManifest);
const componentName = embeddedManifestInfo.id;
const embeddedManifestDirName = posixPath.dirname(embeddedManifest.getPath());
const libraryManifestDirName = posixPath.dirname(libraryInfo.libraryManifest.getPath());
if (isBundledWithLibrary(embeddedManifestInfo.embeddedBy, embeddedManifestDirName,
libraryManifestDirName + "/")) {
bundledComponents.add(componentName);
}
return embeddedArtifactInfo;
});
const embeddedArtifactInfos = await Promise.all(embeddedManifestPromises);
libraryArtifactInfo.embeds = embeddedArtifactInfos;
return libraryArtifactInfo;
};
/**
* Library Info
*
* contains information about the name and the version of the library and its manifest, as well as the nested manifests.
*
* @public
* @typedef {object} LibraryInfo
* @property {string} name The library name, e.g. "lib.x"
* @property {string} version The library version, e.g. "1.0.0"
* @property {@ui5/fs/Resource} libraryManifest library manifest resource,
* e.g. resource with path "lib/x/manifest.json"
* @property {@ui5/fs/Resource[]} embeddedManifests list of embedded manifest resources,
* e.g. resource with path "lib/x/sub/manifest.json"
*/
/**
* Creates sap-ui-version.json.
*
* @public
* @function default
* @static
*
* @param {object} parameters Parameters
* @param {object} parameters.options Options
* @param {string} parameters.options.rootProjectName Name of the root project
* @param {string} parameters.options.rootProjectVersion Version of the root project
* @param {module:@ui5/builder/processors/versionInfoGenerator~LibraryInfo[]} parameters.options.libraryInfos Array of
* objects representing libraries,
* e.g.
* ```
* {
* name: "lib.x",
* version: "1.0.0",
* libraryManifest: @ui5/fs/Resource,
* embeddedManifests: @ui5/fs/Resource[]
* }
* ```
*
* @returns {Promise<@ui5/fs/Resource[]>} Promise resolving with an array containing the versionInfo resource
*/
export default async function({options}) {
if (!options.rootProjectName || options.rootProjectVersion === undefined || options.libraryInfos === undefined) {
throw new Error("[versionInfoGenerator]: Missing options parameters");
}
const buildTimestamp = getTimestamp();
/**
* componentName to dependency info
*
* @type {Map<string, DependencyInfo>}
*/
const dependencyInfoMap = new Map();
// process library infos
const libraryInfosProcessPromises = options.libraryInfos.map((libraryInfo) => {
return processLibraryInfo(libraryInfo);
});
let artifactInfos = await Promise.all(libraryInfosProcessPromises);
artifactInfos = artifactInfos.filter(Boolean);
// fill dependencyInfoMap
artifactInfos.forEach((artifactInfo) => {
dependencyInfoMap.set(artifactInfo.componentName, artifactInfo.dependencyInfo);
});
const libraries = options.libraryInfos.map((libraryInfo) => {
const library = {
name: libraryInfo.name,
version: libraryInfo.version,
buildTimestamp: buildTimestamp,
scmRevision: ""// TODO: insert current library scm revision here
};
const dependencyInfo = dependencyInfoMap.get(libraryInfo.name);
const manifestHints = getManifestHints(dependencyInfo, dependencyInfoMap);
if (manifestHints) {
library.manifestHints = manifestHints;
}
return library;
});
// sort libraries alphabetically
libraries.sort((a, b) => {
return a.name.localeCompare(b.name);
});
// components
let components;
artifactInfos.forEach((artifactInfo) => {
artifactInfo.embeds.forEach((embeddedArtifactInfo) => {
const componentObject = Object.create(null);
const bundledComponents = artifactInfo.bundledComponents;
const componentName = embeddedArtifactInfo.componentName;
if (!bundledComponents.has(componentName)) {
componentObject.hasOwnPreload = true;
}
componentObject.library = artifactInfo.componentName;
const manifestHints = getManifestHints(embeddedArtifactInfo.dependencyInfo, dependencyInfoMap);
if (manifestHints) {
componentObject.manifestHints = manifestHints;
}
components = components || Object.create(null);
components[componentName] = componentObject;
});
});
// sort components alphabetically
components = components && sortObjectKeys(components);
const versionJson = {
name: options.rootProjectName,
version: options.rootProjectVersion, // TODO: insert current application version here
buildTimestamp: buildTimestamp,
scmRevision: "", // TODO: insert current application scm revision here
// gav: "", // TODO: insert current application id + version here
libraries,
components
};
return [createResource({
path: "/resources/sap-ui-version.json",
string: JSON.stringify(versionJson, null, "\t")
})];
}