diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/examples/plugin/init.lua b/examples/plugin/init.lua index d7afea665..564a8ba94 100644 --- a/examples/plugin/init.lua +++ b/examples/plugin/init.lua @@ -1,14 +1,18 @@ -local api = require("interface") -local log = api.log; +local Api = require("interface") +local log = Api.log; -Manager = {} - -Manager.info = { +local manager = { name = "Dioxus-CLI Plugin Demo", repository = "http://github.com/DioxusLabs/cli", author = "YuKun Liu ", } -Manager.onbuild = function () - print("") -end \ No newline at end of file +manager.onLoad = function () + log.info("plugin loaded.") +end + +manager.onStartBuild = function () + log.warn("system start to build") +end + +return manager \ No newline at end of file diff --git a/src/plugin/interface.rs b/src/plugin/interface.rs new file mode 100644 index 000000000..ac2005319 --- /dev/null +++ b/src/plugin/interface.rs @@ -0,0 +1,54 @@ +use mlua::{FromLua, Function}; + +pub struct PluginInfo<'lua> { + name: String, + repository: String, + author: String, + + on_init: Option>, + on_load: Option>, + on_build_start: Option>, + on_build_end: Option>, +} + +impl<'lua> FromLua<'lua> for PluginInfo<'lua> { + fn from_lua(lua_value: mlua::Value<'lua>, lua: &'lua mlua::Lua) -> mlua::Result { + let mut res = Self { + name: String::default(), + repository: String::default(), + author: String::default(), + + on_init: None, + on_load: None, + on_build_start: None, + on_build_end: None, + }; + if let mlua::Value::Table(tab) = lua_value { + if let Ok(v) = tab.get::<_, String>("name") { + res.name = v; + } + if let Ok(v) = tab.get::<_, String>("repository") { + res.repository = v; + } + if let Ok(v) = tab.get::<_, String>("author") { + res.author = v; + } + + if let Ok(v) = tab.get::<_, Function>("onInit") { + res.on_init = Some(v); + } + if let Ok(v) = tab.get::<_, Function>("onLoad") { + res.on_load = Some(v); + } + if let Ok(v) = tab.get::<_, Function>("onBuildStart") { + res.on_build_start = Some(v); + } + if let Ok(v) = tab.get::<_, Function>("onBuildEnd") { + res.on_build_end = Some(v); + } + + } + + Ok(res) + } +} diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 44a08cd74..da312aa00 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,14 +1,17 @@ -use std::{fs::create_dir_all, path::PathBuf}; +use std::{fs::create_dir_all, path::PathBuf, io::Read}; -use anyhow::Ok; use mlua::Lua; use serde::{Deserialize, Serialize}; use walkdir::WalkDir; use crate::tools::app_path; +use self::{log::PluginLogger, interface::PluginInfo}; + pub mod log; +mod interface; + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PluginConfig { available: bool, @@ -29,11 +32,22 @@ impl PluginManager { let manager = lua.create_table().ok()?; + lua.globals().set("plugin_logger", PluginLogger).unwrap(); + let plugin_dir = Self::init_plugin_dir(); + let mut index = 0; for entry in WalkDir::new(plugin_dir).into_iter().filter_map(|e| e.ok()) { let plugin_dir = entry.path().to_path_buf(); if plugin_dir.is_dir() { - + let init_file = plugin_dir.join("init.lua"); + if init_file.is_file() { + let mut file = std::fs::File::open(init_file).unwrap(); + let mut buffer = String::new(); + file.read_to_string(&mut buffer).unwrap(); + let info = lua.load(&buffer).eval::().unwrap(); + let _ = manager.set(index, info); + } + index += 1; } }