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

Initial implementation of the extension #1

Merged
merged 3 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.browser_modules
lib
*.log
*-app/*
!*-app/package.json
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@eclipse-che/che-theia-hosted-plugin-manager-extension",
"keywords": [
"theia-extension"
],
"version": "0.0.1",
"files": [
"lib",
"node"
],
"dependencies": {
"@theia/core": "0.3.10",
"@theia/plugin-ext": "0.3.10",
"@eclipse-che/workspace-client": "latest"
},
"devDependencies": {
"rimraf": "^2.6.2",
"typescript": "2.7.2"
},
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "rimraf lib",
"build": "tsc",
"watch": "tsc -w"
},
"theiaExtensions": [
{
"backend": "lib/node/hosted-plugin-manager-contribution-extension-backend-module"
}
]
}
69 changes: 69 additions & 0 deletions src/node/che-workspace-hosted-plugin-uri-post-processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { injectable } from "inversify";
import URI from "@theia/core/lib/common/uri";
import { HostedPluginUriPostProcessor } from "@theia/plugin-ext";
import WorkspaceClient, { IRemoteAPI, IWorkspace, IServer, IRestAPIConfig } from '@eclipse-che/workspace-client';

@injectable()
export class CheWorkspaceHostedPluginUriPostProcessor implements HostedPluginUriPostProcessor {
protected restApiClient: IRemoteAPI;

constructor() {
const restAPIConfig: IRestAPIConfig = {};
restAPIConfig.baseUrl = process.env.CHE_API;
const token = process.env.CHE_MACHINE_TOKEN;
if (token) {
restAPIConfig.headers = {};
restAPIConfig.headers['Authorization'] = token;
}
this.restApiClient = WorkspaceClient.getRestApi(restAPIConfig);
}

async processUri(uri: URI): Promise<URI> {
const hostedPluginTheiaInstanceServer = await this.getHostedPluginTheiaInnstanceServer();
if (!hostedPluginTheiaInstanceServer) {
throw new Error('No server with type "ide-dev" found.');
}

const externalUri = new URI(hostedPluginTheiaInstanceServer.url);
return externalUri;
}

/**
* Searches for server which exposes hosted Theia instance.
* The server label is the attribute "type": "ide-dev".
*/
protected async getHostedPluginTheiaInnstanceServer(): Promise<IServer | undefined> {
const workspace = await this.getCurrentWorkspace();
if (!workspace.runtime) {
throw new Error('Workspace is not running.');
}

const machines = workspace.runtime.machines;
for (let machineName in machines) {
const servers = machines[machineName].servers;
for (let serverName in servers) {
const serverAttributes = servers[serverName].attributes;
if (serverAttributes && serverAttributes['type'] === 'ide-dev') {
return servers[serverName];
}
}
}
return undefined;
}

protected async getCurrentWorkspace(): Promise<IWorkspace> {
const workspaceId = process.env.CHE_WORKSPACE_ID;
if (!workspaceId) {
throw new Error('Environment variable CHE_WORKSPACE_ID is not set.');
}
return await this.restApiClient.getById<IWorkspace>(workspaceId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { ContainerModule } from "inversify";
import { HostedPluginUriPostProcessorSymbolName } from "@theia/plugin-ext";
import { CheWorkspaceHostedPluginUriPostProcessor } from "./che-workspace-hosted-plugin-uri-post-processor";

export default new ContainerModule(bind => {
bind(Symbol.for(HostedPluginUriPostProcessorSymbolName)).to(CheWorkspaceHostedPluginUriPostProcessor);
});
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"strict": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"emitDecoratorMetadata": true,
"downlevelIteration": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"lib": [
"es6",
"dom"
],
"sourceMap": true,
"rootDir": "src",
"outDir": "lib"
},
"include": [
"src"
]
}