Skip to content
This repository has been archived by the owner on Jul 19, 2019. It is now read-only.

Commit

Permalink
Add as many 'New Terminal' commands as there are containers.
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com>
  • Loading branch information
AndrienkoAleksandr committed Dec 30, 2018
1 parent f7f9ef9 commit b75293e
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1,762 deletions.
35 changes: 18 additions & 17 deletions browser-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
"name": "browser-app",
"version": "0.0.1",
"dependencies": {
"@theia/core": "0.3.14",
"@theia/editor": "0.3.14",
"@theia/extension-manager": "0.3.14",
"@theia/filesystem": "0.3.14",
"@theia/languages": "0.3.14",
"@theia/markers": "0.3.14",
"@theia/messages": "0.3.14",
"@theia/monaco": "0.3.14",
"@theia/navigator": "0.3.14",
"@theia/preferences": "0.3.14",
"@theia/process": "0.3.14",
"@theia/typescript": "0.3.14",
"@theia/workspace": "0.3.14",
"@theia/plugin-ext": "0.3.14",
"@theia/plugin-ext-vscode": "0.3.14",
"@theia/terminal": "0.3.14",
"@theia/core": "0.3.17",
"@theia/editor": "0.3.17",
"@theia/extension-manager": "0.3.17",
"@theia/filesystem": "0.3.17",
"@theia/languages": "0.3.17",
"@theia/markers": "0.3.17",
"@theia/messages": "0.3.17",
"@theia/monaco": "0.3.17",
"@theia/navigator": "0.3.17",
"@theia/preferences": "0.3.17",
"@theia/process": "0.3.17",
"@theia/typescript": "0.3.17",
"@theia/workspace": "0.3.17",
"@theia/plugin-ext": "0.3.17",
"@theia/plugin-ext-vscode": "0.3.17",
"@theia/terminal": "0.3.17",
"@theia/file-search": "0.3.17",
"che-theia-terminal": "0.0.1"
},
"devDependencies": {
"@theia/cli": "0.3.14"
"@theia/cli": "0.3.17"
},
"scripts": {
"prepare": "theia build",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
**********************************************************************/

import { injectable, inject } from 'inversify';
import { CommandRegistry, MenuModelRegistry } from '@theia/core/lib/common';
import { CommandRegistry, MenuModelRegistry, Command } from '@theia/core/lib/common';
import { ApplicationShell, KeybindingRegistry, Key, KeyCode, KeyModifier } from '@theia/core/lib/browser';

import { TerminalQuickOpenService } from './terminal-quick-open';
Expand All @@ -18,6 +18,7 @@ import { TerminalApiEndPointProvider } from '../server-definition/terminal-proxy
import { BrowserMainMenuFactory } from '@theia/core/lib/browser/menu/browser-menu-plugin';
import { MenuBar as MenuBarWidget } from '@phosphor/widgets';
import { TerminalKeybindingContext } from './keybinding-context';
import { CHEWorkspaceService } from '../../common/workspace-service';

export const NewTerminalInSpecificContainer = {
id: 'terminal-in-specific-container:new',
Expand All @@ -39,6 +40,9 @@ export class ExecTerminalFrontendContribution extends TerminalFrontendContributi
@inject(BrowserMainMenuFactory)
protected readonly mainMenuFactory: BrowserMainMenuFactory;

@inject(CHEWorkspaceService)
protected readonly cheWorkspaceService: CHEWorkspaceService;

private readonly mainMenuId = 'theia:menubar';

async registerCommands(registry: CommandRegistry) {
Expand All @@ -49,11 +53,32 @@ export class ExecTerminalFrontendContribution extends TerminalFrontendContributi
this.terminalQuickOpen.displayListMachines();
}
});
await this.registerTerminalCommandPerContainer(registry);
} else {
super.registerCommands(registry);
}
}

