feat: commit code
This commit is contained in:
1 parent
d5afbefddb
commit
ded8f86279
6 files changed
+59
-31
No files matched your search
@@ -1,7 +1,7 @@
|
||||
use mlua::{FromLua, Function};
|
||||
|
||||
pub struct PluginInfo<'lua> {
|
||||
name: String,
|
||||
pub name: String,
|
||||
repository: String,
|
||||
author: String,
|
||||
|
||||
|
||||
File renamed without changes.
+28
-11
@@ -1,21 +1,21 @@
|
||||
use std::{fs::create_dir_all, path::PathBuf, io::Read};
|
||||
use std::{fs::create_dir_all, io::Read, path::PathBuf};
|
||||
|
||||
use mlua::Lua;
|
||||
use mlua::{Lua, Table};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::tools::app_path;
|
||||
use crate::tools::{app_path, clone_repo};
|
||||
|
||||
use self::{log::PluginLogger, interface::PluginInfo};
|
||||
use self::{interface::PluginInfo, logger::PluginLogger};
|
||||
|
||||
pub mod log;
|
||||
pub mod logger;
|
||||
|
||||
mod interface;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PluginConfig {
|
||||
available: bool,
|
||||
required: Vec<String>,
|
||||
pub available: bool,
|
||||
pub required: Vec<String>,
|
||||
}
|
||||
|
||||
pub struct PluginManager {
|
||||
@@ -24,18 +24,19 @@ pub struct PluginManager {
|
||||
|
||||
impl PluginManager {
|
||||
pub fn init(config: &PluginConfig) -> Option<Self> {
|
||||
if config.available {
|
||||
if !config.available {
|
||||
return None;
|
||||
}
|
||||
|
||||
let lua = Lua::new();
|
||||
|
||||
let manager = lua.create_table().ok()?;
|
||||
let manager = lua.create_table().unwrap();
|
||||
|
||||
lua.globals().set("plugin_logger", PluginLogger).unwrap();
|
||||
|
||||
let plugin_dir = Self::init_plugin_dir();
|
||||
let mut index = 0;
|
||||
println!("{plugin_dir:?}");
|
||||
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() {
|
||||
@@ -51,16 +52,32 @@ impl PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
lua.globals().set("manager", manager).ok()?;
|
||||
lua.globals().set("manager", manager).unwrap();
|
||||
|
||||
Some(Self { lua })
|
||||
}
|
||||
|
||||
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) {
|
||||
let v = manager.get::<i32, PluginInfo>(i)?;
|
||||
println!("{:?}", v.name);
|
||||
let code = format!("manager[{i}].onLoad()");
|
||||
lua.load(&code).exec()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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() {
|
||||
create_dir_all(&plugin_path).unwrap();
|
||||
let url = "https://github.com/DioxusLabs/cli-plugin-library";
|
||||
clone_repo(&plugin_path, url).unwrap();
|
||||
}
|
||||
plugin_path
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user