mirror of
https://github.com/certd/certd.git
synced 2026-04-24 20:57:26 +08:00
feat: midway注解方式编写插件
This commit is contained in:
@@ -13,3 +13,6 @@ export type AccessDefine = Registrable & {
|
||||
export interface IAccessService {
|
||||
getById(id: any): Promise<any>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IAccess {}
|
||||
|
||||
@@ -1,71 +1,39 @@
|
||||
// src/decorator/memoryCache.decorator.ts
|
||||
import {
|
||||
attachClassMetadata,
|
||||
attachPropertyDataToClass,
|
||||
getClassMetadata,
|
||||
listModule,
|
||||
listPropertyDataFromClass,
|
||||
saveClassMetadata,
|
||||
saveModule,
|
||||
} from "@midwayjs/decorator";
|
||||
import { AccessDefine, AccessInputDefine } from "./api";
|
||||
import { Decorator } from "../decorator";
|
||||
import _ from "lodash";
|
||||
import { accessRegistry } from "./registry";
|
||||
|
||||
// 提供一个唯一 key
|
||||
export const ACCESS_CLASS_KEY = "decorator:access";
|
||||
export const ACCESS_CLASS_KEY = "pipeline:access";
|
||||
export const ACCESS_INPUT_KEY = "pipeline:access:input";
|
||||
|
||||
export function IsAccess(define: AccessDefine): ClassDecorator {
|
||||
console.log("is access define:", define);
|
||||
return (target: any) => {
|
||||
console.log("is access load:", target);
|
||||
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
||||
saveModule(ACCESS_CLASS_KEY, target);
|
||||
// 保存一些元数据信息,任意你希望存的东西
|
||||
saveClassMetadata(
|
||||
ACCESS_CLASS_KEY,
|
||||
{
|
||||
define,
|
||||
},
|
||||
target
|
||||
);
|
||||
target = Decorator.target(target);
|
||||
|
||||
const inputs: any = {};
|
||||
const properties = Decorator.getClassProperties(target);
|
||||
for (const property in properties) {
|
||||
const input = Reflect.getMetadata(ACCESS_INPUT_KEY, target, property);
|
||||
if (input) {
|
||||
inputs[property] = input;
|
||||
}
|
||||
}
|
||||
_.merge(define, { inputs });
|
||||
Reflect.defineMetadata(ACCESS_CLASS_KEY, define, target);
|
||||
target.define = define;
|
||||
accessRegistry.register(define.name, {
|
||||
define,
|
||||
target,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export const ACCESS_INPUT_KEY = "decorator:access:input";
|
||||
|
||||
export function IsAccessInput(input?: AccessInputDefine): PropertyDecorator {
|
||||
return (target, propertyKey) => {
|
||||
attachPropertyDataToClass(ACCESS_INPUT_KEY, { input }, target, propertyKey, propertyKey as string);
|
||||
|
||||
attachClassMetadata(
|
||||
ACCESS_CLASS_KEY,
|
||||
{
|
||||
inputs: {
|
||||
[propertyKey]: input,
|
||||
},
|
||||
},
|
||||
target
|
||||
);
|
||||
target = Decorator.target(target);
|
||||
// const _type = Reflect.getMetadata("design:type", target, propertyKey);
|
||||
Reflect.defineMetadata(ACCESS_INPUT_KEY, input, target, propertyKey);
|
||||
};
|
||||
}
|
||||
|
||||
export function registerAccess() {
|
||||
const modules = listModule(ACCESS_CLASS_KEY);
|
||||
for (const mod of modules) {
|
||||
console.log("mod", mod);
|
||||
const define = getClassMetadata(ACCESS_CLASS_KEY, mod);
|
||||
console.log("define", define);
|
||||
const inputs = listPropertyDataFromClass(ACCESS_INPUT_KEY, mod);
|
||||
console.log("inputs", inputs);
|
||||
for (const input of inputs) {
|
||||
define.inputs = {};
|
||||
_.merge(define.inputs, input.inputs);
|
||||
}
|
||||
|
||||
accessRegistry.register(define.name, {
|
||||
define,
|
||||
target: mod,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ export class Executor {
|
||||
// @ts-ignore
|
||||
const define: PluginDefine = plugin.define;
|
||||
//从outputContext读取输入参数
|
||||
_.forEach(define.inputs, (item, key) => {
|
||||
_.forEach(define.input, (item, key) => {
|
||||
if (item.component?.name === "pi-output-selector") {
|
||||
const contextKey = step.input[key];
|
||||
if (contextKey != null) {
|
||||
@@ -164,12 +164,12 @@ export class Executor {
|
||||
instance[key] = context[key];
|
||||
});
|
||||
|
||||
const res = await instance.execute();
|
||||
await instance.execute();
|
||||
|
||||
//输出到output context
|
||||
_.forEach(define.outputs, (item, key) => {
|
||||
_.forEach(define.output, (item, key) => {
|
||||
const contextKey = `step.${step.id}.${key}`;
|
||||
this.runtime.context[contextKey] = res[key];
|
||||
this.runtime.context[contextKey] = instance[key];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
const propertyMap: any = {};
|
||||
function attachProperty(target: any, propertyKey: string | symbol) {
|
||||
let props = propertyMap[target];
|
||||
if (props == null) {
|
||||
props = {};
|
||||
propertyMap[target] = props;
|
||||
}
|
||||
props[propertyKey] = true;
|
||||
}
|
||||
|
||||
function getClassProperties(target: any) {
|
||||
return propertyMap[target] || {};
|
||||
}
|
||||
|
||||
function target(target: any, propertyKey?: string | symbol) {
|
||||
if (typeof target === "object" && target.constructor) {
|
||||
target = target.constructor;
|
||||
}
|
||||
if (propertyKey != null) {
|
||||
attachProperty(target, propertyKey);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
export const Decorator = {
|
||||
target,
|
||||
attachProperty,
|
||||
getClassProperties,
|
||||
};
|
||||
@@ -1,42 +1,20 @@
|
||||
// src/decorator/memoryCache.decorator.ts
|
||||
import { getClassMetadata, listModule, Provide, saveClassMetadata, saveModule, Scope, ScopeEnum } from "@midwayjs/decorator";
|
||||
import { dnsProviderRegistry } from "./registry";
|
||||
import { DnsProviderDefine } from "./api";
|
||||
import { Decorator } from "../decorator";
|
||||
|
||||
// 提供一个唯一 key
|
||||
export const DNS_PROVIDER_CLASS_KEY = "decorator:dnsProvider";
|
||||
export const DNS_PROVIDER_CLASS_KEY = "pipeline:dns-provider";
|
||||
|
||||
export function IsDnsProvider(define: DnsProviderDefine): ClassDecorator {
|
||||
console.log("is task plugin define:", define);
|
||||
return (target: any) => {
|
||||
console.log("is task plugin load:", target);
|
||||
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
||||
saveModule(DNS_PROVIDER_CLASS_KEY, target);
|
||||
// 保存一些元数据信息,任意你希望存的东西
|
||||
saveClassMetadata(
|
||||
DNS_PROVIDER_CLASS_KEY,
|
||||
{
|
||||
define,
|
||||
},
|
||||
target
|
||||
);
|
||||
// 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
|
||||
Scope(ScopeEnum.Prototype)(target);
|
||||
target = Decorator.target(target);
|
||||
Reflect.defineMetadata(DNS_PROVIDER_CLASS_KEY, define, target);
|
||||
|
||||
// 调用一下 Provide 装饰器,这样用户的 class 可以省略写 @Provide() 装饰器了
|
||||
Provide()(target);
|
||||
};
|
||||
}
|
||||
|
||||
export function registerDnsProviders() {
|
||||
const modules = listModule(DNS_PROVIDER_CLASS_KEY);
|
||||
for (const mod of modules) {
|
||||
console.log("mod", mod);
|
||||
const define = getClassMetadata(DNS_PROVIDER_CLASS_KEY, mod);
|
||||
console.log("define", define);
|
||||
target.define = define;
|
||||
dnsProviderRegistry.register(define.name, {
|
||||
define,
|
||||
target: mod,
|
||||
target,
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@ import { Config, Configuration, Inject, Logger } from "@midwayjs/decorator";
|
||||
// @ts-ignore
|
||||
import { ILogger } from "@midwayjs/logger";
|
||||
import { IMidwayContainer, MidwayDecoratorService } from "@midwayjs/core";
|
||||
import { registerPlugins } from "../plugin/decorator";
|
||||
import { registerAccess } from "../access/decorator";
|
||||
import { registerDnsProviders } from "../dns-provider";
|
||||
|
||||
// ... (see below) ...
|
||||
@Configuration({
|
||||
@@ -26,9 +23,6 @@ export class PipelineConfiguration {
|
||||
async onReady(container: IMidwayContainer) {
|
||||
this.logger.info("pipeline install");
|
||||
|
||||
registerPlugins();
|
||||
registerAccess();
|
||||
registerDnsProviders();
|
||||
//this.implPropertyDecorator(container);
|
||||
this.logger.info("pipeline installed");
|
||||
}
|
||||
|
||||
@@ -21,14 +21,16 @@ export type TaskInputDefine = FormItemProps;
|
||||
|
||||
export type PluginDefine = Registrable & {
|
||||
default?: any;
|
||||
inputs?: {
|
||||
input?: {
|
||||
[key: string]: TaskInputDefine;
|
||||
};
|
||||
outputs?: {
|
||||
output?: {
|
||||
[key: string]: TaskOutputDefine;
|
||||
};
|
||||
|
||||
autowire?: any;
|
||||
autowire?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
|
||||
export interface ITaskPlugin {
|
||||
|
||||
@@ -1,136 +1,77 @@
|
||||
import {
|
||||
attachClassMetadata,
|
||||
attachPropertyDataToClass,
|
||||
getClassMetadata,
|
||||
listModule,
|
||||
listPropertyDataFromClass,
|
||||
saveClassMetadata,
|
||||
saveModule,
|
||||
} from "@midwayjs/decorator";
|
||||
import _ from "lodash";
|
||||
import { pluginRegistry } from "./registry";
|
||||
import { PluginDefine, TaskInputDefine, TaskOutputDefine } from "./api";
|
||||
import { Decorator } from "../decorator";
|
||||
|
||||
// 提供一个唯一 key
|
||||
export const PLUGIN_CLASS_KEY = "decorator:plugin";
|
||||
export const PLUGIN_CLASS_KEY = "pipeline:plugin";
|
||||
|
||||
export function IsTaskPlugin(define: PluginDefine): ClassDecorator {
|
||||
console.log("is task plugin define:", define);
|
||||
return (target: any) => {
|
||||
console.log("is task plugin load:", target);
|
||||
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
||||
saveModule(PLUGIN_CLASS_KEY, target);
|
||||
// 保存一些元数据信息,任意你希望存的东西
|
||||
saveClassMetadata(
|
||||
PLUGIN_CLASS_KEY,
|
||||
{
|
||||
define,
|
||||
},
|
||||
target
|
||||
);
|
||||
// // 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
|
||||
// Scope(ScopeEnum.Prototype)(target);
|
||||
target = Decorator.target(target);
|
||||
|
||||
// 调用一下 Provide 装饰器,这样用户的 class 可以省略写 @Provide() 装饰器了
|
||||
// Provide()(target);
|
||||
const inputs: any = {};
|
||||
const autowires: any = {};
|
||||
const outputs: any = {};
|
||||
const properties = Decorator.getClassProperties(target);
|
||||
for (const property in properties) {
|
||||
const input = Reflect.getMetadata(PLUGIN_INPUT_KEY, target, property);
|
||||
if (input) {
|
||||
inputs[property] = input;
|
||||
}
|
||||
|
||||
const autowire = Reflect.getMetadata(PLUGIN_AUTOWIRE_KEY, target, property);
|
||||
if (autowire) {
|
||||
autowires[property] = autowire;
|
||||
}
|
||||
|
||||
const output = Reflect.getMetadata(PLUGIN_OUTPUT_KEY, target, property);
|
||||
if (output) {
|
||||
outputs[property] = output;
|
||||
}
|
||||
}
|
||||
_.merge(define, { input: inputs, autowire: autowires, output: outputs });
|
||||
|
||||
Reflect.defineMetadata(PLUGIN_CLASS_KEY, define, target);
|
||||
|
||||
target.define = define;
|
||||
pluginRegistry.register(define.name, {
|
||||
define,
|
||||
target,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export const PLUGIN_INPUT_KEY = "decorator:plugin:input";
|
||||
export const PLUGIN_INPUT_KEY = "pipeline:plugin:input";
|
||||
|
||||
export function TaskInput(input?: TaskInputDefine): PropertyDecorator {
|
||||
return (target, propertyKey) => {
|
||||
attachPropertyDataToClass(PLUGIN_INPUT_KEY, { input }, target, propertyKey, propertyKey as string);
|
||||
|
||||
attachClassMetadata(
|
||||
PLUGIN_CLASS_KEY,
|
||||
{
|
||||
inputs: {
|
||||
[propertyKey]: input,
|
||||
},
|
||||
},
|
||||
target
|
||||
);
|
||||
target = Decorator.target(target, propertyKey);
|
||||
Reflect.defineMetadata(PLUGIN_INPUT_KEY, input, target, propertyKey);
|
||||
};
|
||||
//
|
||||
// return createCustomPropertyDecorator(CLASS_PROPS_KEY, {
|
||||
// input,
|
||||
// });
|
||||
}
|
||||
|
||||
// 装饰器内部的唯一 id
|
||||
export const PLUGIN_OUTPUT_KEY = "decorator:plugin:output";
|
||||
export const PLUGIN_OUTPUT_KEY = "pipeline:plugin:output";
|
||||
export function TaskOutput(output?: TaskOutputDefine): PropertyDecorator {
|
||||
return (target, propertyKey) => {
|
||||
attachPropertyDataToClass(PLUGIN_OUTPUT_KEY, { output }, target, propertyKey, propertyKey as string);
|
||||
|
||||
attachClassMetadata(
|
||||
PLUGIN_CLASS_KEY,
|
||||
{
|
||||
outputs: {
|
||||
[propertyKey]: output,
|
||||
},
|
||||
},
|
||||
target
|
||||
);
|
||||
target = Decorator.target(target, propertyKey);
|
||||
Reflect.defineMetadata(PLUGIN_OUTPUT_KEY, output, target, propertyKey);
|
||||
};
|
||||
//
|
||||
// return createCustomPropertyDecorator(CLASS_PROPS_KEY, {
|
||||
// input,
|
||||
// });
|
||||
}
|
||||
|
||||
export type AutowireProp = {
|
||||
name?: string;
|
||||
type?: any;
|
||||
};
|
||||
export const PLUGIN_AUTOWIRE_KEY = "decorator:plugin:autowire";
|
||||
export const PLUGIN_AUTOWIRE_KEY = "pipeline:plugin:autowire";
|
||||
|
||||
export function Autowire(props?: AutowireProp): PropertyDecorator {
|
||||
return (target, propertyKey) => {
|
||||
attachPropertyDataToClass(
|
||||
PLUGIN_AUTOWIRE_KEY,
|
||||
{
|
||||
autowire: {
|
||||
[propertyKey]: props,
|
||||
},
|
||||
},
|
||||
target,
|
||||
propertyKey,
|
||||
propertyKey as string
|
||||
);
|
||||
const _type = Reflect.getMetadata("design:type", target, propertyKey);
|
||||
target = Decorator.target(target, propertyKey);
|
||||
props = props || {};
|
||||
props.type = _type;
|
||||
Reflect.defineMetadata(PLUGIN_AUTOWIRE_KEY, props || {}, target, propertyKey);
|
||||
};
|
||||
}
|
||||
|
||||
export function registerPlugins() {
|
||||
const modules = listModule(PLUGIN_CLASS_KEY);
|
||||
for (const mod of modules) {
|
||||
console.log("mod", mod);
|
||||
const define: PluginDefine = getClassMetadata(PLUGIN_CLASS_KEY, mod);
|
||||
console.log("define", define);
|
||||
const inputs = listPropertyDataFromClass(PLUGIN_INPUT_KEY, mod);
|
||||
console.log("inputs", inputs);
|
||||
for (const input of inputs) {
|
||||
define.inputs = {};
|
||||
_.merge(define.inputs, input.inputs);
|
||||
}
|
||||
|
||||
const outputs = listPropertyDataFromClass(PLUGIN_OUTPUT_KEY, mod);
|
||||
console.log("outputs", outputs);
|
||||
for (const output of outputs) {
|
||||
define.outputs = {};
|
||||
_.merge(define.outputs, output.outputs);
|
||||
}
|
||||
|
||||
const autowire = listPropertyDataFromClass(PLUGIN_AUTOWIRE_KEY, mod);
|
||||
console.log("autowire", autowire);
|
||||
for (const auto of autowire) {
|
||||
define.autowire = {};
|
||||
_.merge(define.autowire, auto.autowire);
|
||||
}
|
||||
|
||||
pluginRegistry.register(define.name, {
|
||||
define,
|
||||
target: mod,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user