private async registerTerminalCommandPerContainer(registry: CommandRegistry) {
const containers = await this.cheWorkspaceService.getMachineList();

for (const containerName in containers) {
if (containers.hasOwnProperty(containerName)) {
const termCommandPerContainer: Command = {
id: "terminal-for-" + containerName + "-container:new",
label: "New terminal for " + containerName // todo Final solution about command labels
};
registry.registerCommand(termCommandPerContainer, {
execute: async () => {
const termWidget = await this.terminalQuickOpen.newTerminalPerContainer(containerName);
this.terminalQuickOpen.activateTerminal(termWidget);
termWidget.start();
}
});
}
}
}

async registerMenus(menus: MenuModelRegistry) {
const serverUrl = <string | undefined> await this.termApiEndPointProvider();
if (serverUrl) {
Expand Down
52 changes: 26 additions & 26 deletions che-theia-terminal/src/browser/contribution/terminal-quick-open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,44 @@ export class TerminalQuickOpenService implements TerminalService {
}

async newTerminal(options: TerminalWidgetOptions): Promise<TerminalWidget> {
let machineName;
let containerName;

// todo remove rude casting when will be used theia 0.3.16.
if ((options as any).attributes) {
machineName = (options as any).attributes['CHE_MACHINE_NAME'];
containerName = (options as any).attributes['CHE_MACHINE_NAME'];
}

if (!machineName) {
machineName = await this.workspaceService.findEditorMachineName();
if (!containerName) {
containerName = await this.workspaceService.findEditorMachineName();
}

if (machineName) {
const termWidget = await this.createNewTerminal(machineName, options);
if (containerName) {
const termWidget = await this.newTerminalPerContainer(containerName, options);
return termWidget;
}

throw new Error('Unable to create new terminal widget');
}

public async newTerminalPerContainer(containerName: string, options?: TerminalWidgetOptions): Promise<TerminalWidget> {
try {
const workspaceId = <string>await this.baseEnvVariablesServer.getValue('CHE_WORKSPACE_ID').then(v => v ? v.value : undefined);
const termApiEndPoint = <string>await this.termApiEndPointProvider();

const widget = <RemoteTerminalWidget>await this.widgetManager.getOrCreateWidget(REMOTE_TERMINAL_WIDGET_FACTORY_ID, <RemoteTerminalWidgetFactoryOptions>{
created: new Date().toString(),
machineName: containerName,
workspaceId: workspaceId,
endpoint: termApiEndPoint,
...options
});
return widget;
} catch (err) {
console.error('Failed to create terminal widget. Cause: ', err);
}
throw new Error('Unable to create new terminal for machine: ' + containerName);
}

async displayListMachines() {
const items: QuickOpenItem[] = [];
const machines = await this.workspaceService.getMachineList();
Expand All @@ -69,7 +88,7 @@ export class TerminalQuickOpenService implements TerminalService {
continue;
}
items.push(new NewTerminalItem(machineName, async (newTermItemFunc) => {
const termWidget = await this.createNewTerminal(newTermItemFunc.machineName);
const termWidget = await this.newTerminalPerContainer(newTermItemFunc.machineName);
this.activateTerminal(termWidget);
termWidget.start();
}));
Expand Down Expand Up @@ -98,25 +117,6 @@ export class TerminalQuickOpenService implements TerminalService {
}
};
}

protected async createNewTerminal(machineName: string, options?: TerminalWidgetOptions): Promise<TerminalWidget> {
try {
const workspaceId = <string>await this.baseEnvVariablesServer.getValue('CHE_WORKSPACE_ID').then(v => v ? v.value : undefined);
const termApiEndPoint = <string>await this.termApiEndPointProvider();

const widget = <RemoteTerminalWidget>await this.widgetManager.getOrCreateWidget(REMOTE_TERMINAL_WIDGET_FACTORY_ID, <RemoteTerminalWidgetFactoryOptions>{
created: new Date().toString(),
machineName: machineName,
workspaceId: workspaceId,
endpoint: termApiEndPoint,
...options
});
return widget;
} catch (err) {
console.error('Failed to create terminal widget. Cause: ', err);
}
throw new Error('Unable to create new terminal for machine: ' + machineName);
}
}

export class NewTerminalItem extends QuickOpenItem {
Expand Down
Loading

0 comments on commit b75293e

Please sign in to comment.