feat: support config auto save

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-15 14:28:40 +08:00
parent dd88e63d1a
commit bc29221b9c
5 changed files with 53 additions and 24 deletions

View File

@ -43,4 +43,6 @@ ms.plugin.manager.build.error: "§6Load Plugin From §b{file} §6failed. §4Erro
ms.plugin.manager.build.update: "Auto Update Plugin {name} ..." ms.plugin.manager.build.update: "Auto Update Plugin {name} ..."
ms.plugin.manager.build.not.extends: "§4Found error plugin §b{source} §4it's not extends interfaces.Plugin, the plugin will be ignore!" ms.plugin.manager.build.not.extends: "§4Found error plugin §b{source} §4it's not extends interfaces.Plugin, the plugin will be ignore!"
ms.plugin.manager.build.exists: "§4Found duplicate plugin §b{exists} §4and §b{source}§4. the first plugin will be ignore!" ms.plugin.manager.build.exists: "§4Found duplicate plugin §b{exists} §4and §b{source}§4. the first plugin will be ignore!"
ms.plugin.manager.config.load.error: "[{plugin}] config {name}.{format} load failed. Error: {error}"
ms.plugin.manager.config.save.error: "[{plugin}] config {name}.{format} save failed. Error: {error}"
ms.plugin.manager.config.save.default: "[{plugin}] config {name}.{format} not exists. auto create from default variable..." ms.plugin.manager.config.save.default: "[{plugin}] config {name}.{format} not exists. auto create from default variable..."

View File

@ -43,4 +43,6 @@ ms.plugin.manager.build.error: "§6从文件 §b{file} §6加载插件失败 §4
ms.plugin.manager.build.update: "自动更新插件 {name} ..." ms.plugin.manager.build.update: "自动更新插件 {name} ..."
ms.plugin.manager.build.not.extends: "§4发现错误的插件 §b{source} §4未继承接口 interfaces.Plugin, 将不会被载入到服务器!" ms.plugin.manager.build.not.extends: "§4发现错误的插件 §b{source} §4未继承接口 interfaces.Plugin, 将不会被载入到服务器!"
ms.plugin.manager.build.duplicate: "§4发现已存在插件 §b{exists} §4和 §b{source}§4 存在冲突. 已存在插件将会被替换!" ms.plugin.manager.build.duplicate: "§4发现已存在插件 §b{exists} §4和 §b{source}§4 存在冲突. 已存在插件将会被替换!"
ms.plugin.manager.config.load.error: "[{plugin}] 配置 {name}.{format} 加载失败. 错误: {error}"
ms.plugin.manager.config.save.error: "[{plugin}] 配置 {name}.{format} 保存失败. 错误: {error}"
ms.plugin.manager.config.save.default: "[{plugin}] 配置 {name}.{format} 不存在. 从默认值自动创建保存..." ms.plugin.manager.config.save.default: "[{plugin}] 配置 {name}.{format} 不存在. 从默认值自动创建保存..."

View File

@ -10,7 +10,7 @@ export class YamlPluginConfig implements PluginConfigLoader {
return yaml.safeLoad(content); return yaml.safeLoad(content);
} }
dump(variable: any): string { dump(variable: any): string {
return yaml.safeDump(variable); return yaml.safeDump(variable, { skipInvalid: true });
} }
} }

View File

@ -100,6 +100,10 @@ export namespace interfaces {
* yml * yml
*/ */
format?: string; format?: string;
/**
* ()
*/
readonly?: boolean;
} }
export type PluginLike = Plugin | string; export type PluginLike = Plugin | string;
} }

View File

