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

Add 'tasks.onDidStartTask' for Plug-in API #3826

Merged
merged 1 commit into from
Jan 3, 2019
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.3.19
- [cpp] added new `cpp.clangdExecutable` and `cpp.clangdArgs` to customize language server start command.
- [monaco] Fix document-saving that was taking too long.
- [plug-in] added `tasks.onDidStartTask` Plug-in API

## v0.3.18
- [core] added a preference to define how to handle application exit
Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,13 +798,19 @@ export interface LanguagesContributionMain {
export interface CommandProperties {
command: string;
args?: string[];
options?: object;
options?: { [key: string]: any };
}

export interface TaskDto {
type: string;
label: string;
// tslint:disable-next-line:no-any
[key: string]: any;
properties?: { [key: string]: any };
}

export interface TaskExecutionDto {
id: number;
task: TaskDto;
}

export interface ProcessTaskDto extends TaskDto, CommandProperties {
Expand Down Expand Up @@ -987,9 +993,11 @@ export const MAIN_RPC_CONTEXT = {
export interface TasksExt {
$provideTasks(handle: number): Promise<TaskDto[] | undefined>;
$resolveTask(handle: number, task: TaskDto): Promise<TaskDto | undefined>;
$onDidStartTask(execution: TaskExecutionDto): void;
}

export interface TasksMain {
$registerTaskProvider(handle: number, type: string): void;
$unregister(handle: number): void;
$terminateTask(id: number): void;
}
31 changes: 31 additions & 0 deletions packages/plugin-ext/src/main/browser/tasks-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,45 @@ import { RPCProtocol } from '../../api/rpc-protocol';
import { DisposableCollection } from '@theia/core';
import { TaskProviderRegistry, TaskResolverRegistry, TaskProvider, TaskResolver } from '@theia/task/lib/browser/task-contribution';
import { interfaces } from 'inversify';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { TaskInfo } from '@theia/task/lib/common/task-protocol';
import { TaskWatcher } from '@theia/task/lib/common/task-watcher';
import { TaskService } from '@theia/task/lib/browser/task-service';

export class TasksMainImpl implements TasksMain {
private workspaceRootUri: string | undefined = undefined;

private readonly proxy: TasksExt;
private readonly disposables = new Map<number, monaco.IDisposable>();
private readonly taskProviderRegistry: TaskProviderRegistry;
private readonly taskResolverRegistry: TaskResolverRegistry;
private readonly taskWatcher: TaskWatcher;
private readonly taskService: TaskService;
private readonly workspaceService: WorkspaceService;

constructor(rpc: RPCProtocol, container: interfaces.Container, ) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.TASKS_EXT);
this.taskProviderRegistry = container.get(TaskProviderRegistry);
this.taskResolverRegistry = container.get(TaskResolverRegistry);
this.workspaceService = container.get(WorkspaceService);
this.taskWatcher = container.get(TaskWatcher);
this.taskService = container.get(TaskService);

this.workspaceService.roots.then(roots => {
const root = roots[0];
if (root) {
this.workspaceRootUri = root.uri;
}
});

this.taskWatcher.onTaskCreated((event: TaskInfo) => {
if (event.ctx === this.workspaceRootUri) {
this.proxy.$onDidStartTask({
id: event.taskId,
task: event.config
});
}
});
}

$registerTaskProvider(handle: number, type: string): void {
Expand All @@ -55,6 +82,10 @@ export class TasksMainImpl implements TasksMain {
}
}

$terminateTask(id: number): void {
this.taskService.kill(id);
}

protected createTaskProvider(handle: number): TaskProvider {
return {
provideTasks: () =>
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,10 @@ export function createAPIFactory(
const tasks: typeof theia.tasks = {
registerTaskProvider(type: string, provider: theia.TaskProvider): theia.Disposable {
return tasksExt.registerTaskProvider(type, provider);
},

onDidStartTask(listener, thisArg?, disposables?) {
return tasksExt.onDidStartTask(listener, thisArg, disposables);
}
};

Expand Down
35 changes: 34 additions & 1 deletion packages/plugin-ext/src/plugin/tasks/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,39 @@ import {
PLUGIN_RPC_CONTEXT,
TasksExt,
TasksMain,
TaskDto
TaskDto,
TaskExecutionDto
} from '../../api/plugin-api';
import * as theia from '@theia/plugin';
import * as converter from '../type-converters';
import { Disposable } from '../types-impl';
import { RPCProtocol } from '../../api/rpc-protocol';
import { TaskProviderAdapter } from './task-provider';
import { Emitter, Event } from '@theia/core/lib/common/event';

export class TasksExtImpl implements TasksExt {
private proxy: TasksMain;

private callId = 0;
private adaptersMap = new Map<number, TaskProviderAdapter>();
private taskExecutions = new Map<number, theia.TaskExecution>();

private readonly onDidExecuteTask: Emitter<theia.TaskStartEvent> = new Emitter<theia.TaskStartEvent>();

constructor(rpc: RPCProtocol) {
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.TASKS_MAIN);
}

get onDidStartTask(): Event<theia.TaskStartEvent> {
return this.onDidExecuteTask.event;
}

$onDidStartTask(execution: TaskExecutionDto): void {
this.onDidExecuteTask.fire({
execution: this.getTaskExecution(execution)
});
}

registerTaskProvider(type: string, provider: theia.TaskProvider): theia.Disposable {
const callId = this.addNewAdapter(new TaskProviderAdapter(provider));
this.proxy.$registerTaskProvider(callId, type);
Expand Down Expand Up @@ -75,4 +91,21 @@ export class TasksExtImpl implements TasksExt {
this.proxy.$unregister(callId);
});
}

private getTaskExecution(execution: TaskExecutionDto): theia.TaskExecution {
const executionId = execution.id;
let result: theia.TaskExecution | undefined = this.taskExecutions.get(executionId);
if (result) {
return result;
}

result = {
task: converter.toTask(execution.task),
terminate: () => {
this.proxy.$terminateTask(executionId);
}
};
this.taskExecutions.set(executionId, result);
return result;
}
}
82 changes: 82 additions & 0 deletions packages/plugin-ext/src/plugin/type-converters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as theia from '@theia/plugin';
import * as types from './types-impl';
import * as model from '../api/model';
import { MarkdownString, isMarkdownString } from './markdown-string';
import { ProcessTaskDto, TaskDto } from '../api/plugin-api';

describe('Type converters:', () => {

Expand Down Expand Up @@ -167,4 +168,85 @@ describe('Type converters:', () => {

});

describe('convert tasks:', () => {
const type = 'shell';
const label = 'yarn build';
const command = 'yarn';
const args = ['run', 'build'];
const cwd = '/projects/theia';

const shellTaskDto: ProcessTaskDto = {
type: type,
label: label,
command: command,
args: args,
cwd: cwd,
options: {},
properties: {}
};

const shellPluginTask: theia.Task = {
name: label,
definition: {
type: type
},
execution: {
command: command,
args: args,
options: {
cwd: cwd
}
}
};

const taskDtoWithCommandLine: ProcessTaskDto = {
type: type,
label: label,
command: command,
args: args,
cwd: cwd,
options: {},
properties: {}
};

const pluginTaskWithCommandLine: theia.Task = {
name: label,
definition: {
type: type
},
execution: {
commandLine: 'yarn run build',
options: {
cwd: cwd
}
}
};

it('should convert to task dto', () => {
// when
const result: TaskDto | undefined = Converter.fromTask(shellPluginTask);

// then
assert.notEqual(result, undefined);
assert.deepEqual(result, shellTaskDto);
});

it('should convert from task dto', () => {
// when
const result: theia.Task = Converter.toTask(shellTaskDto);

// then
assert.notEqual(result, undefined);
assert.deepEqual(result, shellPluginTask);
});

it('should convert to task dto from task with commandline', () => {
// when
const result: TaskDto | undefined = Converter.fromTask(pluginTaskWithCommandLine);

// then
assert.notEqual(result, undefined);
assert.deepEqual(result, taskDtoWithCommandLine);
});
});
});
Loading