Skip to content

Commit

Permalink
Allow not persistent changes for task before run
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
  • Loading branch information
mmorhun committed May 30, 2019
1 parent 93fb9e7 commit 9022169
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
11 changes: 2 additions & 9 deletions packages/plugin-ext/src/plugin/tasks/task-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

import * as theia from '@theia/plugin';
import * as Converter from '../type-converters';
import { ObjectIdentifier } from '../../common/object-identifier';
import { TaskDto } from '../../common';

export class TaskProviderAdapter {
private cacheId = 0;
private cache = new Map<number, theia.Task>();

constructor(private readonly provider: theia.TaskProvider) { }

Expand All @@ -37,9 +34,6 @@ export class TaskProviderAdapter {
continue;
}

const id = this.cacheId++;
ObjectIdentifier.mixin(data, id);
this.cache.set(id, task);
result.push(data);
}
return result;
Expand All @@ -50,9 +44,8 @@ export class TaskProviderAdapter {
if (typeof this.provider.resolveTask !== 'function') {
return Promise.resolve(undefined);
}
const id = ObjectIdentifier.of(task);
const cached = this.cache.get(id);
const item = cached ? cached : Converter.toTask(task);

const item = Converter.toTask(task);
if (!item) {
return Promise.resolve(undefined);
}
Expand Down
1 change: 1 addition & 0 deletions packages/task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@theia/terminal": "^0.6.0",
"@theia/variable-resolver": "^0.6.0",
"@theia/workspace": "^0.6.0",
"deepmerge": "2.0.1",
"jsonc-parser": "^2.0.2"
},
"publishConfig": {
Expand Down
22 changes: 22 additions & 0 deletions packages/task/src/browser/task-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export namespace TaskCommands {
category: TASK_CATEGORY,
label: 'Clear History'
};

export const TASK_CUSTOM_RUN: Command = {
id: 'task:custom-run',
category: TASK_CATEGORY
};
}

const TASKS_STORAGE_KEY = 'tasks';
Expand Down Expand Up @@ -137,6 +142,23 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
}
}
);
registry.registerCommand(
TaskCommands.TASK_CUSTOM_RUN,
{
isEnabled: () => true,
// tslint:disable-next-line:no-any
execute: (...args: any[]) => {
const [source, label, overrides] = args;
if (source && label) {
if (overrides) {
return this.taskService.customRun(source, label, overrides);
} else {
return this.taskService.run(source, label);
}
}
}
}
);
registry.registerCommand(
TaskCommands.TASK_ATTACH,
{
Expand Down
37 changes: 33 additions & 4 deletions packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { ProvidedTaskConfigurations } from './provided-task-configurations';
import { Range } from 'vscode-languageserver-types';
import URI from '@theia/core/lib/common/uri';

const deepmerge: (args: object[]) => object = require('deepmerge').default.all;

@injectable()
export class TaskService implements TaskConfigurationClient {
/**
Expand Down Expand Up @@ -237,14 +239,41 @@ export class TaskService implements TaskConfigurationClient {
* It looks for configured and provided tasks.
*/
async run(source: string, taskLabel: string): Promise<void> {
const task = await this.getTaskConfiguration(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}

this.runTask(task);
}

protected async getTaskConfiguration(source: string, taskLabel: string): Promise<TaskConfiguration | undefined> {
let task = await this.getProvidedTask(source, taskLabel);
if (!task) {
task = this.taskConfigurations.getTask(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}
}
return task;
}

/**
* The same as run method, but the last parameter may contain additional task type specific properties
* which are used only for this request over persistent configuration of the task.
*/
// tslint:disable-next-line:no-any
async customRun(source: string, taskLabel: string, taskPropertiesOverrides?: { [key: string]: any }): Promise<void> {
if (!taskPropertiesOverrides) {
this.run(source, taskLabel);
return;
}

let task = await this.getTaskConfiguration(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}

task = <TaskConfiguration>deepmerge([task, taskPropertiesOverrides]);

this.runTask(task);
}
Expand Down

0 comments on commit 9022169

Please sign in to comment.