@ui5/builder 3.0.0-alpha.5 → 3.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +34 -1
  2. package/index.js +0 -39
  3. package/lib/lbt/analyzer/JSModuleAnalyzer.js +13 -2
  4. package/lib/lbt/bundle/Builder.js +19 -49
  5. package/lib/lbt/resources/LocatorResource.js +1 -1
  6. package/lib/lbt/resources/ResourceCollector.js +22 -15
  7. package/lib/lbt/resources/ResourceInfoList.js +0 -1
  8. package/lib/lbt/resources/ResourcePool.js +7 -6
  9. package/lib/lbt/utils/escapePropertiesFile.js +3 -6
  10. package/lib/processors/bundlers/moduleBundler.js +2 -3
  11. package/lib/processors/jsdoc/lib/createIndexFiles.js +1 -1
  12. package/lib/processors/jsdoc/lib/transformApiJson.js +20 -20
  13. package/lib/processors/jsdoc/lib/ui5/plugin.js +111 -6
  14. package/lib/processors/jsdoc/lib/ui5/template/publish.js +112 -91
  15. package/lib/processors/jsdoc/lib/ui5/template/utils/versionUtil.js +1 -1
  16. package/lib/processors/manifestCreator.js +8 -45
  17. package/lib/tasks/TaskUtil.js +82 -17
  18. package/lib/tasks/bundlers/generateComponentPreload.js +14 -14
  19. package/lib/tasks/bundlers/generateFlexChangesBundle.js +6 -2
  20. package/lib/tasks/bundlers/generateLibraryPreload.js +84 -91
  21. package/lib/tasks/bundlers/generateManifestBundle.js +8 -10
  22. package/lib/tasks/bundlers/generateStandaloneAppBundle.js +7 -2
  23. package/lib/tasks/bundlers/utils/createModuleNameMapping.js +3 -2
  24. package/lib/tasks/generateCachebusterInfo.js +7 -3
  25. package/lib/tasks/generateLibraryManifest.js +6 -8
  26. package/lib/tasks/generateResourcesJson.js +1 -1
  27. package/lib/tasks/generateThemeDesignerResources.js +8 -2
  28. package/lib/tasks/generateVersionInfo.js +5 -5
  29. package/lib/tasks/taskRepository.js +1 -13
  30. package/lib/tasks/transformBootstrapHtml.js +6 -1
  31. package/package.json +6 -7
  32. package/lib/builder/BuildContext.js +0 -60
  33. package/lib/builder/ProjectBuildContext.js +0 -61
  34. package/lib/builder/builder.js +0 -425
  35. package/lib/types/AbstractBuilder.js +0 -270
  36. package/lib/types/AbstractFormatter.js +0 -66
  37. package/lib/types/AbstractUi5Formatter.js +0 -95
  38. package/lib/types/application/ApplicationBuilder.js +0 -211
  39. package/lib/types/application/ApplicationFormatter.js +0 -227
  40. package/lib/types/application/applicationType.js +0 -15
  41. package/lib/types/library/LibraryBuilder.js +0 -232
  42. package/lib/types/library/LibraryFormatter.js +0 -519
  43. package/lib/types/library/libraryType.js +0 -15
  44. package/lib/types/module/ModuleBuilder.js +0 -7
  45. package/lib/types/module/ModuleFormatter.js +0 -54
  46. package/lib/types/module/moduleType.js +0 -15
  47. package/lib/types/themeLibrary/ThemeLibraryBuilder.js +0 -64
  48. package/lib/types/themeLibrary/ThemeLibraryFormatter.js +0 -90
  49. package/lib/types/themeLibrary/themeLibraryType.js +0 -15
  50. package/lib/types/typeRepository.js +0 -46
