Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract plugin uri generation to injectable class #9027

Merged
merged 1 commit into from
Feb 9, 2021
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
5 changes: 2 additions & 3 deletions packages/plugin-ext-vscode/src/node/scanner-vscode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2015-2018 Red Hat, Inc.
* Copyright (C) 2015-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -17,7 +17,6 @@
import { injectable } from 'inversify';
import { PluginScanner, PluginEngine, PluginPackage, PluginModel, PluginLifecycle } from '@theia/plugin-ext';
import { TheiaPluginScanner } from '@theia/plugin-ext/lib/hosted/node/scanners/scanner-theia';
import { FileUri } from '@theia/core/lib/node/file-uri';

@injectable()
export class VsCodePluginScanner extends TheiaPluginScanner implements PluginScanner {
Expand All @@ -33,7 +32,7 @@ export class VsCodePluginScanner extends TheiaPluginScanner implements PluginSca
const publisher = plugin.publisher || '';
const result: PluginModel = {
packagePath: plugin.packagePath,
packageUri: FileUri.create(plugin.packagePath).toString(),
packageUri: this.pluginUriFactory.createUri(plugin).toString(),
// see id definition: https://github.com/microsoft/vscode/blob/15916055fe0cb9411a5f36119b3b012458fe0a1d/src/vs/platform/extensions/common/extensions.ts#L167-L169
id: `${publisher.toLowerCase()}.${plugin.name.toLowerCase()}`,
name: plugin.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
* Copyright (C) 2018-2021 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -31,6 +31,8 @@ import { HostedPluginProcess, HostedPluginProcessConfiguration } from './hosted-
import { ExtPluginApiProvider } from '../../common/plugin-ext-api-contribution';
import { HostedPluginCliContribution } from './hosted-plugin-cli-contribution';
import { HostedPluginDeployerHandler } from './hosted-plugin-deployer-handler';
import { PluginUriFactory } from './scanners/plugin-uri-factory';
import { FilePluginUriFactory } from './scanners/file-plugin-uri-factory';

const commonHostedConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService }) => {
bind(HostedPluginProcess).toSelf().inSingletonScope();
Expand Down Expand Up @@ -63,6 +65,7 @@ export function bindCommonHostedBackend(bind: interfaces.Bind): void {
bind(HostedPluginProcessConfiguration).toConstantValue({ path: path.resolve(__dirname, 'plugin-host.js') });

bind(ConnectionContainerModule).toConstantValue(commonHostedConnectionModule);
bind(PluginUriFactory).to(FilePluginUriFactory).inSingletonScope();
}

export function bindHostedBackend(bind: interfaces.Bind): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/********************************************************************************
* Copyright (C) 2021 Red Hat, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable } from 'inversify';
import * as path from 'path';
import URI from '@theia/core/lib/common/uri';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { PluginPackage } from '../../../common';
import { PluginUriFactory } from './plugin-uri-factory';
/**
* The default implementation of PluginUriFactory simply returns a File URI from the concatenated
* package path and relative path.
*/
@injectable()
export class FilePluginUriFactory implements PluginUriFactory {
createUri(pkg: PluginPackage, pkgRelativePath?: string): URI {
return FileUri.create(pkgRelativePath ? path.join(pkg.packagePath, pkgRelativePath) : pkg.packagePath);
}
}
33 changes: 33 additions & 0 deletions packages/plugin-ext/src/hosted/node/scanners/plugin-uri-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/********************************************************************************
* Copyright (C) 2021 Red Hat, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import URI from '@theia/core/lib/common/uri';
import { PluginPackage } from '../../../common';

export const PluginUriFactory = Symbol('PluginUriFactory');
/**
* Creates URIs for resources used in plugin contributions. Projects where plugin host is not located on the back-end
* machine and therefor resources cannot be loaded from the local file system in the back end can override the factory.
*/
export interface PluginUriFactory {
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
/**
* Returns a URI that allows a file to be loaded given a plugin package and a path relative to the plugin's package path
*
* @param pkg the package this the file is contained in
* @param pkgRelativePath the path of the file relative to the package path, e.g. 'resources/snippets.json'
*/
createUri(pkg: PluginPackage, pkgRelativePath?: string): URI;
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved
}
15 changes: 9 additions & 6 deletions packages/plugin-ext/src/hosted/node/scanners/scanner-theia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2015-2018 Red Hat, Inc.
* Copyright (C) 2015-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -59,7 +59,6 @@ import { CharacterPair } from '../../../common/plugin-api-rpc';
import * as jsoncparser from 'jsonc-parser';
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
import { deepClone } from '@theia/core/lib/common/objects';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { PreferenceSchema, PreferenceSchemaProperties } from '@theia/core/lib/common/preferences/preference-schema';
import { RecursivePartial } from '@theia/core/lib/common/types';
import {
Expand All @@ -69,6 +68,7 @@ import {
} from '@theia/task/lib/common/task-protocol';
import { ColorDefinition } from '@theia/core/lib/browser/color-registry';
import { ResourceLabelFormatter } from '@theia/core/lib/common/label-protocol';
import { PluginUriFactory } from './plugin-uri-factory';

namespace nls {
export function localize(key: string, _default: string): string {
Expand All @@ -92,14 +92,17 @@ export class TheiaPluginScanner implements PluginScanner {
@inject(GrammarsReader)
private readonly grammarsReader: GrammarsReader;

@inject(PluginUriFactory)
protected readonly pluginUriFactory: PluginUriFactory;

get apiType(): PluginEngine {
return this._apiType;
}

getModel(plugin: PluginPackage): PluginModel {
const result: PluginModel = {
packagePath: plugin.packagePath,
packageUri: FileUri.create(plugin.packagePath).toString(),
packageUri: this.pluginUriFactory.createUri(plugin).toString(),
// see id definition: https://github.com/microsoft/vscode/blob/15916055fe0cb9411a5f36119b3b012458fe0a1d/src/vs/platform/extensions/common/extensions.ts#L167-L169
id: `${plugin.publisher.toLowerCase()}.${plugin.name.toLowerCase()}`,
name: plugin.name,
Expand Down Expand Up @@ -400,7 +403,7 @@ export class TheiaPluginScanner implements PluginScanner {
if (contribution.path) {
result.push({
id: contribution.id,
uri: FileUri.create(path.join(pck.packagePath, contribution.path)).toString(),
uri: this.pluginUriFactory.createUri(pck, contribution.path).toString(),
description: contribution.description,
label: contribution.label,
uiTheme: contribution.uiTheme
Expand All @@ -426,7 +429,7 @@ export class TheiaPluginScanner implements PluginScanner {
}
result.push({
id: contribution.id,
uri: FileUri.create(path.join(pck.packagePath, contribution.path)).toString(),
uri: this.pluginUriFactory.createUri(pck, contribution.path).toString(),
description: contribution.description,
label: contribution.label,
uiTheme: contribution.uiTheme
Expand All @@ -445,7 +448,7 @@ export class TheiaPluginScanner implements PluginScanner {
result.push({
language: contribution.language,
source: pck.displayName || pck.name,
uri: FileUri.create(path.join(pck.packagePath, contribution.path)).toString()
uri: this.pluginUriFactory.createUri(pck, contribution.path).toString()
});
}
}
Expand Down