Skip to content

Commit

Permalink
[FEATURE] SNAPSHOT Consumption: Add cache-mode parameter (#633)
Browse files Browse the repository at this point in the history
Expose cache-mode API parameter via CLI. See also:
SAP/ui5-project#607

---------

Co-authored-by: Günter Klatt <57760635+KlattG@users.noreply.github.com>
  • Loading branch information
RandomByte and KlattG committed Jun 5, 2023
1 parent 1155cfc commit 61d0865
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/cli/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function(cli) {
"config", "dependency-definition", "workspace-config", "workspace", "log-level",

// tree.js, build.js & serve.js
"framework-version",
"framework-version", "cache-mode",

// build.js
"dest",
Expand Down
13 changes: 12 additions & 1 deletion lib/cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ build.builder = function(cli) {
describe: "Overrides the framework version defined by the project",
type: "string"
})
.option("cache-mode", {
describe:
"Cache mode to use when consuming SNAPSHOT versions of a framework: 'Default', 'Force', or 'Off'. " +
"The 'Default' behavior is to invalidate the cache after 9 hours. 'Force' uses the cache only and " +
"does not create any requests. 'Off' invalidates any existing cache and updates from the repository",
type: "string",
default: "Default",
choices: ["Default", "Force", "Off"]
})
.option("experimental-css-variables", {
describe:
"Generate CSS variables (css-variables.css, css-variables.source.less)" +
Expand Down Expand Up @@ -129,12 +138,14 @@ async function handleBuild(argv) {
graph = await graphFromStaticFile({
filePath: argv.dependencyDefinition,
rootConfigPath: argv.config,
versionOverride: argv.frameworkVersion
versionOverride: argv.frameworkVersion,
cacheMode: argv.cacheMode,
});
} else {
graph = await graphFromPackageDependencies({
rootConfigPath: argv.config,
versionOverride: argv.frameworkVersion,
cacheMode: argv.cacheMode,
workspaceConfigPath: argv.workspaceConfig,
workspaceName: argv.workspace === false ? null : argv.workspace,
});
Expand Down
17 changes: 14 additions & 3 deletions lib/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,20 @@ serve.builder = function(cli) {
describe: "Overrides the framework version defined by the project",
type: "string"
})
.option("cache-mode", {
describe:
"Cache mode to use when consuming SNAPSHOT versions of a framework: 'Default', 'Force', or 'Off'. " +
"The 'Default' behavior is to invalidate the cache after 9 hours. 'Force' uses the cache only and " +
"does not create any requests. 'Off' invalidates any existing cache and updates from the repository",
type: "string",
default: "Default",
choices: ["Default", "Force", "Off"]
})
.example("ui5 serve", "Start a web server for the current project")
.example("ui5 serve --h2", "Enable the HTTP/2 protocol for the web server (requires SSL certificate)")
.example("ui5 serve --config /path/to/ui5.yaml", "Use the project configuration from a custom path")
.example("ui5 serve --translator static:/path/to/projectDependencies.yaml",
"Use a \"static\" translator with translator parameters.")
.example("ui5 serve --dependency-definition /path/to/projectDependencies.yaml",
"Use a static dependency definition file")
.example("ui5 serve --port 1337 --open tests/QUnit.html",
"Listen to port 1337 and launch default browser with http://localhost:1337/test/QUnit.html");
};
Expand All @@ -83,12 +92,14 @@ serve.handler = async function(argv) {
if (argv.dependencyDefinition) {
graph = await graphFromStaticFile({
filePath: argv.dependencyDefinition,
versionOverride: argv.frameworkVersion
versionOverride: argv.frameworkVersion,
cacheMode: argv.cacheMode,
});
} else {
graph = await graphFromPackageDependencies({
rootConfigPath: argv.config,
versionOverride: argv.frameworkVersion,
cacheMode: argv.cacheMode,
workspaceConfigPath: argv.workspaceConfig,
workspaceName: argv.workspace === false ? null : argv.workspace,
});
Expand Down
13 changes: 12 additions & 1 deletion lib/cli/commands/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ tree.builder = function(cli) {
describe:
"Overrides the framework version defined by the project",
type: "string"
})
.option("cache-mode", {
describe:
"Cache mode to use when consuming SNAPSHOT versions of a framework: 'Default', 'Force', or 'Off'. " +
"The 'Default' behavior is to invalidate the cache after 9 hours. 'Force' uses the cache only and " +
"does not create any requests. 'Off' invalidates any existing cache and updates from the repository",
type: "string",
default: "Default",
choices: ["Default", "Force", "Off"]
});
};

Expand All @@ -30,12 +39,14 @@ tree.handler = async function(argv) {
if (argv.dependencyDefinition) {
graph = await graphFromStaticFile({
filePath: argv.dependencyDefinition,
versionOverride: argv.frameworkVersion
versionOverride: argv.frameworkVersion,
cacheMode: argv.cacheMode,
});
} else {
graph = await graphFromPackageDependencies({
rootConfigPath: argv.config,
versionOverride: argv.frameworkVersion,
cacheMode: argv.cacheMode,
workspaceConfigPath: argv.workspaceConfig,
workspaceName: argv.workspace === false ? null : argv.workspace,
});
Expand Down
29 changes: 29 additions & 0 deletions test/lib/cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function getDefaultArgv() {
"cleanDest": false,
"experimental-css-variables": false,
"experimentalCssVariables": false,
"cache-mode": "Default",
"cacheMode": "Default",
"$0": "ui5"
};
}
Expand Down Expand Up @@ -127,6 +129,26 @@ test.serial("ui5 build --framework-version", async (t) => {
versionOverride: "1.99.0",
workspaceConfigPath: undefined,
workspaceName: undefined,
cacheMode: "Default",
}, "generateProjectGraph.graphFromPackageDependencies got called with expected arguments"
);
});

test.serial("ui5 build --cache-mode", async (t) => {
const {build, argv, graphFromPackageDependenciesStub} = t.context;

argv.cacheMode = "Off";

await build.handler(argv);

t.deepEqual(
graphFromPackageDependenciesStub.getCall(0).args[0],
{
rootConfigPath: undefined,
versionOverride: undefined,
workspaceConfigPath: undefined,
workspaceName: undefined,
cacheMode: "Off",
}, "generateProjectGraph.graphFromPackageDependencies got called with expected arguments"
);
});
Expand All @@ -145,6 +167,7 @@ test.serial("ui5 build --config", async (t) => {
versionOverride: undefined,
workspaceConfigPath: undefined,
workspaceName: undefined,
cacheMode: "Default",
}, "generateProjectGraph.graphFromPackageDependencies got called with expected arguments"
);
});
Expand All @@ -163,6 +186,7 @@ test.serial("ui5 build --workspace", async (t) => {
versionOverride: undefined,
workspaceConfigPath: undefined,
workspaceName: "dolphin",
cacheMode: "Default",
}, "generateProjectGraph.graphFromPackageDependencies got called with expected arguments"
);
});
Expand All @@ -181,6 +205,7 @@ test.serial("ui5 build --no-workspace", async (t) => {
versionOverride: undefined,
workspaceConfigPath: undefined,
workspaceName: null,
cacheMode: "Default",
}, "generateProjectGraph.graphFromPackageDependencies got called with expected arguments"
);
});
Expand All @@ -200,6 +225,7 @@ test.serial("ui5 build --workspace-config", async (t) => {
versionOverride: undefined,
workspaceConfigPath: fakePath,
workspaceName: undefined,
cacheMode: "Default",
}, "generateProjectGraph.graphFromPackageDependencies got called with expected arguments"
);
});
Expand All @@ -217,6 +243,7 @@ test.serial("ui5 build --dependency-definition", async (t) => {
filePath: "dependencies.yaml",
rootConfigPath: undefined,
versionOverride: undefined,
cacheMode: "Default",
}, "generateProjectGraph.graphFromStaticFile got called with expected arguments"
);
});
Expand All @@ -235,6 +262,7 @@ test.serial("ui5 build --dependency-definition --config", async (t) => {
filePath: "dependencies.yaml",
rootConfigPath: "ui5-test.yaml",
versionOverride: undefined,
cacheMode: "Default",
}, "generateProjectGraph.graphFromStaticFile got called with expected arguments"
);
});
Expand All @@ -254,6 +282,7 @@ test.serial("ui5 build --dependency-definition --config --framework-version", as
filePath: "dependencies.yaml",
rootConfigPath: "ui5-test.yaml",
versionOverride: "1.99.0",
cacheMode: "Default",
}, "generateProjectGraph.graphFromStaticFile got called with expected arguments"
);
});
Expand Down
55 changes: 55 additions & 0 deletions test/lib/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function getDefaultArgv() {
"sapCspPolicies": false,
"serve-csp-reports": false,
"serveCspReports": false,
"cache-mode": "Default",
"cacheMode": "Default",
"$0": "ui5"
};
}
Expand Down Expand Up @@ -87,6 +89,7 @@ test.serial("ui5 serve: default", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -132,6 +135,7 @@ test.serial("ui5 serve --h2", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -173,6 +177,7 @@ test.serial("ui5 serve --accept-remote-connections", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `
Expand Down Expand Up @@ -216,6 +221,7 @@ test.serial("ui5 serve --open", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -256,6 +262,7 @@ test.serial("ui5 serve --open (opens default url)", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -297,6 +304,7 @@ test.serial("ui5 serve --config", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: fakePath, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -332,6 +340,7 @@ test.serial("ui5 serve --dependency-definition", async (t) => {
t.is(graph.graphFromStaticFile.callCount, 1);
t.deepEqual(graph.graphFromStaticFile.getCall(0).args, [{
filePath: fakePath, versionOverride: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -367,6 +376,43 @@ test.serial("ui5 serve --framework-version", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: "1.234.5",
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
URL: http://localhost:8080
`);

t.is(server.serve.callCount, 1);
t.deepEqual(server.serve.getCall(0).args, [
fakeGraph,
{
acceptRemoteConnections: false,
cert: undefined,
changePortIfInUse: true,
h2: false,
key: undefined,
port: 8080,
sendSAPTargetCSP: false,
serveCSPReports: false,
simpleIndex: false,
}
]);
});

