feat: commit plugin interface
This commit is contained in:
1 parent
a21433016a
commit
da53c67841
8 files changed
+222
-140
No files matched your search
@@ -1,54 +0,0 @@
|
||||
use mlua::{FromLua, Function};
|
||||
|
||||
pub struct PluginInfo<'lua> {
|
||||
pub 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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
use mlua::UserData;
|
||||
|
||||
pub struct PluginCommander;
|
||||
impl UserData for PluginCommander {
|
||||
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||
methods.add_function("exec", |_, cmd: Vec<String>| {
|
||||
if cmd.len() == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let cmd_name = cmd.get(0).unwrap();
|
||||
let mut command = Command::new(cmd_name);
|
||||
let t = cmd
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| *i > 0)
|
||||
.map(|v| v.1.clone())
|
||||
.collect::<Vec<String>>();
|
||||
command.args(t);
|
||||
command.stdout(Stdio::inherit());
|
||||
command.output()?;
|
||||
Ok(())
|
||||
});
|
||||
methods.add_function("execQuiet", |_, cmd: Vec<String>| {
|
||||
if cmd.len() == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let cmd_name = cmd.get(0).unwrap();
|
||||
let mut command = Command::new(cmd_name);
|
||||
let t = cmd
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| *i > 0)
|
||||
.map(|v| v.1.clone())
|
||||
.collect::<Vec<String>>();
|
||||
command.args(t);
|
||||
command.stdout(Stdio::null());
|
||||
command.output()?;
|
||||
Ok(())
|
||||
});
|
||||
methods.add_function("execSimple", |_, cmd: String| {
|
||||
let _ = Command::new(cmd).stdout(Stdio::inherit()).output()?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(_fields: &mut F) {
|
||||
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,84 @@
|
||||
use mlua::{FromLua, Function, ToLua};
|
||||
|
||||
pub mod logger;
|
||||
pub mod command;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PluginInfo<'lua> {
|
||||
pub name: String,
|
||||
pub repository: String,
|
||||
pub author: String,
|
||||
|
||||
pub on_init: Option<Function<'lua>>,
|
||||
pub on_load: Option<Function<'lua>>,
|
||||
pub on_build_start: Option<Function<'lua>>,
|
||||
pub on_build_finish: 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_finish: 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>("onBuildFinish") {
|
||||
res.on_build_finish = Some(v);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> ToLua<'lua> for PluginInfo<'lua> {
|
||||
fn to_lua(self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
|
||||
|
||||
let res = lua.create_table()?;
|
||||
|
||||
res.set("name", self.name.to_string())?;
|
||||
res.set("repository", self.repository.to_string())?;
|
||||
res.set("author", self.author.to_string())?;
|
||||
|
||||
if let Some(e) = self.on_init {
|
||||
res.set("onInit", e)?;
|
||||
}
|
||||
if let Some(e) = self.on_load {
|
||||
res.set("onLoad", e)?;
|
||||
}
|
||||
if let Some(e) = self.on_build_start {
|
||||
res.set("onBuildStart", e)?;
|
||||
}
|
||||
if let Some(e) = self.on_build_finish {
|
||||
res.set("onBuildFinish", e)?;
|
||||
}
|
||||
|
||||
Ok(mlua::Value::Table(res))
|
||||
}
|
||||
}
|
||||
+15
-18
@@ -1,16 +1,13 @@
|
||||
use std::{fs::create_dir_all, io::Read, path::PathBuf};
|
||||
use std::{io::Read, path::PathBuf};
|
||||
|
||||
use mlua::{Lua, Table};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::tools::{app_path, clone_repo};
|
||||
|
||||
use self::{interface::PluginInfo, logger::PluginLogger};
|
||||
use self::{interface::{logger::PluginLogger, command::PluginCommander}, interface::PluginInfo};
|
||||
|
||||
pub mod logger;
|
||||
|
||||
mod interface;
|
||||
pub mod interface;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PluginConfig {
|
||||
@@ -33,10 +30,15 @@ impl PluginManager {
|
||||
let manager = lua.create_table().unwrap();
|
||||
|
||||
lua.globals().set("plugin_logger", PluginLogger).unwrap();
|
||||
lua.globals().set("plugin_commander", PluginCommander).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 mut index = 1;
|
||||
for entry in std::fs::read_dir(&plugin_dir).ok()? {
|
||||
if entry.is_err() {
|
||||
continue;
|
||||
}
|
||||
let entry = entry.unwrap();
|
||||
let plugin_dir = entry.path().to_path_buf();
|
||||
if plugin_dir.is_dir() {
|
||||
let init_file = plugin_dir.join("init.lua");
|
||||
@@ -44,16 +46,13 @@ impl PluginManager {
|
||||
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 info = lua.load(&buffer).eval::<PluginInfo>().unwrap();
|
||||
let _ = manager.set(index, info);
|
||||
index += 1;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
lua.globals()
|
||||
.set("package.path", format!("{}", plugin_dir.display())).unwrap();
|
||||
|
||||
lua.globals().set("manager", manager).unwrap();
|
||||
|
||||
Some(Self { lua })
|
||||
@@ -62,10 +61,9 @@ impl PluginManager {
|
||||
pub fn load_all_plugins(&self) -> anyhow::Result<()> {
|
||||
let lua = &self.lua;
|
||||
let manager = lua.globals().get::<_, Table>("manager")?;
|
||||
println!("{:?}", manager.len());
|
||||
for i in 0..(manager.len()? as i32) {
|
||||
for i in 1..(manager.len()? as i32 + 1) {
|
||||
let v = manager.get::<i32, PluginInfo>(i)?;
|
||||
println!("{:?}", v.name);
|
||||
println!("{v:?}");
|
||||
let code = format!("manager[{i}].onLoad()");
|
||||
lua.load(&code).exec()?;
|
||||
}
|
||||
@@ -73,11 +71,10 @@ impl PluginManager {
|
||||
}
|
||||
|
||||
fn init_plugin_dir() -> PathBuf {
|
||||
log::info!("📖 Start to init plugin library ...");
|
||||
|
||||
let app_path = app_path();
|
||||
let plugin_path = app_path.join("plugins");
|
||||
if !plugin_path.is_dir() {
|
||||
log::info!("📖 Start to init plugin library ...");
|
||||
let url = "https://github.com/DioxusLabs/cli-plugin-library";
|
||||
clone_repo(&plugin_path, url).unwrap();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user