Skip to content

Commit

Permalink
[plugin] parametrize plugin model scanning
Browse files Browse the repository at this point in the history
Un order to trim unnecessary data, i.e.:
- don't send contributes to the plugin host
- don't send extensionDependencies to the frontend

Signed-off-by: Anton Kosiakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Sep 25, 2019
1 parent 99a8b90 commit e9acc49
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-dev/src/node/hosted-plugin-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class HostedPluginReader implements BackendApplicationContribution {
protected deployerHandler: HostedPluginDeployerHandler;

async initialize(): Promise<void> {
this.pluginReader.doGetPluginMetadata(process.env.HOSTED_PLUGIN)
this.pluginReader.doGetPluginMetadata(process.env.HOSTED_PLUGIN, {})
.then(this.hostedPlugin.resolve.bind(this.hostedPlugin));

const pluginPath = process.env.HOSTED_PLUGIN;
Expand Down
14 changes: 9 additions & 5 deletions packages/plugin-ext-vscode/src/node/scanner-vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable } from 'inversify';
import { PluginScanner, PluginEngine, PluginPackage, PluginModel, PluginLifecycle } from '@theia/plugin-ext';
import { PluginScanner, PluginEngine, PluginPackage, PluginModel, PluginLifecycle, PluginModelOptions } from '@theia/plugin-ext';
import { TheiaPluginScanner } from '@theia/plugin-ext/lib/hosted/node/scanners/scanner-theia';

@injectable()
Expand All @@ -27,7 +27,7 @@ export class VsCodePluginScanner extends TheiaPluginScanner implements PluginSca
return this.VSCODE_TYPE;
}

getModel(plugin: PluginPackage): PluginModel {
getModel(plugin: PluginPackage, options: PluginModelOptions): PluginModel {
// translate vscode builtins, as they are published with a prefix. See https://github.com/theia-ide/vscode-builtin-extensions/blob/master/src/republish.js#L50
const built_prefix = '@theia/vscode-builtin-';
if (plugin && plugin.name && plugin.name.startsWith(built_prefix)) {
Expand All @@ -47,10 +47,14 @@ export class VsCodePluginScanner extends TheiaPluginScanner implements PluginSca
},
entryPoint: {
backend: plugin.main
},
extensionDependencies: this.getDeployableDependencies(plugin.extensionDependencies || [])
}
};
result.contributes = this.readContributions(plugin);
if (options.contributions) {
result.contributes = this.readContributions(plugin);
}
if (options.dependencies) {
result.extensionDependencies = this.getDeployableDependencies(plugin.extensionDependencies || []);
}
return result;
}

Expand Down
17 changes: 15 additions & 2 deletions packages/plugin-ext/src/common/plugin-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ export interface PluginProblemPatternContribution extends ProblemPatternContribu
name: string;
}

export interface PluginModelOptions {
/** whether to load contributions info */
contributions?: boolean
/** whether to load dependencies info */
dependencies?: boolean
}

export const PluginScanner = Symbol('PluginScanner');
/**
* This scanner process package.json object and returns plugin metadata objects.
Expand All @@ -220,7 +227,7 @@ export interface PluginScanner {
* @param {PluginPackage} plugin
* @returns {PluginModel}
*/
getModel(plugin: PluginPackage): PluginModel;
getModel(plugin: PluginPackage, options: PluginModelOptions): PluginModel;

/**
* Creates plugin's lifecycle.
Expand Down Expand Up @@ -378,12 +385,18 @@ export interface PluginModel {
frontend?: string;
backend?: string;
};

// frontend contributions info -- start
contributes?: PluginContribution;
// frontend contributions info -- end

// backend dependencies info -- start
/**
* The deployable form of extensionDependencies from package.json,
* i.e. not `publisher.name`, but `vscode:extension/publisher.name`.
*/
extensionDependencies?: string[];
// backend dependencies info -- end
}

