-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
plugin-reader.ts
153 lines (136 loc) · 5.51 KB
/
plugin-reader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/********************************************************************************
* Copyright (C) 2018 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
* 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
********************************************************************************/
// tslint:disable:no-any
import * as path from 'path';
import * as fs from 'fs-extra';
import * as express from 'express';
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 { MetadataScanner } from './metadata-scanner';
@injectable()
export class HostedPluginReader implements BackendApplicationContribution {
@inject(ILogger)
protected readonly logger: ILogger;
@inject(MetadataScanner)
private readonly scanner: MetadataScanner;
@optional()
@multiInject(MetadataProcessor) private readonly metadataProcessors: MetadataProcessor[];
/**
* Map between a plugin's id and the local storage
*/
private pluginsIdsFiles: Map<string, string> = new Map();
configure(app: express.Application): void {
app.get('/hostedPlugin/:pluginId/:path(*)', (req, res) => {
const pluginId = req.params.pluginId;
const filePath = decodeURIComponent(req.params.path);
const localPath = this.pluginsIdsFiles.get(pluginId);
if (localPath) {
const fileToServe = path.join(localPath, filePath);
res.sendFile(fileToServe);
} else {
res.status(404).send("The plugin with id '" + pluginId + "' does not exist.");
}
});
}
async getPluginMetadata(pluginPath: string): Promise<PluginMetadata | undefined> {
return this.doGetPluginMetadata(pluginPath);
}
/**
* MUST never throw to isolate plugin deployment
*/
async doGetPluginMetadata(pluginPath: string | undefined) {
try {
if (!pluginPath) {
return undefined;
}
pluginPath = path.normalize(pluginPath + '/');
return await this.loadPluginMetadata(pluginPath);
} catch (e) {
this.logger.error(`Failed to load plugin metadata from "${pluginPath}"`, e);
return undefined;
}
}
protected async loadPluginMetadata(pluginPath: string): Promise<PluginMetadata | undefined> {
const manifest = await this.loadManifest(pluginPath);
if (!manifest) {
return undefined;
}
manifest.packagePath = pluginPath;
const pluginMetadata = this.scanner.getPluginMetadata(manifest);
if (pluginMetadata.model.entryPoint.backend) {
pluginMetadata.model.entryPoint.backend = path.resolve(pluginPath, pluginMetadata.model.entryPoint.backend);
}
if (pluginMetadata) {
// Add post processor
if (this.metadataProcessors) {
this.metadataProcessors.forEach(metadataProcessor => {
metadataProcessor.process(pluginMetadata);
});
}
this.pluginsIdsFiles.set(getPluginId(pluginMetadata.model), pluginPath);
}
return pluginMetadata;
}
protected async loadManifest(pluginPath: string): Promise<any> {
const [manifest, translations] = await Promise.all([
fs.readJson(path.join(pluginPath, 'package.json')),
this.loadTranslations(pluginPath)
]);
return manifest && translations && Object.keys(translations).length ?
this.localize(manifest, translations) :
manifest;
}
protected async loadTranslations(pluginPath: string): Promise<any> {
try {
return await fs.readJson(path.join(pluginPath, 'package.nls.json'));
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
return {};
}
}
protected localize(value: any, translations: {
[key: string]: string
}): any {
if (typeof value === 'string') {
const match = HostedPluginReader.NLS_REGEX.exec(value);
return match && translations[match[1]] || value;
}
if (Array.isArray(value)) {
const result = [];
for (const item of value) {
result.push(this.localize(item, translations));
}
return result;
}
if (value === null) {
return value;
}
if (typeof value === 'object') {
const result: { [key: string]: any } = {};
// tslint:disable-next-line:forin
for (const propertyName in value) {
result[propertyName] = this.localize(value[propertyName], translations);
}
return result;
}
return value;
}
static NLS_REGEX = /^%([\w\d.-]+)%$/i;
}