From 126789d49a3efe5c8d2de4ff923c4b0f46debd40 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 24 Feb 2020 00:12:32 +0800 Subject: [PATCH] feat: add config decorator bate Signed-off-by: MiaoWoo --- packages/plugin/src/constants.ts | 1 + packages/plugin/src/decorators.ts | 23 ++++++++++++++++++++++- packages/plugin/src/interfaces.ts | 3 +++ packages/plugin/src/manager.ts | 11 +++++++++-- packages/plugin/src/utils.ts | 9 +++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/plugin/src/constants.ts b/packages/plugin/src/constants.ts index 58b02b1c..d68b2fbe 100644 --- a/packages/plugin/src/constants.ts +++ b/packages/plugin/src/constants.ts @@ -3,4 +3,5 @@ export const METADATA_KEY = { cmd: "@ms/plugin:cmd", tab: "@ms/plugin:tab", listener: "@ms/plugin:listener", + config: "@ms/plugin:config", }; diff --git a/packages/plugin/src/decorators.ts b/packages/plugin/src/decorators.ts index 0c7054d5..00524f65 100644 --- a/packages/plugin/src/decorators.ts +++ b/packages/plugin/src/decorators.ts @@ -1,7 +1,7 @@ import { injectable, decorate } from "@ms/container"; import { interfaces } from './interfaces' import { METADATA_KEY } from './constants' -import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata } from './utils' +import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata, getPluginConfigMetadata } from './utils' /** * MiaoScript plugin @@ -24,6 +24,7 @@ export function plugin(metadata: interfaces.PluginMetadata) { */ export function cmd(metadata: interfaces.CommandMetadata = {}) { return function(target: any, key: string, value: any) { + checkFunction("command", target[key]); metadata.name = metadata.name || key; metadata.executor = key; metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key) @@ -39,6 +40,7 @@ export function cmd(metadata: interfaces.CommandMetadata = {}) { */ export function tab(metadata: interfaces.TabCompleterMetadata = {}) { return function(target: any, key: string, value: any) { + checkFunction("tab", target[key]); metadata.name = metadata.name || (key.startsWith('tab') ? key.split('tab', 2)[1] : key); if (!metadata.name) { return; } metadata.executor = key; @@ -55,9 +57,28 @@ export function tab(metadata: interfaces.TabCompleterMetadata = {}) { */ export function listener(metadata: interfaces.ListenerMetadata = {}) { return function(target: any, key: string, value: any) { + checkFunction("listener", target[key]); metadata.name = metadata.name || key; metadata.executor = key; const previousMetadata: interfaces.ListenerMetadata[] = getPluginListenerMetadata(target) Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...previousMetadata], target.constructor); }; } + +export function config(metadata: interfaces.ConfigMetadata = { version: 1 }) { + return function(target: any, key: string, value: any) { + if (typeof value === "function") { + throw new Error(`decorate config must config at prototype but is a ${typeof value}!`) + } + metadata.name = metadata.name || key; + const previousMetadata: Map = getPluginConfigMetadata(target) + previousMetadata.set(metadata.name, metadata); + Reflect.defineMetadata(METADATA_KEY.config, previousMetadata, target.constructor); + } +} + +function checkFunction(type: string, value: any) { + if (typeof value !== "function") { + throw new Error(`decorate ${type} must config at function but is a ${typeof value}!`) + } +} \ No newline at end of file diff --git a/packages/plugin/src/interfaces.ts b/packages/plugin/src/interfaces.ts index 93f1d176..92650687 100644 --- a/packages/plugin/src/interfaces.ts +++ b/packages/plugin/src/interfaces.ts @@ -60,5 +60,8 @@ export namespace interfaces { } export interface ListenerMetadata extends ExecMetadata { } + export interface ConfigMetadata extends BaseMetadata { + version?: number; + } export type PluginLike = Plugin | string; } diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index c7c42174..73cb268d 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -2,7 +2,7 @@ import { plugin, server, command, event } from '@ms/api' import { injectable, inject, postConstruct, Container, ContainerInstance } from '@ms/container' import * as fs from '@ms/common/dist/fs' -import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata } from './utils' +import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPlugin, getPluginTabCompleterMetadata, getPluginConfigMetadata } from './utils' import { interfaces } from './interfaces' @injectable() @@ -53,6 +53,7 @@ export class PluginManagerImpl implements plugin.PluginManager { this.logStage(plugin, "Loading") this.runCatch(plugin, 'load') this.runCatch(plugin, `${this.serverType}load`) + this.loadConfig(plugin) }) } @@ -110,7 +111,8 @@ export class PluginManagerImpl implements plugin.PluginManager { var files = [] console.info(`Scanning Plugins in ${plugin} ...`) this.checkUpdateFolder(plugin) - fs.list(plugin).forEach((file: any) => files.push(file.toFile())) + // must check file is exist maybe is a illegal symbolic link file + fs.list(plugin).forEach((file: any) => file.toFile().exists() ? files.push(file.toFile()) : void 0) return files } @@ -195,6 +197,11 @@ export class PluginManagerImpl implements plugin.PluginManager { } } + private loadConfig(pluginInstance: interfaces.Plugin) { + let configs = getPluginConfigMetadata(pluginInstance); + + } + private registryCommand(pluginInstance: interfaces.Plugin) { let cmds = getPluginCommandMetadata(pluginInstance) let tabs = getPluginTabCompleterMetadata(pluginInstance) diff --git a/packages/plugin/src/utils.ts b/packages/plugin/src/utils.ts index 1a463bce..3dbd6f1b 100644 --- a/packages/plugin/src/utils.ts +++ b/packages/plugin/src/utils.ts @@ -49,6 +49,14 @@ function getPluginListenerMetadata(target: any) { return listnerMetadata; } +function getPluginConfigMetadata(target: any) { + let configMetadata: Map = Reflect.getMetadata( + METADATA_KEY.config, + target.constructor + ) || new Map(); + return configMetadata; +} + export { getPlugin, getPlugins, @@ -57,4 +65,5 @@ export { getPluginCommandMetadata, getPluginTabCompleterMetadata, getPluginListenerMetadata, + getPluginConfigMetadata }