From a04c9e1fb56877e9495af19f90a0928ed4242de6 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Thu, 18 Feb 2021 11:27:03 +0800 Subject: [PATCH] feat(plugin): make plugin manager works --- Cargo.lock | 198 ++++++++++++++++++--------- plugin_manager/Cargo.toml | 10 ++ plugin_manager/src/lib.rs | 12 +- plugin_manager/src/plugin_manager.rs | 36 +++++ plugins/coco_swagger/src/lib.rs | 11 ++ 5 files changed, 192 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 520cbc62..f3df93d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,8 +86,8 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ca8ce00b267af8ccebbd647de0d61e0674b6e61185cc7a592ff88772bed655" dependencies = [ - "quote", - "syn", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -96,8 +96,8 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbcb2b608f0accc2f5bcf3dd872194ce13d94ee45b571487035864cf966b04ef" dependencies = [ - "quote", - "syn", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -275,9 +275,9 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad26f77093333e0e7c6ffe54ebe3582d908a104e448723eec6d43d08b07143fb" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -365,9 +365,9 @@ version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -862,8 +862,8 @@ version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10bcb9d7dcbf7002aaffbb53eac22906b64cdcc127971dcc387d8eb7c95d5560" dependencies = [ - "quote", - "syn", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -892,9 +892,9 @@ version = "0.99.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cb0e6161ad61ed084a36ba71fbba9e3ac5aee3606fb607fe08da6acbcf3d8c" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -986,6 +986,29 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +[[package]] +name = "dlopen" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e80ad39f814a9abe68583cd50a2d45c8a67561c3361ab8da240587dda80937" +dependencies = [ + "dlopen_derive", + "lazy_static", + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "dlopen_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f236d9e1b1fbd81cea0f9cbdc8dcc7e8ebcd80e6659cd7cb2ad5f6c05946c581" +dependencies = [ + "libc", + "quote 0.6.13", + "syn 0.15.44", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -1042,9 +1065,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c5f0096a91d210159eceb2ff5e1c4da18388a170e1e3ce948aac9c8fdbbf595" dependencies = [ "heck", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -1085,9 +1108,9 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", "synstructure", ] @@ -1204,9 +1227,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c287d25add322d9f9abdcdc5927ca398917996600182178774032e9f8258fedd" dependencies = [ "proc-macro-hack", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -2040,9 +2063,9 @@ checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55" dependencies = [ "pest", "pest_meta", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -2080,9 +2103,9 @@ version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -2091,9 +2114,9 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caa25a6393f22ce819b0f50e0be89287292fda8d425be38ee0ca14c4931d9e71" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -2130,6 +2153,12 @@ dependencies = [ [[package]] name = "plugin_manager" version = "0.1.0" +dependencies = [ + "core_model", + "dlopen", + "dlopen_derive", + "plugin_interface", +] [[package]] name = "ppv-lite86" @@ -2187,13 +2216,22 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid 0.1.0", +] + [[package]] name = "proc-macro2" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ - "unicode-xid", + "unicode-xid 0.2.1", ] [[package]] @@ -2220,13 +2258,22 @@ dependencies = [ "memchr", ] +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2 0.4.30", +] + [[package]] name = "quote" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.24", ] [[package]] @@ -2437,10 +2484,10 @@ version = "5.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed91c41c42ef7bf687384439c312e75e0da9c149b0390889b94de3c7d9d9e66" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.24", + "quote 1.0.8", "rust-embed-utils", - "syn", + "syn 1.0.58", "walkdir", ] @@ -2562,9 +2609,9 @@ version = "1.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -2729,11 +2776,11 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.24", + "quote 1.0.8", "serde", "serde_derive", - "syn", + "syn 1.0.58", ] [[package]] @@ -2743,13 +2790,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" dependencies = [ "base-x", - "proc-macro2", - "quote", + "proc-macro2 1.0.24", + "quote 1.0.8", "serde", "serde_derive", "serde_json", "sha1", - "syn", + "syn 1.0.58", ] [[package]] @@ -2811,15 +2858,26 @@ dependencies = [ "symbolic-common 8.0.3", ] +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", +] + [[package]] name = "syn" version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", + "proc-macro2 1.0.24", + "quote 1.0.8", + "unicode-xid 0.2.1", ] [[package]] @@ -2828,10 +2886,10 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ - "proc-macro2", - "quote", - "syn", - "unicode-xid", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", + "unicode-xid 0.2.1", ] [[package]] @@ -2929,9 +2987,9 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", ] [[package]] @@ -2995,10 +3053,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5c3be1edfad6027c69f5491cf4cb310d1a71ecd6af742788c6ff8bced86b8fa" dependencies = [ "proc-macro-hack", - "proc-macro2", - "quote", + "proc-macro2 1.0.24", + "quote 1.0.8", "standback", - "syn", + "syn 1.0.58", ] [[package]] @@ -3281,6 +3339,12 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + [[package]] name = "unicode-xid" version = "0.2.1" @@ -3377,9 +3441,9 @@ dependencies = [ "bumpalo", "lazy_static", "log", - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", "wasm-bindgen-shared", ] @@ -3389,7 +3453,7 @@ version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c" dependencies = [ - "quote", + "quote 1.0.8", "wasm-bindgen-macro-support", ] @@ -3399,9 +3463,9 @@ version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/plugin_manager/Cargo.toml b/plugin_manager/Cargo.toml index b12626c6..0b818b66 100644 --- a/plugin_manager/Cargo.toml +++ b/plugin_manager/Cargo.toml @@ -7,3 +7,13 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +dlopen = "0.1.8" +dlopen_derive = "0.1.4" + + +[dependencies.core_model] +path = "../core_model" + + +[dependencies.plugin_interface] +path = "../plugin_interface" diff --git a/plugin_manager/src/lib.rs b/plugin_manager/src/lib.rs index 16fb078f..60330812 100644 --- a/plugin_manager/src/lib.rs +++ b/plugin_manager/src/lib.rs @@ -1,9 +1,5 @@ -pub mod plugin_manager; +extern crate dlopen; +#[macro_use] +extern crate dlopen_derive; -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +pub mod plugin_manager; diff --git a/plugin_manager/src/plugin_manager.rs b/plugin_manager/src/plugin_manager.rs index d482be5e..5882666f 100644 --- a/plugin_manager/src/plugin_manager.rs +++ b/plugin_manager/src/plugin_manager.rs @@ -1 +1,37 @@ +use dlopen::wrapper::{Container, WrapperApi}; + +use plugin_interface::PluginInterface; +use std::path::PathBuf; + +#[derive(WrapperApi)] +struct Wrapper { + plugin: fn() -> Box, +} + pub struct PluginManager {} + +impl PluginManager { + #[allow(dead_code)] + fn run() { + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let plugin_path = "target/debug/libcoco_swagger.dylib"; + let path = root.parent().unwrap().join(plugin_path); + + let cont: Container = + unsafe { Container::load(path) }.expect("Could not open library or load symbols"); + + let plugin = cont.plugin(); + println!("{:?}", plugin.name()); + } +} + +#[cfg(test)] +mod tests { + use crate::plugin_manager::PluginManager; + + #[ignore] + #[test] + fn test_plugin_run_in_local() { + PluginManager::run(); + } +} diff --git a/plugins/coco_swagger/src/lib.rs b/plugins/coco_swagger/src/lib.rs index 9553c7aa..fb3fceec 100644 --- a/plugins/coco_swagger/src/lib.rs +++ b/plugins/coco_swagger/src/lib.rs @@ -16,3 +16,14 @@ impl PluginInterface for CocoSwagger { println!("{:?}", config); } } + +impl Default for CocoSwagger { + fn default() -> Self { + CocoSwagger {} + } +} + +#[no_mangle] +pub fn plugin() -> Box { + Box::new(CocoSwagger::default()) +}