@@ -13,26 +13,24 @@ import AbstractReader from "@ui5/fs/AbstractReader";
1313class BuildReader extends AbstractReader {
1414 #projects;
1515 #projectNames;
16+ #applicationProjectName;
1617 #namespaces = new Map ( ) ;
17- #getReaderForProject;
18- #getReaderForProjects;
18+ #buildServerInterface;
1919
2020 /**
2121 * Creates a new BuildReader instance
2222 *
2323 * @public
2424 * @param {string } name Name of the reader
2525 * @param {Array<@ui5/project/specifications/Project> } projects Array of projects to read from
26- * @param {Function } getReaderForProject Function that returns a reader for a single project by name
27- * @param {Function } getReaderForProjects Function that returns a combined reader for multiple project names
26+ * @param {object } buildServerInterface Function that returns a reader for a single project by name
2827 * @throws {Error } If multiple projects share the same namespace
2928 */
30- constructor ( name , projects , getReaderForProject , getReaderForProjects ) {
29+ constructor ( name , projects , buildServerInterface ) {
3130 super ( name ) ;
3231 this . #projects = projects ;
3332 this . #projectNames = projects . map ( ( p ) => p . getName ( ) ) ;
34- this . #getReaderForProject = getReaderForProject ;
35- this . #getReaderForProjects = getReaderForProjects ;
33+ this . #buildServerInterface = buildServerInterface ;
3634
3735 for ( const project of projects ) {
3836 const ns = project . getNamespace ( ) ;
@@ -44,6 +42,10 @@ class BuildReader extends AbstractReader {
4442 }
4543 this . #namespaces. set ( ns , project . getName ( ) ) ;
4644 }
45+
46+ if ( project . getType ( ) === "application" ) {
47+ this . #applicationProjectName = project . getName ( ) ;
48+ }
4749 }
4850 }
4951
@@ -57,7 +59,7 @@ class BuildReader extends AbstractReader {
5759 * @returns {Promise<Array<@ui5/fs/Resource>> } Promise resolving to list of resources
5860 */
5961 async byGlob ( ...args ) {
60- const reader = await this . #getReaderForProjects( this . #projectNames) ;
62+ const reader = await this . #buildServerInterface . getReaderForProjects ( this . #projectNames) ;
6163 return reader . byGlob ( ...args ) ;
6264 }
6365
@@ -77,7 +79,7 @@ class BuildReader extends AbstractReader {
7779 let res = await reader . byPath ( virPath , ...args ) ;
7880 if ( ! res ) {
7981 // Fallback to unspecified projects
80- const allReader = await this . #getReaderForProjects( this . #projectNames) ;
82+ const allReader = await this . #buildServerInterface . getReaderForProjects ( this . #projectNames) ;
8183 res = await allReader . byPath ( virPath , ...args ) ;
8284 }
8385 return res ;
@@ -94,23 +96,40 @@ class BuildReader extends AbstractReader {
9496 * @returns {Promise<@ui5/fs/AbstractReader> } Promise resolving to appropriate reader
9597 */
9698 async _getReaderForResource ( virPath ) {
97- let reader ;
9899 if ( this . #projects. length === 1 ) {
99100 // Filtering on a single project (typically the root project)
100- reader = await this . #getReaderForProject( this . #projectNames[ 0 ] ) ;
101- } else {
102- // Determine project for resource path
103- const projects = this . _getProjectsForResourcePath ( virPath ) ;
104- if ( projects . length ) {
105- reader = await this . #getReaderForProjects( projects ) ;
106- } else {
107- // Unable to determine project for resource
108- // Request reader for all projects
109- reader = await this . #getReaderForProjects( this . #projectNames) ;
101+ return await this . #buildServerInterface. getReaderForProject ( this . #projectNames[ 0 ] ) ;
102+ }
103+ // Determine project for resource path
104+ const projects = this . _getProjectsForResourcePath ( virPath ) ;
105+ if ( projects . length ) {
106+ return await this . #buildServerInterface. getReaderForProjects ( projects ) ;
107+ }
108+
109+ // Unable to determine project for resource using path
110+ // Fallback 1: Try to find resource in cached readers (if available) to identify the relevant project
111+ const cachedReader = this . #buildServerInterface. getCachedReadersForProjects ( this . #projectNames) ;
112+ if ( cachedReader ) {
113+ const res = await cachedReader . byPath ( virPath ) ;
114+ if ( res ) {
115+ // Found resource in one of the cached readers. Assume it still belongs to the associated project
116+ return this . #buildServerInterface. getReaderForProject ( res . getProject ( ) . getName ( ) ) ;
117+ }
118+ }
119+
120+ // Fallback 2: If the root project is of type application, and the request does not start with
121+ // /resources/ or /test-resources/, test whether the resource can be found in the root project
122+ if ( this . #applicationProjectName && ! virPath . startsWith ( "/resources/" ) &&
123+ ! virPath . startsWith ( "/test-resources/" ) ) {
124+ const appReader = await this . #buildServerInterface. getReaderForProject ( this . #applicationProjectName) ;
125+ const res = await appReader . byPath ( virPath ) ;
126+ if ( res ) {
127+ return appReader ;
110128 }
111129 }
112130
113- return reader ;
131+ // Fallback to request a reader for all projects
132+ return await this . #buildServerInterface. getReaderForProjects ( this . #projectNames) ;
114133 }
115134
116135 /**
0 commit comments