diff --git a/packages/plugin-ext/src/plugin/type-converters.spec.ts b/packages/plugin-ext/src/plugin/type-converters.spec.ts index 620d5e441fba9..49c61b1ede2aa 100644 --- a/packages/plugin-ext/src/plugin/type-converters.spec.ts +++ b/packages/plugin-ext/src/plugin/type-converters.spec.ts @@ -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:', () => { @@ -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, @@ -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, @@ -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: { @@ -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); @@ -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', () => { @@ -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); }); @@ -296,7 +331,7 @@ describe('Type converters:', () => { // then assert.notEqual(result, undefined); - assert.deepEqual(result, customTaskDto); + assert.deepEqual(result, customTaskDtoWithCommandLine); }); }); diff --git a/packages/plugin-ext/src/plugin/type-converters.ts b/packages/plugin-ext/src/plugin/type-converters.ts index 652b02fc494bb..8198967c3dfd6 100644 --- a/packages/plugin-ext/src/plugin/type-converters.ts +++ b/packages/plugin-ext/src/plugin/type-converters.ts @@ -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') { @@ -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(execution, taskDto); } if (taskDefinition.type === 'process' || types.ProcessExecution.is(execution)) { - return fromProcessExecution(execution, processTaskDto); + return fromProcessExecution(execution, taskDto); } - return processTaskDto; + return taskDto; } export function toTask(taskDto: TaskDto): theia.Task { @@ -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) { @@ -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[] {