Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
feat(task-plugin): replace devfile v1 command by devfilev2 command
Browse files Browse the repository at this point in the history
Change-Id: I9b472a7b3a865325c150f91e49b35bc7196545ab
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf committed Mar 12, 2021
1 parent 2fadc87 commit 936b1c4
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 77 deletions.
13 changes: 3 additions & 10 deletions plugins/task-plugin/src/che-workspace-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@ export class CheWorkspaceClient {
return che.devfile.getComponentStatuses();
}

async getCommands(): Promise<cheApi.workspace.Command[]> {
const workspace: cheApi.workspace.Workspace = await this.getCurrentWorkspace();

const runtime: cheApi.workspace.Runtime | undefined = workspace.runtime;
if (!runtime) {
return [];
}

const commands = runtime.commands;
return commands ? commands : [];
async getCommands(): Promise<che.devfile.DevfileCommand[]> {
const devfile = await che.devfile.get();
return devfile.commands || [];
}

getCurrentWorkspace(): Promise<cheApi.workspace.Workspace> {
Expand Down
9 changes: 5 additions & 4 deletions plugins/task-plugin/src/export/export-configs-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as che from '@eclipse-che/plugin';

import { inject, injectable, multiInject } from 'inversify';

import { CheWorkspaceClient } from '../che-workspace-client';
import { LaunchConfigurationsExporter } from './launch-configs-exporter';
import { che as cheApi } from '@eclipse-che/api';

export const ConfigurationsExporter = Symbol('ConfigurationsExporter');

Expand All @@ -23,7 +24,7 @@ export interface ConfigurationsExporter {
* @param workspaceFolder workspace folder for exporting configs in the config file
* @param commands commands with configurations for export
*/
export(commands: cheApi.workspace.Command[]): Promise<void>;
export(commands: che.devfile.DevfileCommand[]): Promise<void>;
}
/** Contains configurations as array of object and as raw content and is used at getting configurations from config file for example */
export interface Configurations<T> {
Expand All @@ -46,7 +47,7 @@ export class ExportConfigurationsManager {
@inject(LaunchConfigurationsExporter)
protected readonly launchConfigurationsExporter: LaunchConfigurationsExporter;

protected cheCommands: cheApi.workspace.Command[] = [];
protected cheCommands: che.devfile.DevfileCommand[] = [];

async init(): Promise<void> {
this.cheCommands = await this.cheWorkspaceClient.getCommands();
Expand All @@ -63,7 +64,7 @@ export class ExportConfigurationsManager {
await Promise.all(exportPromises);
}

private async doExport(cheCommands: cheApi.workspace.Command[], exporter: ConfigurationsExporter): Promise<void> {
private async doExport(cheCommands: che.devfile.DevfileCommand[], exporter: ConfigurationsExporter): Promise<void> {
return exporter.export(cheCommands);
}
}
8 changes: 4 additions & 4 deletions plugins/task-plugin/src/export/launch-configs-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as che from '@eclipse-che/plugin';
import * as startPoint from '../task-plugin-backend';
import * as theia from '@theia/plugin';

Expand All @@ -17,7 +18,6 @@ import { inject, injectable } from 'inversify';
import { ConfigFileLaunchConfigsExtractor } from '../extract/config-file-launch-configs-extractor';
import { ConfigurationsExporter } from './export-configs-manager';
import { VsCodeLaunchConfigsExtractor } from '../extract/vscode-launch-configs-extractor';
import { che as cheApi } from '@eclipse-che/api';
import { resolve } from 'path';

const CONFIG_DIR = '.theia';
Expand All @@ -33,7 +33,7 @@ export class LaunchConfigurationsExporter implements ConfigurationsExporter {
@inject(VsCodeLaunchConfigsExtractor)
protected readonly vsCodeLaunchConfigsExtractor: VsCodeLaunchConfigsExtractor;

async init(commands: cheApi.workspace.Command[]): Promise<void> {
async init(commands: che.devfile.DevfileCommand[]): Promise<void> {
theia.workspace.onDidChangeWorkspaceFolders(
event => {
const workspaceFolders: theia.WorkspaceFolder[] | undefined = event.added;
Expand All @@ -46,7 +46,7 @@ export class LaunchConfigurationsExporter implements ConfigurationsExporter {
);
}

async export(commands: cheApi.workspace.Command[], workspaceFolders?: theia.WorkspaceFolder[]): Promise<void> {
async export(commands: che.devfile.DevfileCommand[], workspaceFolders?: theia.WorkspaceFolder[]): Promise<void> {
workspaceFolders = workspaceFolders ? workspaceFolders : theia.workspace.workspaceFolders;
if (!workspaceFolders) {
return;
Expand All @@ -60,7 +60,7 @@ export class LaunchConfigurationsExporter implements ConfigurationsExporter {
await Promise.all(exportConfigsPromises);
}

async doExport(workspaceFolder: theia.WorkspaceFolder, commands: cheApi.workspace.Command[]): Promise<void> {
async doExport(workspaceFolder: theia.WorkspaceFolder, commands: che.devfile.DevfileCommand[]): Promise<void> {
const workspaceFolderPath = workspaceFolder.uri.path;
const launchConfigFilePath = resolve(workspaceFolderPath, CONFIG_DIR, LAUNCH_CONFIG_FILE);
const configFileConfigs = await this.configFileLaunchConfigsExtractor.extract(launchConfigFilePath);
Expand Down
6 changes: 3 additions & 3 deletions plugins/task-plugin/src/export/task-configs-exporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,6 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as che from '@eclipse-che/plugin';
import * as startPoint from '../task-plugin-backend';

import { inject, injectable } from 'inversify';
Expand All @@ -19,7 +20,6 @@ import { ConfigFileTasksExtractor } from '../extract/config-file-task-configs-ex
import { ConfigurationsExporter } from './export-configs-manager';
import { TaskConfiguration } from '@eclipse-che/plugin';
import { VsCodeTaskConfigsExtractor } from '../extract/vscode-task-configs-extractor';
import { che as cheApi } from '@eclipse-che/api';
import { homedir } from 'os';
import { resolve } from 'path';

Expand Down Expand Up @@ -48,7 +48,7 @@ export class TaskConfigurationsExporter implements ConfigurationsExporter {
@inject(BackwardCompatibilityResolver)
protected readonly backwardCompatibilityResolver: BackwardCompatibilityResolver;

async export(commands: cheApi.workspace.Command[]): Promise<void> {
async export(commands: che.devfile.DevfileCommand[]): Promise<void> {
const configFileTasks = await this.configFileTasksExtractor.extract(THEIA_USER_TASKS_PATH);

const cheTasks = this.cheTaskConfigsExtractor.extract(commands);
Expand Down
13 changes: 5 additions & 8 deletions plugins/task-plugin/src/extract/che-task-configs-extractor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,21 +8,18 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as che from '@eclipse-che/plugin';

import { TaskConfiguration } from '@eclipse-che/plugin';
import { VSCODE_LAUNCH_TYPE } from './vscode-launch-configs-extractor';
import { VSCODE_TASK_TYPE } from './vscode-task-configs-extractor';
import { che as cheApi } from '@eclipse-che/api';
import { injectable } from 'inversify';
import { toTaskConfiguration } from '../task/converter';

/** Extracts CHE configurations of tasks. */
@injectable()
export class CheTaskConfigsExtractor {
extract(commands: cheApi.workspace.Command[]): TaskConfiguration[] {
extract(commands: che.devfile.DevfileCommand[]): TaskConfiguration[] {
// TODO filter should be changed according to task type after resolving https://github.com/eclipse/che/issues/12710
const filteredCommands = commands.filter(
command => command.type !== VSCODE_TASK_TYPE && command.type !== VSCODE_LAUNCH_TYPE
);
const filteredCommands = commands.filter(command => command.exec);

if (filteredCommands.length === 0) {
return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,28 +8,26 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as che from '@eclipse-che/plugin';
import * as theia from '@theia/plugin';

import { Configurations } from '../export/export-configs-manager';
import { che as cheApi } from '@eclipse-che/api';
import { injectable } from 'inversify';
import { parse } from '../utils';

export const VSCODE_LAUNCH_TYPE = 'vscode-launch';

/** Extracts vscode launch configurations. */
@injectable()
export class VsCodeLaunchConfigsExtractor {
extract(commands: cheApi.workspace.Command[]): Configurations<theia.DebugConfiguration> {
extract(commands: che.devfile.DevfileCommand[]): Configurations<theia.DebugConfiguration> {
const emptyContent: Configurations<theia.DebugConfiguration> = { content: '', configs: [] };

const configCommands = commands.filter(command => command.type === VSCODE_LAUNCH_TYPE);
const configCommands = commands.filter(command => command.vscodeLaunch);
if (configCommands.length === 0) {
return emptyContent;
}

if (configCommands.length > 1) {
console.warn(`Found duplicate entry with configurations for type ${VSCODE_LAUNCH_TYPE}`);
console.warn(`Found duplicate entry with configurations for type vscodeLaunch`);
}

const configCommand = configCommands[0];
Expand Down
13 changes: 6 additions & 7 deletions plugins/task-plugin/src/extract/vscode-task-configs-extractor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,27 +8,26 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as che from '@eclipse-che/plugin';

import { Configurations } from '../export/export-configs-manager';
import { TaskConfiguration } from '@eclipse-che/plugin';
import { che as cheApi } from '@eclipse-che/api';
import { injectable } from 'inversify';
import { parse } from '../utils';

export const VSCODE_TASK_TYPE = 'vscode-task';

/** Extracts vscode configurations of tasks. */
@injectable()
export class VsCodeTaskConfigsExtractor {
extract(commands: cheApi.workspace.Command[]): Configurations<TaskConfiguration> {
extract(commands: che.devfile.DevfileCommand[]): Configurations<TaskConfiguration> {
const emptyContent = { content: '', configs: [] } as Configurations<TaskConfiguration>;

const configCommands = commands.filter(command => command.type === VSCODE_TASK_TYPE);
const configCommands = commands.filter(command => command.vscodeTask);
if (configCommands.length === 0) {
return emptyContent;
}

if (configCommands.length > 1) {
console.warn(`Found duplicate entry with configurations for type ${VSCODE_TASK_TYPE}`);
console.warn(`Found duplicate entry with configurations for type vscodeTask`);
}

const configCommand = configCommands[0];
Expand Down
42 changes: 10 additions & 32 deletions plugins/task-plugin/src/task/converter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,28 +8,23 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import {
CHE_TASK_TYPE,
COMPONENT_ALIAS_ATTRIBUTE,
PREVIEW_URL_ATTRIBUTE,
WORKING_DIR_ATTRIBUTE,
} from './task-protocol';
import * as che from '@eclipse-che/plugin';

import { CHE_TASK_TYPE, PREVIEW_URL_ATTRIBUTE } from './task-protocol';

import { Task } from '@theia/plugin';
import { TaskConfiguration } from '@eclipse-che/plugin';
import { che as cheApi } from '@eclipse-che/api';
import { getAttribute } from '../utils';

/** Converts the Che command to Theia Task Configuration */
export function toTaskConfiguration(command: cheApi.workspace.Command): TaskConfiguration {
export function toTaskConfiguration(command: che.devfile.DevfileCommand): TaskConfiguration {
const taskConfig: TaskConfiguration = {
type: CHE_TASK_TYPE,
label: command.name!,
command: command.commandLine,
label: command.id,
command: command.exec?.commandLine,
_scope: '', // not to put into tasks.json
target: {
workingDir: getAttribute(WORKING_DIR_ATTRIBUTE, command.attributes),
component: getAttribute(COMPONENT_ALIAS_ATTRIBUTE, command.attributes),
workingDir: command.exec?.workingDir,
component: command.exec?.component,
},
previewUrl: getAttribute(PREVIEW_URL_ATTRIBUTE, command.attributes),
problemMatcher: [],
Expand All @@ -38,24 +33,7 @@ export function toTaskConfiguration(command: cheApi.workspace.Command): TaskConf
return taskConfig;
}

/** Converts the Che command to Task API object */
export function toTask(command: cheApi.workspace.Command): Task {
return {
definition: {
type: CHE_TASK_TYPE,
command: command.commandLine,
target: {
workingDir: getAttribute(WORKING_DIR_ATTRIBUTE, command.attributes),
component: getAttribute(COMPONENT_ALIAS_ATTRIBUTE, command.attributes),
},
previewUrl: getAttribute(PREVIEW_URL_ATTRIBUTE, command.attributes),
},
name: `${command.name}`,
source: CHE_TASK_TYPE,
};
}

export function getCommandAttribute(command: cheApi.workspace.Command, attrName: string): string | undefined {
export function getCommandAttribute(command: che.devfile.DevfileCommand, attrName: string): string | undefined {
if (!command.attributes) {
return undefined;
}
Expand Down
2 changes: 0 additions & 2 deletions plugins/task-plugin/src/task/task-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { TaskDefinition } from '@theia/plugin';
export const CHE_TASK_TYPE: string = 'che';
export const MACHINE_NAME_ATTRIBUTE: string = 'machineName';
export const PREVIEW_URL_ATTRIBUTE: string = 'previewUrl';
export const WORKING_DIR_ATTRIBUTE: string = 'workingDir';
export const COMPONENT_ALIAS_ATTRIBUTE: string = 'componentAlias';

export interface CheTaskDefinition extends TaskDefinition {
readonly target?: Target;
Expand Down

0 comments on commit 936b1c4

Please sign in to comment.