From bd2853a1945f561441820f61e5d656ba65ebddcc Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Mon, 25 Jul 2022 08:10:06 +0800 Subject: [PATCH] feat: commit lua code --- .vscode/settings.json | 6 +++- examples/plugin/init.lua | 14 +++++++++ examples/plugin/interface.lua | 25 +++++++++++++++++ src/assets/dioxus.toml | 23 ++------------- src/config.rs | 23 +++++---------- src/plugin/log.rs | 28 ++++++++++++++++++ src/plugin/mod.rs | 53 +++++++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 37 deletions(-) create mode 100644 examples/plugin/init.lua create mode 100644 examples/plugin/interface.lua create mode 100644 src/plugin/log.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 0967ef424..e56c13485 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1 +1,5 @@ -{} +{ + "Lua.diagnostics.globals": [ + "plugin_logger" + ] +} diff --git a/examples/plugin/init.lua b/examples/plugin/init.lua new file mode 100644 index 000000000..d7afea665 --- /dev/null +++ b/examples/plugin/init.lua @@ -0,0 +1,14 @@ +local api = require("interface") +local log = api.log; + +Manager = {} + +Manager.info = { + name = "Dioxus-CLI Plugin Demo", + repository = "http://github.com/DioxusLabs/cli", + author = "YuKun Liu ", +} + +Manager.onbuild = function () + print("") +end \ No newline at end of file diff --git a/examples/plugin/interface.lua b/examples/plugin/interface.lua new file mode 100644 index 000000000..bc6c91cb1 --- /dev/null +++ b/examples/plugin/interface.lua @@ -0,0 +1,25 @@ +local interface = {} + +if plugin_logger ~= nil then + interface.log = plugin_logger +else + interface.log = { + trace = function (info) + print("trace: " .. info) + end, + debug = function (info) + print("debug: " .. info) + end, + info = function (info) + print("info: " .. info) + end, + warn = function (info) + print("warn: " .. info) + end, + error = function (info) + print("error: " .. info) + end, + } +end + +return interface \ No newline at end of file diff --git a/src/assets/dioxus.toml b/src/assets/dioxus.toml index 649aa2c3c..42a1bd3f1 100644 --- a/src/assets/dioxus.toml +++ b/src/assets/dioxus.toml @@ -40,25 +40,8 @@ script = [] # serve: [dev-server] only script = [] -[application.tools] +[application.plugins] -# use binaryen.wasm-opt for output Wasm file -# binaryen just will trigger in `web` platform -binaryen = { wasm_opt = true } +available = true -# use sass auto will auto check sass file and build it. - - -# [application.tools.sass] - -# auto will check the assets dirs, and auto to transform all scss file to css file. -# input = "*" - -# or you can specify some scss file -> css file -# input = [ -# # some sass file path -# # this file will translate to `/css/test.css` -# "/css/test.scss" -# ] - -# source_map = true \ No newline at end of file +required = [] \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 3952f973e..3d6fe65a4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use crate::error::Result; +use crate::{error::Result, plugin::PluginConfig}; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, fs::File, io::Read, path::PathBuf}; @@ -34,7 +34,10 @@ impl Default for DioxusConfig { default_platform: "web".to_string(), out_dir: Some(PathBuf::from("dist")), asset_dir: Some(PathBuf::from("public")), + tools: None, + plugins: None, + sub_package: None, }, web: WebConfig { @@ -66,7 +69,10 @@ pub struct ApplicationConfig { pub default_platform: String, pub out_dir: Option, pub asset_dir: Option, + pub tools: Option>, + pub plugins: Option, + pub sub_package: Option, } @@ -218,19 +224,4 @@ impl CrateConfig { self } - // pub fn with_build_options(&mut self, options: &BuildOptions) { - // if let Some(name) = &options.example { - // self.as_example(name.clone()); - // } - // self.release = options.release; - // self.out_dir = options.outdir.clone().into(); - // } - - // pub fn with_develop_options(&mut self, options: &DevelopOptions) { - // if let Some(name) = &options.example { - // self.as_example(name.clone()); - // } - // self.release = options.release; - // self.out_dir = tempfile::Builder::new().tempdir().expect("").into_path(); - // } } diff --git a/src/plugin/log.rs b/src/plugin/log.rs new file mode 100644 index 000000000..d67ae0316 --- /dev/null +++ b/src/plugin/log.rs @@ -0,0 +1,28 @@ +use log; +use mlua::UserData; + +pub struct PluginLogger; +impl UserData for PluginLogger { + fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_function("trace", |_, info: String| { + log::trace!("{}", info); + Ok(()) + }); + methods.add_function("info", |_, info: String| { + log::info!("{}", info); + Ok(()) + }); + methods.add_function("debug", |_, info: String| { + log::debug!("{}", info); + Ok(()) + }); + methods.add_function("warn", |_, info: String| { + log::warn!("{}", info); + Ok(()) + }); + methods.add_function("error", |_, info: String| { + log::error!("{}", info); + Ok(()) + }); + } +} \ No newline at end of file diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index e69de29bb..44a08cd74 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -0,0 +1,53 @@ +use std::{fs::create_dir_all, path::PathBuf}; + +use anyhow::Ok; +use mlua::Lua; +use serde::{Deserialize, Serialize}; +use walkdir::WalkDir; + +use crate::tools::app_path; + +pub mod log; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PluginConfig { + available: bool, + required: Vec, +} + +pub struct PluginManager { + lua: Lua, +} + +impl PluginManager { + pub fn init(config: &PluginConfig) -> Option { + if config.available { + return None; + } + + let lua = Lua::new(); + + let manager = lua.create_table().ok()?; + + let plugin_dir = Self::init_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() { + + } + } + + lua.globals().set("manager", manager).ok()?; + + Some(Self { lua }) + } + + fn init_plugin_dir() -> PathBuf { + let app_path = app_path(); + let plugin_path = app_path.join("plugins"); + if !plugin_path.is_dir() { + create_dir_all(&plugin_path).unwrap(); + } + plugin_path + } +}