feat: 实现动态插件系统 (6阶段完成)
- 阶段1: 消息类型序列化 (Serialize/Deserialize, &'static str → String) - 阶段2: FFI 边界类型 + Plugin SDK (plugin_abi, showen-plugin-sdk crate) - 阶段3: PluginLoader + DynamicPlugin (libloading 动态加载 .so) - 阶段4: 版本管理 + 错误策略 (VersionManager, PluginState, 自动回退) - 阶段5: 远程仓库客户端 (HTTP 下载 + tar.gz 安装) - 阶段6: 示例插件 + HTTP 管理 API + 全目录 README 文档 54/54 测试通过,0 warnings。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
19
plugins/README.md
Normal file
19
plugins/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# plugins/ — 外部动态插件
|
||||
|
||||
此目录存放独立编译的动态插件项目(cdylib crate)。
|
||||
|
||||
## 目录
|
||||
|
||||
| 目录 | 说明 |
|
||||
|------|------|
|
||||
| `example-plugin/` | 示例插件,演示 SDK 用法 |
|
||||
|
||||
## 开发流程
|
||||
|
||||
1. 创建新 crate,依赖 `showen-plugin-sdk`
|
||||
2. 实现 `ShowenPlugin` trait
|
||||
3. 用 `export_plugin!` 宏导出
|
||||
4. `cargo build --release` 编译为 `.so`
|
||||
5. 将产物放入 `plugin_store/<id>/<version>/`
|
||||
|
||||
详见 `plugin-sdk/README.md`。
|
||||
12
plugins/example-plugin/Cargo.toml
Normal file
12
plugins/example-plugin/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "showen-example-plugin"
|
||||
version = "0.1.0"
|
||||
authors = ["showen"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
showen-plugin-sdk = { path = "../../plugin-sdk" }
|
||||
serde_json = "1"
|
||||
29
plugins/example-plugin/README.md
Normal file
29
plugins/example-plugin/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Example Plugin — 示例动态插件
|
||||
|
||||
演示如何使用 `showen-plugin-sdk` 编写动态插件。
|
||||
|
||||
## 功能
|
||||
|
||||
- 仅打印日志,用于验证动态加载流程
|
||||
- 展示 `ShowenPlugin` trait 的完整实现
|
||||
- 编译为 `cdylib`(`.so` 文件)
|
||||
|
||||
## 编译
|
||||
|
||||
```bash
|
||||
cd plugins/example-plugin
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
产物: `target/release/libshowen_example_plugin.so`
|
||||
|
||||
## 安装
|
||||
|
||||
将 `.so` 和 `manifest.json` 放入 `plugin_store/example-plugin/<version>/` 目录即可被主程序动态加载。
|
||||
|
||||
## 文件
|
||||
|
||||
| 文件 | 说明 |
|
||||
|------|------|
|
||||
| `src/lib.rs` | 插件实现,使用 `export_plugin!` 宏导出 |
|
||||
| `Cargo.toml` | crate 配置,类型为 cdylib |
|
||||
72
plugins/example-plugin/src/lib.rs
Normal file
72
plugins/example-plugin/src/lib.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
//! 示例动态插件 — 展示如何使用 showen-plugin-sdk 编写插件
|
||||
//!
|
||||
//! 此插件仅打印日志,用于验证动态加载流程。
|
||||
|
||||
use showen_plugin_sdk::{
|
||||
export_plugin, Message, MessageSender, PluginInfo, ShowenPlugin,
|
||||
};
|
||||
|
||||
pub struct ExamplePlugin {
|
||||
sender: Option<MessageSender>,
|
||||
}
|
||||
|
||||
impl ExamplePlugin {
|
||||
pub fn new() -> Self {
|
||||
Self { sender: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl ShowenPlugin for ExamplePlugin {
|
||||
fn info(&self) -> PluginInfo {
|
||||
PluginInfo {
|
||||
name: "example-plugin".to_string(),
|
||||
version: "0.1.0".to_string(),
|
||||
description: "示例动态插件".to_string(),
|
||||
platform: "Any".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn init(&mut self, config_json: &str, sender: MessageSender) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] init called, config length: {}", config_json.len());
|
||||
self.sender = Some(sender);
|
||||
|
||||
// 通知主程序就绪
|
||||
if let Some(sender) = &self.sender {
|
||||
sender.send_to_manager(
|
||||
"example-plugin",
|
||||
Message::PluginReady("example-plugin".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start(&mut self) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] started");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_message(&mut self, message: Message) -> Result<(), String> {
|
||||
match &message {
|
||||
Message::Shutdown => {
|
||||
eprintln!("[ExamplePlugin] received shutdown");
|
||||
}
|
||||
Message::Custom { kind, payload } => {
|
||||
eprintln!("[ExamplePlugin] custom message: kind={kind}, payload={payload}");
|
||||
}
|
||||
_ => {
|
||||
eprintln!("[ExamplePlugin] received message: {:?}", message);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] stopped");
|
||||
self.sender = None;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// 导出 FFI 接口
|
||||
export_plugin!(ExamplePlugin, ExamplePlugin::new());
|
||||
Reference in New Issue
Block a user