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

Commit

Permalink
Replicate tasks to user settings
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Mäder <[email protected]>
  • Loading branch information
tsmaeder committed May 25, 2020
1 parent 54f39d6 commit b310506
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
18 changes: 4 additions & 14 deletions plugins/task-plugin/src/export/export-configs-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import { injectable, inject, multiInject } from 'inversify';
import { CheWorkspaceClient } from '../che-workspace-client';
import * as theia from '@theia/plugin';
import { che as cheApi } from '@eclipse-che/api';

export const ConfigurationsExporter = Symbol('ConfigurationsExporter');
Expand All @@ -23,7 +22,7 @@ export interface ConfigurationsExporter {
* @param workspaceFolder workspace folder for exporting configs in the config file
* @param commands commands with configurations for export
*/
export(workspaceFolder: theia.WorkspaceFolder, commands: cheApi.workspace.Command[]): Promise<void>;
export(commands: cheApi.workspace.Command[]): 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,25 +45,16 @@ export class ExportConfigurationsManager {
protected readonly exporters: ConfigurationsExporter[];

async export(): Promise<void> {
const workspaceFolders = theia.workspace.workspaceFolders;
if (!workspaceFolders || workspaceFolders.length < 1) {
return;
}

const exportPromises = [];
const cheCommands = await this.cheWorkspaceClient.getCommands();
for (const exporter of this.exporters) {
exportPromises.push(this.doExport(workspaceFolders, cheCommands, exporter));
exportPromises.push(this.doExport(cheCommands, exporter));
}

await Promise.all(exportPromises);
}

private async doExport(workspaceFolders: theia.WorkspaceFolder[], cheCommands: cheApi.workspace.Command[], exporter: ConfigurationsExporter): Promise<void> {
const exportConfigsPromises = [];
for (const workspaceFolder of workspaceFolders) {
exportConfigsPromises.push(exporter.export(workspaceFolder, cheCommands));
}
await Promise.all(exportConfigsPromises);
private async doExport(cheCommands: cheApi.workspace.Command[], exporter: ConfigurationsExporter): Promise<void> {
return exporter.export(cheCommands);
}
}
16 changes: 14 additions & 2 deletions plugins/task-plugin/src/export/launch-configs-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ export class LaunchConfigurationsExporter implements ConfigurationsExporter {
@inject(VsCodeLaunchConfigsExtractor)
protected readonly vsCodeLaunchConfigsExtractor: VsCodeLaunchConfigsExtractor;

async export(workspaceFolder: theia.WorkspaceFolder, commands: cheApi.workspace.Command[]): Promise<void> {
async export(commands: cheApi.workspace.Command[]): Promise<void> {
if (!theia.workspace.workspaceFolders) {
return;
}

const exportConfigsPromises: Promise<void>[] = [];

for (const workspaceFolder of theia.workspace.workspaceFolders) {
exportConfigsPromises.push(this.doExport(workspaceFolder, commands));
}
await Promise.all(exportConfigsPromises);
}

async doExport(workspaceFolder: theia.WorkspaceFolder, commands: cheApi.workspace.Command[]): Promise<void> {
const launchConfigFileUri = this.getConfigFileUri(workspaceFolder.uri.path);
const configFileConfigs = this.configFileLaunchConfigsExtractor.extract(launchConfigFileUri);
const vsCodeConfigs = this.vsCodeLaunchConfigsExtractor.extract(commands);
Expand Down Expand Up @@ -67,7 +80,6 @@ export class LaunchConfigurationsExporter implements ConfigurationsExporter {

conflictHandler(conflict, config2);
}

return result;
}

Expand Down
21 changes: 10 additions & 11 deletions plugins/task-plugin/src/export/task-configs-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
**********************************************************************/

import { injectable, inject } from 'inversify';
import * as theia from '@theia/plugin';
import * as startPoint from '../task-plugin-backend';
import { che as cheApi } from '@eclipse-che/api';
import { TaskConfiguration } from '@eclipse-che/plugin';
Expand All @@ -20,13 +19,18 @@ import { VsCodeTaskConfigsExtractor } from '../extract/vscode-task-configs-extra
import { ConfigurationsExporter } from './export-configs-manager';
import { ConfigFileTasksExtractor } from '../extract/config-file-task-configs-extractor';
import { BackwardCompatibilityResolver } from '../task/backward-compatibility';
import { homedir } from 'os';

const CONFIG_DIR = '.theia';
const TASK_CONFIG_FILE = 'tasks.json';
const formattingOptions = { tabSize: 4, insertSpaces: true, eol: '' };

export const VSCODE_TASK_TYPE = 'vscode-task';

// this really should be handled through some theia API, but there is none available in plugins
// it only works on linux and only if this plugin runs in the theia container
export const THEIA_USER_TASKS_PATH = resolve(homedir(), CONFIG_DIR, TASK_CONFIG_FILE);

/** Exports configurations of tasks in the config file. */
@injectable()
export class TaskConfigurationsExporter implements ConfigurationsExporter {
Expand All @@ -43,9 +47,8 @@ export class TaskConfigurationsExporter implements ConfigurationsExporter {
@inject(BackwardCompatibilityResolver)
protected readonly backwardCompatibilityResolver: BackwardCompatibilityResolver;

async export(workspaceFolder: theia.WorkspaceFolder, commands: cheApi.workspace.Command[]): Promise<void> {
const tasksConfigFileUri = this.getConfigFileUri(workspaceFolder.uri.path);
const configFileTasks = this.configFileTasksExtractor.extract(tasksConfigFileUri);
async export(commands: cheApi.workspace.Command[]): Promise<void> {
const configFileTasks = this.configFileTasksExtractor.extract(THEIA_USER_TASKS_PATH);

const cheTasks = this.cheTaskConfigsExtractor.extract(commands);
const vsCodeTasks = this.vsCodeTaskConfigsExtractor.extract(commands);
Expand All @@ -54,18 +57,18 @@ export class TaskConfigurationsExporter implements ConfigurationsExporter {

const configFileContent = configFileTasks.content;
if (configFileContent) {
this.saveConfigs(tasksConfigFileUri, configFileContent, this.merge(configFileConfigs, devfileConfigs, this.getConsoleConflictLogger()));
this.saveConfigs(THEIA_USER_TASKS_PATH, configFileContent, this.merge(configFileConfigs, devfileConfigs, this.getConsoleConflictLogger()));
return;
}

const vsCodeTasksContent = vsCodeTasks.content;
if (vsCodeTasksContent) {
this.saveConfigs(tasksConfigFileUri, vsCodeTasksContent, devfileConfigs);
this.saveConfigs(THEIA_USER_TASKS_PATH, vsCodeTasksContent, devfileConfigs);
return;
}

if (cheTasks) {
this.saveConfigs(tasksConfigFileUri, '', cheTasks);
this.saveConfigs(THEIA_USER_TASKS_PATH, '', cheTasks);
}
}

Expand Down Expand Up @@ -103,10 +106,6 @@ export class TaskConfigurationsExporter implements ConfigurationsExporter {
return JSON.stringify(properties1) === JSON.stringify(properties2);
}

private getConfigFileUri(rootDir: string): string {
return resolve(rootDir.toString(), CONFIG_DIR, TASK_CONFIG_FILE);
}

private saveConfigs(tasksConfigFileUri: string, content: string, configurations: TaskConfiguration[]): void {
const result = modify(content, ['tasks'], configurations.map(config => Object.assign(config, { _scope: undefined })), formattingOptions);
writeFileSync(tasksConfigFileUri, result);
Expand Down

0 comments on commit b310506

Please sign in to comment.