Flatten snapshot middleware to be much simpler (#324)

* First take at flattening middleware for simpler code and better perf

* Undo debug prints

* Fix using wrong path in snapshot_from_vfs

* Disable some broken tests

* Re-enable (mistakenly?) disabled CSV test

* Fix some tests

* Update project file tests

* Fix benchmark
This commit is contained in:
Lucien Greathouse
2020-06-17 13:47:09 -07:00
committed by GitHub
parent bdd1afea57
commit 486b067567
18 changed files with 489 additions and 713 deletions

View File

@@ -35,6 +35,7 @@ fn place_setup<P: AsRef<Path>>(input_path: P) -> (TempDir, BuildCommand) {
let options = BuildCommand { let options = BuildCommand {
project: input, project: input,
watch: false,
output, output,
}; };

View File

@@ -8,54 +8,41 @@ use serde::Serialize;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{ use super::{
error::SnapshotError, error::SnapshotError, meta_file::AdjacentMetadata, middleware::SnapshotInstanceResult,
meta_file::AdjacentMetadata,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotCsv; pub fn snapshot_csv(
_context: &InstanceContext,
vfs: &Vfs,
path: &Path,
instance_name: &str,
) -> SnapshotInstanceResult {
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
let contents = vfs.read(path)?;
impl SnapshotMiddleware for SnapshotCsv { let table_contents = convert_localization_csv(&contents)
fn from_vfs(_context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { .map_err(|source| SnapshotError::malformed_l10n_csv(source, path))?;
let meta = vfs.metadata(path)?;
if meta.is_dir() { let mut snapshot = InstanceSnapshot::new()
return Ok(None); .name(instance_name)
} .class_name("LocalizationTable")
.properties(hashmap! {
"Contents".to_owned() => RbxValue::String {
value: table_contents,
},
})
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()]),
);
let instance_name = match match_file_name(path, ".csv") { if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
Some(name) => name, let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
None => return Ok(None), metadata.apply_all(&mut snapshot);
};
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
let contents = vfs.read(path)?;
let table_contents = convert_localization_csv(&contents)
.map_err(|source| SnapshotError::malformed_l10n_csv(source, path))?;
let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("LocalizationTable")
.properties(hashmap! {
"Contents".to_owned() => RbxValue::String {
value: table_contents,
},
})
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()]),
);
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
metadata.apply_all(&mut snapshot);
}
Ok(Some(snapshot))
} }
Ok(Some(snapshot))
} }
/// Struct that holds any valid row from a Roblox CSV translation table. /// Struct that holds any valid row from a Roblox CSV translation table.
@@ -156,10 +143,14 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_csv(
SnapshotCsv::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.csv")) &InstanceContext::default(),
.unwrap() &mut vfs,
.unwrap(); Path::new("/foo.csv"),
"foo",
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -184,9 +175,15 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_csv(
SnapshotCsv::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.csv")) &InstanceContext::default(),
.unwrap() &mut vfs,
.unwrap(); Path::new("/foo.csv"),
"foo",
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
} }
} }

View File

