Skip to content

Commit 014b217

Browse files
committed
[INTERNAL] GLOB match virtual dir fragments
1 parent 68956fb commit 014b217

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
@@ -49,6 +50,25 @@ class AbstractAdapter extends AbstractReaderWriter {
4950
return [];
5051
}
5152
patterns = Array.prototype.concat.apply([], patterns);
53+
if (!options.nodir) {
54+
for (let i = patterns.length - 1; i >= 0; i--) {
55+
const idx = this._virBaseDir.indexOf(patterns[i]);
56+
if (patterns[i] && idx !== -1 && idx < this._virBaseDir.length) {
57+
const subPath = patterns[i];
58+
return Promise.resolve([
59+
new Resource({
60+
project: this.project,
61+
statInfo: { // TODO: make closer to fs stat info
62+
isDirectory: function() {
63+
return true;
64+
}
65+
},
66+
path: subPath
67+
})
68+
]);
69+
}
70+
}
71+
}
5272
return this._runGlob(patterns, options, trace);
5373
});
5474
}
@@ -62,6 +82,7 @@ class AbstractAdapter extends AbstractReaderWriter {
6282
*/
6383
_normalizePattern(virPattern) {
6484
return Promise.resolve().then(() => {
85+
const that = this;
6586
const mm = new minimatch.Minimatch(virPattern);
6687

6788
const basePathParts = this._virBaseDir.split("/");
@@ -71,41 +92,50 @@ class AbstractAdapter extends AbstractReaderWriter {
7192
for (i = 0; i < basePathParts.length; i++) {
7293
const globPart = subset[i];
7394
if (globPart === undefined) {
74-
log.verbose("Ran out of glob parts to match. This should not happen.");
75-
return -42;
95+
log.verbose("Ran out of glob parts to match (this should not happen):");
96+
if (that._project) { // project is optional
97+
log.verbose(`Project: ${that._project.metadata.name}`);
98+
}
99+
log.verbose(`Virtual base path: ${that._virBaseDir}`);
100+
log.verbose(`Pattern to match: ${virPattern}`);
101+
log.verbose(`Current subset (tried index ${i}):`);
102+
log.verbose(subset);
103+
return {idx: i, virtualMatch: true};
76104
}
77105
const basePathPart = basePathParts[i];
78106
if (typeof globPart === "string") {
79107
if (globPart !== basePathPart) {
80-
return -42;
108+
return null;
81109
} else {
82110
continue;
83111
}
84112
} else if (globPart === minimatch.GLOBSTAR) {
85113
return i;
86114
} else { // Regex
87115
if (!globPart.test(basePathPart)) {
88-
return -42;
116+
return null;
89117
} else {
90118
continue;
91119
}
92120
}
93121
}
94122
if (subset.length === basePathParts.length) {
95-
return -1;
123+
return {rootMatch: true};
96124
}
97-
return i;
125+
return {idx: i};
98126
}
99127

100128
const resultGlobs = [];
101129
for (let i = 0; i < mm.set.length; i++) {
102-
const matchIdx = matchSubset(mm.set[i]);
103-
let resultPattern;
104-
if (matchIdx !== -42) {
105-
if (matchIdx === -1) { // matched one up
130+
const match = matchSubset(mm.set[i]);
131+
if (match) {
132+
let resultPattern;
133+
if (match.virtualMatch) {
134+
resultPattern = basePathParts.slice(0, match.idx).join("/");
135+
} else if (match.rootMatch) { // matched one up
106136
resultPattern = ""; // root "/"
107137
} else { // matched at some part of the glob
108-
resultPattern = mm.globParts[i].slice(matchIdx).join("/");
138+
resultPattern = mm.globParts[i].slice(match.idx).join("/");
109139
if (resultPattern.startsWith("/")) {
110140
resultPattern = resultPattern.substr(1);
111141
}

lib/adapters/FileSystem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ class FileSystem extends AbstractAdapter {
120120
statInfo: { // TODO: make closer to fs stat info
121121
isDirectory: function() {
122122
return true;
123-
}
123+
},
124+
mtime: new Date()
124125
},
125126
path: virPath
126127
}));

lib/adapters/Memory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class Memory extends AbstractAdapter {
4242
statInfo: { // TODO: make closer to fs stat info
4343
isDirectory: function() {
4444
return true;
45-
}
45+
},
46+
mtime: new Date()
4647
},
4748
path: this._virBasePath.slice(0, -1)
4849
})

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)