Skip to content

Commit

Permalink
Fix TaskExecution instantiation
Browse files Browse the repository at this point in the history
Signed-off-by: Anatoliy Bazko <abazko@redhat.com>
  • Loading branch information
tolusha committed Dec 12, 2019
1 parent 928e1e0 commit e3abdde
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 72 deletions.
47 changes: 41 additions & 6 deletions packages/plugin-ext/src/plugin/type-converters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as theia from '@theia/plugin';
import * as types from './types-impl';
import * as model from '../common/plugin-api-rpc-model';
import { MarkdownString, isMarkdownString } from './markdown-string';
import { ProcessTaskDto, TaskDto } from '../common/plugin-api-rpc';
import { TaskDto } from '../common/plugin-api-rpc';

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

Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Type converters:', () => {
const cwd = '/projects/theia';
const additionalProperty = 'some property';

const shellTaskDto: ProcessTaskDto = {
const shellTaskDto: TaskDto = {
type: shellType,
label,
source,
Expand All @@ -190,6 +190,16 @@ describe('Type converters:', () => {
additionalProperty
};

const shellTaskDtoWithCommandLine: TaskDto = {
type: shellType,
label,
source,
scope: undefined,
command: commandLine,
options: { cwd },
additionalProperty
};

const shellPluginTask: theia.Task = {
name: label,
source,
Expand Down Expand Up @@ -221,7 +231,9 @@ describe('Type converters:', () => {
}
};

const customTaskDto: ProcessTaskDto = { ...shellTaskDto, type: customType };
const customTaskDto: TaskDto = { ...shellTaskDto, type: customType };

const customTaskDtoWithCommandLine: TaskDto = { ...shellTaskDtoWithCommandLine, type: customType };

const customPluginTask: theia.Task = {
...shellPluginTask, definition: {
Expand Down Expand Up @@ -258,6 +270,18 @@ describe('Type converters:', () => {
// when
const result: theia.Task = Converter.toTask(shellTaskDto);

assert.strictEqual(result.execution instanceof types.ShellExecution, true);

if (result.execution instanceof types.ShellExecution) {
assert.strictEqual(result.execution.commandLine, undefined);

result.execution = {
args: result.execution.args,
options: result.execution.options,
command: result.execution.command
};
}

// then
assert.notEqual(result, undefined);
assert.deepEqual(result, shellPluginTask);
Expand All @@ -269,7 +293,7 @@ describe('Type converters:', () => {

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

it('should convert task with custom type to dto', () => {
Expand All @@ -285,8 +309,19 @@ describe('Type converters:', () => {
// when
const result: theia.Task = Converter.toTask(customTaskDto);

assert.strictEqual(result.execution instanceof types.ShellExecution, true);

if (result.execution instanceof types.ShellExecution) {
assert.strictEqual(result.execution.commandLine, undefined);

result.execution = {
args: result.execution.args,
options: result.execution.options,
command: result.execution.command
};
}

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

Expand All @@ -296,7 +331,7 @@ describe('Type converters:', () => {

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

Expand Down
105 changes: 39 additions & 66 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,19 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import {
EditorPosition,
Selection,
Position,
DecorationOptions,
WorkspaceEditDto,
ResourceTextEditDto,
ResourceFileEditDto,
TaskDto,
ProcessTaskDto,
PickOpenItem,
Plugin
} from '../common/plugin-api-rpc';
import * as theia from '@theia/plugin';
import { Position as P, Range as R, SymbolInformation, SymbolKind as S } from 'vscode-languageserver-types';
import URI from 'vscode-uri';
import * as rpc from '../common/plugin-api-rpc';
import { DecorationOptions, EditorPosition, PickOpenItem, Plugin, Position, ResourceFileEditDto, ResourceTextEditDto, Selection, TaskDto, WorkspaceEditDto } from '../common/plugin-api-rpc';
import * as model from '../common/plugin-api-rpc-model';
import * as theia from '@theia/plugin';
import * as types from './types-impl';
import { LanguageSelector, LanguageFilter, RelativePattern } from './languages';
import { LanguageFilter, LanguageSelector, RelativePattern } from './languages';
import { isMarkdownString, MarkdownString } from './markdown-string';
import URI from 'vscode-uri';
import { Item } from './quick-open';
import * as types from './types-impl';

const SIDE_GROUP = -2;
const ACTIVE_GROUP = -1;
import { SymbolInformation, Range as R, Position as P, SymbolKind as S } from 'vscode-languageserver-types';
import { Item } from './quick-open';

export function toViewColumn(ep?: EditorPosition): theia.ViewColumn | undefined {
if (typeof ep !== 'number') {
Expand Down Expand Up @@ -631,16 +619,15 @@ export function fromTask(task: theia.Task): TaskDto | undefined {
return taskDto;
}

const processTaskDto = taskDto as ProcessTaskDto;
if (taskDefinition.type === 'shell' || types.ShellExecution.is(execution)) {
return fromShellExecution(execution, processTaskDto);
return fromShellExecution(<theia.ShellExecution>execution, taskDto);
}

if (taskDefinition.type === 'process' || types.ProcessExecution.is(execution)) {
return fromProcessExecution(<theia.ProcessExecution>execution, processTaskDto);
return fromProcessExecution(<theia.ProcessExecution>execution, taskDto);
}

return processTaskDto;
return taskDto;
}

export function toTask(taskDto: TaskDto): theia.Task {
Expand Down Expand Up @@ -669,12 +656,12 @@ export function toTask(taskDto: TaskDto): theia.Task {
result.definition = taskDefinition;

if (taskType === 'process') {
result.execution = getProcessExecution(taskDto as ProcessTaskDto);
result.execution = getProcessExecution(taskDto);
}

const execution = { command, args, options };
if (taskType === 'shell' || types.ShellExecution.is(execution)) {
result.execution = getShellExecution(taskDto as ProcessTaskDto);
result.execution = getShellExecution(taskDto);
}

if (!properties) {
Expand All @@ -690,70 +677,56 @@ export function toTask(taskDto: TaskDto): theia.Task {
return result;
}

export function fromProcessExecution(execution: theia.ProcessExecution, processTaskDto: ProcessTaskDto): ProcessTaskDto {
processTaskDto.command = execution.process;
processTaskDto.args = execution.args;
export function fromProcessExecution(execution: theia.ProcessExecution, taskDto: TaskDto): TaskDto {
taskDto.command = execution.process;
taskDto.args = execution.args;

const options = execution.options;
if (options) {
processTaskDto.options = options;
taskDto.options = options;
}
return processTaskDto;
return taskDto;
}

export function fromShellExecution(execution: theia.ShellExecution, processTaskDto: ProcessTaskDto): ProcessTaskDto {
export function fromShellExecution(execution: theia.ShellExecution, taskDto: TaskDto): TaskDto {
const options = execution.options;
if (options) {
processTaskDto.options = getShellExecutionOptions(options);
taskDto.options = getShellExecutionOptions(options);
}

const commandLine = execution.commandLine;
if (commandLine) {
const args = commandLine.split(' ');
const taskCommand = args.shift();

if (taskCommand) {
processTaskDto.command = taskCommand;
}

processTaskDto.args = args;
return processTaskDto;
taskDto.command = commandLine;
return taskDto;
}

const command = execution.command;
if (typeof command === 'string') {
processTaskDto.command = command;
processTaskDto.args = getShellArgs(execution.args);
return processTaskDto;
taskDto.command = command;
taskDto.args = getShellArgs(execution.args);
return taskDto;
} else {
throw new Error('Converting ShellQuotedString command is not implemented');
}
}

export function getProcessExecution(processTaskDto: ProcessTaskDto): theia.ProcessExecution {
const execution = {} as theia.ProcessExecution;

execution.process = processTaskDto.command;

const processArgs = processTaskDto.args;
execution.args = processArgs ? processArgs : [];

const options = processTaskDto.options;
execution.options = options ? options : {};

return execution;
export function getProcessExecution(taskDto: TaskDto): theia.ProcessExecution {
return new types.ProcessExecution(
taskDto.command,
taskDto.args || [],
taskDto.options || {});
}

export function getShellExecution(processTaskDto: ProcessTaskDto): theia.ShellExecution {
const execution = {} as theia.ShellExecution;

const options = processTaskDto.options;
execution.options = options ? options : {};
execution.args = processTaskDto.args;

execution.command = processTaskDto.command;

return execution;
export function getShellExecution(taskDto: TaskDto): theia.ShellExecution {
if (taskDto.command && Array.isArray(taskDto.args) && taskDto.args.length !== 0) {
return new types.ShellExecution(
taskDto.command,
taskDto.args,
taskDto.options || {});
}
return new types.ShellExecution(
taskDto.command || taskDto.commandLine,
taskDto.options || {});
}

export function getShellArgs(args: undefined | (string | theia.ShellQuotedString)[]): string[] {
Expand Down

0 comments on commit e3abdde

Please sign in to comment.