1+ const ProjectResource = require ( "@ui5/fs" ) . ProjectResource ;
2+
13/**
24 * Convenience functions for UI5 Builder tasks.
35 * An instance of this class is passed to every standard UI5 Builder task that requires it.
@@ -51,14 +53,26 @@ class TaskUtil {
5153 * This method is only available to custom task extensions defining
5254 * <b>Specification Version 2.2 and above</b>.
5355 *
54- * @param {string| module:@ui5/fs.Resource } resourcePath Path or resource-instance the tag should be stored for
56+ * @param {module:@ui5/fs.ProjectResource } resource Resource -instance the tag should be stored for
5557 * @param {string } tag Name of the tag.
5658 * Currently only the [STANDARD_TAGS]{@link module:@ui5/builder.tasks.TaskUtil#STANDARD_TAGS} are allowed
5759 * @param {string|boolean|integer } [value=true] Tag value. Must be primitive
5860 * @public
5961 */
60- setTag ( resourcePath , tag , value ) {
61- return this . _projectBuildContext . getResourceTagCollection ( ) . setTag ( resourcePath , tag , value ) ;
62+ setTag ( resource , tag , value ) {
63+ if ( typeof resource === "string" ) {
64+ throw new Error ( "TODO 3.0: Deprecated" ) ;
65+ }
66+ if ( ! ( resource instanceof ProjectResource ) ) {
67+ throw new Error ( "Resource must be an instance of ProjectResource" ) ;
68+ }
69+ const project = resource . getProject ( ) ;
70+ if ( project !== this . _projectBuildContext . getProject ( ) ) {
71+ throw new Error (
72+ `Unable to set tag for project ${ project . getName ( ) } ` +
73+ `while building project ${ this . _projectBuildContext . getProject ( ) . getName ( ) } ` ) ;
74+ }
75+ return project . setTag ( resource . getPath ( ) , tag , value ) ;
6276 }
6377
6478 /**
@@ -68,14 +82,20 @@ class TaskUtil {
6882 * This method is only available to custom task extensions defining
6983 * <b>Specification Version 2.2 and above</b>.
7084 *
71- * @param {string| module:@ui5/fs.Resource } resourcePath Path or resource-instance the tag should be retrieved for
85+ * @param {module:@ui5/fs.ProjectResource } resource Resource -instance the tag should be retrieved for
7286 * @param {string } tag Name of the tag
7387 * @returns {string|boolean|integer|undefined } Tag value for the given resource.
7488 * <code>undefined</code> if no value is available
7589 * @public
7690 */
77- getTag ( resourcePath , tag ) {
78- return this . _projectBuildContext . getResourceTagCollection ( ) . getTag ( resourcePath , tag ) ;
91+ getTag ( resource , tag ) {
92+ if ( typeof resource === "string" ) {
93+ throw new Error ( "TODO 3.0: Deprecated" ) ;
94+ }
95+ if ( ! ( resource instanceof ProjectResource ) ) {
96+ throw new Error ( "Resource must be an instance of ProjectResource" ) ;
97+ }
98+ return resource . getProject ( ) . getTag ( resource . getPath ( ) , tag ) ;
7999 }
80100
81101 /**
@@ -86,12 +106,18 @@ class TaskUtil {
86106 * This method is only available to custom task extensions defining
87107 * <b>Specification Version 2.2 and above</b>.
88108 *
89- * @param {string| module:@ui5/fs.Resource } resourcePath Path or resource-instance the tag should be cleared for
109+ * @param {module:@ui5/fs.ProjectResource } resource Resource -instance the tag should be cleared for
90110 * @param {string } tag Tag
91111 * @public
92112 */
93- clearTag ( resourcePath , tag ) {
94- return this . _projectBuildContext . getResourceTagCollection ( ) . clearTag ( resourcePath , tag ) ;
113+ clearTag ( resource , tag ) {
114+ if ( typeof resource === "string" ) {
115+ throw new Error ( "TODO 3.0: Deprecated" ) ;
116+ }
117+ if ( ! ( resource instanceof ProjectResource ) ) {
118+ throw new Error ( "Resource must be an instance of ProjectResource" ) ;
119+ }
120+ return resource . getProject ( ) . clearTag ( resource . getPath ( ) , tag ) ;
95121 }
96122
97123 /**
@@ -162,23 +188,42 @@ class TaskUtil {
162188
163189 const baseInterface = {
164190 STANDARD_TAGS : this . STANDARD_TAGS ,
165- setTag : this . setTag . bind ( this ) ,
166- clearTag : this . clearTag . bind ( this ) ,
167- getTag : this . getTag . bind ( this ) ,
168- isRootProject : this . isRootProject . bind ( this ) ,
169- registerCleanupTask : this . registerCleanupTask . bind ( this )
170191 } ;
192+ bindFunctions ( this , baseInterface , [
193+ "setTag" , "clearTag" , "getTag" , "isRootProject" , "registerCleanupTask"
194+ ] ) ;
171195 switch ( specVersion ) {
172196 case "2.2" :
173197 case "2.3" :
174198 case "2.4" :
175199 case "2.5" :
176200 case "2.6" :
177201 return baseInterface ;
202+ case "3.0" :
203+ baseInterface . getProject = ( projectName ) => {
204+ const project = this . getProject ( projectName ) ;
205+ const baseProjectInterface = { } ;
206+ bindFunctions ( project , baseProjectInterface , [
207+ "getName" , "getVersion" , "getNamespace"
208+ ] ) ;
209+ switch ( specVersion ) {
210+ case "3.0" :
211+ return baseProjectInterface ;
212+ default :
213+ throw new Error ( `TaskUtil: Unknown or unsupported Specification Version ${ specVersion } ` ) ;
214+ }
215+ } ;
216+ return baseInterface ;
178217 default :
179218 throw new Error ( `TaskUtil: Unknown or unsupported Specification Version ${ specVersion } ` ) ;
180219 }
181220 }
182221}
183222
223+ function bindFunctions ( sourceObject , targetObject , funcNames ) {
224+ funcNames . forEach ( ( funcName ) => {
225+ targetObject [ funcName ] = sourceObject [ funcName ] . bind ( sourceObject ) ;
226+ } ) ;
227+ }
228+
184229module . exports = TaskUtil ;
0 commit comments