Selaa lähdekoodia

feat: commit lua code

mrxiaozhuox 2 vuotta sitten
vanhempi
commit
bd2853a194
7 muutettua tiedostoa jossa 135 lisäystä ja 37 poistoa
  1. 5 1
      .vscode/settings.json
  2. 14 0
      examples/plugin/init.lua
  3. 25 0
      examples/plugin/interface.lua
  4. 3 20
      src/assets/dioxus.toml
  5. 7 16
      src/config.rs
  6. 28 0
      src/plugin/log.rs
  7. 53 0
      src/plugin/mod.rs

+ 5 - 1
.vscode/settings.json

@@ -1 +1,5 @@
-{}
+{
+    "Lua.diagnostics.globals": [
+        "plugin_logger"
+    ]
+}

+ 14 - 0
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 <mrxzx.info@gmail.com>",
+}
+
+Manager.onbuild = function ()
+    print("")
+end

+ 25 - 0
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

+ 3 - 20
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
+required = []

+ 7 - 16
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<PathBuf>,
     pub asset_dir: Option<PathBuf>,
+    
     pub tools: Option<HashMap<String, toml::Value>>,
+    pub plugins: Option<PluginConfig>,
+
     pub sub_package: Option<String>,
 }
 
@@ -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();
-    // }
 }

+ 28 - 0
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(())
+        });
+    }
+}

+ 53 - 0
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<String>,
+}
+
+pub struct PluginManager {
+    lua: Lua,
+}
+
+impl PluginManager {
+    pub fn init(config: &PluginConfig) -> Option<Self> {
+        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
+    }
+}