/**
Expand Down Expand Up @@ -615,7 +628,7 @@ export interface PluginDeployerHandler {
deployFrontendPlugins(frontendPlugins: PluginDeployerEntry[]): Promise<void>;
deployBackendPlugins(backendPlugins: PluginDeployerEntry[]): Promise<void>;

getPluginMetadata(pluginToBeInstalled: PluginDeployerEntry): Promise<PluginMetadata | undefined>
getPluginMetadata(pluginToBeInstalled: PluginDeployerEntry, options: PluginModelOptions): Promise<PluginMetadata | undefined>
}

export interface GetDeployedPluginsParams {
Expand Down
12 changes: 11 additions & 1 deletion packages/plugin-ext/src/hosted/browser/hosted-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,17 @@ export class HostedPluginSupport {
if (!manager) {
return;
}
const plugins = hostContributions.map(contributions => contributions.plugin);
const plugins = hostContributions.map(contributions => {
const metadata = {
...contributions.plugin,
model: {
...contributions.plugin.model
}
};
// remove contributions info in order to reduce size of JSON-RPC messages
delete metadata.model.contributes;
return metadata;
});
thenable.push((async () => {
try {
const activationEvents = [...this.activationEvents];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

import { TheiaPluginScanner } from '../node/scanners/scanner-theia';
import { injectable } from 'inversify';
import { PluginPackage, PluginModel } from '../../common/plugin-protocol';
import { PluginPackage, PluginModel, PluginModelOptions } from '../../common/plugin-protocol';
@injectable()
export class TheiaPluginScannerElectron extends TheiaPluginScanner {
getModel(plugin: PluginPackage): PluginModel {
const result = super.getModel(plugin);
getModel(plugin: PluginPackage, options: PluginModelOptions): PluginModel {
const result = super.getModel(plugin, options);
if (result.entryPoint.frontend) {
result.entryPoint.frontend = plugin.packagePath + result.entryPoint.frontend;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { injectable, inject } from 'inversify';
import { ILogger } from '@theia/core';
import { PluginDeployerHandler, PluginDeployerEntry, PluginMetadata } from '../../common/plugin-protocol';
import { PluginDeployerHandler, PluginDeployerEntry, PluginMetadata, PluginModelOptions } from '../../common/plugin-protocol';
import { HostedPluginReader } from './plugin-reader';
import { Deferred } from '@theia/core/lib/common/promise-util';

Expand Down Expand Up @@ -65,13 +65,13 @@ export class HostedPluginDeployerHandler implements PluginDeployerHandler {
return this.currentFrontendPluginsMetadata.get(pluginId);
}

getPluginMetadata(plugin: PluginDeployerEntry): Promise<PluginMetadata | undefined> {
return this.reader.getPluginMetadata(plugin.path());
getPluginMetadata(plugin: PluginDeployerEntry, options: PluginModelOptions): Promise<PluginMetadata | undefined> {
return this.reader.getPluginMetadata(plugin.path(), options);
}

async deployFrontendPlugins(frontendPlugins: PluginDeployerEntry[]): Promise<void> {
for (const plugin of frontendPlugins) {
const metadata = await this.reader.getPluginMetadata(plugin.path());
const metadata = await this.getPluginMetadata(plugin, { contributions: true });
if (metadata) {
if (this.currentFrontendPluginsMetadata.has(metadata.model.id)) {
continue;
Expand All @@ -88,7 +88,7 @@ export class HostedPluginDeployerHandler implements PluginDeployerHandler {

async deployBackendPlugins(backendPlugins: PluginDeployerEntry[]): Promise<void> {
for (const plugin of backendPlugins) {
const metadata = await this.reader.getPluginMetadata(plugin.path());
const metadata = await this.reader.getPluginMetadata(plugin.path(), { contributions: true });
if (metadata) {
if (this.currentBackendPluginsMetadata.has(metadata.model.id)) {
continue;
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-ext/src/hosted/node/metadata-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, multiInject } from 'inversify';
import { PluginPackage, PluginScanner, PluginMetadata } from '../../common/plugin-protocol';
import { PluginPackage, PluginScanner, PluginMetadata, PluginModelOptions } from '../../common/plugin-protocol';

@injectable()
export class MetadataScanner {
Expand All @@ -29,12 +29,12 @@ export class MetadataScanner {
});
}

getPluginMetadata(plugin: PluginPackage): PluginMetadata {
getPluginMetadata(plugin: PluginPackage, options: PluginModelOptions): PluginMetadata {
const scanner = this.getScanner(plugin);
return {
host: 'main',
source: plugin,
model: scanner.getModel(plugin),
model: scanner.getModel(plugin, options),
lifecycle: scanner.getLifecycle(plugin)
};
}
Expand Down
14 changes: 7 additions & 7 deletions packages/plugin-ext/src/hosted/node/plugin-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as escape_html from 'escape-html';
import { ILogger } from '@theia/core';
import { inject, injectable, optional, multiInject } from 'inversify';
import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application';
import { PluginMetadata, getPluginId, MetadataProcessor } from '../../common/plugin-protocol';
import { PluginMetadata, getPluginId, MetadataProcessor, PluginModelOptions } from '../../common/plugin-protocol';
import { MetadataScanner } from './metadata-scanner';

@injectable()
Expand Down Expand Up @@ -77,33 +77,33 @@ export class HostedPluginReader implements BackendApplicationContribution {
res.status(404).send(`The plugin with id '${escape_html(pluginId)}' does not exist.`);
}

async getPluginMetadata(pluginPath: string): Promise<PluginMetadata | undefined> {
return this.doGetPluginMetadata(pluginPath);
async getPluginMetadata(pluginPath: string, options: PluginModelOptions): Promise<PluginMetadata | undefined> {
return this.doGetPluginMetadata(pluginPath, options);
}

/**
* MUST never throw to isolate plugin deployment
*/
async doGetPluginMetadata(pluginPath: string | undefined): Promise<PluginMetadata | undefined> {
async doGetPluginMetadata(pluginPath: string | undefined, options: PluginModelOptions): Promise<PluginMetadata | undefined> {
try {
if (!pluginPath) {
return undefined;
}
pluginPath = path.normalize(pluginPath + '/');
return await this.loadPluginMetadata(pluginPath);
return await this.loadPluginMetadata(pluginPath, options);
} catch (e) {
this.logger.error(`Failed to load plugin metadata from "${pluginPath}"`, e);
return undefined;
}
}

protected async loadPluginMetadata(pluginPath: string): Promise<PluginMetadata | undefined> {
protected async loadPluginMetadata(pluginPath: string, options: PluginModelOptions): Promise<PluginMetadata | undefined> {
const manifest = await this.loadManifest(pluginPath);
if (!manifest) {
return undefined;
}
manifest.packagePath = pluginPath;
const pluginMetadata = this.scanner.getPluginMetadata(manifest);
const pluginMetadata = this.scanner.getPluginMetadata(manifest, options);
if (pluginMetadata.model.entryPoint.backend) {
pluginMetadata.model.entryPoint.backend = path.resolve(pluginPath, pluginMetadata.model.entryPoint.backend);
}
Expand Down
15 changes: 10 additions & 5 deletions packages/plugin-ext/src/hosted/node/scanners/scanner-theia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import {
SnippetContribution,
PluginPackageCommand,
PluginCommand,
IconUrl
IconUrl,
PluginModelOptions
} from '../../../common/plugin-protocol';
import * as fs from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -82,7 +83,7 @@ export class TheiaPluginScanner implements PluginScanner {
return this._apiType;
}

getModel(plugin: PluginPackage): PluginModel {
getModel(plugin: PluginPackage, options: PluginModelOptions): PluginModel {
const result: PluginModel = {
// 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()}`,
Expand All @@ -98,10 +99,14 @@ export class TheiaPluginScanner implements PluginScanner {
entryPoint: {
frontend: plugin.theiaPlugin!.frontend,
backend: plugin.theiaPlugin!.backend
},
extensionDependencies: plugin.extensionDependencies || []
}
};
result.contributes = this.readContributions(plugin);
if (options.contributions) {
result.contributes = this.readContributions(plugin);
}
if (options.dependencies) {
// skip it since there is no way to load transitive dependencies for Theia plugins yet
}
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-ext/src/main/node/plugin-deployer-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export class PluginDeployerImpl implements PluginDeployer {
await this.applyFileHandlers(pluginDeployerEntries);
await this.applyDirectoryFileHandlers(pluginDeployerEntries);
for (const deployerEntry of pluginDeployerEntries) {
const metadata = await this.pluginDeployerHandler.getPluginMetadata(deployerEntry);
const metadata = await this.pluginDeployerHandler.getPluginMetadata(deployerEntry, {
dependencies: true
});
if (metadata && !pluginsToDeploy.has(metadata.model.id)) {
pluginsToDeploy.set(metadata.model.id, deployerEntry);
chunk.push(metadata);
Expand Down

0 comments on commit e9acc49

Please sign in to comment.