@@ -5,81 +5,69 @@ use memofs::{DirEntry, IoResultExt, Vfs};
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{ use super::{
error::SnapshotError, error::SnapshotError, meta_file::DirectoryMetadata, middleware::SnapshotInstanceResult,
meta_file::DirectoryMetadata,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
snapshot_from_vfs, snapshot_from_vfs,
}; };
pub struct SnapshotDir; pub fn snapshot_dir(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
let passes_filter_rules = |child: &DirEntry| {
context
.path_ignore_rules
.iter()
.all(|rule| rule.passes(child.path()))
};
impl SnapshotMiddleware for SnapshotDir { let mut snapshot_children = Vec::new();
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
let meta = vfs.metadata(path)?;
if meta.is_file() { for entry in vfs.read_dir(path)? {
return Ok(None); let entry = entry?;
if !passes_filter_rules(&entry) {
continue;
} }
let passes_filter_rules = |child: &DirEntry| { if let Some(child_snapshot) = snapshot_from_vfs(context, vfs, entry.path())? {
context snapshot_children.push(child_snapshot);
.path_ignore_rules
.iter()
.all(|rule| rule.passes(child.path()))
};
let mut snapshot_children = Vec::new();
for entry in vfs.read_dir(path)? {
let entry = entry?;
if !passes_filter_rules(&entry) {
continue;
}
if let Some(child_snapshot) = snapshot_from_vfs(context, vfs, entry.path())? {
snapshot_children.push(child_snapshot);
}
} }
let instance_name = path
.file_name()
.expect("Could not extract file name")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(path))?
.to_string();
let meta_path = path.join("init.meta.json");
let relevant_paths = vec![
path.to_path_buf(),
meta_path.clone(),
// TODO: We shouldn't need to know about Lua existing in this
// middleware. Should we figure out a way for that function to add
// relevant paths to this middleware?
path.join("init.lua"),
path.join("init.server.lua"),
path.join("init.client.lua"),
];
let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("Folder")
.children(snapshot_children)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(relevant_paths)
.context(context),
);
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let mut metadata = DirectoryMetadata::from_slice(&meta_contents, &meta_path)?;
metadata.apply_all(&mut snapshot);
}
Ok(Some(snapshot))
} }
let instance_name = path
.file_name()
.expect("Could not extract file name")
.to_str()
.ok_or_else(|| SnapshotError::file_name_bad_unicode(path))?
.to_string();
let meta_path = path.join("init.meta.json");
let relevant_paths = vec![
path.to_path_buf(),
meta_path.clone(),
// TODO: We shouldn't need to know about Lua existing in this
// middleware. Should we figure out a way for that function to add
// relevant paths to this middleware?
path.join("init.lua"),
path.join("init.server.lua"),
path.join("init.client.lua"),
];
let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("Folder")
.children(snapshot_children)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(relevant_paths)
.context(context),
);
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let mut metadata = DirectoryMetadata::from_slice(&meta_contents, &meta_path)?;
metadata.apply_all(&mut snapshot);
}
Ok(Some(snapshot))
} }
#[cfg(test)] #[cfg(test)]
@@ -98,7 +86,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot =
SnapshotDir::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) snapshot_dir(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
.unwrap() .unwrap()
.unwrap(); .unwrap();
@@ -119,7 +107,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot =
SnapshotDir::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) snapshot_dir(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
.unwrap() .unwrap()
.unwrap(); .unwrap();

View File

@@ -10,69 +10,47 @@ use crate::{
}; };
use super::{ use super::{
error::SnapshotError, error::SnapshotError, meta_file::AdjacentMetadata, middleware::SnapshotInstanceResult,
meta_file::AdjacentMetadata,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
/// Catch-all middleware for snapshots on JSON files that aren't used for other pub fn snapshot_json(
/// features, like Rojo projects, JSON models, or meta files. context: &InstanceContext,
pub struct SnapshotJson; vfs: &Vfs,
path: &Path,
instance_name: &str,
) -> SnapshotInstanceResult {
let contents = vfs.read(path)?;
impl SnapshotMiddleware for SnapshotJson { let value: serde_json::Value = serde_json::from_slice(&contents)
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { .map_err(|err| SnapshotError::malformed_json(err, path))?;
let meta = vfs.metadata(path)?;
if meta.is_dir() { let as_lua = json_to_lua(value).to_string();
return Ok(None);
}
// FIXME: This middleware should not need to know about the .meta.json let properties = hashmap! {
// middleware. Should there be a way to signal "I'm not returning an "Source".to_owned() => RbxValue::String {
// instance and no one should"? value: as_lua,
if match_file_name(path, ".meta.json").is_some() { },
return Ok(None); };
}
let instance_name = match match_file_name(path, ".json") { let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
Some(name) => name,
None => return Ok(None),
};
let contents = vfs.read(path)?; let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("ModuleScript")
.properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
.context(context),
);
let value: serde_json::Value = serde_json::from_slice(&contents) if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
.map_err(|err| SnapshotError::malformed_json(err, path))?; let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
metadata.apply_all(&mut snapshot);
let as_lua = json_to_lua(value).to_string();
let properties = hashmap! {
"Source".to_owned() => RbxValue::String {
value: as_lua,
},
};
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("ModuleScript")
.properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
.context(context),
);
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
metadata.apply_all(&mut snapshot);
}
Ok(Some(snapshot))
} }
Ok(Some(snapshot))
} }
fn json_to_lua(value: serde_json::Value) -> Statement { fn json_to_lua(value: serde_json::Value) -> Statement {
@@ -129,10 +107,11 @@ mod test {
let mut vfs = Vfs::new(imfs.clone()); let mut vfs = Vfs::new(imfs.clone());
let instance_snapshot = SnapshotJson::from_vfs( let instance_snapshot = snapshot_json(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.json"), Path::new("/foo.json"),
"foo",
) )
.unwrap() .unwrap()
.unwrap(); .unwrap();

View File

@@ -7,68 +7,45 @@ use serde::Deserialize;
use crate::snapshot::{InstanceContext, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceSnapshot};
use super::{ use super::{error::SnapshotError, middleware::SnapshotInstanceResult};
error::SnapshotError,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
};
pub struct SnapshotJsonModel; pub fn snapshot_json_model(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
instance_name: &str,
) -> SnapshotInstanceResult {
let contents = vfs.read(path)?;
let instance: JsonModel = serde_json::from_slice(&contents)
.map_err(|source| SnapshotError::malformed_model_json(source, path))?;
impl SnapshotMiddleware for SnapshotJsonModel { if let Some(json_name) = &instance.name {
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { if json_name != instance_name {
let meta = vfs.metadata(path)?; log::warn!(
"Name from JSON model did not match its file name: {}",
if meta.is_dir() { path.display()
return Ok(None); );
log::warn!(
"In Rojo < alpha 14, this model is named \"{}\" (from its 'Name' property)",
json_name
);
log::warn!(
"In Rojo >= alpha 14, this model is named \"{}\" (from its file name)",
instance_name
);
log::warn!("'Name' for the top-level instance in a JSON model is now optional and will be ignored.");
} }
let instance_name = match match_file_name(path, ".model.json") {
Some(name) => name,
None => return Ok(None),
};
let contents = vfs.read(path)?;
let instance: JsonModel = serde_json::from_slice(&contents)
.map_err(|source| SnapshotError::malformed_model_json(source, path))?;
if let Some(json_name) = &instance.name {
if json_name != instance_name {
log::warn!(
"Name from JSON model did not match its file name: {}",
path.display()
);
log::warn!(
"In Rojo < alpha 14, this model is named \"{}\" (from its 'Name' property)",
json_name
);
log::warn!(
"In Rojo >= alpha 14, this model is named \"{}\" (from its file name)",
instance_name
);
log::warn!("'Name' for the top-level instance in a JSON model is now optional and will be ignored.");
}
}
let mut snapshot = instance.core.into_snapshot(instance_name.to_owned());
snapshot.metadata = snapshot
.metadata
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context);
Ok(Some(snapshot))
} }
}
fn match_trailing<'a>(input: &'a str, trailer: &str) -> Option<&'a str> { let mut snapshot = instance.core.into_snapshot(instance_name.to_owned());
if input.ends_with(trailer) {
let end = input.len().saturating_sub(trailer.len()); snapshot.metadata = snapshot
Some(&input[..end]) .metadata
} else { .instigating_source(path)
None .relevant_paths(vec![path.to_path_buf()])
} .context(context);
Ok(Some(snapshot))
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@@ -164,10 +141,11 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotJsonModel::from_vfs( let instance_snapshot = snapshot_json_model(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.model.json"), Path::new("/foo.model.json"),
"foo",
) )
.unwrap() .unwrap()
.unwrap(); .unwrap();

View File

@@ -7,50 +7,12 @@ use rbx_dom_weak::RbxValue;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{ use super::{
dir::SnapshotDir, dir::snapshot_dir, meta_file::AdjacentMetadata, middleware::SnapshotInstanceResult,
meta_file::AdjacentMetadata,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_trailing, util::match_trailing,
}; };
pub struct SnapshotLua;
impl SnapshotMiddleware for SnapshotLua {
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
let file_name = path.file_name().unwrap().to_string_lossy();
// These paths alter their parent instance, so we don't need to turn
// them into a script instance here.
match &*file_name {
"init.lua" | "init.server.lua" | "init.client.lua" => return Ok(None),
_ => {}
}
let meta = vfs.metadata(path)?;
if meta.is_file() {
snapshot_lua_file(context, vfs, path)
} else {
// At this point, our entry is definitely a directory!
if let Some(snapshot) = snapshot_init(context, vfs, path, "init.lua")? {
// An `init.lua` file turns its parent into a ModuleScript
Ok(Some(snapshot))
} else if let Some(snapshot) = snapshot_init(context, vfs, path, "init.server.lua")? {
// An `init.server.lua` file turns its parent into a Script
Ok(Some(snapshot))
} else if let Some(snapshot) = snapshot_init(context, vfs, path, "init.client.lua")? {
// An `init.client.lua` file turns its parent into a LocalScript
Ok(Some(snapshot))
} else {
Ok(None)
}
}
}
}
/// Core routine for turning Lua files into snapshots. /// Core routine for turning Lua files into snapshots.
fn snapshot_lua_file(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { pub fn snapshot_lua(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
let file_name = path.file_name().unwrap().to_string_lossy(); let file_name = path.file_name().unwrap().to_string_lossy();
let (class_name, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua") let (class_name, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua")
@@ -100,35 +62,29 @@ fn snapshot_lua_file(context: &InstanceContext, vfs: &Vfs, path: &Path) -> Snaps
/// ///
/// Scripts named `init.lua`, `init.server.lua`, or `init.client.lua` usurp /// Scripts named `init.lua`, `init.server.lua`, or `init.client.lua` usurp
/// their parents, which acts similarly to `__init__.py` from the Python world. /// their parents, which acts similarly to `__init__.py` from the Python world.
fn snapshot_init( pub fn snapshot_lua_init(
context: &InstanceContext, context: &InstanceContext,
vfs: &Vfs, vfs: &Vfs,
folder_path: &Path, init_path: &Path,
init_name: &str,
) -> SnapshotInstanceResult { ) -> SnapshotInstanceResult {
let init_path = folder_path.join(init_name); let folder_path = init_path.parent().unwrap();
let dir_snapshot = snapshot_dir(context, vfs, folder_path)?.unwrap();
if vfs.metadata(&init_path).with_not_found()?.is_some() { if dir_snapshot.class_name != "Folder" {
if let Some(dir_snapshot) = SnapshotDir::from_vfs(context, vfs, folder_path)? { panic!(
if let Some(mut init_snapshot) = snapshot_lua_file(context, vfs, &init_path)? { "init.lua, init.server.lua, and init.client.lua can \
if dir_snapshot.class_name != "Folder" { only be used if the instance produced by the parent \
panic!( directory would be a Folder."
"init.lua, init.server.lua, and init.client.lua can \ );
only be used if the instance produced by the parent \
directory would be a Folder."
);
}
init_snapshot.name = dir_snapshot.name;
init_snapshot.children = dir_snapshot.children;
init_snapshot.metadata = dir_snapshot.metadata;
return Ok(Some(init_snapshot));
}
}
} }
Ok(None) let mut init_snapshot = snapshot_lua(context, vfs, init_path)?.unwrap();
init_snapshot.name = dir_snapshot.name;
init_snapshot.children = dir_snapshot.children;
init_snapshot.metadata = dir_snapshot.metadata;
Ok(Some(init_snapshot))
} }
#[cfg(test)] #[cfg(test)]
@@ -146,7 +102,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot =
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua")) snapshot_lua(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua"))
.unwrap() .unwrap()
.unwrap(); .unwrap();
@@ -161,7 +117,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotLua::from_vfs( let instance_snapshot = snapshot_lua(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.server.lua"), Path::new("/foo.server.lua"),
@@ -180,7 +136,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotLua::from_vfs( let instance_snapshot = snapshot_lua(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.client.lua"), Path::new("/foo.client.lua"),
@@ -191,6 +147,7 @@ mod test {
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
#[ignore = "init.lua functionality has moved to the root snapshot function"]
#[test] #[test]
fn init_module_from_vfs() { fn init_module_from_vfs() {
let mut imfs = InMemoryFs::new(); let mut imfs = InMemoryFs::new();
@@ -205,7 +162,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot =
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/root")) snapshot_lua(&InstanceContext::default(), &mut vfs, Path::new("/root"))
.unwrap() .unwrap()
.unwrap(); .unwrap();
@@ -232,7 +189,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot =
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua")) snapshot_lua(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua"))
.unwrap() .unwrap()
.unwrap(); .unwrap();
@@ -258,7 +215,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotLua::from_vfs( let instance_snapshot = snapshot_lua(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.server.lua"), Path::new("/foo.server.lua"),
@@ -290,7 +247,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotLua::from_vfs( let instance_snapshot = snapshot_lua(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/bar.server.lua"), Path::new("/bar.server.lua"),

View File

@@ -1,13 +1,5 @@
use std::path::Path; use crate::snapshot::InstanceSnapshot;
use memofs::Vfs;
use crate::snapshot::{InstanceContext, InstanceSnapshot};
use super::error::SnapshotError; use super::error::SnapshotError;
pub type SnapshotInstanceResult = Result<Option<InstanceSnapshot>, SnapshotError>; pub type SnapshotInstanceResult = Result<Option<InstanceSnapshot>, SnapshotError>;
pub trait SnapshotMiddleware {
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult;
}

View File

@@ -12,7 +12,6 @@ mod lua;
mod meta_file; mod meta_file;
mod middleware; mod middleware;
mod project; mod project;
mod rbxlx;
mod rbxm; mod rbxm;
mod rbxmx; mod rbxmx;
mod txt; mod txt;
@@ -20,59 +19,87 @@ mod util;
use std::path::Path; use std::path::Path;
use memofs::Vfs; use memofs::{IoResultExt, Vfs};
use crate::snapshot::InstanceContext; use crate::snapshot::InstanceContext;
use self::{ use self::{
csv::SnapshotCsv, csv::snapshot_csv,
dir::SnapshotDir, dir::snapshot_dir,
json::SnapshotJson, json::snapshot_json,
json_model::SnapshotJsonModel, json_model::snapshot_json_model,
lua::SnapshotLua, lua::{snapshot_lua, snapshot_lua_init},
middleware::{SnapshotInstanceResult, SnapshotMiddleware}, middleware::SnapshotInstanceResult,
project::SnapshotProject, project::snapshot_project,
rbxlx::SnapshotRbxlx, rbxm::snapshot_rbxm,
rbxm::SnapshotRbxm, rbxmx::snapshot_rbxmx,
rbxmx::SnapshotRbxmx, txt::snapshot_txt,
txt::SnapshotTxt, util::match_file_name,
}; };
pub use self::error::*; pub use self::error::*;
pub use self::project::snapshot_project_node; pub use self::project::snapshot_project_node;
macro_rules! middlewares { pub fn snapshot_from_vfs(
( $($middleware: ident,)* ) => { context: &InstanceContext,
/// Generates a snapshot of instances from the given path. vfs: &Vfs,
pub fn snapshot_from_vfs( path: &Path,
context: &InstanceContext, ) -> SnapshotInstanceResult {
vfs: &Vfs, let meta = match vfs.metadata(path).with_not_found()? {
path: &Path, Some(meta) => meta,
) -> SnapshotInstanceResult { None => return Ok(None),
$(
log::trace!("trying middleware {} on {}", stringify!($middleware), path.display());
if let Some(snapshot) = $middleware::from_vfs(context, vfs, path)? {
log::trace!("middleware {} success on {}", stringify!($middleware), path.display());
return Ok(Some(snapshot));
}
)*
log::trace!("no middleware returned Ok(Some)");
Ok(None)
}
}; };
}
middlewares! { if meta.is_dir() {
SnapshotProject, let project_path = path.join("default.project.json");
SnapshotJsonModel, if vfs.metadata(&project_path).with_not_found()?.is_some() {
SnapshotRbxlx, return snapshot_project(context, vfs, &project_path);
SnapshotRbxmx, }
SnapshotRbxm,
SnapshotLua, let init_path = path.join("init.lua");
SnapshotCsv, if vfs.metadata(&init_path).with_not_found()?.is_some() {
SnapshotTxt, return snapshot_lua_init(context, vfs, &init_path);
SnapshotJson, }
SnapshotDir,
let init_path = path.join("init.server.lua");
if vfs.metadata(&init_path).with_not_found()?.is_some() {
return snapshot_lua_init(context, vfs, &init_path);
}
let init_path = path.join("init.client.lua");
if vfs.metadata(&init_path).with_not_found()?.is_some() {
return snapshot_lua_init(context, vfs, &init_path);
}
snapshot_dir(context, vfs, path)
} else {
if let Some(name) = match_file_name(path, ".lua") {
match name {
// init scripts are handled elsewhere and should not turn into
// their own children.
"init" | "init.client" | "init.server" => return Ok(None),
_ => return snapshot_lua(context, vfs, path),
}
} else if let Some(_name) = match_file_name(path, ".project.json") {
return snapshot_project(context, vfs, path);
} else if let Some(name) = match_file_name(path, ".model.json") {
return snapshot_json_model(context, vfs, path, name);
} else if let Some(_name) = match_file_name(path, ".meta.json") {
// .meta.json files do not turn into their own instances.
return Ok(None);
} else if let Some(name) = match_file_name(path, ".json") {
return snapshot_json(context, vfs, path, name);
} else if let Some(name) = match_file_name(path, ".csv") {
return snapshot_csv(context, vfs, path, name);
} else if let Some(name) = match_file_name(path, ".txt") {
return snapshot_txt(context, vfs, path, name);
} else if let Some(name) = match_file_name(path, ".rbxmx") {
return snapshot_rbxmx(context, vfs, path, name);
} else if let Some(name) = match_file_name(path, ".rbxm") {
return snapshot_rbxm(context, vfs, path, name);
}
Ok(None)
}
} }

View File

@@ -1,6 +1,6 @@
use std::{borrow::Cow, collections::HashMap, path::Path}; use std::{borrow::Cow, collections::HashMap, path::Path};
use memofs::{IoResultExt, Vfs}; use memofs::Vfs;
use rbx_reflection::{get_class_descriptor, try_resolve_value}; use rbx_reflection::{get_class_descriptor, try_resolve_value};
use crate::{ use crate::{
@@ -10,92 +10,61 @@ use crate::{
}, },
}; };
use super::{ use super::{error::SnapshotError, middleware::SnapshotInstanceResult, snapshot_from_vfs};
error::SnapshotError,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
snapshot_from_vfs,
};
/// Handles snapshots for: pub fn snapshot_project(
/// * Files ending in `.project.json` context: &InstanceContext,
/// * Folders containing a file named `default.project.json` vfs: &Vfs,
pub struct SnapshotProject; path: &Path,
) -> SnapshotInstanceResult {
let project = Project::load_from_slice(&vfs.read(path)?, path)
.map_err(|err| SnapshotError::malformed_project(err, path))?;
impl SnapshotMiddleware for SnapshotProject { let mut context = context.clone();
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
let meta = vfs.metadata(path)?;
if meta.is_dir() { let rules = project.glob_ignore_paths.iter().map(|glob| PathIgnoreRule {
let project_path = path.join("default.project.json"); glob: glob.clone(),
base_path: project.folder_location().to_path_buf(),
});
match vfs.metadata(&project_path).with_not_found()? { context.add_path_ignore_rules(rules);
// TODO: Do we need to muck with the relevant paths if we're a
// project file within a folder? Should the folder path be the
// relevant path instead of the project file path?
Some(_meta) => return SnapshotProject::from_vfs(context, vfs, &project_path),
None => return Ok(None),
}
}
if !path.to_string_lossy().ends_with(".project.json") { // TODO: If this project node is a path to an instance that Rojo doesn't
// This isn't a project file, so it's not our job. // understand, this may panic!
return Ok(None); let mut snapshot =
} snapshot_project_node(&context, path, &project.name, &project.tree, vfs, None)?.unwrap();
let project = Project::load_from_slice(&vfs.read(path)?, path) // Setting the instigating source to the project file path is a little
.map_err(|err| SnapshotError::malformed_project(err, path))?; // coarse.
//
// Ideally, we'd only snapshot the project file if the project file
// actually changed. Because Rojo only has the concept of one
// relevant path -> snapshot path mapping per instance, we pick the more
// conservative approach of snapshotting the project file if any
// relevant paths changed.
snapshot.metadata.instigating_source = Some(path.to_path_buf().into());
let mut context = context.clone(); // Mark this snapshot (the root node of the project file) as being
// related to the project file.
//
// We SHOULD NOT mark the project file as a relevant path for any
// nodes that aren't roots. They'll be updated as part of the project
// file being updated.
snapshot.metadata.relevant_paths.push(path.to_path_buf());
let rules = project.glob_ignore_paths.iter().map(|glob| PathIgnoreRule { Ok(Some(snapshot))
glob: glob.clone(),
base_path: project.folder_location().to_path_buf(),
});
context.add_path_ignore_rules(rules);
// Snapshotting a project should always return an instance, so this
// unwrap is safe.
let mut snapshot = snapshot_project_node(
&context,
project.folder_location(),
&project.name,
&project.tree,
vfs,
None,
)?
.unwrap();
// Setting the instigating source to the project file path is a little
// coarse.
//
// Ideally, we'd only snapshot the project file if the project file
// actually changed. Because Rojo only has the concept of one
// relevant path -> snapshot path mapping per instance, we pick the more
// conservative approach of snapshotting the project file if any
// relevant paths changed.
snapshot.metadata.instigating_source = Some(path.to_path_buf().into());
// Mark this snapshot (the root node of the project file) as being
// related to the project file.
//
// We SHOULD NOT mark the project file as a relevant path for any
// nodes that aren't roots. They'll be updated as part of the project
// file being updated.
snapshot.metadata.relevant_paths.push(path.to_path_buf());
Ok(Some(snapshot))
}
} }
pub fn snapshot_project_node( pub fn snapshot_project_node(
context: &InstanceContext, context: &InstanceContext,
project_folder: &Path, project_path: &Path,
instance_name: &str, instance_name: &str,
node: &ProjectNode, node: &ProjectNode,
vfs: &Vfs, vfs: &Vfs,
parent_class: Option<&str>, parent_class: Option<&str>,
) -> SnapshotInstanceResult { ) -> SnapshotInstanceResult {
let project_folder = project_path.parent().unwrap();
let name = Cow::Owned(instance_name.to_owned()); let name = Cow::Owned(instance_name.to_owned());
let mut class_name = node let mut class_name = node
.class_name .class_name
@@ -191,7 +160,7 @@ pub fn snapshot_project_node(
for (child_name, child_project_node) in &node.children { for (child_name, child_project_node) in &node.children {
if let Some(child) = snapshot_project_node( if let Some(child) = snapshot_project_node(
context, context,
project_folder, project_path,
child_name, child_name,
child_project_node, child_project_node,
vfs, vfs,
@@ -223,7 +192,7 @@ pub fn snapshot_project_node(
} }
metadata.instigating_source = Some(InstigatingSource::ProjectNode( metadata.instigating_source = Some(InstigatingSource::ProjectNode(
project_folder.to_path_buf(), project_path.to_path_buf(),
instance_name.to_string(), instance_name.to_string(),
node.clone(), node.clone(),
parent_class.map(|name| name.to_owned()), parent_class.map(|name| name.to_owned()),
@@ -239,6 +208,7 @@ pub fn snapshot_project_node(
})) }))
} }
// #[cfg(feature = "broken-tests")]
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@@ -246,6 +216,7 @@ mod test {
use maplit::hashmap; use maplit::hashmap;
use memofs::{InMemoryFs, VfsSnapshot}; use memofs::{InMemoryFs, VfsSnapshot};
#[ignore = "Functionality moved to root snapshot middleware"]
#[test] #[test]
fn project_from_folder() { fn project_from_folder() {
let _ = env_logger::try_init(); let _ = env_logger::try_init();
@@ -269,7 +240,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot =
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) snapshot_project(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
.expect("snapshot error") .expect("snapshot error")
.expect("snapshot returned no instances"); .expect("snapshot returned no instances");
@@ -298,7 +269,7 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotProject::from_vfs( let instance_snapshot = snapshot_project(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo/hello.project.json"), Path::new("/foo/hello.project.json"),
@@ -315,9 +286,9 @@ mod test {
let mut imfs = InMemoryFs::new(); let mut imfs = InMemoryFs::new();
imfs.load_snapshot( imfs.load_snapshot(
"/foo", "/foo.project.json",
VfsSnapshot::dir(hashmap! { VfsSnapshot::file(
"default.project.json" => VfsSnapshot::file(r#" r#"
{ {
"name": "resolved-properties", "name": "resolved-properties",
"tree": { "tree": {
@@ -330,17 +301,20 @@ mod test {
} }
} }
} }
"#), "#,
}), ),
) )
.unwrap(); .unwrap();
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -351,9 +325,9 @@ mod test {
let mut imfs = InMemoryFs::new(); let mut imfs = InMemoryFs::new();
imfs.load_snapshot( imfs.load_snapshot(
"/foo", "/foo.project.json",
VfsSnapshot::dir(hashmap! { VfsSnapshot::file(
"default.project.json" => VfsSnapshot::file(r#" r#"
{ {
"name": "unresolved-properties", "name": "unresolved-properties",
"tree": { "tree": {
@@ -363,17 +337,20 @@ mod test {
} }
} }
} }
"#), "#,
}), ),
) )
.unwrap(); .unwrap();
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -384,9 +361,9 @@ mod test {
let mut imfs = InMemoryFs::new(); let mut imfs = InMemoryFs::new();
imfs.load_snapshot( imfs.load_snapshot(
"/foo", "/foo.project.json",
VfsSnapshot::dir(hashmap! { VfsSnapshot::file(
"default.project.json" => VfsSnapshot::file(r#" r#"
{ {
"name": "children", "name": "children",
"tree": { "tree": {
@@ -397,17 +374,20 @@ mod test {
} }
} }
} }
"#), "#,
}), ),
) )
.unwrap(); .unwrap();
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -435,10 +415,13 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo/default.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -473,10 +456,13 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo/default.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -515,10 +501,13 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo/default.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }
@@ -562,10 +551,13 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = let instance_snapshot = snapshot_project(
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo")) &InstanceContext::default(),
.expect("snapshot error") &mut vfs,
.expect("snapshot returned no instances"); Path::new("/foo/default.project.json"),
)
.expect("snapshot error")
.expect("snapshot returned no instances");
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }

View File

@@ -1,46 +0,0 @@
use std::path::Path;
use memofs::Vfs;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
};
pub struct SnapshotRbxlx;
impl SnapshotMiddleware for SnapshotRbxlx {
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
let meta = vfs.metadata(path)?;
if meta.is_dir() {
return Ok(None);
}
let instance_name = match match_file_name(path, ".rbxlx") {
Some(name) => name,
None => return Ok(None),
};
let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
let temp_tree = rbx_xml::from_reader(vfs.read(path)?.as_slice(), options)
.expect("TODO: Handle rbx_xml errors");
let root_id = temp_tree.get_root_id();
let snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id)
.name(instance_name)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context),
);
Ok(Some(snapshot))
}
}

View File

@@ -5,53 +5,40 @@ use rbx_dom_weak::{RbxInstanceProperties, RbxTree};
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{ use super::middleware::SnapshotInstanceResult;
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
};
pub struct SnapshotRbxm; pub fn snapshot_rbxm(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
instance_name: &str,
) -> SnapshotInstanceResult {
let mut temp_tree = RbxTree::new(RbxInstanceProperties {
name: "DataModel".to_owned(),
class_name: "DataModel".to_owned(),
properties: HashMap::new(),
});
impl SnapshotMiddleware for SnapshotRbxm { let root_id = temp_tree.get_root_id();
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { rbx_binary::decode(&mut temp_tree, root_id, vfs.read(path)?.as_slice())
let meta = vfs.metadata(path)?; .expect("TODO: Handle rbx_binary errors");
if meta.is_dir() { let root_instance = temp_tree.get_instance(root_id).unwrap();
return Ok(None); let children = root_instance.get_children_ids();
}
let instance_name = match match_file_name(path, ".rbxm") { if children.len() == 1 {
Some(name) => name, let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
None => return Ok(None), .name(instance_name)
}; .metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context),
);
let mut temp_tree = RbxTree::new(RbxInstanceProperties { Ok(Some(snapshot))
name: "DataModel".to_owned(), } else {
class_name: "DataModel".to_owned(), panic!("Rojo doesn't have support for model files with zero or more than one top-level instances yet.");
properties: HashMap::new(),
});
let root_id = temp_tree.get_root_id();
rbx_binary::decode(&mut temp_tree, root_id, vfs.read(path)?.as_slice())
.expect("TODO: Handle rbx_binary errors");
let root_instance = temp_tree.get_instance(root_id).unwrap();
let children = root_instance.get_children_ids();
if children.len() == 1 {
let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
.name(instance_name)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context),
);
Ok(Some(snapshot))
} else {
panic!("Rojo doesn't have support for model files with zero or more than one top-level instances yet.");
}
} }
} }
@@ -72,10 +59,11 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotRbxm::from_vfs( let instance_snapshot = snapshot_rbxm(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.rbxm"), Path::new("/foo.rbxm"),
"foo",
) )
.unwrap() .unwrap()
.unwrap(); .unwrap();

View File

@@ -4,49 +4,36 @@ use memofs::Vfs;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{ use super::middleware::SnapshotInstanceResult;
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
};
pub struct SnapshotRbxmx; pub fn snapshot_rbxmx(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
instance_name: &str,
) -> SnapshotInstanceResult {
let options = rbx_xml::DecodeOptions::new()
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
impl SnapshotMiddleware for SnapshotRbxmx { let temp_tree = rbx_xml::from_reader(vfs.read(path)?.as_slice(), options)
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { .expect("TODO: Handle rbx_xml errors");
let meta = vfs.metadata(path)?;
if meta.is_dir() { let root_instance = temp_tree.get_instance(temp_tree.get_root_id()).unwrap();
return Ok(None); let children = root_instance.get_children_ids();
}
let instance_name = match match_file_name(path, ".rbxmx") { if children.len() == 1 {
Some(name) => name, let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
None => return Ok(None), .name(instance_name)
}; .metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context),
);
let options = rbx_xml::DecodeOptions::new() Ok(Some(snapshot))
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); } else {
panic!("Rojo doesn't have support for model files with zero or more than one top-level instances yet.");
let temp_tree = rbx_xml::from_reader(vfs.read(path)?.as_slice(), options)
.expect("TODO: Handle rbx_xml errors");
let root_instance = temp_tree.get_instance(temp_tree.get_root_id()).unwrap();
let children = root_instance.get_children_ids();
if children.len() == 1 {
let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
.name(instance_name)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context),
);
Ok(Some(snapshot))
} else {
panic!("Rojo doesn't have support for model files with zero or more than one top-level instances yet.");
}
} }
} }
@@ -77,10 +64,11 @@ mod test {
let mut vfs = Vfs::new(imfs); let mut vfs = Vfs::new(imfs);
let instance_snapshot = SnapshotRbxmx::from_vfs( let instance_snapshot = snapshot_rbxmx(
&InstanceContext::default(), &InstanceContext::default(),
&mut vfs, &mut vfs,
Path::new("/foo.rbxmx"), Path::new("/foo.rbxmx"),
"foo",
) )
.unwrap() .unwrap()
.unwrap(); .unwrap();

View File

@@ -6,9 +6,9 @@ snapshot_id: ~
metadata: metadata:
ignore_unknown_instances: true ignore_unknown_instances: true
instigating_source: instigating_source:
Path: /foo/default.project.json Path: /foo.project.json
relevant_paths: relevant_paths:
- /foo/default.project.json - /foo.project.json
context: {} context: {}
name: children name: children
class_name: Folder class_name: Folder
@@ -19,7 +19,7 @@ children:
ignore_unknown_instances: true ignore_unknown_instances: true
instigating_source: instigating_source:
ProjectNode: ProjectNode:
- /foo - /foo.project.json
- Child - Child
- $className: Model - $className: Model
- Folder - Folder

View File

@@ -20,7 +20,7 @@ children:
ignore_unknown_instances: true ignore_unknown_instances: true
instigating_source: instigating_source:
ProjectNode: ProjectNode:
- /foo - /foo/other.project.json
- SomeChild - SomeChild
- $className: Model - $className: Model
- Folder - Folder

View File

@@ -6,9 +6,9 @@ snapshot_id: ~
metadata: metadata:
ignore_unknown_instances: true ignore_unknown_instances: true
instigating_source: instigating_source:
Path: /foo/default.project.json Path: /foo.project.json
relevant_paths: relevant_paths:
- /foo/default.project.json - /foo.project.json
context: {} context: {}
name: resolved-properties name: resolved-properties
class_name: StringValue class_name: StringValue

View File

@@ -6,9 +6,9 @@ snapshot_id: ~
metadata: metadata:
ignore_unknown_instances: true ignore_unknown_instances: true
instigating_source: instigating_source:
Path: /foo/default.project.json Path: /foo.project.json
relevant_paths: relevant_paths:
- /foo/default.project.json - /foo.project.json
context: {} context: {}
name: unresolved-properties name: unresolved-properties
class_name: StringValue class_name: StringValue

View File

@@ -7,58 +7,45 @@ use rbx_dom_weak::RbxValue;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
use super::{ use super::{
error::SnapshotError, error::SnapshotError, meta_file::AdjacentMetadata, middleware::SnapshotInstanceResult,
meta_file::AdjacentMetadata,
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
util::match_file_name,
}; };
pub struct SnapshotTxt; pub fn snapshot_txt(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
instance_name: &str,
) -> SnapshotInstanceResult {
let contents = vfs.read(path)?;
let contents_str = str::from_utf8(&contents)
.map_err(|err| SnapshotError::file_contents_bad_unicode(err, path))?
.to_string();
impl SnapshotMiddleware for SnapshotTxt { let properties = hashmap! {
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult { "Value".to_owned() => RbxValue::String {
let meta = vfs.metadata(path)?; value: contents_str,
},
};
if meta.is_dir() { let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
return Ok(None);
}
let instance_name = match match_file_name(path, ".txt") { let mut snapshot = InstanceSnapshot::new()
Some(name) => name, .name(instance_name)
None => return Ok(None), .class_name("StringValue")
}; .properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
.context(context),
);
let contents = vfs.read(path)?; if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let contents_str = str::from_utf8(&contents) let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
.map_err(|err| SnapshotError::file_contents_bad_unicode(err, path))? metadata.apply_all(&mut snapshot);
.to_string();
let properties = hashmap! {
"Value".to_owned() => RbxValue::String {
value: contents_str,
},
};
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("StringValue")
.properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
.context(context),
);
if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let mut metadata = AdjacentMetadata::from_slice(&meta_contents, &meta_path)?;
metadata.apply_all(&mut snapshot);
}
Ok(Some(snapshot))
} }
Ok(Some(snapshot))
} }
#[cfg(test)] #[cfg(test)]
@@ -75,10 +62,14 @@ mod test {
let mut vfs = Vfs::new(imfs.clone()); let mut vfs = Vfs::new(imfs.clone());
let instance_snapshot = let instance_snapshot = snapshot_txt(
SnapshotTxt::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.txt")) &InstanceContext::default(),
.unwrap() &mut vfs,
.unwrap(); Path::new("/foo.txt"),
"foo",
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot); insta::assert_yaml_snapshot!(instance_snapshot);
} }

