From 01ff09a0645c0d1b0248d6a7c3a25b051d5607cc Mon Sep 17 00:00:00 2001 From: Florent Benoit Date: Mon, 15 Feb 2021 16:45:53 +0100 Subject: [PATCH] feat(namespace): Export @eclipse-che/plugin namespace using VS Code extension mechanism Change-Id: I4d8d6c49b6797bd98b76486127ec3e38f243e120 Signed-off-by: Florent Benoit --- che-theia-init-sources.yml | 1 + plugins/ext-plugin/.gitignore | 4 ++ plugins/ext-plugin/README.md | 15 +++++ .../__mocks__/@eclipse-che/plugin.ts | 28 +++++++++ plugins/ext-plugin/package.json | 62 +++++++++++++++++++ plugins/ext-plugin/src/ext-plugin.ts | 18 ++++++ plugins/ext-plugin/tests/ext-plugin.spec.ts | 25 ++++++++ plugins/ext-plugin/tsconfig.json | 19 ++++++ plugins/workspace-plugin/package.json | 3 +- 9 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 plugins/ext-plugin/.gitignore create mode 100644 plugins/ext-plugin/README.md create mode 100644 plugins/ext-plugin/__mocks__/@eclipse-che/plugin.ts create mode 100644 plugins/ext-plugin/package.json create mode 100644 plugins/ext-plugin/src/ext-plugin.ts create mode 100644 plugins/ext-plugin/tests/ext-plugin.spec.ts create mode 100644 plugins/ext-plugin/tsconfig.json diff --git a/che-theia-init-sources.yml b/che-theia-init-sources.yml index bbf7fcab0..c32775cdf 100644 --- a/che-theia-init-sources.yml +++ b/che-theia-init-sources.yml @@ -22,6 +22,7 @@ sources: - extensions/eclipse-che-theia-remote-impl-che-server plugins: - plugins/containers-plugin + - plugins/ext-plugin - plugins/workspace-plugin - plugins/resource-monitor-plugin - plugins/ports-plugin diff --git a/plugins/ext-plugin/.gitignore b/plugins/ext-plugin/.gitignore new file mode 100644 index 000000000..2b8cac984 --- /dev/null +++ b/plugins/ext-plugin/.gitignore @@ -0,0 +1,4 @@ +lib/ +node_modules/ +*.theia +coverage diff --git a/plugins/ext-plugin/README.md b/plugins/ext-plugin/README.md new file mode 100644 index 000000000..1009f43d9 --- /dev/null +++ b/plugins/ext-plugin/README.md @@ -0,0 +1,15 @@ +# Ext Plug-in +This plug-in is exposing some Eclipse Che API to be consumed by other VS Code extensions using the VS Code extension mechanism. + +## Example + +```typescript +const eclipseCheExtPlugin = vscode.extensions.getExtension('@eclipse-che.ext-plugin'); +if (eclipseCheExtPlugin) { + // grab user + const user = await eclipseCheExtPlugin.exports.user.getCurrentUser(); + vscode.window.showInformationMessage(`Eclipse Che user information: id ${user.id} with name ${user.name}`); +} +``` + +Exported code is coming from https://github.com/eclipse/che-theia/blob/master/extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts diff --git a/plugins/ext-plugin/__mocks__/@eclipse-che/plugin.ts b/plugins/ext-plugin/__mocks__/@eclipse-che/plugin.ts new file mode 100644 index 000000000..21dd52857 --- /dev/null +++ b/plugins/ext-plugin/__mocks__/@eclipse-che/plugin.ts @@ -0,0 +1,28 @@ +/********************************************************************** + * Copyright (c) 2021 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 + ***********************************************************************/ + +/** + * Mock of @eclipse-che/plugin module + * @author Florent Benoit + */ +const che: any = {}; +let currentWorkspace: any = undefined; + +che.setWorkspaceOutput = (input: any) => { + currentWorkspace = input; +}; + +che.workspace = {}; + +che.workspace.getCurrentWorkspace = () => { + return currentWorkspace; +}; + +module.exports = che; diff --git a/plugins/ext-plugin/package.json b/plugins/ext-plugin/package.json new file mode 100644 index 000000000..dd1c26c4d --- /dev/null +++ b/plugins/ext-plugin/package.json @@ -0,0 +1,62 @@ +{ + "name": "ext-plugin", + "publisher": "@eclipse-che", + "version": "0.0.1", + "keywords": [ + "theia-plugin" + ], + "description": "Exports @eclipse-che/plugin namespace", + "license": "EPL-2.0", + "files": [ + "src" + ], + "activationEvents": [ + "*" + ], + "dependencies": {}, + "devDependencies": { + "@eclipse-che/plugin": "latest", + "@theia/plugin": "next", + "@theia/plugin-packager": "latest" + }, + "scripts": { + "prepare": "yarn clean && yarn build && yarn lint:fix && yarn test", + "clean": "rimraf lib", + "format": "if-env SKIP_FORMAT=true && echo 'skip format check' || prettier --check '{src,tests}/**/*.ts' package.json", + "format:fix": "prettier --write '{src,tests}/**/*.ts' package.json", + "lint": "if-env SKIP_LINT=true && echo 'skip lint check' || eslint --cache=true --no-error-on-unmatched-pattern=true '{src,tests}/**/*.ts'", + "lint:fix": "eslint --fix --cache=true --no-error-on-unmatched-pattern=true \"{src,tests}/**/*.{ts,tsx}\"", + "compile": "tsc", + "build": "concurrently -n \"format,lint,compile\" -c \"red,green,blue\" \"yarn format\" \"yarn lint\" \"yarn compile\" && theia-plugin pack", + "watch": "tsc -w", + "test": "if-env SKIP_TEST=true && echo 'skip test' || jest --forceExit", + "test-watch": "jest --watchAll" + }, + "engines": { + "theiaPlugin": "next" + }, + "theiaPlugin": { + "backend": "lib/ext-plugin.js" + }, + "jest": { + "collectCoverage": true, + "collectCoverageFrom": [ + "src/**/*.ts" + ], + "coverageDirectory": "./coverage", + "transform": { + "^.+\\.tsx?$": "ts-jest" + }, + "modulePathIgnorePatterns": [ + "/dist" + ], + "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", + "moduleFileExtensions": [ + "ts", + "tsx", + "js", + "jsx", + "json" + ] + } +} diff --git a/plugins/ext-plugin/src/ext-plugin.ts b/plugins/ext-plugin/src/ext-plugin.ts new file mode 100644 index 000000000..26678cca4 --- /dev/null +++ b/plugins/ext-plugin/src/ext-plugin.ts @@ -0,0 +1,18 @@ +/********************************************************************** + * Copyright (c) 2021 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 * as che from '@eclipse-che/plugin'; +/** + * Export @eclipse-che/plugin namespace as extensions.getExtension('@eclipse-che/ext-plugin') value + */ +export async function start(): Promise { + const apiObject = che; + return apiObject; +} diff --git a/plugins/ext-plugin/tests/ext-plugin.spec.ts b/plugins/ext-plugin/tests/ext-plugin.spec.ts new file mode 100644 index 000000000..b4fa2a72a --- /dev/null +++ b/plugins/ext-plugin/tests/ext-plugin.spec.ts @@ -0,0 +1,25 @@ +/********************************************************************** + * Copyright (c) 2021 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 + ***********************************************************************/ + +/* eslint-disable @typescript-eslint/no-explicit-any */ + +import * as che from '@eclipse-che/plugin'; +import * as extPlugin from '../src/ext-plugin'; + +describe('Test ExtPlugin', () => { + test('start', async () => { + (che as any).setWorkspaceOutput({ id: '1234' }); + + const api: any = await extPlugin.start(); + expect(api).toBeDefined(); + const workspace = await api.workspace.getCurrentWorkspace(); + expect(workspace.id).toBe('1234'); + }); +}); diff --git a/plugins/ext-plugin/tsconfig.json b/plugins/ext-plugin/tsconfig.json new file mode 100644 index 000000000..c5a4f3c5a --- /dev/null +++ b/plugins/ext-plugin/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "../../configs/base.tsconfig", + "compilerOptions": { + "target": "es5", + "lib": [ + "es6", + "webworker" + ], + "sourceMap": true, + "rootDir": "src", + "outDir": "lib", + "types": [ + "node", "jest" + ] + }, + "include": [ + "src" + ] +} diff --git a/plugins/workspace-plugin/package.json b/plugins/workspace-plugin/package.json index d9a1020f9..dd7c7dbdf 100644 --- a/plugins/workspace-plugin/package.json +++ b/plugins/workspace-plugin/package.json @@ -19,7 +19,8 @@ "@theia/plugin-packager": "latest" }, "extensionDependencies": [ - "Eclipse Che.@eclipse-che/theia-ssh-plugin" + "Eclipse Che.@eclipse-che/theia-ssh-plugin", + "@eclipse-che.ext-plugin" ], "scripts": { "prepare": "yarn clean && yarn lint:fix && yarn build && yarn test",