* 安装插件 * * 会自动检查依赖并按正确顺序安装。 * * @param plugin - 插件实例 * @throws 如果依赖检查失败或安装失败
(plugin: IPlugin)
| 69 | * @throws 如果依赖检查失败或安装失败 |
| 70 | */ |
| 71 | public async install(plugin: IPlugin): Promise<void> { |
| 72 | if (!this._core || !this._services) { |
| 73 | throw new Error('PluginManager not initialized. Call initialize() first.'); |
| 74 | } |
| 75 | |
| 76 | // 检查是否已安装 |
| 77 | if (this._plugins.has(plugin.name)) { |
| 78 | logger.warn(`Plugin ${plugin.name} is already installed`); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // 检查依赖 |
| 83 | if (plugin.dependencies && plugin.dependencies.length > 0) { |
| 84 | this._checkDependencies(plugin); |
| 85 | } |
| 86 | |
| 87 | // 创建元数据 |
| 88 | const metadata: IPluginMetadata = { |
| 89 | name: plugin.name, |
| 90 | version: plugin.version, |
| 91 | state: PluginState.NotInstalled, |
| 92 | installedAt: Date.now() |
| 93 | }; |
| 94 | |
| 95 | this._metadata.set(plugin.name, metadata); |
| 96 | |
| 97 | try { |
| 98 | // 调用插件的安装方法 |
| 99 | logger.info(`Installing plugin: ${plugin.name} v${plugin.version}`); |
| 100 | await plugin.install(this._core, this._services); |
| 101 | |
| 102 | // 标记为已安装 |
| 103 | this._plugins.set(plugin.name, plugin); |
| 104 | metadata.state = PluginState.Installed; |
| 105 | |
| 106 | logger.info(`Plugin ${plugin.name} installed successfully`); |
| 107 | } catch (error) { |
| 108 | // 安装失败 |
| 109 | metadata.state = PluginState.Failed; |
| 110 | metadata.error = error instanceof Error ? error.message : String(error); |
| 111 | |
| 112 | logger.error(`Failed to install plugin ${plugin.name}:`, error); |
| 113 | throw error; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * 卸载插件 |