Skip to content

Commit 78eb482

Browse files
authored
[FEATURE] Sapui5MavenSnapshotResolver: Expose cacheMode parameter through all APIs (#607)
1 parent cec7b4d commit 78eb482

File tree

11 files changed

+90
-40
lines changed

11 files changed

+90
-40
lines changed

lib/graph/graph.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const log = getLogger("generateProjectGraph");
3131
* Whether framework dependencies should be added to the graph
3232
* @param {string} [options.workspaceName]
3333
* Name of the workspace configuration that should be used if any is provided
34+
* @param {module:@ui5/project/ui5Framework/maven/CacheMode} [options.cacheMode]
35+
* Cache mode to use when consuming SNAPSHOT versions of a framework
3436
* @param {string} [options.workspaceConfigPath=ui5-workspace.yaml]
3537
* Workspace configuration file to use if no object has been provided
3638
* @param {@ui5/project/graph/Workspace~Configuration} [options.workspaceConfiguration]
@@ -40,7 +42,7 @@ const log = getLogger("generateProjectGraph");
4042
*/
4143
export async function graphFromPackageDependencies({
4244
cwd, rootConfiguration, rootConfigPath,
43-
versionOverride, resolveFrameworkDependencies = true,
45+
versionOverride, cacheMode, resolveFrameworkDependencies = true,
4446
workspaceName /* TODO 4.0: default workspaceName to "default" ? */,
4547
workspaceConfiguration, workspaceConfigPath = "ui5-workspace.yaml"
4648
}) {
@@ -71,7 +73,7 @@ export async function graphFromPackageDependencies({
7173
const projectGraph = await projectGraphBuilder(provider, workspace);
7274

7375
if (resolveFrameworkDependencies) {
74-
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride, workspace});
76+
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride, cacheMode, workspace});
7577
}
7678

