This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export tasks and launch configurations in config files of workspace f…
…older Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
- Loading branch information
1 parent
7bccaab
commit b458583
Showing
10 changed files
with
333 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
|
||
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'); | ||
|
||
/** Exports content with configurations in the config file */ | ||
export interface ConfigurationsExporter { | ||
|
||
/** Type of the exporter corresponds to type of command which brings content with configs */ | ||
readonly type: string; | ||
|
||
/** | ||
* Exports given content with configurations in the config file of given workspace folder | ||
* @param configsContent content with configurations for export | ||
* @param workspaceFolder workspace folder for exporting configs in the config file | ||
*/ | ||
export(configsContent: string, workspaceFolder: theia.WorkspaceFolder): void; | ||
} | ||
|
||
/** Reads the commands from the current Che workspace and exports task and launch configurations in the config files. */ | ||
@injectable() | ||
export class ExportConfigurationsManager { | ||
|
||
@inject(CheWorkspaceClient) | ||
protected readonly cheWorkspaceClient: CheWorkspaceClient; | ||
|
||
@multiInject(ConfigurationsExporter) | ||
protected readonly exporters: ConfigurationsExporter[]; | ||
|
||
async export() { | ||
const workspaceFolders = theia.workspace.workspaceFolders; | ||
if (!workspaceFolders || workspaceFolders.length < 1) { | ||
return; | ||
} | ||
|
||
const cheCommands = await this.cheWorkspaceClient.getCommands(); | ||
for (const exporter of this.exporters) { | ||
const configsContent = this.extractConfigsContent(exporter.type, cheCommands); | ||
if (!configsContent) { | ||
continue; | ||
} | ||
|
||
this.exportContent(configsContent, exporter, workspaceFolders); | ||
} | ||
} | ||
|
||
private exportContent(configsContent: string, exporter: ConfigurationsExporter, workspaceFolders: theia.WorkspaceFolder[]) { | ||
for (const workspaceFolder of workspaceFolders) { | ||
exporter.export(configsContent, workspaceFolder); | ||
} | ||
} | ||
|
||
private extractConfigsContent(type: string, commands: cheApi.workspace.Command[]): string { | ||
const configCommands = commands.filter(command => command.type === type); | ||
if (configCommands.length === 0) { | ||
return ''; | ||
} | ||
|
||
if (configCommands.length > 1) { | ||
console.warn(`Found duplicate entry for type ${type}`); | ||
} | ||
|
||
const configCommand = configCommands[0]; | ||
if (!configCommand || !configCommand.attributes || !configCommand.attributes.actionReferenceContent) { | ||
return ''; | ||
} | ||
|
||
return configCommand.attributes.actionReferenceContent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
|
||
import { injectable } from 'inversify'; | ||
import * as theia from '@theia/plugin'; | ||
import { resolve } from 'path'; | ||
import { readFileSync, writeFileSync, format, modify, parse } from '../utils'; | ||
import { ConfigurationsExporter } from './export-configs-manager'; | ||
|
||
const CONFIG_DIR = '.theia'; | ||
const LAUNCH_CONFIG_FILE = 'launch.json'; | ||
const formattingOptions = { tabSize: 4, insertSpaces: true, eol: '' }; | ||
|
||
export const VSCODE_LAUNCH_TYPE = 'vscode-launch'; | ||
|
||
/** Exports content with launch configurations in the config file. */ | ||
@injectable() | ||
export class LaunchConfigurationsExporter implements ConfigurationsExporter { | ||
readonly type: string = VSCODE_LAUNCH_TYPE; | ||
|
||
export(configsContent: string, workspaceFolder: theia.WorkspaceFolder): void { | ||
const launchConfigFileUri = this.getConfigFileUri(workspaceFolder.uri.path); | ||
const existingContent = readFileSync(launchConfigFileUri); | ||
if (configsContent === existingContent) { | ||
return; | ||
} | ||
|
||
const configsJson = parse(configsContent); | ||
if (!configsJson || !configsJson.configurations) { | ||
return; | ||
} | ||
|
||
const existingJson = parse(existingContent); | ||
if (!existingJson || !existingJson.configurations) { | ||
writeFileSync(launchConfigFileUri, format(configsContent, formattingOptions)); | ||
return; | ||
} | ||
|
||
const mergedConfigs = this.merge(existingJson.configurations, configsJson.configurations); | ||
const result = modify(configsContent, ['configurations'], mergedConfigs, formattingOptions); | ||
writeFileSync(launchConfigFileUri, result); | ||
} | ||
|
||
private merge(existingConfigs: theia.DebugConfiguration[], newConfigs: theia.DebugConfiguration[]): theia.DebugConfiguration[] { | ||
const result: theia.DebugConfiguration[] = Object.assign([], newConfigs); | ||
for (const existing of existingConfigs) { | ||
if (!newConfigs.some(config => config.name === existing.name)) { | ||
result.push(existing); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private getConfigFileUri(rootDir: string): string { | ||
return resolve(rootDir.toString(), CONFIG_DIR, LAUNCH_CONFIG_FILE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
|
||
import { injectable } from 'inversify'; | ||
import * as theia from '@theia/plugin'; | ||
import { TaskConfiguration } from '@eclipse-che/plugin'; | ||
import { resolve } from 'path'; | ||
import { readFileSync, writeFileSync, format, modify, parse } from '../utils'; | ||
import { ConfigurationsExporter } from './export-configs-manager'; | ||
|
||
const CONFIG_DIR = '.theia'; | ||
const TASK_CONFIG_FILE = 'tasks.json'; | ||
const formattingOptions = { tabSize: 4, insertSpaces: true, eol: '' }; | ||
|
||
export const VSCODE_TASK_TYPE = 'vscode-task'; | ||
|
||
/** Exports configurations of tasks in the config file. */ | ||
@injectable() | ||
export class TaskConfigurationsExporter implements ConfigurationsExporter { | ||
readonly type: string = VSCODE_TASK_TYPE; | ||
|
||
export(tasksContent: string, workspaceFolder: theia.WorkspaceFolder): void { | ||
const tasksConfigFileUri = this.getConfigFileUri(workspaceFolder.uri.path); | ||
const existingContent = readFileSync(tasksConfigFileUri); | ||
if (tasksContent === existingContent) { | ||
return; | ||
} | ||
|
||
const tasksJson = parse(tasksContent); | ||
if (!tasksJson || !tasksJson.tasks) { | ||
return; | ||
} | ||
|
||
const existingJson = parse(existingContent); | ||
if (!existingJson || !existingJson.tasks) { | ||
writeFileSync(tasksConfigFileUri, format(tasksContent, formattingOptions)); | ||
return; | ||
} | ||
|
||
const mergedConfigs = this.merge(existingJson.tasks, tasksJson.tasks); | ||
const result = modify(tasksContent, ['tasks'], mergedConfigs, formattingOptions); | ||
writeFileSync(tasksConfigFileUri, result); | ||
} | ||
|
||
private merge(existingConfigs: TaskConfiguration[], newConfigs: TaskConfiguration[]): TaskConfiguration[] { | ||
const result: TaskConfiguration[] = Object.assign([], newConfigs); | ||
for (const existing of existingConfigs) { | ||
if (!newConfigs.some(config => config.label === existing.label)) { | ||
result.push(existing); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private getConfigFileUri(rootDir: string): string { | ||
return resolve(rootDir.toString(), CONFIG_DIR, TASK_CONFIG_FILE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.