From 85e164361856ecc30eb185ee9a22810acc1f93e6 Mon Sep 17 00:00:00 2001 From: Elliot Xu Date: Sat, 20 Feb 2021 16:15:12 +0800 Subject: [PATCH] feat(plugin): support for loading plugin config and run it --- Cargo.lock | 1 + Cargo.toml | 3 +++ core_model/src/coco_config.rs | 6 +++++- plugin_manager/src/plugin_manager.rs | 7 +++---- src/bin/coco.rs | 14 +++++++++++++- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9102332d..ee75b2b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -643,6 +643,7 @@ dependencies = [ "mime_guess", "pest", "pest_derive", + "plugin_manager", "rayon", "regex", "rust-embed", diff --git a/Cargo.toml b/Cargo.toml index c7806d7d..b28f9c47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,6 +96,9 @@ path = "framework" [dependencies.core_model] path = "core_model" +[dependencies.plugin_manager] +path = "plugin_manager" + [workspace] members = [ 'framework', diff --git a/core_model/src/coco_config.rs b/core_model/src/coco_config.rs index 1ee8ac0a..84f31e3c 100644 --- a/core_model/src/coco_config.rs +++ b/core_model/src/coco_config.rs @@ -4,11 +4,15 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct CocoConfig { pub repo: Vec, + pub plugins: Vec, } impl Default for CocoConfig { fn default() -> Self { - CocoConfig { repo: vec![] } + CocoConfig { + repo: vec![], + plugins: vec![], + } } } diff --git a/plugin_manager/src/plugin_manager.rs b/plugin_manager/src/plugin_manager.rs index 5882666f..7bc4ba5b 100644 --- a/plugin_manager/src/plugin_manager.rs +++ b/plugin_manager/src/plugin_manager.rs @@ -11,10 +11,9 @@ struct Wrapper { pub struct PluginManager {} impl PluginManager { - #[allow(dead_code)] - fn run() { + pub fn run(plugin_name: &str) { let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let plugin_path = "target/debug/libcoco_swagger.dylib"; + let plugin_path = format!("target/debug/libcoco_{}.dylib", plugin_name); let path = root.parent().unwrap().join(plugin_path); let cont: Container = @@ -32,6 +31,6 @@ mod tests { #[ignore] #[test] fn test_plugin_run_in_local() { - PluginManager::run(); + PluginManager::run("swagger"); } } diff --git a/src/bin/coco.rs b/src/bin/coco.rs index d033655e..3bc1e9a4 100644 --- a/src/bin/coco.rs +++ b/src/bin/coco.rs @@ -10,6 +10,7 @@ use coco::app::{cloc_analysis, framework_analysis}; use coco::infrastructure::url_format; use core_model::Settings; use core_model::{CocoConfig, RepoConfig}; +use plugin_manager::plugin_manager::PluginManager; use std::time::Instant; const VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -37,6 +38,7 @@ fn main() { println!("found config file: {}", config_file); + run_plugins(&config); run_analysis(config.repo, cli_option); } @@ -49,7 +51,10 @@ fn create_config(config_file: &str) -> CocoConfig { repo.push(RepoConfig { url: current.into_os_string().to_str().unwrap().to_string(), }); - CocoConfig { repo } + CocoConfig { + repo, + plugins: vec![], + } } } } @@ -135,6 +140,12 @@ fn analysis_architecture(url_str: &str) { fs::write(output_file, branches_info).expect("cannot write file"); } +fn run_plugins(config: &CocoConfig) { + for plugin in config.plugins.iter() { + PluginManager::run(&plugin); + } +} + #[cfg(test)] mod test { use crate::create_config; @@ -148,5 +159,6 @@ mod test { assert_eq!(config.repo.len(), 1); assert_eq!(url, config.repo[0].url); + assert_eq!(config.plugins.len(), 0); } }