View File

@@ -1,56 +0,0 @@
use crate::{
snapshot::InstanceContext,
vfs::{Vfs, VfsEntry, VfsFetcher},
};
use super::middleware::{SnapshotInstanceResult, SnapshotMiddleware};
/// Handles snapshotting of any file that a user plugin wants to handle.
///
/// User plugins are specified in the project file, but there are never user
/// plugins specified unless a Cargo feature is enabled, `user-plugins`.
/// Additionally, extra data needs to be set up inside the snapshot context
/// which is not currently wired up.
pub struct SnapshotUserPlugins;
impl SnapshotMiddleware for SnapshotUserPlugins {
fn from_vfs<F: VfsFetcher>(
_context: &InstanceContext,
_vfs: &Vfs<F>,
_entry: &VfsEntry,
) -> SnapshotInstanceResult {
// TODO: Invoke plugin here and get result out.
// The current plan for plugins here is to make them work
// like Redux/Rodux middleware. A plugin will be a function
// that accepts the next middleware in the chain as a
// function and the snapshot subject (the VFS entry).
//
// Plugins can (but don't have to) invoke the next snapshot
// function and may or may not mutate the result. The hope
// is that this model enables the most flexibility possible
// for plugins to modify existing Rojo output, as well as
// generate new outputs.
//
// Open questions:
// * How will middleware be ordered? Does putting user
// middleware always at the beginning or always at the end
// of the chain reduce the scope of what that middleware
// can do?
//
// * Will plugins hurt Rojo's ability to parallelize
// snapshotting in the future?
//
// * Do the mutable handles to the Vfs and the snapshot
// context prevent plugins from invoking other plugins
// indirectly?
//
// * Will there be problems using a single Lua state because
// of re-entrancy?
//
// * Can the Lua <-> Rojo bindings used for middleware be
// reused for or from another project like Remodel?
Ok(None)
}
}