@@ -1,425 +0,0 @@
1
- const {promisify} = require("util");
2
- const rimraf = promisify(require("rimraf"));
3
- const log = require("@ui5/logger").getGroupLogger("builder:builder");
4
- const resourceFactory = require("@ui5/fs").resourceFactory;
5
- const MemAdapter = require("@ui5/fs").adapters.Memory;
6
- const typeRepository = require("../types/typeRepository");
7
- const taskRepository = require("../tasks/taskRepository");
8
- const BuildContext = require("./BuildContext");
9
-
10
-
11
- // Set of tasks for development
12
- const devTasks = [
13
- "replaceCopyright",
14
- "replaceVersion",
15
- "replaceBuildtime",
16
- "buildThemes"
17
- ];
18
-
19
- /**
20
- * Calculates the elapsed build time and returns a prettified output
21
- *
22
- * @private
23
- * @param {Array} startTime Array provided by <code>process.hrtime()</code>
24
- * @returns {string} Difference between now and the provided time array as formatted string
25
- */
26
- function getElapsedTime(startTime) {
27
- const prettyHrtime = require("pretty-hrtime");
28
- const timeDiff = process.hrtime(startTime);
29
- return prettyHrtime(timeDiff);
30
- }
31
-
32
- /**
33
- * Creates the list of tasks to be executed by the build process
34
- *
35
- * Sets specific tasks to be disabled by default, these tasks need to be included explicitly.
36
- * Based on the selected build mode (dev|selfContained|preload), different tasks are enabled.
37
- * Tasks can be enabled or disabled. The wildcard <code>*</code> is also supported and affects all tasks.
38
- *
39
- * @private
40
- * @param {object} parameters
41
- * @param {boolean} parameters.dev Sets development mode, which only runs essential tasks
42
- * @param {boolean} parameters.selfContained
43
- * True if a the build should be self-contained or false for prelead build bundles
44
- * @param {boolean} parameters.jsdoc True if a JSDoc build should be executed
45
- * @param {Array} parameters.includedTasks Task list to be included from build
46
- * @param {Array} parameters.excludedTasks Task list to be excluded from build
47
- * @returns {Array} Return a task list for the builder
48
- */
49
- function composeTaskList({dev, selfContained, jsdoc, includedTasks, excludedTasks}) {
50
- const definedTasks = taskRepository.getAllTaskNames();
51
- let selectedTasks = definedTasks.reduce((list, key) => {
52
- list[key] = true;
53
- return list;
54
- }, {});
55
-
56
- // Exclude non default tasks
57
- selectedTasks.generateManifestBundle = false;
58
- selectedTasks.generateStandaloneAppBundle = false;
59
- selectedTasks.transformBootstrapHtml = false;
60
- selectedTasks.generateJsdoc = false;
61
- selectedTasks.executeJsdocSdkTransformation = false;
62
- selectedTasks.generateCachebusterInfo = false;
63
- selectedTasks.generateApiIndex = false;
64
- selectedTasks.generateThemeDesignerResources = false;
65
-
66
- // Disable generateResourcesJson due to performance.
67
- // When executed it analyzes each module's AST and therefore
68
- // takes up much time (~10% more)
69
- selectedTasks.generateResourcesJson = false;
70
-
71
- if (selfContained) {
72
- // No preloads, bundle only
73
- selectedTasks.generateComponentPreload = false;
74
- selectedTasks.generateStandaloneAppBundle = true;
75
- selectedTasks.transformBootstrapHtml = true;
76
- selectedTasks.generateLibraryPreload = false;
77
- }
78
-
79
- // TODO 3.0: exclude generateVersionInfo if not --all is used
80
-
81
- if (jsdoc) {
82
- // Include JSDoc tasks
83
- selectedTasks.generateJsdoc = true;
84
- selectedTasks.executeJsdocSdkTransformation = true;
85
- selectedTasks.generateApiIndex = true;
86
-
87
- // Include theme build as required for SDK
88
- selectedTasks.buildThemes = true;
89
-
90
- // Exclude all tasks not relevant to JSDoc generation
91
- selectedTasks.replaceCopyright = false;
92
- selectedTasks.replaceVersion = false;
93
- selectedTasks.replaceBuildtime = false;
94
- selectedTasks.generateComponentPreload = false;
95
- selectedTasks.generateLibraryPreload = false;
96
- selectedTasks.generateLibraryManifest = false;
97
- selectedTasks.minify = false;
98
- selectedTasks.generateFlexChangesBundle = false;
99
- selectedTasks.generateManifestBundle = false;
100
- }
101
-
102
- // Only run essential tasks in development mode, it is not desired to run time consuming tasks during development.
103
- if (dev) {
104
- // Overwrite all other tasks with noop promise
105
- Object.keys(selectedTasks).forEach((key) => {
106
- if (devTasks.indexOf(key) === -1) {
107
- selectedTasks[key] = false;
108
- }
109
- });
110
- }
111
-
112
- // Exclude tasks
113
- for (let i = 0; i < excludedTasks.length; i++) {
114
- const taskName = excludedTasks[i];
115
- if (taskName === "*") {
116
- Object.keys(selectedTasks).forEach((sKey) => {
117
- selectedTasks[sKey] = false;
118
- });
119
- break;
120
- }
121
- if (selectedTasks[taskName] === true) {
122
- selectedTasks[taskName] = false;
123
- } else if (typeof selectedTasks[taskName] === "undefined") {
124
- log.warn(`Unable to exclude task '${taskName}': Task is unknown`);
125
- }
126
- }
127
-
128
- // Include tasks
129
- for (let i = 0; i < includedTasks.length; i++) {
130
- const taskName = includedTasks[i];
131
- if (taskName === "*") {
132
- Object.keys(selectedTasks).forEach((sKey) => {
133
- selectedTasks[sKey] = true;
134
- });
135
- break;
136
- }
137
- if (selectedTasks[taskName] === false) {
138
- selectedTasks[taskName] = true;
139
- } else if (typeof selectedTasks[taskName] === "undefined") {
140
- log.warn(`Unable to include task '${taskName}': Task is unknown`);
141
- }
142
- }
143
-
144
- // Filter only for tasks that will be executed
145
- selectedTasks = Object.keys(selectedTasks).filter((task) => selectedTasks[task]);
146
-
147
- return selectedTasks;
148
- }
149
-
150
- async function executeCleanupTasks(buildContext) {
151
- log.info("Executing cleanup tasks...");
152
- await buildContext.executeCleanupTasks();
153
- }
154
-
155
- function registerCleanupSigHooks(buildContext) {
156
- function createListener(exitCode) {
157
- return function() {
158
- // Asynchronously cleanup resources, then exit
159
- executeCleanupTasks(buildContext).then(() => {
160
- process.exit(exitCode);
161
- });
162
- };
163
- }
164
-
165
- const processSignals = {
166
- "SIGHUP": createListener(128 + 1),
167
- "SIGINT": createListener(128 + 2),
168
- "SIGTERM": createListener(128 + 15),
169
- "SIGBREAK": createListener(128 + 21)
170
- };
171
-
172
- for (const signal of Object.keys(processSignals)) {
173
- process.on(signal, processSignals[signal]);
174
- }
175
-
176
- // == TO BE DISCUSSED: Also cleanup for unhandled rejections and exceptions?
177
- // Add additional events like signals since they are registered on the process
178
- // event emitter in a similar fashion
179
- // processSignals["unhandledRejection"] = createListener(1);
180
- // process.once("unhandledRejection", processSignals["unhandledRejection"]);
181
- // processSignals["uncaughtException"] = function(err, origin) {
182
- // const fs = require("fs");
183
- // fs.writeSync(
184
- // process.stderr.fd,
185
- // `Caught exception: ${err}\n` +
186
- // `Exception origin: ${origin}`
187
- // );
188
- // createListener(1)();
189
- // };
190
- // process.once("uncaughtException", processSignals["uncaughtException"]);
191
-
192
- return processSignals;
193
- }
194
-
195
- function deregisterCleanupSigHooks(signals) {
196
- for (const signal of Object.keys(signals)) {
197
- process.removeListener(signal, signals[signal]);
198
- }
199
- }
200
-
201
- /**
202
- * Builder
203
- *
204
- * @public
205
- * @namespace
206
- * @alias module:@ui5/builder.builder
207
- */
208
- module.exports = {
209
- /**
210
- * Configures the project build and starts it.
211
- *
212
- * @public
213
- * @param {object} parameters Parameters
214
- * @param {object} parameters.tree Project tree as generated by the
215
- * [@ui5/project.normalizer]{@link module:@ui5/project.normalizer}
216
- * @param {string} parameters.destPath Target path
217
- * @param {boolean} [parameters.cleanDest=false] Decides whether project should clean the target path before build
218
- * @param {boolean} [parameters.buildDependencies=false] Decides whether project dependencies are built as well
219
- * @param {Array.<string|RegExp>} [parameters.includedDependencies=[]]
220
- * List of build dependencies to be included if buildDependencies is true
221
- * @param {Array.<string|RegExp>} [parameters.excludedDependencies=[]]
222
- * List of build dependencies to be excluded if buildDependencies is true.
223
- * If the wildcard '*' is provided, only the included dependencies will be built.
224
- * @param {boolean} [parameters.dev=false]
225
- * Decides whether a development build should be activated (skips non-essential and time-intensive tasks)
226
- * @param {boolean} [parameters.selfContained=false] Flag to activate self contained build
227
- * @param {boolean} [parameters.cssVariables=false] Flag to activate CSS variables generation
228
- * @param {boolean} [parameters.jsdoc=false] Flag to activate JSDoc build
229
- * @param {Array.<string>} [parameters.includedTasks=[]] List of tasks to be included
230
- * @param {Array.<string>} [parameters.excludedTasks=[]] List of tasks to be excluded.
231
- * If the wildcard '*' is provided, only the included tasks will be executed.
232
- * @param {Array.<string>} [parameters.devExcludeProject=[]] List of projects to be excluded from development build
233
- * @returns {Promise} Promise resolving to <code>undefined</code> once build has finished
234
- */
235
- async build({
236
- tree, destPath, cleanDest = false,
237
- buildDependencies = false, includedDependencies = [], excludedDependencies = [],
238
- dev = false, selfContained = false, cssVariables = false, jsdoc = false,
239
- includedTasks = [], excludedTasks = [], devExcludeProject = []
240
- }) {
241
- const startTime = process.hrtime();
242
- log.info(`Building project ${tree.metadata.name}` + (buildDependencies ? "" : " not") +
243
- " including dependencies..." + (dev ? " [dev mode]" : ""));
244
- log.verbose(`Building to ${destPath}...`);
245
-
246
- const selectedTasks = composeTaskList({dev, selfContained, jsdoc, includedTasks, excludedTasks});
247
-
248
- const fsTarget = resourceFactory.createAdapter({
249
- fsBasePath: destPath,
250
- virBasePath: "/"
251
- });
252
-
253
- const buildContext = new BuildContext({
254
- rootProject: tree,
255
- options: {
256
- cssVariables: cssVariables
257
- }
258
- });
259
- const cleanupSigHooks = registerCleanupSigHooks(buildContext);
260
-
261
- const projects = {}; // Unique project index to prevent building the same project multiple times
262
- const projectWriters = {}; // Collection of memory adapters of already built libraries
263
- function projectFilter(project) {
264
- function projectMatchesAny(deps) {
265
- return deps.some((dep) => dep instanceof RegExp ?
266
- dep.test(project.metadata.name) : dep === project.metadata.name);
267
- }
268
-
269
- // if everything is included, this overrules exclude lists
270
- if (includedDependencies.includes("*")) return true;
271
- let test = !excludedDependencies.includes("*"); // exclude everything?
272
-
273
- if (test && projectMatchesAny(excludedDependencies)) {
274
- test = false;
275
- }
276
- if (!test && projectMatchesAny(includedDependencies)) {
277
- test = true;
278
- }
279
-
280
- return test;
281
- }
282
-
283
- const projectCountMarker = {};
284
- function projectCount(project, count = 0) {
285
- if (buildDependencies) {
286
- count = project.dependencies.filter(projectFilter).reduce((depCount, depProject) => {
287
- return projectCount(depProject, depCount);
288
- }, count);
289
- }
290
- if (!projectCountMarker[project.metadata.name]) {
291
- count++;
292
- projectCountMarker[project.metadata.name] = true;
293
- }
294
- return count;
295
- }
296
- const buildLogger = log.createTaskLogger("🛠 ", projectCount(tree));
297
-
298
- function buildProject(project) {
299
- const projectBasePath = `/resources/${project.metadata.namespace}`;
300
- let depPromise;
301
- let projectTasks = selectedTasks;
302
-
303
- // Build dependencies in sequence as it is far easier to detect issues and reduces
304
- // side effects or other issues such as too many open files
305
- if (buildDependencies) {
306
- depPromise = project.dependencies.filter(projectFilter).reduce(function(p, depProject) {
307
- return p.then(() => buildProject(depProject));
308
- }, Promise.resolve());
309
- } else {
310
- depPromise = Promise.resolve();
311
- }
312
-
313
- // Build the project after all dependencies have been built
314
- return depPromise.then(() => {
315
- if (projects[project.metadata.name]) {
316
- return Promise.resolve();
317
- } else {
318
- projects[project.metadata.name] = true;
319
- }
320
- buildLogger.startWork(`Building project ${project.metadata.name}`);
321
-
322
- const projectType = typeRepository.getType(project.type);
323
- const resourceCollections = resourceFactory.createCollectionsForTree(project, {
324
- virtualReaders: projectWriters,
325
- getVirtualBasePathPrefix: function({project, virBasePath}) {
326
- if (project.type === "application" && project.metadata.namespace) {
327
- return projectBasePath;
328
- }
329
- },
330
- getProjectExcludes: function(project) {
331
- if (project.builder && project.builder.resources) {
332
- return project.builder.resources.excludes;
333
- }
334
- }
335
- });
336
-
337
- const writer = new MemAdapter({
338
- virBasePath: "/"
339
- });
340
- // Store project writer as virtual reader for parent projects
341
- // so they can access the build results of this project
342
- projectWriters[project.metadata.name] = writer;
343
-
344
- // TODO: Add getter for writer of DuplexColection
345
- const workspace = resourceFactory.createWorkspace({
346
- virBasePath: "/",
347
- writer,
348
- reader: resourceCollections.source,
349
- name: project.metadata.name
350
- });
351
-
352
- const projectContext = buildContext.createProjectContext({
353
- project, // TODO 2.0: Add project facade object/instance here
354
- resources: {
355
- workspace,
356
- dependencies: resourceCollections.dependencies
357
- }
358
- });
359
-
360
- const TaskUtil = require("../tasks/TaskUtil");
361
- const taskUtil = new TaskUtil({
362
- projectBuildContext: projectContext
363
- });
364
-
365
- if (dev && devExcludeProject.indexOf(project.metadata.name) !== -1) {
366
- projectTasks = composeTaskList({dev: false, selfContained, includedTasks, excludedTasks});
367
- }
368
-
369
- return projectType.build({
370
- resourceCollections: {
371
- workspace,
372
- dependencies: resourceCollections.dependencies
373
- },
374
- tasks: projectTasks,
375
- project,
376
- parentLogger: log,
377
- taskUtil
378
- }).then(() => {
379
- log.verbose("Finished building project %s. Writing out files...", project.metadata.name);
380
- buildLogger.completeWork(1);
381
-
382
- return workspace.byGlob("/**/*").then((resources) => {
383
- const tagCollection = projectContext.getResourceTagCollection();
384
- return Promise.all(resources.map((resource) => {
385
- if (tagCollection.getTag(resource, projectContext.STANDARD_TAGS.OmitFromBuildResult)) {
386
- log.verbose(`Skipping write of resource tagged as "OmitFromBuildResult": ` +
387
- resource.getPath());
388
- return; // Skip target write for this resource
389
- }
390
- if (projectContext.isRootProject() && project.type === "application" &&
391
- project.metadata.namespace) {
392
- // Root-application projects only: Remove namespace prefix if given
393
- const resourcePath = resource.getPath();
394
- if (resourcePath.startsWith(projectBasePath)) {
395
- resource.setPath(resourcePath.replace(projectBasePath, ""));
396
- }
397
- }
398
- return fsTarget.write(resource);
399
- }));
400
- });
401
- });
402
- });
403
- }
404
-
405
- try {
406
- if (cleanDest) {
407
- await rimraf(destPath);
408
- }
409
- await buildProject(tree);
410
- log.info(`Build succeeded in ${getElapsedTime(startTime)}`);
411
- } catch (err) {
412
- log.error(`Build failed in ${getElapsedTime(startTime)}`);
413
- throw err;
414
- } finally {
415
- deregisterCleanupSigHooks(cleanupSigHooks);
416
- await executeCleanupTasks(buildContext);
417
- }
418
- }
419
- };
420
-
421
- // Export local function for testing only
422
- /* istanbul ignore else */
423
- if (process.env.NODE_ENV === "test") {
424
- module.exports._composeTaskList = composeTaskList;
425
- }
@@ -1,270 +0,0 @@
1
-
2
- /**
3
- * Resource collections
4
- *
5
- * @public
6
- * @typedef module:@ui5/builder.BuilderResourceCollections
7
- * @property {module:@ui5/fs.DuplexCollection} workspace Workspace Resource
8
- * @property {module:@ui5/fs.ReaderCollection} dependencies Workspace Resource
9
- */
10
-
11
- /**
12
- * Base class for the builder implementation of a project type
13
- *
14
- * @abstract
15
- */
16
- class AbstractBuilder {
17
- /**
18
- * Constructor
19
- *
20
- * @param {object} parameters
21
- * @param {BuilderResourceCollections} parameters.resourceCollections Resource collections
22
- * @param {object} parameters.project Project configuration
23
- * @param {GroupLogger} parameters.parentLogger Logger to use
24
- * @param {object} parameters.taskUtil
25
- */
26
- constructor({resourceCollections, project, parentLogger, taskUtil}) {
27
- if (new.target === AbstractBuilder) {
28
- throw new TypeError("Class 'AbstractBuilder' is abstract");
29
- }
30
-
31
- this.project = project;
32
-
33
- this.log = parentLogger.createSubLogger(project.type + " " + project.metadata.name, 0.2);
34
- this.taskLog = this.log.createTaskLogger("🔨");
35
-
36
- this.tasks = {};
37
- this.taskExecutionOrder = [];
38
- this.addStandardTasks({
39
- resourceCollections,
40
- project,
41
- log: this.log,
42
- taskUtil
43
- });
44
- this.addCustomTasks({
45
- resourceCollections,
46
- project,
47
- taskUtil
48
- });
49
- }
50
-
51
- /**
52
- * Adds all standard tasks to execute
53
- *
54
- * @abstract
55
- * @protected
56
- * @param {object} parameters
57
- * @param {BuilderResourceCollections} parameters.resourceCollections Resource collections
58
- * @param {object} parameters.taskUtil
59
- * @param {object} parameters.project Project configuration
60
- * @param {object} parameters.log <code>@ui5/logger</code> logger instance
61
- */
62
- addStandardTasks({resourceCollections, project, log, taskUtil}) {
63
- throw new Error("Function 'addStandardTasks' is not implemented");
64
- }
65
-
66
- /**
67
- * Adds custom tasks to execute
68
- *
69
- * @private
70
- * @param {object} parameters
71
- * @param {BuilderResourceCollections} parameters.resourceCollections Resource collections
72
- * @param {object} parameters.taskUtil
73
- * @param {object} parameters.project Project configuration
74
- */
75
- addCustomTasks({resourceCollections, project, taskUtil}) {
76
- const projectCustomTasks = project.builder && project.builder.customTasks;
77
- if (!projectCustomTasks || projectCustomTasks.length === 0) {
78
- return; // No custom tasks defined
79
- }
80
- const taskRepository = require("../tasks/taskRepository");
81
- for (let i = 0; i < projectCustomTasks.length; i++) {
82
- const taskDef = projectCustomTasks[i];
83
- if (!taskDef.name) {
84
- throw new Error(`Missing name for custom task definition of project ${project.metadata.name} ` +
85
- `at index ${i}`);
86
- }
87
- if (taskDef.beforeTask && taskDef.afterTask) {
88
- throw new Error(`Custom task definition ${taskDef.name} of project ${project.metadata.name} ` +
89
- `defines both "beforeTask" and "afterTask" parameters. Only one must be defined.`);
90
- }
91
- if (this.taskExecutionOrder.length && !taskDef.beforeTask && !taskDef.afterTask) {
92
- // Iff there are tasks configured, beforeTask or afterTask must be given
93
- throw new Error(`Custom task definition ${taskDef.name} of project ${project.metadata.name} ` +
94
- `defines neither a "beforeTask" nor an "afterTask" parameter. One must be defined.`);
95
- }
96
-
97
- let newTaskName = taskDef.name;
98
- if (this.tasks[newTaskName]) {
99
- // Task is already known
100
- // => add a suffix to allow for multiple configurations of the same task
101
- let suffixCounter = 0;
102
- while (this.tasks[newTaskName]) {
103
- suffixCounter++; // Start at 1
104
- newTaskName = `${taskDef.name}--${suffixCounter}`;
105
- }
106
- }
107
- // Create custom task if not already done (task might be referenced multiple times, first one wins)
108
- const {specVersion, task} = taskRepository.getTask(taskDef.name);
109
- const execTask = function() {
110
- /* Custom Task Interface
111
- Parameters:
112
- {Object} parameters Parameters
113
- {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
114
- {module:@ui5/fs.AbstractReader} parameters.dependencies
115
- Reader or Collection to read dependency files
116
- {Object} parameters.taskUtil Specification Version dependent interface to a
117
- [TaskUtil]{@link module:@ui5/builder.tasks.TaskUtil} instance
118
- {Object} parameters.options Options
119
- {string} parameters.options.projectName Project name
120
- {string} [parameters.options.projectNamespace] Project namespace if available
121
- {string} [parameters.options.configuration] Task configuration if given in ui5.yaml
122
- Returns:
123
- {Promise<undefined>} Promise resolving with undefined once data has been written
124
- */
125
- const params = {
126
- workspace: resourceCollections.workspace,
127
- dependencies: resourceCollections.dependencies,
128
- options: {
129
- projectName: project.metadata.name,
130
- projectNamespace: project.metadata.namespace,
131
- configuration: taskDef.configuration
132
- }
133
- };
134
-
135
- const taskUtilInterface = taskUtil.getInterface(specVersion);
136
- // Interface is undefined if specVersion does not support taskUtil
137
- if (taskUtilInterface) {
138
- params.taskUtil = taskUtilInterface;
139
- }
140
- return task(params);
141
- };
142
-
143
- this.tasks[newTaskName] = execTask;
144
-
145
- if (this.taskExecutionOrder.length) {
146
- // There is at least one task configured. Use before- and afterTask to add the custom task
147
- const refTaskName = taskDef.beforeTask || taskDef.afterTask;
148
- let refTaskIdx = this.taskExecutionOrder.indexOf(refTaskName);
149
- if (refTaskIdx === -1) {
150
- throw new Error(`Could not find task ${refTaskName}, referenced by custom task ${newTaskName}, ` +
151
- `to be scheduled for project ${project.metadata.name}`);
152
- }
153
- if (taskDef.afterTask) {
154
- // Insert after index of referenced task
155
- refTaskIdx++;
156
- }
157
- this.taskExecutionOrder.splice(refTaskIdx, 0, newTaskName);
158
- } else {
159
- // There is no task configured so far. Just add the custom task
160
- this.taskExecutionOrder.push(newTaskName);
161
- }
162
- }
163
- }
164
-
165
- /**
166
- * Adds a executable task to the builder
167
- *
168
- * The order this function is being called defines the build order. FIFO.
169
- *
170
- * @param {string} taskName Name of the task which should be in the list availableTasks.
171
- * @param {Function} taskFunction
172
- */
173
- addTask(taskName, taskFunction) {
174
- if (this.tasks[taskName]) {
175
- throw new Error(`Failed to add duplicate task ${taskName} for project ${this.project.metadata.name}`);
176
- }
177
- if (this.taskExecutionOrder.includes(taskName)) {
178
- throw new Error(`Builder: Failed to add task ${taskName} for project ${this.project.metadata.name}. ` +
179
- `It has already been scheduled for execution.`);
180
- }
181
- this.tasks[taskName] = taskFunction;
182
- this.taskExecutionOrder.push(taskName);
183
- }
184
-
185
- /**
186
- * Check whether a task is defined
187
- *
188
- * @private
189
- * @param {string} taskName
190
- * @returns {boolean}
191
- */
192
- hasTask(taskName) {
193
- // TODO 3.0: Check whether this method is still required.
194
- // Only usage within #build seems to be unnecessary as all tasks are also added to the taskExecutionOrder
195
- return Object.prototype.hasOwnProperty.call(this.tasks, taskName);
196
- }
197
-
198
- /**
199
- * Takes a list of tasks which should be executed from the available task list of the current builder
200
- *
201
- * @param {Array} tasksToRun List of tasks which should be executed
202
- * @returns {Promise} Returns promise chain with tasks
203
- */
204
- build(tasksToRun) {
205
- const allTasks = this.taskExecutionOrder.filter((taskName) => {
206
- // There might be a numeric suffix in case a custom task is configured multiple times.
207
- // The suffix needs to be removed in order to check against the list of tasks to run.
208
- //
209
- // Note: The 'tasksToRun' parameter only allows to specify the custom task name
210
- // (without suffix), so it executes either all or nothing.
211
- // It's currently not possible to just execute some occurrences of a custom task.
212
- // This would require a more robust contract to identify task executions
213
- // (e.g. via an 'id' that can be assigned to a specific execution in the configuration).
214
- const taskWithoutSuffixCounter = taskName.replace(/--\d+$/, "");
215
- return this.hasTask(taskName) && tasksToRun.includes(taskWithoutSuffixCounter);
216
- });
217
-
218
- this.taskLog.addWork(allTasks.length);
219
-
220
- return allTasks.reduce((taskChain, taskName) => {
221
- const taskFunction = this.tasks[taskName];
222
-
223
- if (typeof taskFunction === "function") {
224
- taskChain = taskChain.then(this.wrapTask(taskName, taskFunction));
225
- }
226
-
227
- return taskChain;
228
- }, Promise.resolve());
229
- }
230
-
231
- /**
232
- * Adds progress related functionality to task function.
233
- *
234
- * @private
235
- * @param {string} taskName Name of the task
236
- * @param {Function} taskFunction Function which executed the task
237
- * @returns {Function} Wrapped task function
238
- */
239
- wrapTask(taskName, taskFunction) {
240
- return () => {
241
- this.taskLog.startWork(`Running task ${taskName}...`);
242
- return taskFunction().then(() => this.taskLog.completeWork(1));
243
- };
244
- }
245
-
246
- /**
247
- * Appends the list of 'excludes' to the list of 'patterns'. To harmonize both lists, the 'excludes'
248
- * are negated and the 'patternPrefix' is added to make them absolute.
249
- *
250
- * @private
251
- * @param {string[]} patterns
252
- * List of absolute default patterns.
253
- * @param {string[]} excludes
254
- * List of relative patterns to be excluded. Excludes with a leading "!" are meant to be re-included.
255
- * @param {string} patternPrefix
256
- * Prefix to be added to the excludes to make them absolute. The prefix must have a leading and a
257
- * trailing "/".
258
- */
259
- enhancePatternWithExcludes(patterns, excludes, patternPrefix) {
260
- excludes.forEach((exclude) => {
261
- if (exclude.startsWith("!")) {
262
- patterns.push(`${patternPrefix}${exclude.slice(1)}`);
263
- } else {
264
- patterns.push(`!${patternPrefix}${exclude}`);
265
- }
266
- });
267
- }
268
- }
269
-
270
- module.exports = AbstractBuilder;