Skip to content

Commit

Permalink
support user-defined labels for detected tasks
Browse files Browse the repository at this point in the history
- With this change users would be able to define labels in the task configs to overwrite the task names from providers.
- fixes #6507
- fixes #7515

Signed-off-by: Liang Huang <lhuang4@ualberta.ca>
  • Loading branch information
elaihau committed Apr 16, 2020
1 parent f691918 commit 6defb0b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/task/src/browser/task-configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ export class TaskConfigurations implements Disposable {
return [...configuredTasks, ...detectedTasksAsConfigured];
}

getRawTaskConfigurations(rootFolder?: string): (TaskCustomization | TaskConfiguration)[] {
if (!rootFolder) {
const tasks: (TaskCustomization | TaskConfiguration)[] = [];
for (const configs of this.rawTaskConfigurations.values()) {
tasks.push(...configs);
}
return tasks;
}
if (this.rawTaskConfigurations.has(rootFolder)) {
return Array.from(this.rawTaskConfigurations.get(rootFolder)!.values());
}
return [];
}

/**
* returns a collection of invalid task configs as per the task schema defined in Theia.
*/
Expand All @@ -176,6 +190,24 @@ export class TaskConfigurations implements Disposable {
}
}

/** returns the customized task for a given label or undefined if none */
async getCustomizedTask(rootFolderPath: string, taskLabel: string): Promise<TaskConfiguration | undefined> {
const customizations = this.taskCustomizationMap.get(rootFolderPath);
if (customizations) {
const customization = customizations.find(cus => cus.label === taskLabel);
if (customization) {
const detected = await this.providedTaskConfigurations.getTaskToCustomize(customization, rootFolderPath);
if (detected) {
return {
...detected,
...customization,
type: detected.type
};
}
}
}
}

/** removes tasks configured in the given task config file */
private removeTasks(configFileUri: string): void {
const source = this.getSourceFolderFromConfigUri(configFileUri);
Expand Down
2 changes: 1 addition & 1 deletion packages/task/src/browser/task-definition-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class TaskDefinitionRegistry {
});
}

compareTasks(one: TaskConfiguration, other: TaskConfiguration): boolean {
compareTasks(one: TaskConfiguration | TaskCustomization, other: TaskConfiguration | TaskCustomization): boolean {
const oneType = one.taskType || one.type;
const otherType = other.taskType || other.type;
if (oneType !== otherType) {
Expand Down
13 changes: 13 additions & 0 deletions packages/task/src/browser/task-name-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,31 @@
import { inject, injectable } from 'inversify';
import { TaskConfiguration, ContributedTaskConfiguration } from '../common';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { TaskConfigurations } from './task-configurations';

@injectable()
export class TaskNameResolver {
@inject(TaskDefinitionRegistry)
protected taskDefinitionRegistry: TaskDefinitionRegistry;

@inject(TaskConfigurations)
protected readonly taskConfigurations: TaskConfigurations;

/**
* Returns task name to display.
* It is aligned with VS Code.
*/
resolve(task: TaskConfiguration): string {
if (this.isDetectedTask(task)) {
const scope = task._scope;
const rawConfigs = this.taskConfigurations.getRawTaskConfigurations(scope);
const jsonConfig = rawConfigs.find(rawConfig => this.taskDefinitionRegistry.compareTasks({
...rawConfig, _scope: scope
}, task));
// detected task that has a `label` defined in `tasks.json`
if (jsonConfig && jsonConfig.label) {
return jsonConfig.label;
}
return `${task.source || task._source}: ${task.label}`;
}

Expand Down
1 change: 1 addition & 0 deletions packages/task/src/browser/task-schema-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class TaskSchemaUpdater {
}
customizedDetectedTask.properties![taskProp] = { ...def.properties.schema.properties![taskProp] };
});
customizedDetectedTask.properties!.label = taskLabel;
customizedDetectedTask.properties!.problemMatcher = problemMatcher;
customizedDetectedTask.properties!.presentation = presentation;
customizedDetectedTask.properties!.options = commandOptionsSchema;
Expand Down
6 changes: 5 additions & 1 deletion packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,13 @@ export class TaskService implements TaskConfigurationClient {
* It looks for configured and detected tasks.
*/
async run(source: string, taskLabel: string, scope?: string): Promise<TaskInfo | undefined> {
let task = await this.getProvidedTask(source, taskLabel, scope);
let task: TaskConfiguration | undefined;
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 && scope) { // find from the customized detected tasks
task = await this.taskConfigurations.getCustomizedTask(scope, taskLabel);
}
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
Expand Down

0 comments on commit 6defb0b

Please sign in to comment.