@ -16,7 +16,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
@inject(plugin.PluginInstance) @inject(plugin.PluginInstance)
private pluginInstance: any private pluginInstance: any
@inject(plugin.PluginFolder) @inject(plugin.PluginFolder)
private pluginFolder: string; private pluginFolder: string
@inject(server.ServerType) @inject(server.ServerType)
private serverType: string private serverType: string
@inject(command.Command) @inject(command.Command)
@ -33,11 +33,11 @@ export class PluginManagerImpl implements plugin.PluginManager {
if (this.pluginInstance !== null && this.initialized !== true) { if (this.pluginInstance !== null && this.initialized !== true) {
// 如果plugin不等于null 则代表是正式环境 // 如果plugin不等于null 则代表是正式环境
console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader }) console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance, loader: Thread.currentThread().contextClassLoader })
console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType }); console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType })
this.pluginRequireMap = new Map() this.pluginRequireMap = new Map()
this.pluginInstanceMap = new Map() this.pluginInstanceMap = new Map()
this.pluginMetadataMap = getPluginSources(); this.pluginMetadataMap = getPluginSources()
this.initialized = true; this.initialized = true
} }
} }
@ -69,11 +69,11 @@ export class PluginManagerImpl implements plugin.PluginManager {
* @param file java.io.File * @param file java.io.File
*/ */
loadFromFile(file: string): interfaces.Plugin { loadFromFile(file: string): interfaces.Plugin {
let metadata = this.loadPlugin(file); let metadata = this.loadPlugin(file)
let plugin = this.buildPlugin(metadata && metadata.description ? metadata.description : this.pluginMetadataMap.get(file.toString())) let plugin = this.buildPlugin(metadata && metadata.description ? metadata.description : this.pluginMetadataMap.get(file.toString()))
this.load(plugin) this.load(plugin)
this.enable(plugin) this.enable(plugin)
return plugin; return plugin
} }
load(...args: any[]): void { load(...args: any[]): void {
@ -96,6 +96,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
disable(...args: any[]): void { disable(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.runPluginStage(plugin, 'disable', () => { this.runPluginStage(plugin, 'disable', () => {
this.saveConfig(plugin)
this.unregistryCommand(plugin) this.unregistryCommand(plugin)
this.unregistryListener(plugin) this.unregistryListener(plugin)
}) })
@ -197,14 +198,14 @@ export class PluginManagerImpl implements plugin.PluginManager {
private createPlugin(file: string) { private createPlugin(file: string) {
//@ts-ignore //@ts-ignore
let instance = require(file, { cache: false }) let instance = require(file, { cache: false })
this.pluginRequireMap.set(file, instance); this.pluginRequireMap.set(file, instance)
return instance; return instance
} }
private buildPlugins() { private buildPlugins() {
let metadatas = []; let metadatas = []
let pluginMetadatas = getPluginMetadatas() let pluginMetadatas = getPluginMetadatas()
for (const [_, metadata] of pluginMetadatas) { metadatas.push(metadata); } for (const [_, metadata] of pluginMetadatas) { metadatas.push(metadata) }
for (const [_, instance] of this.pluginRequireMap) { if (instance.description) { this.buildPlugin(instance.description) } } for (const [_, instance] of this.pluginRequireMap) { if (instance.description) { this.buildPlugin(instance.description) } }
for (const metadata of metadatas) { for (const metadata of metadatas) {
if (!this.allowProcess(metadata.servers)) { continue } if (!this.allowProcess(metadata.servers)) { continue }
@ -213,7 +214,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
private buildPlugin(metadata: interfaces.PluginMetadata) { private buildPlugin(metadata: interfaces.PluginMetadata) {
let pluginInstance: interfaces.Plugin; let pluginInstance: interfaces.Plugin
switch (metadata.type) { switch (metadata.type) {
case "ioc": case "ioc":
try { try {
@ -227,15 +228,15 @@ export class PluginManagerImpl implements plugin.PluginManager {
console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex }) console.i18n("ms.plugin.manager.initialize.error", { name: metadata.name, ex })
console.ex(ex) console.ex(ex)
} }
break; break
case "basic": case "basic":
pluginInstance = this.pluginRequireMap.get(metadata.source.toString()) pluginInstance = this.pluginRequireMap.get(metadata.source.toString())
break; break
default: default:
throw new Error('§4不支持的插件类型 请检查加载器是否正常启用!') throw new Error('§4不支持的插件类型 请检查加载器是否正常启用!')
} }
this.pluginInstanceMap.set(metadata.name, pluginInstance) this.pluginInstanceMap.set(metadata.name, pluginInstance)
return pluginInstance; return pluginInstance
} }
private bindPlugin(metadata: interfaces.PluginMetadata) { private bindPlugin(metadata: interfaces.PluginMetadata) {
@ -251,15 +252,35 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
private loadConfig(plugin: interfaces.Plugin) { private loadConfig(plugin: interfaces.Plugin) {
let configs = getPluginConfigMetadata(plugin); let configs = getPluginConfigMetadata(plugin)
for (let [_, config] of configs) { for (let [_, config] of configs) {
let configFile = fs.concat(root, this.pluginFolder, plugin.description.name, config.name + '.' + config.format) try {
let configFactory = getConfigLoader(config.format); let configFile = fs.concat(root, this.pluginFolder, plugin.description.name, config.name + '.' + config.format)
if (!fs.exists(configFile)) { let configFactory = getConfigLoader(config.format)
base.save(configFile, configFactory.dump(plugin[config.variable])) if (!fs.exists(configFile)) {
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: config.name, format: config.format }) base.save(configFile, configFactory.dump(plugin[config.variable]))
} else { console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: config.name, format: config.format })
plugin[config.variable] = configFactory.load(base.read(configFile)); } else {
plugin[config.variable] = configFactory.load(base.read(configFile))
plugin[config.variable].save = () => base.save(configFile, configFactory.dump(plugin[config.variable]))
}
} catch (error) {
console.i18n("ms.plugin.manager.config.load.error", { plugin: plugin.description.name, name: config.name, format: config.format, error })
console.ex(error)
}
}
}
private saveConfig(plugin: interfaces.Plugin) {
let configs = getPluginConfigMetadata(plugin)
for (let [_, config] of configs) {
try {
let configFile = fs.concat(root, this.pluginFolder, plugin.description.name, config.name + '.' + config.format)
let configFactory = getConfigLoader(config.format)
if (!config.readonly) { base.save(configFile, configFactory.dump(plugin[config.variable])) }
} catch (error) {
console.i18n("ms.plugin.manager.config.save.error", { plugin: plugin.description.name, name: config.name, format: config.format, error })
console.ex(error)
} }
} }
} }
@ -299,7 +320,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
} }
private execPluginStage(pluginInstance: interfaces.Plugin, stageName: string) { private execPluginStage(pluginInstance: interfaces.Plugin, stageName: string) {
let stages = getPluginStageMetadata(pluginInstance, stageName); let stages = getPluginStageMetadata(pluginInstance, stageName)
for (const stage of stages) { for (const stage of stages) {
if (!this.allowProcess(stage.servers)) { continue } if (!this.allowProcess(stage.servers)) { continue }
console.i18n("ms.plugin.manager.stage.exec", { plugin: pluginInstance.description.name, name: stage.executor, stage: stageName, servers: stage.servers }) console.i18n("ms.plugin.manager.stage.exec", { plugin: pluginInstance.description.name, name: stage.executor, stage: stageName, servers: stage.servers })