Commit bcea58d2 authored by nanahira's avatar nanahira

put everything in metadata

parent 127e7cbd
...@@ -96,14 +96,7 @@ export const InjectContext = (select?: Selection) => ...@@ -96,14 +96,7 @@ export const InjectContext = (select?: Selection) =>
export const InjectApp = () => InjectSystem((obj) => obj.__ctx.app); export const InjectApp = () => InjectSystem((obj) => obj.__ctx.app);
export const InjectConfig = () => InjectSystem((obj) => obj.__config); export const InjectConfig = () => InjectSystem((obj) => obj.__config);
export const InjectLogger = (name?: string) => export const InjectLogger = (name?: string) =>
InjectSystem((obj, config) => InjectSystem((obj) => obj.__ctx.logger(name || obj.constructor.name));
obj.__ctx.logger(
name ||
config.name ||
Object.getPrototypeOf(Object.getPrototypeOf(obj))?.constructor?.name ||
'default',
),
);
export const Caller = () => export const Caller = () =>
InjectSystem((obj) => { InjectSystem((obj) => {
const targetCtx: Context = obj[Context.current] || obj.__ctx; const targetCtx: Context = obj[Context.current] || obj.__ctx;
......
...@@ -28,3 +28,5 @@ export interface MetadataMap { ...@@ -28,3 +28,5 @@ export interface MetadataMap {
KoishiPredefineSchema: Schema | ClassType<any>; KoishiPredefineSchema: Schema | ClassType<any>;
KoishiPredefineName: string; KoishiPredefineName: string;
} }
export const ThirdEyeSym = Symbol('ThirdEyeSym');
...@@ -5,10 +5,7 @@ export * from 'koishi-decorators/dist/src/def/interfaces'; ...@@ -5,10 +5,7 @@ export * from 'koishi-decorators/dist/src/def/interfaces';
// Command stuff // Command stuff
export type SystemInjectFun = <T = any>( export type SystemInjectFun = <T = any>(obj: PluginClass<T>) => any;
obj: PluginClass<T>,
pluginMeta: KoishiPluginRegistrationOptions<T>,
) => any;
export interface ProvideOptions { export interface ProvideOptions {
immediate?: boolean; immediate?: boolean;
......
...@@ -12,6 +12,7 @@ import { reflector } from './meta/meta-fetch'; ...@@ -12,6 +12,7 @@ import { reflector } from './meta/meta-fetch';
import { SchemaClass } from 'schemastery-gen'; import { SchemaClass } from 'schemastery-gen';
import _ from 'lodash'; import _ from 'lodash';
import { Registrar, Type } from 'koishi-decorators'; import { Registrar, Type } from 'koishi-decorators';
import { PluginName, PluginSchema, UsingService } from './decorators';
export interface KoishiPluginRegistrationOptions<T = any> { export interface KoishiPluginRegistrationOptions<T = any> {
name?: string; name?: string;
...@@ -52,19 +53,26 @@ export function DefinePlugin<T = any>( ...@@ -52,19 +53,26 @@ export function DefinePlugin<T = any>(
new (...args: any[]): any; new (...args: any[]): any;
} & KoishiPluginRegistrationOptions<T>, } & KoishiPluginRegistrationOptions<T>,
>(originalClass: C) { >(originalClass: C) {
if (options.name) {
PluginName(options.name)(originalClass);
}
if (options.schema) {
PluginSchema(options.schema)(originalClass);
}
if (options.using) {
UsingService(...options.using)(originalClass);
}
const newClass = class extends originalClass implements PluginClass { const newClass = class extends originalClass implements PluginClass {
static get Config() { static get Config() {
const schemaType = const schemaType =
reflector.get('KoishiPredefineSchema', newClass) || reflector.get('KoishiPredefineSchema', newClass) ||
reflector.get('KoishiPredefineSchema', originalClass) || reflector.get('KoishiPredefineSchema', originalClass);
options.schema;
return schemaType ? SchemaClass(schemaType) : undefined; return schemaType ? SchemaClass(schemaType) : undefined;
} }
static get using() { static get using() {
const list = reflector const list = reflector
.getArray(KoishiAddUsingList, originalClass) .getArray(KoishiAddUsingList, originalClass)
.concat(options.using || [])
.concat(reflector.getArray(KoishiAddUsingList, newClass)); .concat(reflector.getArray(KoishiAddUsingList, newClass));
return _.uniq(list); return _.uniq(list);
} }
...@@ -81,7 +89,7 @@ export function DefinePlugin<T = any>( ...@@ -81,7 +89,7 @@ export function DefinePlugin<T = any>(
Object.defineProperty(this, key, { Object.defineProperty(this, key, {
configurable: true, configurable: true,
enumerable: true, enumerable: true,
get: () => valueFunction(this, options), get: () => valueFunction(this),
}); });
} }
} }
...@@ -135,7 +143,7 @@ export function DefinePlugin<T = any>( ...@@ -135,7 +143,7 @@ export function DefinePlugin<T = any>(
methodKey, methodKey,
); );
if (partialUsing.length) { if (partialUsing.length) {
const name = `${options.name || originalClass.name}-${methodKey}`; const name = `${newClass.name}-${methodKey}`;
const innerPlugin: Plugin.Object = { const innerPlugin: Plugin.Object = {
name, name,
using: partialUsing, using: partialUsing,
...@@ -233,7 +241,6 @@ export function DefinePlugin<T = any>( ...@@ -233,7 +241,6 @@ export function DefinePlugin<T = any>(
get: () => get: () =>
reflector.get('KoishiPredefineName', newClass) || reflector.get('KoishiPredefineName', newClass) ||
reflector.get('KoishiPredefineName', originalClass) || reflector.get('KoishiPredefineName', originalClass) ||
options.name ||
originalClass.name, originalClass.name,
}); });
return newClass; return newClass;
......
import { DefinePlugin, Inject, UsingService } from '..'; import { RegisterSchema, SchemaProperty } from '..';
import { Assets, Bot, Cache, Context } from 'koishi'; import { Assets, Bot, Cache, Context } from 'koishi';
import { PluginName, UsingService, Inject, PluginSchema } from '../src/decorators';
import { DefinePlugin } from '../src/register';
@RegisterSchema()
class Config {
@SchemaProperty()
foo: string;
}
describe('InjectUsing', () => { describe('InjectUsing', () => {
@PluginSchema(Config)
@PluginName('foo-plugin')
@UsingService('router') @UsingService('router')
@DefinePlugin({ using: ['database'] }) @DefinePlugin({ using: ['database'] })
@UsingService('http') @UsingService('http')
...@@ -17,6 +27,8 @@ describe('InjectUsing', () => { ...@@ -17,6 +27,8 @@ describe('InjectUsing', () => {
} }
it('Should include injected using services', () => { it('Should include injected using services', () => {
expect(MyPlugin.name).toBe('foo-plugin');
expect(MyPlugin['Config']).toEqual(Config);
const usingList = (MyPlugin as any).using as (keyof Context.Services)[]; const usingList = (MyPlugin as any).using as (keyof Context.Services)[];
expect(usingList).toBeInstanceOf(Array); expect(usingList).toBeInstanceOf(Array);
expect(usingList.length).toEqual(5); expect(usingList.length).toEqual(5);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment