feat: commit plugin init
This commit is contained in:
1 parent
7609406bcd
commit
f915a38ad4
3 files changed
+39
-7
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs::{create_dir, create_dir_all},
|
fs::{create_dir, create_dir_all, remove_dir_all},
|
||||||
path::PathBuf, io::{Read, Write},
|
path::PathBuf, io::{Read, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -34,6 +34,11 @@ impl UserData for PluginFileSystem {
|
|||||||
}
|
}
|
||||||
Ok(true)
|
Ok(true)
|
||||||
});
|
});
|
||||||
|
methods.add_function("remove_dir", |_, path: String| {
|
||||||
|
let path = PathBuf::from(path);
|
||||||
|
let r = remove_dir_all(path);
|
||||||
|
Ok(r.is_ok())
|
||||||
|
});
|
||||||
methods.add_function("file_get_content", |_, path: String| {
|
methods.add_function("file_get_content", |_, path: String| {
|
||||||
let path = PathBuf::from(path);
|
let path = PathBuf::from(path);
|
||||||
let mut file = std::fs::File::open(path)?;
|
let mut file = std::fs::File::open(path)?;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ pub mod fs;
|
|||||||
pub mod download;
|
pub mod download;
|
||||||
pub mod dirs;
|
pub mod dirs;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PluginInfo<'lua> {
|
pub struct PluginInfo<'lua> {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub repository: String,
|
pub repository: String,
|
||||||
|
|||||||
+32
-5
@@ -1,7 +1,11 @@
|
|||||||
use std::{io::Read, path::PathBuf};
|
use std::{
|
||||||
|
io::{Read, Write},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
use mlua::{Lua, Table};
|
use mlua::{Lua, Table};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::tools::{app_path, clone_repo};
|
use crate::tools::{app_path, clone_repo};
|
||||||
|
|
||||||
@@ -46,7 +50,8 @@ impl PluginManager {
|
|||||||
lua.globals().set("PLUGIN_DIRS", PluginDirs).unwrap();
|
lua.globals().set("PLUGIN_DIRS", PluginDirs).unwrap();
|
||||||
|
|
||||||
let plugin_dir = Self::init_plugin_dir();
|
let plugin_dir = Self::init_plugin_dir();
|
||||||
let mut index = 1;
|
let mut index: u32 = 1;
|
||||||
|
let mut init_list: Vec<(u32, PathBuf, PluginInfo)> = Vec::new();
|
||||||
for entry in std::fs::read_dir(&plugin_dir).ok()? {
|
for entry in std::fs::read_dir(&plugin_dir).ok()? {
|
||||||
if entry.is_err() {
|
if entry.is_err() {
|
||||||
continue;
|
continue;
|
||||||
@@ -60,7 +65,14 @@ impl PluginManager {
|
|||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
file.read_to_string(&mut buffer).unwrap();
|
file.read_to_string(&mut buffer).unwrap();
|
||||||
let info = lua.load(&buffer).eval::<PluginInfo>().unwrap();
|
let info = lua.load(&buffer).eval::<PluginInfo>().unwrap();
|
||||||
let _ = manager.set(index, info);
|
let _ = manager.set(index, info.clone());
|
||||||
|
|
||||||
|
// call `on_init` if file "dcp.json" not exists
|
||||||
|
let dcp_file = plugin_dir.join("dcp.json");
|
||||||
|
if !dcp_file.is_file() {
|
||||||
|
init_list.push((index, dcp_file, info));
|
||||||
|
}
|
||||||
|
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,6 +83,22 @@ impl PluginManager {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
lua.globals().set("manager", manager).unwrap();
|
lua.globals().set("manager", manager).unwrap();
|
||||||
|
|
||||||
|
for (idx, path, info) in init_list {
|
||||||
|
let res = lua.load(&format!("manager[{idx}].on_init()")).exec();
|
||||||
|
if res.is_ok() {
|
||||||
|
let mut file = std::fs::File::create(path).unwrap();
|
||||||
|
let buffer = json!({
|
||||||
|
"name": info.name,
|
||||||
|
"author": info.author,
|
||||||
|
"repository": info.repository,
|
||||||
|
"version": info.version,
|
||||||
|
})
|
||||||
|
.to_string();
|
||||||
|
let buffer = buffer.as_bytes();
|
||||||
|
file.write_all(buffer).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Some(Self { lua })
|
Some(Self { lua })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +106,7 @@ impl PluginManager {
|
|||||||
let lua = &self.lua;
|
let lua = &self.lua;
|
||||||
let manager = lua.globals().get::<_, Table>("manager")?;
|
let manager = lua.globals().get::<_, Table>("manager")?;
|
||||||
for i in 1..(manager.len()? as i32 + 1) {
|
for i in 1..(manager.len()? as i32 + 1) {
|
||||||
let v = manager.get::<i32, PluginInfo>(i)?;
|
let _ = manager.get::<i32, PluginInfo>(i)?;
|
||||||
println!("{v:?}");
|
|
||||||
let code = format!("manager[{i}].on_load()");
|
let code = format!("manager[{i}].on_load()");
|
||||||
lua.load(&code).exec()?;
|
lua.load(&code).exec()?;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user