Skip to content

Commit db7c03e

Browse files
committed
[INTERNAL] GLOB match virtual dir fragments
1 parent 7ba869d commit db7c03e

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

lib/adapters/AbstractAdapter.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const log = require("@ui5/logger").getLogger("resources:adapters:AbstractAdapter");
22
const minimatch = require("minimatch");
33
const AbstractReaderWriter = require("../AbstractReaderWriter");
4+
const Resource = require("../Resource");
45

56
/**
67
* Abstract Resource Adapter
@@ -45,6 +46,25 @@ class AbstractAdapter extends AbstractReaderWriter {
4546
return [];
4647
}
4748
patterns = Array.prototype.concat.apply([], patterns);
49+
if (!options.nodir) {
50+
for (let i = patterns.length - 1; i >= 0; i--) {
51+
const idx = this._virBaseDir.indexOf(patterns[i]);
52+
if (patterns[i] && idx !== -1 && idx < this._virBaseDir.length) {
53+
const subPath = patterns[i];
54+
return Promise.resolve([
55+
new Resource({
56+
project: this.project,
57+
statInfo: { // TODO: make closer to fs stat info
58+
isDirectory: function() {
59+
return true;
60+
}
61+
},
62+
path: subPath
63+
})
64+
]);
65+
}
66+
}
67+
}
4868
return this._runGlob(patterns, options, trace);
4969
});
5070
}
@@ -58,6 +78,7 @@ class AbstractAdapter extends AbstractReaderWriter {
5878
*/
5979
_normalizePattern(virPattern) {
6080
return Promise.resolve().then(() => {
81+
const that = this;
6182
const mm = new minimatch.Minimatch(virPattern);
6283

6384
const basePathParts = this._virBaseDir.split("/");
@@ -67,41 +88,50 @@ class AbstractAdapter extends AbstractReaderWriter {
6788
for (i = 0; i < basePathParts.length; i++) {
6889
const globPart = subset[i];
6990
if (globPart === undefined) {
70-
log.verbose("Ran out of glob parts to match. This should not happen.");
71-
return -42;
91+
log.verbose("Ran out of glob parts to match (this should not happen):");
92+
if (that._project) { // project is optional
93+
log.verbose(`Project: ${that._project.metadata.name}`);
94+
}
95+
log.verbose(`Virtual base path: ${that._virBaseDir}`);
96+
log.verbose(`Pattern to match: ${virPattern}`);
97+
log.verbose(`Current subset (tried index ${i}):`);
98+
log.verbose(subset);
99+
return {idx: i, virtualMatch: true};
72100
}
73101
const basePathPart = basePathParts[i];
74102
if (typeof globPart === "string") {
75103
if (globPart !== basePathPart) {
76-
return -42;
104+
return null;
77105
} else {
78106
continue;
79107
}
80108
} else if (globPart === minimatch.GLOBSTAR) {
81109
return i;
82110
} else { // Regex
83111
if (!globPart.test(basePathPart)) {
84-
return -42;
112+
return null;
85113
} else {
86114
continue;
87115
}
88116
}
89117
}
90118
if (subset.length === basePathParts.length) {
91-
return -1;
119+
return {rootMatch: true};
92120
}
93-
return i;
121+
return {idx: i};
94122
}
95123

96124
const resultGlobs = [];
97125
for (let i = 0; i < mm.set.length; i++) {
98-
const matchIdx = matchSubset(mm.set[i]);
99-
let resultPattern;
100-
if (matchIdx !== -42) {
101-
if (matchIdx === -1) { // matched one up
126+
const match = matchSubset(mm.set[i]);
127+
if (match) {
128+
let resultPattern;
129+
if (match.virtualMatch) {
130+
resultPattern = basePathParts.slice(0, match.idx).join("/");
131+
} else if (match.rootMatch) { // matched one up
102132
resultPattern = ""; // root "/"
103133
} else { // matched at some part of the glob
104-
resultPattern = mm.globParts[i].slice(matchIdx).join("/");
134+
resultPattern = mm.globParts[i].slice(match.idx).join("/");
105135
if (resultPattern.startsWith("/")) {
106136
resultPattern = resultPattern.substr(1);
107137
}

lib/adapters/FileSystem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ class FileSystem extends AbstractAdapter {
118118
statInfo: { // TODO: make closer to fs stat info
119119
isDirectory: function() {
120120
return true;
121-
}
121+
},
122+
mtime: new Date()
122123
},
123124
path: virPath
124125
}));

lib/adapters/Memory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class Memory extends AbstractAdapter {
3838
statInfo: { // TODO: make closer to fs stat info
3939
isDirectory: function() {
4040
return true;
41-
}
41+
},
42+
mtime: new Date()
4243
},
4344
path: this._virBasePath.slice(0, -1)
4445
})

test/lib/resources.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ test("Virtual RL: GLOB virtual directory w/o virtual base path prefix", (t) => {
238238
);
239239
});
240240

241+
test("Virtual RL: GLOB virtual directory w/ virtual base path prefix", (t) => {
242+
// TODO: Add similar test (globbing on empty directory) for FS RL
243+
// TODO: Also add tests for nodir: true option
244+
const readerWriter = ui5Fs.resourceFactory.createAdapter({
245+
virBasePath: "/app/one/two/"
246+
});
247+
248+
// Get resource from one readerWriter
249+
return t.notThrows(
250+
readerWriter.byGlob("/*", {nodir: false})
251+
.then((resources) => {
252+
t.deepEqual(resources.length, 1, "Found exactly one resource");
253+
})
254+
);
255+
});
256+
241257
test("createCollectionsForTree", (t) => {
242258
// Creates resource reader collections for a given tree
243259
const resourceReaders = ui5Fs.resourceFactory.createCollectionsForTree(applicationBTree);

0 commit comments

Comments
 (0)