Ver Fonte

feat: commit dirs library

mrxiaozhuox há 2 anos atrás
pai
commit
54dc59360e
3 ficheiros alterados com 38 adições e 3 exclusões
  1. 29 0
      src/plugin/interface/dirs.rs
  2. 1 0
      src/plugin/interface/mod.rs
  3. 8 3
      src/plugin/mod.rs

+ 29 - 0
src/plugin/interface/dirs.rs

@@ -0,0 +1,29 @@
+use mlua::UserData;
+
+use crate::tools::app_path;
+
+pub struct PluginDirs;
+impl UserData for PluginDirs {
+    fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
+        methods.add_function("plugin_dir", |_, ()| {
+            let path = app_path().join("plugins");
+            Ok(path.to_str().unwrap().to_string())
+        });
+        methods.add_function("self_dir", |_, name: String| {
+            let path = app_path().join("plugins").join(name);
+            Ok(path.to_str().unwrap().to_string())
+        });
+        methods.add_function("document_dir", |_, ()| {
+            let path = dirs::document_dir().unwrap().to_str().unwrap().to_string();
+            Ok(path)
+        });
+        methods.add_function("download_dir", |_, ()| {
+            let path = dirs::download_dir().unwrap().to_str().unwrap().to_string();
+            Ok(path)
+        });
+        methods.add_function("cache_dir", |_, ()| {
+            let path = dirs::cache_dir().unwrap().to_str().unwrap().to_string();
+            Ok(path)
+        });
+    }
+}

+ 1 - 0
src/plugin/interface/mod.rs

@@ -4,6 +4,7 @@ pub mod logger;
 pub mod command;
 pub mod fs;
 pub mod download;
+pub mod dirs;
 
 #[derive(Debug)]
 pub struct PluginInfo<'lua> {

+ 8 - 3
src/plugin/mod.rs

@@ -7,7 +7,10 @@ use crate::tools::{app_path, clone_repo};
 
 use self::{
     interface::PluginInfo,
-    interface::{command::PluginCommander, logger::PluginLogger, fs::PluginFileSystem, download::PluginDownloader},
+    interface::{
+        command::PluginCommander, download::PluginDownloader, fs::PluginFileSystem,
+        logger::PluginLogger,
+    },
 };
 
 pub mod interface;
@@ -37,7 +40,9 @@ impl PluginManager {
             .set("PLUGIN_COMMAND", PluginCommander)
             .unwrap();
         lua.globals().set("PLUGIN_FS", PluginFileSystem).unwrap();
-        lua.globals().set("PLUGIN_DOWNLOAD", PluginDownloader).unwrap();
+        lua.globals()
+            .set("PLUGIN_DOWNLOAD", PluginDownloader)
+            .unwrap();
 
         let plugin_dir = Self::init_plugin_dir();
         let mut index = 1;
@@ -60,10 +65,10 @@ impl PluginManager {
             }
         }
 
-        lua.globals().set("manager", manager).unwrap();
         lua.globals()
             .set("LIBDIR", plugin_dir.join("library").to_str().unwrap())
             .unwrap();
+        lua.globals().set("manager", manager).unwrap();
 
         Some(Self { lua })
     }