From 6ee9a48e20b990ec785a00f377d3f66f1c42bb4a Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 18 Dec 2017 01:52:08 -0800 Subject: [PATCH] Use plugin chain code in Vfs --- src/bin.rs | 11 ++++++++++- src/plugin.rs | 15 +++++++++++++-- src/vfs.rs | 23 +++++++++++++++++------ src/web.rs | 1 - 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 18378c7f..57520201 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -33,6 +33,8 @@ use std::thread; use core::Config; use pathext::canonicalish; use project::{Project, ProjectLoadError}; +use plugin::{PluginChain}; +use plugins::{DefaultPlugin, ScriptPlugin}; use vfs::Vfs; use vfs_watch::VfsWatcher; @@ -148,6 +150,13 @@ fn main() { } }; + lazy_static! { + static ref PLUGIN_CHAIN: PluginChain = PluginChain::new(vec![ + Box::new(ScriptPlugin::new()), + Box::new(DefaultPlugin::new()), + ]); + } + let config = Config { port, verbose, @@ -159,7 +168,7 @@ fn main() { } let vfs = { - let mut vfs = Vfs::new(config.clone()); + let mut vfs = Vfs::new(config.clone(), &PLUGIN_CHAIN); for (name, project_partition) in &project.partitions { let path = { diff --git a/src/plugin.rs b/src/plugin.rs index fa56a701..cd0c90a8 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -18,11 +18,11 @@ pub trait Plugin { } pub struct PluginChain { - plugins: Vec>, + plugins: Vec>, } impl PluginChain { - pub fn new(plugins: Vec>) -> PluginChain { + pub fn new(plugins: Vec>) -> PluginChain { PluginChain { plugins, } @@ -38,4 +38,15 @@ impl PluginChain { None } + + pub fn handle_file_change(&self, route: &Route) -> Option> { + for plugin in &self.plugins { + match plugin.handle_file_change(route) { + FileChangeResult::MarkChanged(changes) => return changes, + FileChangeResult::Pass => {}, + } + } + + None + } } diff --git a/src/vfs.rs b/src/vfs.rs index 8b39ced4..483a93b2 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -6,6 +6,7 @@ use std::path::{Path, PathBuf}; use std::time::Instant; use core::Config; +use plugin::PluginChain; /// Represents a virtual layer over multiple parts of the filesystem. /// @@ -24,6 +25,8 @@ pub struct Vfs { /// created, along with a timestamp denoting when. pub change_history: Vec, + plugin_chain: &'static PluginChain, + config: Config, } @@ -51,11 +54,12 @@ impl VfsItem { } impl Vfs { - pub fn new(config: Config) -> Vfs { + pub fn new(config: Config, plugin_chain: &'static PluginChain) -> Vfs { Vfs { partitions: HashMap::new(), start_time: Instant::now(), change_history: Vec::new(), + plugin_chain, config, } } @@ -164,13 +168,20 @@ impl Vfs { pub fn add_change(&mut self, timestamp: f64, route: Vec) { if self.config.verbose { - println!("Added change {:?}", route); + println!("Received change {:?}, running through plugins...", route); } - self.change_history.push(VfsChange { - timestamp, - route, - }); + match self.plugin_chain.handle_file_change(&route) { + Some(routes) => { + for route in routes { + self.change_history.push(VfsChange { + timestamp, + route, + }); + } + }, + None => {} + } } pub fn changes_since(&self, timestamp: f64) -> &[VfsChange] { diff --git a/src/web.rs b/src/web.rs index f15dc7e1..2a0e1118 100644 --- a/src/web.rs +++ b/src/web.rs @@ -1,6 +1,5 @@ use std::io::Read; use std::sync::{Arc, Mutex}; -use std::thread; use rouille; use serde;