Skip to content

Commit

Permalink
Fixed some issues of Custom Task:
Browse files Browse the repository at this point in the history
1. fixed custom task copyright
2. fixed TerminalWidgetImpl error log
3. fixed repeat task multiple callback close
4. fixed have no exit code

Signed-off-by: Lewin Tan <e_tanhaiyang@163.com>
  • Loading branch information
shyshywhy committed Mar 17, 2021
1 parent 3fe0ad7 commit 98e9348
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ export const MAIN_RPC_CONTEXT = {
export interface TasksExt {
$provideTasks(handle: number, token?: CancellationToken): Promise<TaskDto[] | undefined>;
$resolveTask(handle: number, task: TaskDto, token?: CancellationToken): Promise<TaskDto | undefined>;
$onDidStartTask(execution: TaskExecutionDto, terminalId: number, resolvedDefinition: theia.TaskDefinition): void;
$onDidStartTask(execution: TaskExecutionDto, terminalId: number): void;
$onDidEndTask(id: number): void;
$onDidStartTaskProcess(processId: number | undefined, execution: TaskExecutionDto): void;
$onDidEndTaskProcess(exitCode: number | undefined, taskId: number): void;
Expand Down
12 changes: 1 addition & 11 deletions packages/plugin-ext/src/main/browser/tasks-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { TaskInfo, TaskExitedEvent, TaskConfiguration, TaskCustomization } from
import { TaskWatcher } from '@theia/task/lib/common/task-watcher';
import { TaskService } from '@theia/task/lib/browser/task-service';
import { TaskDefinitionRegistry } from '@theia/task/lib/browser';
import * as theia from '@theia/plugin';

export class TasksMainImpl implements TasksMain, Disposable {
private readonly proxy: TasksExt;
Expand All @@ -51,19 +50,10 @@ export class TasksMainImpl implements TasksMain, Disposable {
this.taskDefinitionRegistry = container.get(TaskDefinitionRegistry);

this.toDispose.push(this.taskWatcher.onTaskCreated((event: TaskInfo) => {
const taskDefinition = {
type: event.config.type
} as theia.TaskDefinition;
const { type, ...properties } = event.config;
for (const key in properties) {
if (properties.hasOwnProperty(key)) {
taskDefinition[key] = properties[key];
}
}
this.proxy.$onDidStartTask({
id: event.taskId,
task: this.fromTaskConfiguration(event.config)
}, event.terminalId!!, taskDefinition);
}, event.terminalId!);
}));

this.toDispose.push(this.taskWatcher.onTaskExit((event: TaskExitedEvent) => {
Expand Down
8 changes: 5 additions & 3 deletions packages/plugin-ext/src/plugin/tasks/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class TasksExtImpl implements TasksExt {
return this.onDidExecuteTask.event;
}

async $onDidStartTask(execution: TaskExecutionDto, terminalId: number, resolvedDefinition: theia.TaskDefinition): Promise<void> {
async $onDidStartTask(execution: TaskExecutionDto, terminalId: number): Promise<void> {
const customExecution: CustomExecution | undefined = this.providedCustomExecutions.get(execution.task.id);
if (customExecution) {
if (this.activeCustomExecutions.get(execution.id) !== undefined) {
Expand All @@ -73,10 +73,12 @@ export class TasksExtImpl implements TasksExt {

// Clone the custom execution to keep the original untouched. This is important for multiple runs of the same task.
this.activeCustomExecutions.set(execution.id, customExecution);
const pty = await customExecution.callback(resolvedDefinition);
const taskDefinition = converter.toTask(execution.task).definition;
const pty = await customExecution.callback(taskDefinition);
this.terminalExt.attachPtyToTerminal(terminalId, pty);
if (pty.onDidClose) {
pty.onDidClose((e: number | void = undefined) => {
const disposable = pty.onDidClose((e: number | void = undefined) => {
disposable.dispose();
// eslint-disable-next-line no-void
this.proxy.$customExecutionComplete(execution.id, e === void 0 ? undefined : e);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/terminal-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class TerminalServiceExtImpl implements TerminalServiceExt {
return this.obtainTerminal(id, options.name || 'Terminal');
}

public attachPtyToTerminal(terminalId: number, pty: theia.Pseudoterminal): void {
attachPtyToTerminal(terminalId: number, pty: theia.Pseudoterminal): void {
this._pseudoTerminals.set(terminalId.toString(), new PseudoTerminal(terminalId, this.proxy, pty, true));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
* Copyright (C) 2021 ByteDance and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
* Copyright (C) 2021 ByteDance and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down
29 changes: 6 additions & 23 deletions packages/task/src/node/custom/custom-task-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2017-2019 Ericsson and others.
* Copyright (C) 2021 ByteDance and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -14,19 +14,13 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TaskConfiguration } from '../../common';
import { Task } from '../task';
import { TaskRunner } from '../task-runner';
import { injectable, inject, named } from 'inversify';
import { ILogger } from '@theia/core';
import { TaskFactory } from './custom-task';
import {
RawProcessFactory,
TerminalProcessFactory,
Process,
TerminalProcessOptions,
Expand All @@ -41,34 +35,23 @@ export class CustomTaskRunner implements TaskRunner {
@inject(ILogger) @named('task')
protected readonly logger: ILogger;

@inject(RawProcessFactory)
protected readonly rawProcessFactory: RawProcessFactory;

@inject(TerminalProcessFactory)
protected readonly terminalProcessFactory: TerminalProcessFactory;

@inject(TaskFactory)
protected readonly taskFactory: TaskFactory;

async run(tskConfig: TaskConfiguration, ctx?: string): Promise<Task> {
async run(taskConfig: TaskConfiguration, ctx?: string): Promise<Task> {
try {
const terminalProcessOptions = { isPseudo: true } as TerminalProcessOptions;
const terminal: Process = this.terminalProcessFactory(terminalProcessOptions);

// Wait for the confirmation that the process is successfully started, or has failed to start.
// await new Promise((resolve, reject) => {
// terminal.onStart(resolve);
// terminal.onError((error: ProcessErrorEvent) => {
// reject(ProcessTaskError.CouldNotRun(error.code));
// });
// });

return Promise.resolve(this.taskFactory({
return this.taskFactory({
context: ctx,
config: tskConfig,
label: tskConfig.label,
config: taskConfig,
label: taskConfig.label,
process: terminal,
}));
});
} catch (error) {
this.logger.error(`Error occurred while creating task: ${error}`);
throw error;
Expand Down
11 changes: 3 additions & 8 deletions packages/task/src/node/custom/custom-task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2017 Ericsson and others.
* Copyright (C) 2021 ByteDance and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -14,11 +14,6 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { injectable, inject, named } from 'inversify';
import { ILogger, MaybePromise } from '@theia/core/lib/common/';
import { Task, TaskOptions } from '../task';
Expand Down Expand Up @@ -57,7 +52,7 @@ export class CustomTask extends Task {
ctx: this.context,
config: this.options.config,
terminalId: this.process.id,
processId: this.process.id,
processId: this.process.id
};
}

Expand All @@ -68,7 +63,7 @@ export class CustomTask extends Task {
config: this.options.config,
terminalId: this.process.id,
processId: this.process.id,
code: exitCode,
code: exitCode || 0
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/task/src/node/task-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ export class TaskServerImpl implements TaskServer, Disposable {
async customExecutionComplete(id: number, exitCode: number | undefined): Promise<void> {
const task = this.taskManager.get(id) as CustomTask;
await task.callbackTaskComplete(exitCode);
return;
}

protected fireTaskExitedEvent(event: TaskExitedEvent, task?: Task): void {
Expand Down
18 changes: 13 additions & 5 deletions packages/terminal/src/browser/terminal-widget-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { TerminalCopyOnSelectionHandler } from './terminal-copy-on-selection-han
import { TerminalThemeService } from './terminal-theme-service';
import { CommandLineOptions, ShellCommandBuilder } from '@theia/process/lib/common/shell-command-builder';
import { Key } from '@theia/core/lib/browser/keys';

export const TERMINAL_WIDGET_FACTORY_ID = 'terminal';

export interface TerminalWidgetFactoryOptions extends Partial<TerminalWidgetOptions> {
Expand All @@ -59,6 +60,7 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
protected waitForConnection: Deferred<MessageConnection> | undefined;
protected hoverMessage: HTMLDivElement;
protected lastTouchEnd: TouchEvent | undefined;
protected isAttachedCloseListener: boolean = false;

@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
@inject(WebSocketConnectionProvider) protected readonly webSocketConnectionProvider: WebSocketConnectionProvider;
Expand Down Expand Up @@ -487,7 +489,7 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget

// Excludes the device status code emitted by Xterm.js
const sendData = (data?: string) => {
if (data && !this.deviceStatusCodes.has(data)) {
if (data && !this.deviceStatusCodes.has(data) && !this.disableEnterWhenAttachCloseListener()) {
return connection.sendRequest('write', data);
}
};
Expand Down Expand Up @@ -615,7 +617,7 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
return;
}
if (!IBaseTerminalServer.validateId(this.terminalId)
&& !this.terminalService.getById(this.id)) {
|| !this.terminalService.getById(this.id)) {
return;
}
const { cols, rows } = this.term;
Expand Down Expand Up @@ -683,17 +685,23 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
this.shellTerminalServer.close(this.terminalId);
this.onTermDidClose.fire(this);
}
this.attachPressAnyKeyToCloseListener(this.term);
this.attachPressEnterKeyToCloseListener(this.term);
return;
}
return this.dispose();
this.dispose();
}

private attachPressAnyKeyToCloseListener(term: Terminal): void {
private attachPressEnterKeyToCloseListener(term: Terminal): void {
if (term.textarea) {
this.isAttachedCloseListener = true;
this.addKeyListener(term.textarea, Key.ENTER, (event: KeyboardEvent) => {
this.dispose();
this.isAttachedCloseListener = false;
});
}
}

private disableEnterWhenAttachCloseListener(): boolean {
return this.isAttachedCloseListener;
}
}

0 comments on commit 98e9348

Please sign in to comment.