builder/lib/tasks/TaskUtil.js

  1. /**
  2. * Convenience functions for UI5 Builder tasks.
  3. * An instance of this class is passed to every standard UI5 Builder task that requires it.
  4. *
  5. * Custom tasks that define a specification version >= 2.2 will receive an interface
  6. * to an instance of this class when called.
  7. * The set of available functions on that interface depends on the specification
  8. * version defined for the extension.
  9. *
  10. * @public
  11. * @memberof module:@ui5/builder.tasks
  12. */
  13. class TaskUtil {
  14. /**
  15. * Standard Build Tags. See UI5 Tooling
  16. * [RFC 0008]{@link https://github.com/SAP/ui5-tooling/blob/master/rfcs/0008-resource-tagging-during-build.md}
  17. * for details.
  18. *
  19. * @public
  20. * @typedef {object} module:@ui5/builder.tasks.TaskUtil~StandardBuildTags
  21. * @property {string} OmitFromBuildResult
  22. * Setting this tag to true for a resource will prevent it from being written to the build target
  23. * @property {string} IsBundle
  24. * This tag identifies resources that contain (i.e. bundle) multiple other resources
  25. */
  26. /**
  27. * Since <code>@ui5/builder.builder.ProjectBuildContext</code> is a private class, TaskUtil must not be
  28. * instantiated by modules other than @ui5/builder itself.
  29. *
  30. * @param {object} parameters
  31. * @param {module:@ui5/builder.builder.ProjectBuildContext} parameters.projectBuildContext ProjectBuildContext
  32. * @public
  33. */
  34. constructor({projectBuildContext}) {
  35. this._projectBuildContext = projectBuildContext;
  36. /**
  37. * @member {module:@ui5/builder.tasks.TaskUtil~StandardBuildTags}
  38. * @public
  39. */
  40. this.STANDARD_TAGS = this._projectBuildContext.STANDARD_TAGS;
  41. }
  42. /**
  43. * Stores a tag with value for a given resource's path. Note that the tag is independent of the supplied
  44. * resource instance. For two resource instances with the same path, the same tag value is returned.
  45. * If the path of a resource is changed, any tag information previously stored for that resource is lost.
  46. *
  47. * </br></br>
  48. * This method is only available to custom task extensions defining
  49. * <b>Specification Version 2.2 and above</b>.
  50. *
  51. * @param {module:@ui5/fs.Resource} resource The resource the tag should be stored for
  52. * @param {string} tag Name of the tag.
  53. * Currently only the [STANDARD_TAGS]{@link module:@ui5/builder.tasks.TaskUtil#STANDARD_TAGS} are allowed
  54. * @param {string|boolean|integer} [value=true] Tag value. Must be primitive
  55. * @public
  56. */
  57. setTag(resource, tag, value) {
  58. return this._projectBuildContext.getResourceTagCollection().setTag(resource, tag, value);
  59. }
  60. /**
  61. * Retrieves the value for a stored tag. If no value is stored, <code>undefined</code> is returned.
  62. *
  63. * </br></br>
  64. * This method is only available to custom task extensions defining
  65. * <b>Specification Version 2.2 and above</b>.
  66. *
  67. * @param {module:@ui5/fs.Resource} resource The resource the tag should be retrieved for
  68. * @param {string} tag Name of the tag
  69. * @returns {string|boolean|integer|undefined} Tag value for the given resource.
  70. * <code>undefined</code> if no value is available
  71. * @public
  72. */
  73. getTag(resource, tag) {
  74. return this._projectBuildContext.getResourceTagCollection().getTag(resource, tag);
  75. }
  76. /**
  77. * Clears the value of a tag stored for the given resource's path.
  78. * It's like the tag was never set for that resource.
  79. *
  80. * </br></br>
  81. * This method is only available to custom task extensions defining
  82. * <b>Specification Version 2.2 and above</b>.
  83. *
  84. * @param {module:@ui5/fs.Resource} resource The resource the tag should be cleared for
  85. * @param {string} tag Tag
  86. * @public
  87. */
  88. clearTag(resource, tag) {
  89. return this._projectBuildContext.getResourceTagCollection().clearTag(resource, tag);
  90. }
  91. /**
  92. * Check whether the project currently being built is the root project.
  93. *
  94. * </br></br>
  95. * This method is only available to custom task extensions defining
  96. * <b>Specification Version 2.2 and above</b>.
  97. *
  98. * @returns {boolean} <code>true</code> if the currently built project is the root project
  99. * @public
  100. */
  101. isRootProject() {
  102. return this._projectBuildContext.isRootProject();
  103. }
  104. /**
  105. * Register a function that must be executed once the build is finished. This can be used to, for example,
  106. * clean up files temporarily created on the file system. If the callback returns a Promise, it will be waited for.
  107. * It will also be executed in cases where the build has failed or has been aborted.
  108. *
  109. * </br></br>
  110. * This method is only available to custom task extensions defining
  111. * <b>Specification Version 2.2 and above</b>.
  112. *
  113. * @param {Function} callback Callback to register. If it returns a Promise, it will be waited for
  114. * @public
  115. */
  116. registerCleanupTask(callback) {
  117. return this._projectBuildContext.registerCleanupTask(callback);
  118. }
  119. /**
  120. * Get an interface to an instance of this class that only provides those functions
  121. * that are supported by the given custom task extension specification version.
  122. *
  123. * @param {string} specVersion Specification version of custom task extension
  124. * @returns {object} An object with bound instance methods supported by the given specification version
  125. */
  126. getInterface(specVersion) {
  127. if (["0.1", "1.0", "1.1", "2.0", "2.1"].includes(specVersion)) {
  128. return undefined;
  129. }
  130. const baseInterface = {
  131. STANDARD_TAGS: this.STANDARD_TAGS,
  132. setTag: this.setTag.bind(this),
  133. clearTag: this.clearTag.bind(this),
  134. getTag: this.getTag.bind(this),
  135. isRootProject: this.isRootProject.bind(this),
  136. registerCleanupTask: this.registerCleanupTask.bind(this)
  137. };
  138. switch (specVersion) {
  139. case "2.2":
  140. case "2.3":
  141. case "2.4":
  142. case "2.5":
  143. case "2.6":
  144. return baseInterface;
  145. default:
  146. throw new Error(`TaskUtil: Unknown or unsupported Specification Version ${specVersion}`);
  147. }
  148. }
  149. }
  150. module.exports = TaskUtil;