Skip to content

Commit 04d3217

Browse files
committed
WIP: GLOB match virtual dir fragments
1 parent 7abadbb commit 04d3217

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

6283
let basePathParts = this._virBaseDir.split("/");
@@ -66,41 +87,50 @@ class AbstractAdapter extends AbstractReaderWriter {
6687
for (i = 0; i < basePathParts.length; i++) {
6788
const globPart = subset[i];
6889
if (globPart === undefined) {
69-
log.verbose("Ran out of glob parts to match. This should not happen.");
70-
return -42;
90+
log.verbose("Ran out of glob parts to match (this should not happen):");
91+
if (that._project) { // project is optional
92+
log.verbose(`Project: ${that._project.metadata.name}`);
93+
}
94+
log.verbose(`Virtual base path: ${that._virBaseDir}`);
95+
log.verbose(`Pattern to match: ${virPattern}`);
96+
log.verbose(`Current subset (tried index ${i}):`);
97+
log.verbose(subset);
98+
return {idx: i, virtualMatch: true};
7199
}
72100
const basePathPart = basePathParts[i];
73101
if (typeof globPart === "string") {
74102
if (globPart !== basePathPart) {
75-
return -42;
103+
return null;
76104
} else {
77105
continue;
78106
}
79107
} else if (globPart === minimatch.GLOBSTAR) {
80108
return i;
81109
} else { // Regex
82110
if (!globPart.test(basePathPart)) {
83-
return -42;
111+
return null;
84112
} else {
85113
continue;
86114
}
87115
}
88116
}
89117
if (subset.length === basePathParts.length) {
90-
return -1;
118+
return {rootMatch: true};
91119
}
92-
return i;
120+
return {idx: i};
93121
}
94122

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

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
@@ -109,3 +109,19 @@ test("Virtual RL: GLOB virtual directory w/o virtual base path prefix", (t) => {
109109
})
110110
);
111111
});
112+
113+
test.only("Virtual RL: GLOB virtual directory w/ virtual base path prefix", (t) => {
114+
// TODO: Add similar test (globbing on empty directory) for FS RL
115+
// TODO: Also add tests for nodir: true option
116+
let readerWriter = ui5Fs.resourceFactory.createAdapter({
117+
virBasePath: "/app/one/two/"
118+
});
119+
120+
// Get resource from one readerWriter
121+
return t.notThrows(
122+
readerWriter.byGlob("/*", {nodir: false})
123+
.then((resources) => {
124+
t.deepEqual(resources.length, 1, "Found exactly one resource");
125+
})
126+
);
127+
});

0 commit comments

Comments
 (0)