Skip to content

Commit

Permalink
search detected tasks by scope
Browse files Browse the repository at this point in the history
This pull request adds the support of detected tasks that have
- same label, and
- different scopes
in a multi-root workspace. This change fixes #6715.

Signed-off-by: Liang Huang <liang.huang@ericsson.com>
  • Loading branch information
Liang Huang committed Dec 8, 2019
1 parent 4d3a125 commit f42682d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.
36 changes: 25 additions & 11 deletions packages/task/src/browser/provided-task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export class ProvidedTaskConfigurations {

/**
* Map of source (name of extension, or path of root folder that the task config comes from) and `task config map`.
* For the inner map (i.e., `task config map`), the key is task label and value TaskConfiguration
* For the second level of inner map, the key is task label.
* For the third level of inner map, the key is the task scope and value TaskConfiguration.
*/
protected tasksMap = new Map<string, Map<string, TaskConfiguration>>();
protected tasksMap = new Map<string, Map<string, Map<string | undefined, TaskConfiguration>>>();

@inject(TaskProviderRegistry)
protected readonly taskProviderRegistry: TaskProviderRegistry;
Expand All @@ -45,13 +46,13 @@ export class ProvidedTaskConfigurations {
}

/** returns the task configuration for a given source and label or undefined if none */
async getTask(source: string, taskLabel: string): Promise<TaskConfiguration | undefined> {
const task = this.getCachedTask(source, taskLabel);
async getTask(source: string, taskLabel: string, scope?: string): Promise<TaskConfiguration | undefined> {
const task = this.getCachedTask(source, taskLabel, scope);
if (task) {
return task;
} else {
await this.getTasks();
return this.getCachedTask(source, taskLabel);
return this.getCachedTask(source, taskLabel, scope);
}
}

Expand Down Expand Up @@ -100,23 +101,36 @@ export class ProvidedTaskConfigurations {
return matchedTask;
}

protected getCachedTask(source: string, taskLabel: string): TaskConfiguration | undefined {
protected getCachedTask(source: string, taskLabel: string, scope?: string): TaskConfiguration | undefined {
const labelConfigMap = this.tasksMap.get(source);
if (labelConfigMap) {
return labelConfigMap.get(taskLabel);
const scopeConfigMap = labelConfigMap.get(taskLabel);
if (scopeConfigMap) {
return scopeConfigMap.get(scope);
}
}
}

protected cacheTasks(tasks: TaskConfiguration[]): void {
for (const task of tasks) {
const label = task.label;
const source = task._source;
const scope = task._scope;
if (this.tasksMap.has(source)) {
this.tasksMap.get(source)!.set(label, task);
const labelConfigMap = this.tasksMap.get(source)!;
if (labelConfigMap.has(label)) {
labelConfigMap.get(label)!.set(scope, task);
} else {
const newScopeConfigMap = new Map<undefined | string, TaskConfiguration>();
newScopeConfigMap.set(scope, task);
labelConfigMap.set(label, newScopeConfigMap);
}
} else {
const labelTaskMap = new Map<string, TaskConfiguration>();
labelTaskMap.set(label, task);
this.tasksMap.set(source, labelTaskMap);
const newLabelConfigMap = new Map<string, Map<undefined | string, TaskConfiguration>>();
const newScopeConfigMap = new Map<undefined | string, TaskConfiguration>();
newScopeConfigMap.set(scope, task);
newLabelConfigMap.set(label, newScopeConfigMap);
this.tasksMap.set(source, newLabelConfigMap);
}
}
}
Expand Down
39 changes: 23 additions & 16 deletions packages/task/src/browser/quick-open-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { FileSystem } from '@theia/filesystem/lib/common';
import { QuickOpenModel, QuickOpenItem, QuickOpenActionProvider, QuickOpenMode, QuickOpenGroupItem, QuickOpenGroupItemOptions } from '@theia/core/lib/common/quick-open-model';
import { PreferenceService } from '@theia/core/lib/browser';
import { TaskNameResolver } from './task-name-resolver';
import { TaskSourceResolver } from './task-source-resolver';
import { TaskConfigurationManager } from './task-configuration-manager';

@injectable()
Expand Down Expand Up @@ -57,6 +58,9 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
@inject(TaskNameResolver)
protected readonly taskNameResolver: TaskNameResolver;

@inject(TaskSourceResolver)
protected readonly taskSourceResolver: TaskSourceResolver;

@inject(FileSystem)
protected readonly fileSystem: FileSystem;

Expand All @@ -80,8 +84,7 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
const item = new TaskRunQuickOpenItem(task, this.taskService, isMulti, {
groupLabel: index === 0 ? 'recently used tasks' : undefined,
showBorder: false
}, this.taskNameResolver);
item['taskDefinitionRegistry'] = this.taskDefinitionRegistry;
}, this.taskDefinitionRegistry, this.taskNameResolver, this.taskSourceResolver);
return item;
}),
...filteredConfiguredTasks.map((task, index) => {
Expand All @@ -92,8 +95,7 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
? false
: index === 0 ? true : false
)
}, this.taskNameResolver);
item['taskDefinitionRegistry'] = this.taskDefinitionRegistry;
}, this.taskDefinitionRegistry, this.taskNameResolver, this.taskSourceResolver);
return item;
}),
...filteredProvidedTasks.map((task, index) => {
Expand All @@ -104,8 +106,7 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
? false
: index === 0 ? true : false
)
}, this.taskNameResolver);
item['taskDefinitionRegistry'] = this.taskDefinitionRegistry;
}, this.taskDefinitionRegistry, this.taskNameResolver, this.taskSourceResolver);
return item;
})
);
Expand Down Expand Up @@ -281,10 +282,12 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
if (defaultBuildOrTestTasks.length === 1) { // run the default build / test task
const defaultBuildOrTestTask = defaultBuildOrTestTasks[0];
const taskToRun = (defaultBuildOrTestTask as TaskRunQuickOpenItem).getTask();
const scope = this.taskSourceResolver.resolve(taskToRun);

if (this.taskDefinitionRegistry && !!this.taskDefinitionRegistry.getDefinition(taskToRun)) {
this.taskService.run(taskToRun.source, taskToRun.label);
this.taskService.run(taskToRun.source, taskToRun.label, scope);
} else {
this.taskService.run(taskToRun._source, taskToRun.label);
this.taskService.run(taskToRun._source, taskToRun.label, scope);
}
return;
}
Expand All @@ -311,9 +314,10 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {
item.options,
this.taskNameResolver,
shouldRunBuildTask,
this.taskConfigurationManager
this.taskConfigurationManager,
this.taskDefinitionRegistry,
this.taskSourceResolver
);
newItem['taskDefinitionRegistry'] = this.taskDefinitionRegistry;
return newItem;
});
this.quickOpenService.open(this, {
Expand Down Expand Up @@ -408,14 +412,14 @@ export class QuickOpenTask implements QuickOpenModel, QuickOpenHandler {

export class TaskRunQuickOpenItem extends QuickOpenGroupItem {

protected taskDefinitionRegistry: TaskDefinitionRegistry;

constructor(
protected readonly task: TaskConfiguration,
protected taskService: TaskService,
protected isMulti: boolean,
public readonly options: QuickOpenGroupItemOptions,
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry,
protected readonly taskNameResolver: TaskNameResolver,
protected readonly taskSourceResolver: TaskSourceResolver
) {
super(options);
}
Expand Down Expand Up @@ -452,10 +456,11 @@ export class TaskRunQuickOpenItem extends QuickOpenGroupItem {
return false;
}

const scope = this.taskSourceResolver.resolve(this.task);
if (this.taskDefinitionRegistry && !!this.taskDefinitionRegistry.getDefinition(this.task)) {
this.taskService.run(this.task.source || this.task._source, this.task.label);
this.taskService.run(this.task.source || this.task._source, this.task.label, scope);
} else {
this.taskService.run(this.task._source, this.task.label);
this.taskService.run(this.task._source, this.task.label, scope);
}
return true;
}
Expand All @@ -469,9 +474,11 @@ export class ConfigureBuildOrTestTaskQuickOpenItem extends TaskRunQuickOpenItem
public readonly options: QuickOpenGroupItemOptions,
protected readonly taskNameResolver: TaskNameResolver,
protected readonly isBuildTask: boolean,
protected taskConfigurationManager: TaskConfigurationManager
protected taskConfigurationManager: TaskConfigurationManager,
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry,
protected readonly taskSourceResolver: TaskSourceResolver
) {
super(task, taskService, isMulti, options, taskNameResolver);
super(task, taskService, isMulti, options, taskDefinitionRegistry, taskNameResolver, taskSourceResolver);
}

run(mode: QuickOpenMode): boolean {
Expand Down
4 changes: 2 additions & 2 deletions packages/task/src/browser/task-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
isEnabled: () => true,
// tslint:disable-next-line:no-any
execute: (...args: any[]) => {
const [source, label] = args;
const [source, label, scope] = args;
if (source && label) {
return this.taskService.run(source, label);
return this.taskService.run(source, label, scope);
}
return this.quickOpenTask.open();
}
Expand Down
8 changes: 4 additions & 4 deletions packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ export class TaskService implements TaskConfigurationClient {
* Returns a task configuration provided by an extension by task source and label.
* If there are no task configuration, returns undefined.
*/
async getProvidedTask(source: string, label: string): Promise<TaskConfiguration | undefined> {
return this.providedTaskConfigurations.getTask(source, label);
async getProvidedTask(source: string, label: string, scope?: string): Promise<TaskConfiguration | undefined> {
return this.providedTaskConfigurations.getTask(source, label, scope);
}

/** Returns an array of running tasks 'TaskInfo' objects */
Expand Down Expand Up @@ -370,8 +370,8 @@ export class TaskService implements TaskConfigurationClient {
* Runs a task, by the source and label of the task configuration.
* It looks for configured and detected tasks.
*/
async run(source: string, taskLabel: string): Promise<TaskInfo | undefined> {
let task = await this.getProvidedTask(source, taskLabel);
async run(source: string, taskLabel: string, scope?: string): Promise<TaskInfo | undefined> {
let task = await this.getProvidedTask(source, taskLabel, scope);
if (!task) { // if a detected task cannot be found, search from tasks.json
task = this.taskConfigurations.getTask(source, taskLabel);
if (!task) {
Expand Down

0 comments on commit f42682d

Please sign in to comment.