2020-06-17 10:39:24 +00:00
|
|
|
import { plugin as pluginApi } from "@ccms/api"
|
2020-05-26 07:53:41 +00:00
|
|
|
import { injectable, decorate } from "@ccms/container"
|
2019-09-07 04:23:15 +00:00
|
|
|
import { interfaces } from './interfaces'
|
|
|
|
import { METADATA_KEY } from './constants'
|
2020-05-07 09:12:15 +00:00
|
|
|
import { getPluginMetadatas, getPluginCommandMetadata, getPluginListenerMetadata, getPluginTabCompleterMetadata, getPluginConfigMetadata, getPluginStageMetadata, getPluginSources } from './utils'
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript plugin
|
2019-09-10 09:22:00 +00:00
|
|
|
* @param metadata PluginMetadata
|
2019-09-07 04:23:15 +00:00
|
|
|
*/
|
2020-07-06 08:06:47 +00:00
|
|
|
export function plugin(metadata: pluginApi.PluginMetadata | any) {
|
2020-04-24 07:38:00 +00:00
|
|
|
return function (target: any) {
|
2020-07-06 08:06:47 +00:00
|
|
|
if (!metadata.source) metadata = { souece: metadata }
|
|
|
|
metadata = { name: target.name, version: '1.0.0', author: 'Unknow', target, type: 'ioc', ...metadata }
|
2020-05-26 07:53:41 +00:00
|
|
|
decorate(injectable(), target)
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.plugin, metadata, target)
|
2020-06-17 10:39:24 +00:00
|
|
|
const previousMetadata: Map<string, pluginApi.PluginMetadata> = getPluginMetadatas()
|
2020-05-26 07:53:41 +00:00
|
|
|
previousMetadata.set(metadata.name, metadata)
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.plugin, previousMetadata, Reflect)
|
2020-06-17 10:39:24 +00:00
|
|
|
const previousSources: Map<string, pluginApi.PluginMetadata> = getPluginSources()
|
2020-05-26 07:53:41 +00:00
|
|
|
previousSources.set(metadata.source.toString(), metadata)
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.souece, previousSources, Reflect)
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-19 10:59:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript Command
|
|
|
|
* @param metadata CommandMetadata
|
|
|
|
*/
|
|
|
|
export function cmd(metadata: interfaces.CommandMetadata = {}) {
|
2020-04-24 07:38:00 +00:00
|
|
|
return function (target: any, key: string, value: any) {
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.name = metadata.name || key
|
2020-09-24 02:33:16 +00:00
|
|
|
metadata.alias = metadata.alias || []
|
2020-09-17 09:44:48 +00:00
|
|
|
metadata.target = target
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.executor = key
|
2019-09-19 10:59:32 +00:00
|
|
|
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
2020-09-24 02:33:16 +00:00
|
|
|
Reflect.defineMetadata(METADATA_KEY.cmd, [metadata, ...getPluginCommandMetadata(target)], target.constructor)
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
2019-09-19 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-04 12:28:52 +00:00
|
|
|
* MiaoScript TabComplete
|
|
|
|
* @param metadata TabCompleterMetadata
|
2019-09-19 10:59:32 +00:00
|
|
|
*/
|
2020-04-01 03:08:57 +00:00
|
|
|
export function tab(metadata: interfaces.CommandMetadata = {}) {
|
2020-04-24 07:38:00 +00:00
|
|
|
return function (target: any, key: string, value: any) {
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.name = metadata.name || (key.startsWith('tab') ? key.split('tab', 2)[1] : key)
|
|
|
|
if (!metadata.name) { return }
|
2020-09-24 02:33:16 +00:00
|
|
|
metadata.alias = metadata.alias || []
|
2020-09-17 09:44:48 +00:00
|
|
|
metadata.target = target
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.executor = key
|
2019-09-19 10:59:32 +00:00
|
|
|
metadata.paramtypes = Reflect.getMetadata("design:paramtypes", target, key)
|
2020-09-24 02:33:16 +00:00
|
|
|
let previousMetadata = getPluginTabCompleterMetadata(target)
|
2019-09-19 10:59:32 +00:00
|
|
|
previousMetadata.set(metadata.name, metadata)
|
2020-09-22 09:33:59 +00:00
|
|
|
metadata.alias?.forEach((name) => previousMetadata.set(name, metadata))
|
2020-05-26 07:53:41 +00:00
|
|
|
Reflect.defineMetadata(METADATA_KEY.tab, previousMetadata, target.constructor)
|
|
|
|
}
|
2019-09-19 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MiaoScript Listener
|
|
|
|
* @param metadata ListenerMetadata
|
|
|
|
*/
|
|
|
|
export function listener(metadata: interfaces.ListenerMetadata = {}) {
|
2020-04-24 07:38:00 +00:00
|
|
|
return function (target: any, key: string, value: any) {
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.name = metadata.name || key
|
2020-09-17 09:44:48 +00:00
|
|
|
metadata.target = target
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.executor = key
|
2020-09-24 02:33:16 +00:00
|
|
|
Reflect.defineMetadata(METADATA_KEY.listener, [metadata, ...getPluginListenerMetadata(target)], target.constructor)
|
2020-09-17 09:44:48 +00:00
|
|
|
Reflect.defineMetadata(METADATA_KEY.listener, metadata, target[key])
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
2019-09-21 07:05:37 +00:00
|
|
|
}
|
2020-02-23 16:12:32 +00:00
|
|
|
|
2020-05-26 07:53:41 +00:00
|
|
|
export function config(metadata: interfaces.ConfigMetadata = {}) {
|
2020-04-24 07:38:00 +00:00
|
|
|
return function (target: any, key: string) {
|
2020-05-26 07:53:41 +00:00
|
|
|
metadata.name = metadata.name || key
|
|
|
|
metadata.variable = key
|
|
|
|
metadata.version = metadata.version ?? 1
|
|
|
|
metadata.format = metadata.format ?? 'yml'
|
2020-09-24 02:33:16 +00:00
|
|
|
let previousMetadata = getPluginConfigMetadata(target)
|
2020-05-26 07:53:41 +00:00
|
|
|
previousMetadata.set(metadata.name, metadata)
|
|
|
|
Reflect.defineMetadata(METADATA_KEY.config, previousMetadata, target.constructor)
|
2020-02-23 16:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 07:38:00 +00:00
|
|
|
|
2020-06-30 07:58:51 +00:00
|
|
|
function stage(stage: string) {
|
|
|
|
return (metadata: interfaces.ExecMetadata = {}) => {
|
|
|
|
return function (target: any, key: string, value: any) {
|
|
|
|
metadata.name = metadata.name || key
|
|
|
|
metadata.executor = key
|
2020-09-24 02:33:16 +00:00
|
|
|
Reflect.defineMetadata(METADATA_KEY.stage[stage], [metadata, ...getPluginStageMetadata(target, stage)], target.constructor)
|
2020-06-30 07:58:51 +00:00
|
|
|
}
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
2020-04-24 07:38:00 +00:00
|
|
|
}
|
2020-06-30 07:58:51 +00:00
|
|
|
export const load = stage('load')
|
|
|
|
export const enable = stage('enable')
|
|
|
|
export const disable = stage('disable')
|