feat: make new plugin api

This commit is contained in:
mrxiaozhuox committed 2022-08-14 18:30:34 -07:00
1 parent b28043a944
commit 6f4028f065
7 files changed
+62 -32

No files matched your search

+1 -17
View File
@@ -5,25 +5,9 @@ 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", |_, ()| {
methods.add_function("plugins_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)
});
}
}
File renamed without changes.
+4 -2
View File
@@ -1,10 +1,12 @@
use mlua::{FromLua, Function, ToLua};
pub mod logger;
pub mod log;
pub mod command;
pub mod fs;
pub mod download;
pub mod network;
pub mod dirs;
pub mod path;
pub mod os;
#[derive(Debug, Clone)]
pub struct PluginInfo<'lua> {
@@ -2,8 +2,8 @@ use std::{io::Cursor, path::PathBuf};
use mlua::UserData;
pub struct PluginDownloader;
impl UserData for PluginDownloader {
pub struct PluginNetwork;
impl UserData for PluginNetwork {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_function("download_file", |_, args: (String, String)| async move {
let url = args.0;
+20
View File
@@ -0,0 +1,20 @@
use std::path::PathBuf;
use mlua::UserData;
pub struct PluginOS;
impl UserData for PluginOS {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function("current_platform", |_, ()| {
if cfg!(target_os = "windows") {
Ok("windows")
} else if cfg!(target_os = "macos") {
Ok("macos")
} else if cfg!(target_os = "linux") {
Ok("linux")
} else {
panic!("unsupported platformm");
}
});
}
}
+14
View File
@@ -0,0 +1,14 @@
use std::path::PathBuf;
use mlua::UserData;
pub struct PluginPath;
impl UserData for PluginPath {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function("join", |_, args: (String, String)| {
let current_path = PathBuf::from(args.0);
let new_path = current_path.join(args.1);
Ok(new_path.to_str().unwrap().to_string())
});
}
}
+21 -11
View File
@@ -12,8 +12,8 @@ use crate::tools::{app_path, clone_repo};
use self::{
interface::PluginInfo,
interface::{
command::PluginCommander, dirs::PluginDirs, download::PluginDownloader,
fs::PluginFileSystem, logger::PluginLogger,
command::PluginCommander, dirs::PluginDirs, fs::PluginFileSystem, log::PluginLogger,
network::PluginNetwork,
},
};
@@ -39,15 +39,25 @@ impl PluginManager {
let manager = lua.create_table().unwrap();
lua.globals().set("PLUGIN_LOGGER", PluginLogger).unwrap();
lua.globals()
.set("PLUGIN_COMMAND", PluginCommander)
.unwrap();
lua.globals().set("PLUGIN_FS", PluginFileSystem).unwrap();
lua.globals()
.set("PLUGIN_DOWNLOAD", PluginDownloader)
.unwrap();
lua.globals().set("PLUGIN_DIRS", PluginDirs).unwrap();
let api = lua.create_table().unwrap();
api.set("log", PluginLogger).unwrap();
api.set("command", PluginCommander).unwrap();
api.set("network", PluginNetwork).unwrap();
api.set("dirs", PluginDirs).unwrap();
api.set("fs", PluginFileSystem).unwrap();
lua.globals().set("plugin_lib", api).unwrap();
// lua.globals().set("PLUGIN_LOGGER", PluginLogger).unwrap();
// lua.globals()
// .set("PLUGIN_COMMAND", PluginCommander)
// .unwrap();
// lua.globals().set("PLUGIN_FS", PluginFileSystem).unwrap();
// lua.globals()
// .set("PLUGIN_DOWNLOAD", PluginDownloader)
// .unwrap();
// lua.globals().set("PLUGIN_DIRS", PluginDirs).unwrap();
let plugin_dir = Self::init_plugin_dir();
let mut index: u32 = 1;