From 5dd313b75567b169dd43928c352e9c8152902215 Mon Sep 17 00:00:00 2001 From: Merlin Beutlberger Date: Thu, 7 Mar 2019 17:46:33 +0100 Subject: [PATCH] [INTERNAL] resourceFactory: Mix-in virtual reader of dependencies --- lib/resourceFactory.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/resourceFactory.js b/lib/resourceFactory.js index 42dba16a..14c3611a 100644 --- a/lib/resourceFactory.js +++ b/lib/resourceFactory.js @@ -2,6 +2,7 @@ const path = require("path"); const FsAdapter = require("./adapters/FileSystem"); const MemAdapter = require("./adapters/Memory"); const ReaderCollection = require("./ReaderCollection"); +const ReaderCollectionPrioritized = require("./ReaderCollectionPrioritized"); const DuplexCollection = require("./DuplexCollection"); const Resource = require("./Resource"); @@ -28,14 +29,21 @@ const resourceFactory = { * @param {boolean} [parameters.useNamespaces=false] Use project namespaces as path prefixes * @returns {Object} Object containing source and dependencies resource readers */ - createCollectionsForTree(tree, {useNamespaces=false} = {}) { + createCollectionsForTree(tree, {useNamespaces=false, virtualReaders={}} = {}) { + // TODO: virtualReaders is private API. The virtual reader of a project should be stored on the + // project itself. This requires projects to become objects independent from the dependency tree. + // Also see: https://github.com/SAP/ui5-project/issues/122 + const dependencyCollection = []; const dependencyPathIndex = {}; + const virtualReaderIndex = {}; const sourceResourceLocators = []; function processDependencies(project) { if (project.resources && project.resources.pathMappings) { + const fsReaders = []; for (let virBasePath in project.resources.pathMappings) { + // Create an fs reader for every path mapping if (project.resources.pathMappings.hasOwnProperty(virBasePath)) { const fsPath = project.resources.pathMappings[virBasePath]; const fsBasePath = path.join(project.path, fsPath); @@ -51,9 +59,23 @@ const resourceFactory = { } dependencyPathIndex[key] = true; - dependencyCollection.push(resourceFactory.createAdapter({fsBasePath, virBasePath, project})); + const fsReader = resourceFactory.createAdapter({fsBasePath, virBasePath, project}); + fsReaders.push(fsReader); } } + + if (!virtualReaderIndex[project.metadata.name] && virtualReaders[project.metadata.name]) { + // Mix-in virtual reader of dependency if available and not already added + virtualReaderIndex[project.metadata.name] = true; + const virtualReader = virtualReaders[project.metadata.name]; + const readerCollection = new ReaderCollectionPrioritized({ + name: `fs & vir reader collection for project ${project.metadata.name}`, + readers: [virtualReader, ...fsReaders] + }); + dependencyCollection.push(readerCollection); + } else { + dependencyCollection.push(...fsReaders); + } } project.dependencies.forEach(function(depProject) {