Use plugin chain code in Vfs

This commit is contained in:
Lucien Greathouse
2017-12-18 01:52:08 -08:00
parent f90c51e923
commit 6ee9a48e20
4 changed files with 40 additions and 10 deletions

View File

@@ -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 = {

View File

@@ -18,11 +18,11 @@ pub trait Plugin {
}
pub struct PluginChain {
plugins: Vec<Box<Plugin + Send>>,
plugins: Vec<Box<Plugin + Send + Sync>>,
}
impl PluginChain {
pub fn new(plugins: Vec<Box<Plugin + Send>>) -> PluginChain {
pub fn new(plugins: Vec<Box<Plugin + Send + Sync>>) -> PluginChain {
PluginChain {
plugins,
}
@@ -38,4 +38,15 @@ impl PluginChain {
None
}
pub fn handle_file_change(&self, route: &Route) -> Option<Vec<Route>> {
for plugin in &self.plugins {
match plugin.handle_file_change(route) {
FileChangeResult::MarkChanged(changes) => return changes,
FileChangeResult::Pass => {},
}
}
None
}
}

View File

@@ -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<VfsChange>,
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<String>) {
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] {

View File

@@ -1,6 +1,5 @@
use std::io::Read;
use std::sync::{Arc, Mutex};
use std::thread;
use rouille;
use serde;