Add handle_file_change to plugin API

This solves the problem I was running into with the ScriptPlugin implementation -- if foo/init.lua changes, foo needs to be requested, not 'foo/init.lua' (which would then erroneously create a ModuleScript before this commit)
This commit is contained in:
Lucien Greathouse
2017-12-17 22:46:56 -08:00
parent c75cbebbf0
commit 6472a2cbce
4 changed files with 51 additions and 16 deletions

View File

@@ -4,3 +4,5 @@ pub struct Config {
pub verbose: bool,
pub server_id: u64,
}
pub type Route = Vec<String>;

View File

@@ -1,13 +1,20 @@
use rbx::RbxItem;
use vfs::VfsItem;
use core::Route;
pub enum PluginResult {
pub enum TransformResult {
Value(Option<RbxItem>),
Pass,
}
pub enum FileChangeResult {
MarkChanged(Option<Vec<Route>>),
Pass,
}
pub trait Plugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> PluginResult;
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformResult;
fn handle_file_change(&self, route: &Route) -> FileChangeResult;
}
pub struct PluginChain {
@@ -24,8 +31,8 @@ impl PluginChain {
pub fn transform_file(&self, vfs_item: &VfsItem) -> Option<RbxItem> {
for plugin in &self.plugins {
match plugin.transform_file(self, vfs_item) {
PluginResult::Value(rbx_item) => return rbx_item,
PluginResult::Pass => {},
TransformResult::Value(rbx_item) => return rbx_item,
TransformResult::Pass => {},
}
}

View File

@@ -1,6 +1,7 @@
use std::collections::HashMap;
use plugin::{Plugin, PluginChain, PluginResult};
use core::Route;
use plugin::{Plugin, PluginChain, TransformResult, FileChangeResult};
use rbx::{RbxItem, RbxValue};
use vfs::VfsItem;
@@ -16,7 +17,7 @@ impl DefaultPlugin {
}
impl Plugin for DefaultPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> PluginResult {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformResult {
match vfs_item {
&VfsItem::File { ref contents, ref name } => {
let mut properties = HashMap::new();
@@ -25,7 +26,7 @@ impl Plugin for DefaultPlugin {
value: contents.clone(),
});
PluginResult::Value(Some(RbxItem {
TransformResult::Value(Some(RbxItem {
name: name.clone(),
class_name: "StringValue".to_string(),
children: Vec::new(),
@@ -44,7 +45,7 @@ impl Plugin for DefaultPlugin {
}
}
PluginResult::Value(Some(RbxItem {
TransformResult::Value(Some(RbxItem {
name: name.clone(),
class_name: "Folder".to_string(),
children: rbx_children,
@@ -53,4 +54,8 @@ impl Plugin for DefaultPlugin {
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
FileChangeResult::MarkChanged(Some(vec![route.clone()]))
}
}

View File

@@ -2,7 +2,8 @@ use std::collections::HashMap;
use regex::Regex;
use plugin::{Plugin, PluginChain, PluginResult};
use core::Route;
use plugin::{Plugin, PluginChain, TransformResult, FileChangeResult};
use rbx::{RbxItem, RbxValue};
use vfs::VfsItem;
@@ -25,7 +26,7 @@ impl ScriptPlugin {
}
impl Plugin for ScriptPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> PluginResult {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformResult {
match vfs_item {
&VfsItem::File { ref contents, ref name } => {
let (class_name, rbx_name) = {
@@ -36,7 +37,7 @@ impl Plugin for ScriptPlugin {
} else if let Some(captures) = MODULE_PATTERN.captures(name) {
("ModuleScript".to_string(), captures.get(1).unwrap().as_str().to_string())
} else {
return PluginResult::Pass;
return TransformResult::Pass;
}
};
@@ -46,7 +47,7 @@ impl Plugin for ScriptPlugin {
value: contents.clone(),
});
PluginResult::Value(Some(RbxItem {
TransformResult::Value(Some(RbxItem {
name: rbx_name,
class_name: class_name,
children: Vec::new(),
@@ -61,15 +62,15 @@ impl Plugin for ScriptPlugin {
match maybe_item {
Some(v) => v,
None => return PluginResult::Pass,
None => return TransformResult::Pass,
}
};
let mut rbx_item = match self.transform_file(plugins, init_item) {
PluginResult::Value(Some(item)) => item,
TransformResult::Value(Some(item)) => item,
_ => {
eprintln!("Inconsistency detected in ScriptPlugin!");
return PluginResult::Pass;
return TransformResult::Pass;
},
};
@@ -89,8 +90,28 @@ impl Plugin for ScriptPlugin {
}
}
PluginResult::Value(Some(rbx_item))
TransformResult::Value(Some(rbx_item))
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
let leaf = match route.last() {
Some(v) => v,
None => return FileChangeResult::Pass,
};
let is_init = leaf == SERVER_INIT
|| leaf == CLIENT_INIT
|| leaf == MODULE_INIT;
if is_init {
let mut changed = route.clone();
changed.pop();
FileChangeResult::MarkChanged(Some(vec![changed]))
} else {
FileChangeResult::Pass
}
}
}