Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] TaskUtil: Add 'force' flag to cleanup task callback #677

Merged
merged 14 commits into from
Nov 15, 2023
7 changes: 4 additions & 3 deletions lib/build/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,18 @@ class ProjectBuilder {
}));
}

async _executeCleanupTasks() {
async _executeCleanupTasks(force) {
this.#log.info("Executing cleanup tasks...");
await this._buildContext.executeCleanupTasks();

await this._buildContext.executeCleanupTasks(force);
}

_registerCleanupSigHooks() {
const that = this;
function createListener(exitCode) {
return function() {
// Asynchronously cleanup resources, then exit
that._executeCleanupTasks().then(() => {
that._executeCleanupTasks(true).then(() => {
process.exit(exitCode);
});
};
Expand Down
4 changes: 2 additions & 2 deletions lib/build/helpers/BuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class BuildContext {
return projectBuildContext;
}

async executeCleanupTasks() {
async executeCleanupTasks(force) {
await Promise.all(this._projectBuildContexts.map((ctx) => {
return ctx.executeCleanupTasks();
return ctx.executeCleanupTasks(force);
}));
d3xter666 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/build/helpers/ProjectBuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class ProjectBuildContext {
this._queues.cleanup.push(callback);
}

async executeCleanupTasks() {
async executeCleanupTasks(force) {
await Promise.all(this._queues.cleanup.map((callback) => {
return callback();
return callback(force);
}));
}

Expand Down
13 changes: 12 additions & 1 deletion lib/build/helpers/TaskUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ class TaskUtil {
return this._projectBuildContext.getOption(key);
}

/**
* Callback that will be executed once the build is finished
d3xter666 marked this conversation as resolved.
Show resolved Hide resolved
*
* @public
* @callback @ui5/project/build/helpers/TaskUtil~cleanupTaskCallback
* @param {boolean} force Specifies whether the cleanup callback should
d3xter666 marked this conversation as resolved.
Show resolved Hide resolved
* gracefully wait for certain jobs to be completed
* or enforce immediate termination and cleanup of those jobs.
*/

/**
* Register a function that must be executed once the build is finished. This can be used to, for example,
* clean up files temporarily created on the file system. If the callback returns a Promise, it will be waited for.
Expand All @@ -172,7 +182,8 @@ class TaskUtil {
* This method is only available to custom task extensions defining
* <b>Specification Version 2.2 and above</b>.
*
* @param {Function} callback Callback to register. If it returns a Promise, it will be waited for
* @param {@ui5/project/build/helpers/TaskUtil~cleanupTaskCallback} callback Callback to register.
* If it returns a Promise, it will be waited for
d3xter666 marked this conversation as resolved.
Show resolved Hide resolved
* @public
*/
registerCleanupTask(callback) {
Expand Down
10 changes: 9 additions & 1 deletion test/lib/build/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,16 @@ test("_executeCleanupTasks", async (t) => {
const executeCleanupTasksStub = sinon.stub(builder._buildContext, "executeCleanupTasks");
await builder._executeCleanupTasks();
t.is(executeCleanupTasksStub.callCount, 1, "BuildContext#executeCleanupTasks got called once");
t.deepEqual(executeCleanupTasksStub.getCall(0).args, [],
t.deepEqual(executeCleanupTasksStub.getCall(0).args, [undefined],
d3xter666 marked this conversation as resolved.
Show resolved Hide resolved
"BuildContext#executeCleanupTasks got called with no arguments");

// reset stub
executeCleanupTasksStub.reset();
// Call with enforcement flag
await builder._executeCleanupTasks(true);
t.is(executeCleanupTasksStub.callCount, 1, "BuildContext#executeCleanupTasks got called once");
t.deepEqual(executeCleanupTasksStub.getCall(0).args, [true],
"BuildContext#executeCleanupTasks got called with force flag");
});

test("instantiate new logger for every ProjectBuilder", async (t) => {
Expand Down
Loading