test.serial("ui5 serve --cache-mode", async (t) => {
const {argv, serve, graph, server, fakeGraph} = t.context;

argv.cacheMode = "Force";

await serve.handler(argv);

t.is(graph.graphFromStaticFile.callCount, 0);
t.is(graph.graphFromPackageDependencies.callCount, 1);
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Force",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -402,6 +448,7 @@ test.serial("ui5 serve --workspace", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: "dolphin",
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -437,6 +484,7 @@ test.serial("ui5 serve --no-workspace", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: null,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -473,6 +521,7 @@ test.serial("ui5 serve --workspace-config", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: fakePath, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -508,6 +557,7 @@ test.serial("ui5 serve --sap-csp-policies", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -543,6 +593,7 @@ test.serial("ui5 serve --serve-csp-reports", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -578,6 +629,7 @@ test.serial("ui5 serve --simple-index", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -620,6 +672,7 @@ test.serial("ui5 serve with ui5.yaml port setting", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -669,6 +722,7 @@ test.serial("ui5 serve --h2 with ui5.yaml port setting", async (t) => {
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down Expand Up @@ -725,6 +779,7 @@ test.serial("ui5 serve --h2 with ui5.yaml port setting and port CLI argument", a
t.deepEqual(graph.graphFromPackageDependencies.getCall(0).args, [{
rootConfigPath: undefined, versionOverride: undefined,
workspaceConfigPath: undefined, workspaceName: undefined,
cacheMode: "Default",
}]);

t.is(t.context.consoleOutput, `Server started
Expand Down
Loading

0 comments on commit 61d0865

Please sign in to comment.