mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
139 lines
4.2 KiB
TypeScript
139 lines
4.2 KiB
TypeScript
/**
|
|
* Copyright (c) 2021 OpenLens Authors
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
* the Software without restriction, including without limitation the rights to
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
* subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
import type { InstalledExtension } from "./extension-discovery/extension-discovery";
|
|
import { action, observable, makeObservable, computed } from "mobx";
|
|
import { FilesystemProvisionerStore } from "../main/extension-filesystem";
|
|
import logger from "../main/logger";
|
|
import type { ProtocolHandlerRegistration } from "./registries";
|
|
import type { PackageJson } from "type-fest";
|
|
import { Disposer, disposer } from "../common/utils";
|
|
|
|
export type LensExtensionId = string; // path to manifest (package.json)
|
|
export type LensExtensionConstructor = new (...args: ConstructorParameters<typeof LensExtension>) => LensExtension;
|
|
|
|
export interface LensExtensionManifest extends PackageJson {
|
|
name: string;
|
|
version: string;
|
|
main?: string; // path to %ext/dist/main.js
|
|
renderer?: string; // path to %ext/dist/renderer.js
|
|
}
|
|
|
|
export const Disposers = Symbol();
|
|
|
|
export class LensExtension {
|
|
readonly id: LensExtensionId;
|
|
readonly manifest: LensExtensionManifest;
|
|
readonly manifestPath: string;
|
|
readonly isBundled: boolean;
|
|
|
|
protocolHandlers: ProtocolHandlerRegistration[] = [];
|
|
|
|
@observable private _isEnabled = false;
|
|
|
|
@computed get isEnabled() {
|
|
return this._isEnabled;
|
|
}
|
|
|
|
[Disposers] = disposer();
|
|
|
|
constructor({ id, manifest, manifestPath, isBundled }: InstalledExtension) {
|
|
makeObservable(this);
|
|
this.id = id;
|
|
this.manifest = manifest;
|
|
this.manifestPath = manifestPath;
|
|
this.isBundled = !!isBundled;
|
|
}
|
|
|
|
get name() {
|
|
return this.manifest.name;
|
|
}
|
|
|
|
get version() {
|
|
return this.manifest.version;
|
|
}
|
|
|
|
get description() {
|
|
return this.manifest.description;
|
|
}
|
|
|
|
/**
|
|
* getExtensionFileFolder returns the path to an already created folder. This
|
|
* folder is for the sole use of this extension.
|
|
*
|
|
* Note: there is no security done on this folder, only obfuscation of the
|
|
* folder name.
|
|
*/
|
|
async getExtensionFileFolder(): Promise<string> {
|
|
return FilesystemProvisionerStore.getInstance().requestDirectory(this.id);
|
|
}
|
|
|
|
@action
|
|
async enable(register: (ext: LensExtension) => Promise<Disposer[]>) {
|
|
if (this._isEnabled) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await this.onActivate();
|
|
this._isEnabled = true;
|
|
|
|
this[Disposers].push(...await register(this));
|
|
logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`);
|
|
} catch (error) {
|
|
logger.error(`[EXTENSION]: failed to activate ${this.name}@${this.version}: ${error}`);
|
|
}
|
|
}
|
|
|
|
@action
|
|
async disable() {
|
|
if (!this._isEnabled) {
|
|
return;
|
|
}
|
|
|
|
this._isEnabled = false;
|
|
|
|
try {
|
|
await this.onDeactivate();
|
|
this[Disposers]();
|
|
logger.info(`[EXTENSION]: disabled ${this.name}@${this.version}`);
|
|
} catch (error) {
|
|
logger.error(`[EXTENSION]: disabling ${this.name}@${this.version} threw an error: ${error}`);
|
|
}
|
|
}
|
|
|
|
protected onActivate(): Promise<void> | void {
|
|
return;
|
|
}
|
|
|
|
protected onDeactivate(): Promise<void> | void {
|
|
return;
|
|
}
|
|
}
|
|
|
|
export function sanitizeExtensionName(name: string) {
|
|
return name.replace("@", "").replace("/", "--");
|
|
}
|
|
|
|
export function extensionDisplayName(name: string, version: string) {
|
|
return `${name}@${version}`;
|
|
}
|