feat: commit code

This commit is contained in:
mrxiaozhuox 2022-07-25 13:53:45 +08:00
parent bd2853a194
commit d5afbefddb
4 changed files with 83 additions and 11 deletions

0
examples/README.md Normal file
View file

View file

@ -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 <mrxzx.info@gmail.com>",
}
Manager.onbuild = function ()
print("")
end
manager.onLoad = function ()
log.info("plugin loaded.")
end
manager.onStartBuild = function ()
log.warn("system start to build")
end
return manager

54
src/plugin/interface.rs Normal file
View file

@ -0,0 +1,54 @@
use mlua::{FromLua, Function};
pub struct PluginInfo<'lua> {
name: String,
repository: String,
author: String,
on_init: Option<Function<'lua>>,
on_load: Option<Function<'lua>>,
on_build_start: Option<Function<'lua>>,
on_build_end: Option<Function<'lua>>,
}
impl<'lua> FromLua<'lua> for PluginInfo<'lua> {
fn from_lua(lua_value: mlua::Value<'lua>, lua: &'lua mlua::Lua) -> mlua::Result<Self> {
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)
}
}

View file

@ -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::<mlua::Value>().unwrap();
let _ = manager.set(index, info);
}
index += 1;
}
}