Skip to content

Commit

Permalink
Add ability to restart running task
Browse files Browse the repository at this point in the history
Fixes #5330

- adds a new command and menu item to `restart running task`.
- updates the task-service methods `restartTask` and `terminateTask` to make them public so
others may call them.

Signed-off-by: vince-fugnitto <vincent.fugnitto@ericsson.com>
  • Loading branch information
vince-fugnitto committed Jan 2, 2020
1 parent 3b4b1e5 commit 019409e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
43 changes: 43 additions & 0 deletions packages/task/src/browser/quick-open-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,46 @@ export class TaskRunningQuickOpen implements QuickOpenModel {
});
}
}

@injectable()
export class TaskRestartRunningQuickOpen implements QuickOpenModel {

@inject(QuickOpenService)
protected readonly quickOpenService: QuickOpenService;

@inject(TaskService)
protected readonly taskService: TaskService;

async onType(_lookFor: string, acceptor: (items: QuickOpenItem[]) => void): Promise<void> {
const items: QuickOpenItem[] = [];
const runningTasks: TaskInfo[] = await this.taskService.getRunningTasks();
if (runningTasks.length <= 0) {
items.push(new QuickOpenItem({
label: 'No task to restart',
run: (): boolean => false,
}));
} else {
runningTasks.forEach((task: TaskInfo) => {
items.push(new QuickOpenItem({
label: task.config.label,
run: (mode: QuickOpenMode): boolean => {
if (mode !== QuickOpenMode.OPEN) {
return false;
}
this.taskService.restartTask(task);
return true;
}
}));
});
}
acceptor(items);
}

async open(): Promise<void> {
this.quickOpenService.open(this, {
placeholder: 'Select task to restart',
fuzzyMatchLabel: true,
fuzzyMatchDescription: true,
});
}
}
26 changes: 24 additions & 2 deletions packages/task/src/browser/task-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { inject, injectable, named, postConstruct } from 'inversify';
import { ILogger, ContributionProvider } from '@theia/core/lib/common';
import { QuickOpenTask, TaskTerminateQuickOpen, TaskRunningQuickOpen } from './quick-open-task';
import { QuickOpenTask, TaskTerminateQuickOpen, TaskRunningQuickOpen, TaskRestartRunningQuickOpen } from './quick-open-task';
import { CommandContribution, Command, CommandRegistry, MenuContribution, MenuModelRegistry } from '@theia/core/lib/common';
import {
FrontendApplication, FrontendApplicationContribution, QuickOpenContribution,
Expand Down Expand Up @@ -97,6 +97,12 @@ export namespace TaskCommands {
category: TASK_CATEGORY,
label: 'Terminate Task'
};

export const TASK_RESTART_RUNNING: Command = {
id: 'task:restart-running',
category: TASK_CATEGORY,
label: 'Restart Running Task...'
};
}

const TASKS_STORAGE_KEY = 'tasks';
Expand Down Expand Up @@ -142,6 +148,9 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
@inject(TaskTerminateQuickOpen)
protected readonly taskTerminateQuickOpen: TaskTerminateQuickOpen;

@inject(TaskRestartRunningQuickOpen)
protected readonly taskRestartRunningQuickOpen: TaskRestartRunningQuickOpen;

@inject(TaskWatcher)
protected readonly taskWatcher: TaskWatcher;

Expand Down Expand Up @@ -293,6 +302,13 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
execute: () => this.taskTerminateQuickOpen.open()
}
);

registry.registerCommand(
TaskCommands.TASK_RESTART_RUNNING,
{
execute: () => this.taskRestartRunningQuickOpen.open()
}
);
}

registerMenus(menus: MenuModelRegistry): void {
Expand Down Expand Up @@ -332,10 +348,16 @@ export class TaskFrontendContribution implements CommandContribution, MenuContri
order: '0'
});

menus.registerMenuAction(TerminalMenus.TERMINAL_TASKS_INFO, {
commandId: TaskCommands.TASK_RESTART_RUNNING.id,
label: TaskCommands.TASK_RESTART_RUNNING.label,
order: '1'
});

menus.registerMenuAction(TerminalMenus.TERMINAL_TASKS_INFO, {
commandId: TaskCommands.TASK_TERMINATE.id,
label: 'Terminate Task...',
order: '1'
order: '2'
});

menus.registerMenuAction(TerminalMenus.TERMINAL_TASKS_CONFIG, {
Expand Down
3 changes: 2 additions & 1 deletion packages/task/src/browser/task-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ContainerModule } from 'inversify';
import { FrontendApplicationContribution, QuickOpenContribution, KeybindingContribution } from '@theia/core/lib/browser';
import { CommandContribution, MenuContribution, bindContributionProvider } from '@theia/core/lib/common';
import { WebSocketConnectionProvider } from '@theia/core/lib/browser/messaging';
import { QuickOpenTask, TaskTerminateQuickOpen, TaskRunningQuickOpen, TaskActionProvider, ConfigureTaskAction } from './quick-open-task';
import { QuickOpenTask, TaskTerminateQuickOpen, TaskRestartRunningQuickOpen, TaskRunningQuickOpen, TaskActionProvider, ConfigureTaskAction } from './quick-open-task';
import { TaskContribution, TaskProviderRegistry, TaskResolverRegistry } from './task-contribution';
import { TaskService } from './task-service';
import { TaskConfigurations } from './task-configurations';
Expand Down Expand Up @@ -53,6 +53,7 @@ export default new ContainerModule(bind => {
bind(QuickOpenTask).toSelf().inSingletonScope();
bind(TaskRunningQuickOpen).toSelf().inSingletonScope();
bind(TaskTerminateQuickOpen).toSelf().inSingletonScope();
bind(TaskRestartRunningQuickOpen).toSelf().inSingletonScope();
bind(TaskConfigurations).toSelf().inSingletonScope();
bind(ProvidedTaskConfigurations).toSelf().inSingletonScope();
bind(TaskConfigurationManager).toSelf().inSingletonScope();
Expand Down
4 changes: 2 additions & 2 deletions packages/task/src/browser/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ export class TaskService implements TaskConfigurationClient {
* Terminates a task that is actively running.
* @param activeTaskInfo the TaskInfo of the task that is actively running
*/
protected async terminateTask(activeTaskInfo: TaskInfo): Promise<void> {
async terminateTask(activeTaskInfo: TaskInfo): Promise<void> {
const taskId = activeTaskInfo.taskId;
return this.kill(taskId);
}
Expand All @@ -683,7 +683,7 @@ export class TaskService implements TaskConfigurationClient {
* Terminates a task that is actively running, and restarts it.
* @param activeTaskInfo the TaskInfo of the task that is actively running
*/
protected async restartTask(activeTaskInfo: TaskInfo, option?: RunTaskOption): Promise<TaskInfo | undefined> {
async restartTask(activeTaskInfo: TaskInfo, option?: RunTaskOption): Promise<TaskInfo | undefined> {
await this.terminateTask(activeTaskInfo);
return this.doRunTask(activeTaskInfo.config, option);
}
Expand Down

0 comments on commit 019409e

Please sign in to comment.