7779
return projectGraph;
@@ -93,14 +95,16 @@ export async function graphFromPackageDependencies({
9395
* Configuration file to use for the root module instead the default ui5.yaml. Either a path relative to
9496
* <code>cwd</code> or an absolute path. In both case, platform-specific path segment separators must be used.
9597
* @param {string} [options.versionOverride] Framework version to use instead of the one defined in the root project
98+
* @param {module:@ui5/project/ui5Framework/maven/CacheMode} [options.cacheMode]
99+
* Cache mode to use when consuming SNAPSHOT versions of a framework
96100
* @param {string} [options.resolveFrameworkDependencies=true]
97101
* Whether framework dependencies should be added to the graph
98102
* @returns {Promise<@ui5/project/graph/ProjectGraph>} Promise resolving to a Project Graph instance
99103
*/
100104
export async function graphFromStaticFile({
101105
filePath = "projectDependencies.yaml", cwd,
102106
rootConfiguration, rootConfigPath,
103-
versionOverride, resolveFrameworkDependencies = true
107+
versionOverride, cacheMode, resolveFrameworkDependencies = true
104108
}) {
105109
log.verbose(`Creating project graph using static file...`);
106110
const {
@@ -121,7 +125,7 @@ export async function graphFromStaticFile({
121125
const projectGraph = await projectGraphBuilder(provider);
122126

123127
if (resolveFrameworkDependencies) {
124-
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride});
128+
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride, cacheMode});
125129
}
126130

127131
return projectGraph;
@@ -143,14 +147,16 @@ export async function graphFromStaticFile({
143147
* Configuration file to use for the root module instead the default ui5.yaml. Either a path relative to
144148
* <code>cwd</code> or an absolute path. In both case, platform-specific path segment separators must be used.
145149
* @param {string} [options.versionOverride] Framework version to use instead of the one defined in the root project
150+
* @param {module:@ui5/project/ui5Framework/maven/CacheMode} [options.cacheMode]
151+
* Cache mode to use when consuming SNAPSHOT versions of a framework
146152
* @param {string} [options.resolveFrameworkDependencies=true]
147153
* Whether framework dependencies should be added to the graph
148154
* @returns {Promise<@ui5/project/graph/ProjectGraph>} Promise resolving to a Project Graph instance
149155
*/
150156
export async function graphFromObject({
151157
dependencyTree, cwd,
152158
rootConfiguration, rootConfigPath,
153-
versionOverride, resolveFrameworkDependencies = true
159+
versionOverride, cacheMode, resolveFrameworkDependencies = true
154160
}) {
155161
log.verbose(`Creating project graph using object...`);
156162
const {
@@ -169,7 +175,7 @@ export async function graphFromObject({
169175
const projectGraph = await projectGraphBuilder(dependencyTreeProvider);
170176

171177
if (resolveFrameworkDependencies) {
172-
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride});
178+
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride, cacheMode});
173179
}
174180

175181
return projectGraph;

lib/graph/helpers/ui5Framework.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,15 @@ export default {
280280
* @param {object} [options]
281281
* @param {string} [options.versionOverride] Framework version to use instead of the root projects framework
282282
* version
283+
* @param {module:@ui5/project/ui5Framework/maven/CacheMode} [options.cacheMode]
284+
* Cache mode to use when consuming SNAPSHOT versions of a framework
283285
* @param {@ui5/project/graph/Workspace} [options.workspace]
284286
* Optional workspace instance to use for overriding node resolutions
285-
* @param {object} [options.resolverConfig]
286-
* Optional configuration object to use for the resolvers
287287
* @returns {Promise<@ui5/project/graph/ProjectGraph>}
288288
* Promise resolving with the given graph instance to allow method chaining
289289
*/
290290
enrichProjectGraph: async function(projectGraph, options = {}) {
291-
const {workspace} = options;
291+
const {workspace, cacheMode} = options;
292292
const rootProject = projectGraph.getRoot();
293293
const frameworkName = rootProject.getFrameworkName();
294294
const frameworkVersion = rootProject.getFrameworkVersion();
@@ -369,7 +369,7 @@ export default {
369369
cwd: rootProject.getRootPath(),
370370
version,
371371
providedLibraryMetadata,
372-
...options.resolverConfig,
372+
cacheMode
373373
});
374374

375375
let startTime;

lib/ui5Framework/Sapui5MavenSnapshotResolver.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class Sapui5MavenSnapshotResolver extends AbstractResolver {
3333
* @param {string} [options.cwd=process.cwd()] Current working directory
3434
* @param {string} [options.ui5HomeDir="~/.ui5"] UI5 home directory location. This will be used to store packages,
3535
* metadata and configuration used by the resolvers. Relative to `process.cwd()`
36-
* @param {string} [options.cacheMode="default"] Can be "default" (cache everything, invalidate after 9 hours),
37-
* "off" (do not cache) and "force" (use cache only - no requests)
36+
* @param {module:@ui5/project/ui5Framework/maven/CacheMode} [options.cacheMode=Default]
37+
* Cache mode to use
3838
*/
3939
constructor(options) {
4040
super(options);
@@ -136,10 +136,10 @@ class Sapui5MavenSnapshotResolver extends AbstractResolver {
136136
snapshotEndpointUrl = process.env.UI5_MAVEN_SNAPSHOT_ENDPOINT_URL || snapshotEndpointUrl;
137137

138138
if (!snapshotEndpointUrl) {
139-
// If we resolve the settings.xml at this point, we'd need to always ask the end
140-
// user for confirmation. In some cases where the resources are already cached,
141-
// this is not necessary and we could skip it as a real request to the repository won't
142-
// be made.
139+
// Here we return a function which returns a promise that resolves with the URL.
140+
// If we would already start resolving the settings.xml at this point, we'd need to always ask the
141+
// end user for confirmation whether the resolved URL should be used. In some cases where the resources
142+
// are already cached, this is actually not necessary and could be skipped
143143
return Sapui5MavenSnapshotResolver._resolveSnapshotEndpointUrl;
144144
} else {
145145
return () => Promise.resolve(snapshotEndpointUrl);

lib/ui5Framework/maven/CacheMode.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
/**
4+
* Cache modes for maven consumption
5+
*
6+
* @public
7+
* @readonly
8+
* @enum {string}
9+
* @property {string} Default Cache everything, invalidate after 9 hours
10+
* @property {string} Force Use cache only
11+
* @property {string} Off Do not use the cache
12+
* @module @ui5/project/ui5Framework/maven/CacheMode
13+
*/
14+
export default {
15+
Default: "Default",
16+
Force: "Force",
17+
Off: "Off"
18+
};

lib/ui5Framework/maven/Installer.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const StreamZip = _StreamZip.async;
66
import {promisify} from "node:util";
77
import Registry from "./Registry.js";
88
import AbstractInstaller from "../AbstractInstaller.js";
9+
import CacheMode from "./CacheMode.js";
910
import {rimraf} from "rimraf";
1011
const stat = promisify(fs.stat);
1112
const readFile = promisify(fs.readFile);
@@ -26,10 +27,9 @@ class Installer extends AbstractInstaller {
2627
* @param {Function} parameters.snapshotEndpointUrlCb Callback that returns a Promise <string>,
2728
* resolving to the Maven repository URL.
2829
* Example: <code>https://registry.corp/vendor/build-snapshots/</code>
29-
* @param {string} [parameters.cacheMode="default"] Can be "default" (cache everything, invalidate after 9 hours),
30-
* "off" (do not cache), "force" (use cache only - no requests)
30+
* @param {module:@ui5/project/ui5Framework/maven/CacheMode} [parameters.cacheMode=Default] Cache mode to use
3131
*/
32-
constructor({ui5HomeDir, snapshotEndpointUrlCb, cacheMode = "default"}) {
32+
constructor({ui5HomeDir, snapshotEndpointUrlCb, cacheMode = CacheMode.Default}) {
3333
super(ui5HomeDir);
3434

3535
this._artifactsDir = path.join(ui5HomeDir, "framework", "artifacts");
@@ -43,6 +43,10 @@ class Installer extends AbstractInstaller {
4343
if (!this._snapshotEndpointUrlCb) {
4444
throw new Error(`Installer: Missing Snapshot-Endpoint URL callback parameter`);
4545
}
46+
if (!Object.values(CacheMode).includes(cacheMode)) {
47+
throw new Error(`Installer: Invalid value '${cacheMode}' for cacheMode parameter. ` +
48+
`Must be one of ${Object.values(CacheMode).join(", ")}`);
49+
}
4650

4751
log.verbose(`Installing Maven artifacts to: ${this._artifactsDir}`);
4852
log.verbose(`Installing Packages to: ${this._packagesDir}`);
@@ -118,16 +122,16 @@ class Installer extends AbstractInstaller {
118122
return this._synchronize("metadata-" + fsId, async () => {
119123
const localMetadata = await this._getLocalArtifactMetadata(fsId);
120124

121-
if (this._cacheMode === "force" && !localMetadata.revision) {
125+
if (this._cacheMode === CacheMode.Force && !localMetadata.revision) {
122126
throw new Error(`Could not find artifact ` +
123127
`${logId} in local cache`);
124128
}
125129

126130
const now = new Date().getTime();
127131
const timeSinceLastCheck = now - localMetadata.lastCheck;
128132

129-
if (this._cacheMode !== "force" &&
130-
(timeSinceLastCheck > CACHE_TIME || this._cacheMode === "off")) {
133+
if (this._cacheMode !== CacheMode.Force &&
134+
(timeSinceLastCheck > CACHE_TIME || this._cacheMode === CacheMode.Off)) {
131135
// No cached metadata (-> timeSinceLastCheck equals time since 1970) or
132136
// too old metadata or disabled cache
133137
// => Retrieve metadata from repository

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"./ui5Framework/Sapui5MavenSnapshotResolver": "./lib/ui5Framework/Sapui5MavenSnapshotResolver.js",
2525
"./ui5Framework/Openui5Resolver": "./lib/ui5Framework/Openui5Resolver.js",
2626
"./ui5Framework/Sapui5Resolver": "./lib/ui5Framework/Sapui5Resolver.js",
27+
"./ui5Framework/maven/CacheMode": "./lib/ui5Framework/maven/CacheMode.js",
2728
"./validation/validator": "./lib/validation/validator.js",
2829
"./validation/ValidationError": "./lib/validation/ValidationError.js",
2930
"./graph/ProjectGraph": "./lib/graph/ProjectGraph.js",

test/lib/graph/graph.integration.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "node:path";
44
import sinonGlobal from "sinon";
55
import esmock from "esmock";
66
import Workspace from "../../../lib/graph/Workspace.js";
7+
import CacheMode from "../../../lib/ui5Framework/maven/CacheMode.js";
78
const __dirname = path.dirname(fileURLToPath(import.meta.url));
89

910
const fixturesPath = path.join(__dirname, "..", "..", "fixtures");
@@ -253,7 +254,8 @@ test.serial("graphFromPackageDependencies with inactive workspace file at custom
253254
rootConfigPath: "/rootConfigPath",
254255
versionOverride: "versionOverride",
255256
workspaceName: "default",
256-
workspaceConfigPath: path.join(libraryHPath, "custom-ui5-workspace.yaml")
257+
workspaceConfigPath: path.join(libraryHPath, "custom-ui5-workspace.yaml"),
258+
cacheMode: CacheMode.Force
257259
});
258260

259261
t.is(res, "graph");
@@ -276,6 +278,7 @@ test.serial("graphFromPackageDependencies with inactive workspace file at custom
276278
"enrichProjectGraph got called with graph");
277279
t.deepEqual(enrichProjectGraphStub.getCall(0).args[1], {
278280
versionOverride: "versionOverride",
279-
workspace: null
281+
workspace: null,
282+
cacheMode: "Force"
280283
}, "enrichProjectGraph got called with correct options");
281284
});

test/lib/graph/graph.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {fileURLToPath} from "node:url";
33
import path from "node:path";
44
import sinonGlobal from "sinon";
55
import esmock from "esmock";
6+
import CacheMode from "../../../lib/ui5Framework/maven/CacheMode.js";
67

78
const __dirname = path.dirname(fileURLToPath(import.meta.url));
89
const fixturesPath = path.join(__dirname, "..", "..", "fixtures");
@@ -57,7 +58,8 @@ test.serial("graphFromPackageDependencies", async (t) => {
5758
cwd: "cwd",
5859
rootConfiguration: "rootConfiguration",
5960
rootConfigPath: "/rootConfigPath",
60-
versionOverride: "versionOverride"
61+
versionOverride: "versionOverride",
62+
cacheMode: CacheMode.Off
6163
});
6264

6365
t.is(res, "graph");
@@ -81,7 +83,8 @@ test.serial("graphFromPackageDependencies", async (t) => {
8183
"enrichProjectGraph got called with graph");
8284
t.deepEqual(enrichProjectGraphStub.getCall(0).args[1], {
8385
versionOverride: "versionOverride",
84-
workspace: undefined
86+
workspace: undefined,
87+
cacheMode: "Off"
8588
}, "enrichProjectGraph got called with correct options");
8689
});
8790

@@ -98,6 +101,7 @@ test.serial("graphFromPackageDependencies with workspace name", async (t) => {
98101
rootConfigPath: "/rootConfigPath",
99102
versionOverride: "versionOverride",
100103
workspaceName: "dolphin",
104+
cacheMode: CacheMode.Off
101105
});
102106

103107
t.is(res, "graph");
@@ -128,7 +132,8 @@ test.serial("graphFromPackageDependencies with workspace name", async (t) => {
128132
"enrichProjectGraph got called with graph");
129133
t.deepEqual(enrichProjectGraphStub.getCall(0).args[1], {
130134
versionOverride: "versionOverride",
131-
workspace: "workspace"
135+
workspace: "workspace",
136+
cacheMode: "Off"
132137
}, "enrichProjectGraph got called with correct options");
133138
});
134139

@@ -225,6 +230,7 @@ test.serial("graphFromPackageDependencies with empty workspace", async (t) => {
225230
rootConfigPath: "/rootConfigPath",
226231
versionOverride: "versionOverride",
227232
workspaceName: "dolphin",
233+
cacheMode: CacheMode.Off
228234
});
229235

230236
t.is(res, "graph");
@@ -255,7 +261,8 @@ test.serial("graphFromPackageDependencies with empty workspace", async (t) => {
255261
"enrichProjectGraph got called with graph");
256262
t.deepEqual(enrichProjectGraphStub.getCall(0).args[1], {
257263
versionOverride: "versionOverride",
258-
workspace: null
264+
workspace: null,
265+
cacheMode: "Off"
259266
}, "enrichProjectGraph got called with correct options");
260267
});
261268

@@ -290,7 +297,8 @@ test.serial("graphFromStaticFile", async (t) => {
290297
filePath: "file/path",
291298
rootConfiguration: "rootConfiguration",
292299
rootConfigPath: "/rootConfigPath",
293-
versionOverride: "versionOverride"
300+
versionOverride: "versionOverride",
301+
cacheMode: CacheMode.Off
294302
});
295303

296304
t.is(res, "graph");
@@ -316,7 +324,8 @@ test.serial("graphFromStaticFile", async (t) => {
316324
t.is(enrichProjectGraphStub.getCall(0).args[0], "graph",
317325
"enrichProjectGraph got called with graph");
318326
t.deepEqual(enrichProjectGraphStub.getCall(0).args[1], {
319-
versionOverride: "versionOverride"
327+
versionOverride: "versionOverride",
328+
cacheMode: "Off"
320329
}, "enrichProjectGraph got called with correct options");
321330
});
322331

@@ -351,7 +360,8 @@ test.serial("usingObject", async (t) => {
351360
dependencyTree: "dependencyTree",
352361
rootConfiguration: "rootConfiguration",
353362
rootConfigPath: "/rootConfigPath",
354-
versionOverride: "versionOverride"
363+
versionOverride: "versionOverride",
364+
cacheMode: "Off"
355365
});
356366

357367
t.is(res, "graph");
@@ -371,7 +381,8 @@ test.serial("usingObject", async (t) => {
371381
t.is(enrichProjectGraphStub.getCall(0).args[0], "graph",
372382
"enrichProjectGraph got called with graph");
373383
t.deepEqual(enrichProjectGraphStub.getCall(0).args[1], {
374-
versionOverride: "versionOverride"
384+
versionOverride: "versionOverride",
385+
cacheMode: "Off"
375386
}, "enrichProjectGraph got called with correct options");
376387
});
377388

0 commit comments

Comments
 (0)