feat: add config decorator bate

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-24 00:12:32 +08:00
parent 81a76af7be
commit 8d344492d9
5 changed files with 44 additions and 3 deletions

View File

@ -3,4 +3,5 @@ export const METADATA_KEY = {
cmd: "@ms/plugin:cmd",
tab: "@ms/plugin:tab",
listener: "@ms/plugin:listener",
config: "@ms/plugin:config",
};

View File

@ -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<string, interfaces.ConfigMetadata> = 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}!`)
}
}

View File

@ -60,5 +60,8 @@ export namespace interfaces {
}
export interface ListenerMetadata extends ExecMetadata {
}
export interface ConfigMetadata extends BaseMetadata {
version?: number;
}
export type PluginLike = Plugin | string;
}

View File

@ -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)

View File

@ -49,6 +49,14 @@ function getPluginListenerMetadata(target: any) {
return listnerMetadata;
}
function getPluginConfigMetadata(target: any) {
let configMetadata: Map<string, interfaces.ConfigMetadata> = Reflect.getMetadata(
METADATA_KEY.config,
target.constructor
) || new Map<string, interfaces.ConfigMetadata>();
return configMetadata;
}
export {
getPlugin,
getPlugins,
@ -57,4 +65,5 @@ export {
getPluginCommandMetadata,
getPluginTabCompleterMetadata,
getPluginListenerMetadata,
getPluginConfigMetadata
}