From f915a38ad4a1f352a73642823a46718d15b1569b Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Tue, 2 Aug 2022 15:22:48 +0800 Subject: [PATCH] feat: commit plugin init --- src/plugin/interface/fs.rs | 7 ++++++- src/plugin/interface/mod.rs | 2 +- src/plugin/mod.rs | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/plugin/interface/fs.rs b/src/plugin/interface/fs.rs index 13b5e928e..3557c343a 100644 --- a/src/plugin/interface/fs.rs +++ b/src/plugin/interface/fs.rs @@ -1,5 +1,5 @@ use std::{ - fs::{create_dir, create_dir_all}, + fs::{create_dir, create_dir_all, remove_dir_all}, path::PathBuf, io::{Read, Write}, }; @@ -34,6 +34,11 @@ impl UserData for PluginFileSystem { } 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| { let path = PathBuf::from(path); let mut file = std::fs::File::open(path)?; diff --git a/src/plugin/interface/mod.rs b/src/plugin/interface/mod.rs index c23bc71d1..b5f0b2f30 100644 --- a/src/plugin/interface/mod.rs +++ b/src/plugin/interface/mod.rs @@ -6,7 +6,7 @@ pub mod fs; pub mod download; pub mod dirs; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct PluginInfo<'lua> { pub name: String, pub repository: String, diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 090dbc6f1..1fca4874a 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,7 +1,11 @@ -use std::{io::Read, path::PathBuf}; +use std::{ + io::{Read, Write}, + path::PathBuf, +}; use mlua::{Lua, Table}; use serde::{Deserialize, Serialize}; +use serde_json::json; use crate::tools::{app_path, clone_repo}; @@ -46,7 +50,8 @@ impl PluginManager { lua.globals().set("PLUGIN_DIRS", PluginDirs).unwrap(); 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()? { if entry.is_err() { continue; @@ -60,7 +65,14 @@ impl PluginManager { let mut buffer = String::new(); file.read_to_string(&mut buffer).unwrap(); let info = lua.load(&buffer).eval::().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; } } @@ -71,6 +83,22 @@ impl PluginManager { .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 }) } @@ -78,8 +106,7 @@ impl PluginManager { let lua = &self.lua; let manager = lua.globals().get::<_, Table>("manager")?; for i in 1..(manager.len()? as i32 + 1) { - let v = manager.get::(i)?; - println!("{v:?}"); + let _ = manager.get::(i)?; let code = format!("manager[{i}].on_load()"); lua.load(&code).exec()?; }