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

fetch detected tasks twice on clicking "Run Tasks" #7536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 64 additions & 32 deletions packages/task/src/browser/provided-task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,43 +72,26 @@ export class ProvidedTaskConfigurations {
* In case that more than one customization is found, return the one that has the biggest number of matched properties.
*
* @param customization the task customization
* @param rootFolderPath the root folder that the detected task comes from
* @return the detected task for the given task customization. If the task customization is not found, `undefined` is returned.
*/
async getTaskToCustomize(customization: TaskCustomization, rootFolderPath: string): Promise<TaskConfiguration | undefined> {
const definition = this.taskDefinitionRegistry.getDefinition(customization);
if (!definition) {
return undefined;
}

const matchedTasks: TaskConfiguration[] = [];
let highest = -1;
const tasks = await this.getTasks();
for (const task of tasks) { // find detected tasks that match the `definition`
let score = 0;
if (!definition.properties.required.every(requiredProp => customization[requiredProp] !== undefined)) {
continue;
}
score += definition.properties.required.length; // number of required properties
const requiredProps = new Set(definition.properties.required);
// number of optional properties
score += definition.properties.all.filter(p => !requiredProps.has(p) && customization[p] !== undefined).length;
if (score >= highest) {
if (score > highest) {
highest = score;
matchedTasks.length = 0;
}
matchedTasks.push(task);
}
}
return this.findMatchedTask(tasks, customization, rootFolderPath);
}

// find the task that matches the `customization`.
// The scenario where more than one match is found should not happen unless users manually enter multiple customizations for one type of task
// If this does happen, return the first match
const rootFolderUri = new URI(rootFolderPath).toString();
const matchedTask = matchedTasks.filter(t =>
rootFolderUri === t._scope && definition.properties.all.every(p => t[p] === customization[p])
)[0];
return matchedTask;
/**
* Finds the detected task from the cache for the given task customization.
* The detected task is considered as a "match" to the task customization if it has all the `required` properties.
* In case that more than one customization is found, return the one that has the biggest number of matched properties.
*
* @param customization the task customization
* @param rootFolderPath the root folder that the detected task comes from
* @return the detected task for the given task customization. If the task customization is not found, `undefined` is returned.
*/
getCachedTaskToCustomize(customization: TaskCustomization, rootFolderPath: string): TaskConfiguration | undefined {
const tasks = this.getCachedTasks();
return this.findMatchedTask(tasks, customization, rootFolderPath);
}

protected getCachedTask(source: string, taskLabel: string, scope?: string): TaskConfiguration | undefined {
Expand All @@ -124,6 +107,18 @@ export class ProvidedTaskConfigurations {
}
}

protected getCachedTasks(): TaskConfiguration[] {
const tasks: TaskConfiguration[] = [];
for (const taskLabelMap of this.tasksMap.values()) {
for (const taskScopeMap of taskLabelMap.values()) {
for (const task of taskScopeMap.values()) {
tasks.push(task);
}
}
}
return tasks;
}

protected cacheTasks(tasks: TaskConfiguration[]): void {
for (const task of tasks) {
const label = task.label;
Expand All @@ -147,4 +142,41 @@ export class ProvidedTaskConfigurations {
}
}
}

// find tasks that matches the `customization` from `tasks`.
private findMatchedTask(tasks: TaskConfiguration[], customization: TaskCustomization, rootFolderPath: string): TaskConfiguration | undefined {
const definition = this.taskDefinitionRegistry.getDefinition(customization);
if (definition) {
const matchedTasks: TaskConfiguration[] = [];
let highest = -1;
for (const task of tasks) { // find detected tasks that match the `definition`
let score = 0;
if (!definition.properties.required.every(requiredProp => customization[requiredProp] !== undefined)) {
continue;
}
score += definition.properties.required.length; // number of required properties
const requiredProps = new Set(definition.properties.required);
// number of optional properties
score += definition.properties.all.filter(p => !requiredProps.has(p) && customization[p] !== undefined).length;
if (score >= highest) {
if (score > highest) {
highest = score;
matchedTasks.length = 0;
}
matchedTasks.push(task);
}
}

if (matchedTasks.length > 0) {
// find the task that matches the `customization`.
// The scenario where more than one match is found should not happen unless users manually enter multiple customizations for one type of task
// If this does happen, return the first match
const rootFolderUri = new URI(rootFolderPath).toString();
const matchedTask = matchedTasks.filter(t =>
rootFolderUri === t._scope && definition.properties.all.every(p => t[p] === customization[p])
)[0];
return matchedTask;
}
}
}
}
6 changes: 5 additions & 1 deletion packages/task/src/browser/task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ export class TaskConfigurations implements Disposable {
async getTasks(): Promise<TaskConfiguration[]> {
const configuredTasks = Array.from(this.tasksMap.values()).reduce((acc, labelConfigMap) => acc.concat(Array.from(labelConfigMap.values())), [] as TaskConfiguration[]);
const detectedTasksAsConfigured: TaskConfiguration[] = [];
let fetchTasksFromProviders = true;
for (const [rootFolder, customizations] of Array.from(this.taskCustomizationMap.entries())) {
for (const cus of customizations) {
const detected = await this.providedTaskConfigurations.getTaskToCustomize(cus, rootFolder);
const detected = fetchTasksFromProviders
? await this.providedTaskConfigurations.getTaskToCustomize(cus, rootFolder)
: this.providedTaskConfigurations.getCachedTaskToCustomize(cus, rootFolder);
fetchTasksFromProviders = false;
if (detected) {
detectedTasksAsConfigured.push({ ...detected, ...cus });
}
Expand Down