mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-23 22:25:26 +00:00
VFS in external crate (#297)
* vroom * Port dir middleware * Filter rules * Directory metadata * Project support * Enable Lua support * StringValue support * CSV * rbxm, rbxmx, and rbxlx * JSON models * Clean up some warnings * Strip out PathMap * Unwatch paths when they're reported as removed * Fix 'rojo upload' behavior * Upgrade to Insta 0.13.1 * Update dependencies * Release 0.6.0-alpha.2 * Fix bad merge * Replace MemoryBackend with InMemoryFs * Sledgehammer tests into passing for now * Txt middleware * Update easy snapshot tests * Lua tests * Project middleware tests * Try to fix test failures by sorting * Port first set of serve session tests * Add InMemoryFs::raise_event * Finish porting serve session tests * Remove UI code for introspecting VFS for now * VFS docs
This commit is contained in:
committed by
GitHub
parent
a884f693ae
commit
477e0ada32
@@ -1,13 +1,11 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::{collections::BTreeMap, path::Path};
|
||||
|
||||
use maplit::hashmap;
|
||||
use rbx_dom_weak::RbxValue;
|
||||
use serde::Serialize;
|
||||
use vfs::{IoResultExt, Vfs};
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{FsResultExt, Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
meta_file::AdjacentMetadata,
|
||||
@@ -18,25 +16,21 @@ use super::{
|
||||
pub struct SnapshotCsv;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotCsv {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
_context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
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(entry.path(), ".csv") {
|
||||
let instance_name = match match_file_name(path, ".csv") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let meta_path = entry
|
||||
.path()
|
||||
.with_file_name(format!("{}.meta.json", instance_name));
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
|
||||
|
||||
let table_contents = convert_localization_csv(&entry.contents(vfs)?);
|
||||
let table_contents = convert_localization_csv(&vfs.read(path)?);
|
||||
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
.name(instance_name)
|
||||
@@ -48,12 +42,11 @@ impl SnapshotMiddleware for SnapshotCsv {
|
||||
})
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf(), meta_path.clone()]),
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()]),
|
||||
);
|
||||
|
||||
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
|
||||
let meta_contents = meta_entry.contents(vfs)?;
|
||||
if let Some(meta_contents) = vfs.read(meta_path).with_not_found()? {
|
||||
let mut metadata = AdjacentMetadata::from_slice(&meta_contents);
|
||||
metadata.apply_all(&mut snapshot);
|
||||
}
|
||||
@@ -138,7 +131,7 @@ fn convert_localization_csv(contents: &[u8]) -> String {
|
||||
serde_json::to_string(&entries).expect("Could not encode JSON for localization table")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(all(test, feature = "FIXME"))]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
|
||||
@@ -1,32 +1,27 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use rbx_dom_weak::{RbxId, RbxTree};
|
||||
use vfs::{DirEntry, IoResultExt, Vfs};
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{DirectorySnapshot, FsResultExt, Vfs, VfsEntry, VfsFetcher, VfsSnapshot},
|
||||
};
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
error::SnapshotError,
|
||||
meta_file::DirectoryMetadata,
|
||||
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
|
||||
snapshot_from_instance, snapshot_from_vfs,
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
snapshot_from_vfs,
|
||||
};
|
||||
|
||||
pub struct SnapshotDir;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotDir {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_file() {
|
||||
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
|
||||
let meta = vfs.metadata(path)?;
|
||||
|
||||
if meta.is_file() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let passes_filter_rules = |child: &VfsEntry| {
|
||||
let passes_filter_rules = |child: &DirEntry| {
|
||||
context
|
||||
.path_ignore_rules
|
||||
.iter()
|
||||
@@ -35,31 +30,36 @@ impl SnapshotMiddleware for SnapshotDir {
|
||||
|
||||
let mut snapshot_children = Vec::new();
|
||||
|
||||
for child in entry.children(vfs)?.into_iter().filter(passes_filter_rules) {
|
||||
if let Some(child_snapshot) = snapshot_from_vfs(context, vfs, &child)? {
|
||||
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 = entry
|
||||
.path()
|
||||
let instance_name = path
|
||||
.file_name()
|
||||
.expect("Could not extract file name")
|
||||
.to_str()
|
||||
.ok_or_else(|| SnapshotError::file_name_bad_unicode(entry.path()))?
|
||||
.ok_or_else(|| SnapshotError::file_name_bad_unicode(path))?
|
||||
.to_string();
|
||||
|
||||
let meta_path = entry.path().join("init.meta.json");
|
||||
let meta_path = path.join("init.meta.json");
|
||||
|
||||
let relevant_paths = vec![
|
||||
entry.path().to_path_buf(),
|
||||
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?
|
||||
entry.path().join("init.lua"),
|
||||
entry.path().join("init.server.lua"),
|
||||
entry.path().join("init.client.lua"),
|
||||
path.join("init.lua"),
|
||||
path.join("init.server.lua"),
|
||||
path.join("init.client.lua"),
|
||||
];
|
||||
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
@@ -68,81 +68,61 @@ impl SnapshotMiddleware for SnapshotDir {
|
||||
.children(snapshot_children)
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.instigating_source(path)
|
||||
.relevant_paths(relevant_paths)
|
||||
.context(context),
|
||||
);
|
||||
|
||||
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
|
||||
let meta_contents = meta_entry.contents(vfs)?;
|
||||
if let Some(meta_contents) = vfs.read(meta_path).with_not_found()? {
|
||||
let mut metadata = DirectoryMetadata::from_slice(&meta_contents);
|
||||
metadata.apply_all(&mut snapshot);
|
||||
}
|
||||
|
||||
Ok(Some(snapshot))
|
||||
}
|
||||
|
||||
fn from_instance(tree: &RbxTree, id: RbxId) -> SnapshotFileResult {
|
||||
let instance = tree.get_instance(id).unwrap();
|
||||
|
||||
if instance.class_name != "Folder" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut children = HashMap::new();
|
||||
|
||||
for child_id in instance.get_children_ids() {
|
||||
if let Some((name, child)) = snapshot_from_instance(tree, *child_id) {
|
||||
children.insert(name, child);
|
||||
}
|
||||
}
|
||||
|
||||
let snapshot = VfsSnapshot::Directory(DirectorySnapshot { children });
|
||||
|
||||
Some((instance.name.clone(), snapshot))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use insta::assert_yaml_snapshot;
|
||||
use maplit::hashmap;
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn empty_folder() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir::<String>(HashMap::new());
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo", VfsSnapshot::empty_dir())
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotDir::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotDir::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folder_in_folder() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"Child" => VfsSnapshot::dir::<String>(HashMap::new()),
|
||||
});
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"Child" => VfsSnapshot::empty_dir(),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotDir::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotDir::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use std::{error::Error, fmt, io, path::PathBuf};
|
||||
|
||||
use crate::vfs::FsError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SnapshotError {
|
||||
detail: SnapshotErrorDetail,
|
||||
@@ -73,11 +71,9 @@ impl fmt::Display for SnapshotError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FsError> for SnapshotError {
|
||||
fn from(error: FsError) -> Self {
|
||||
let (inner, path) = error.into_raw();
|
||||
|
||||
Self::new(inner.into(), Some(path))
|
||||
impl From<io::Error> for SnapshotError {
|
||||
fn from(inner: io::Error) -> Self {
|
||||
Self::new(inner.into(), Option::<PathBuf>::None)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
use std::{borrow::Cow, collections::HashMap};
|
||||
use std::{borrow::Cow, collections::HashMap, path::Path};
|
||||
|
||||
use rbx_dom_weak::UnresolvedRbxValue;
|
||||
use rbx_reflection::try_resolve_value;
|
||||
use serde::Deserialize;
|
||||
use vfs::Vfs;
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceSnapshot},
|
||||
vfs::{Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use crate::snapshot::{InstanceContext, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
@@ -17,28 +15,26 @@ use super::{
|
||||
pub struct SnapshotJsonModel;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotJsonModel {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
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(entry.path(), ".model.json") {
|
||||
let instance_name = match match_file_name(path, ".model.json") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let instance: JsonModel =
|
||||
serde_json::from_slice(&entry.contents(vfs)?).expect("TODO: Handle serde_json errors");
|
||||
serde_json::from_slice(&vfs.read(path)?).expect("TODO: Handle serde_json errors");
|
||||
|
||||
if let Some(json_name) = &instance.name {
|
||||
if json_name != instance_name {
|
||||
log::warn!(
|
||||
"Name from JSON model did not match its file name: {}",
|
||||
entry.path().display()
|
||||
path.display()
|
||||
);
|
||||
log::warn!(
|
||||
"In Rojo < alpha 14, this model is named \"{}\" (from its 'Name' property)",
|
||||
@@ -56,8 +52,8 @@ impl SnapshotMiddleware for SnapshotJsonModel {
|
||||
|
||||
snapshot.metadata = snapshot
|
||||
.metadata
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf()])
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf()])
|
||||
.context(context);
|
||||
|
||||
Ok(Some(snapshot))
|
||||
@@ -137,39 +133,43 @@ impl JsonModelCore {
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use insta::assert_yaml_snapshot;
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug, VfsSnapshot};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn model_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"Name": "children",
|
||||
"ClassName": "IntValue",
|
||||
"Properties": {
|
||||
"Value": 5
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
"Name": "The Child",
|
||||
"ClassName": "StringValue"
|
||||
}
|
||||
]
|
||||
}
|
||||
"#,
|
||||
);
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo.model.json",
|
||||
VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"Name": "children",
|
||||
"ClassName": "IntValue",
|
||||
"Properties": {
|
||||
"Value": 5
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
"Name": "The Child",
|
||||
"ClassName": "StringValue"
|
||||
}
|
||||
]
|
||||
}
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.model.json", file);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.model.json").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotJsonModel::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotJsonModel::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.model.json"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
use std::str;
|
||||
use std::{path::Path, str};
|
||||
|
||||
use maplit::hashmap;
|
||||
use rbx_dom_weak::RbxValue;
|
||||
use vfs::{IoResultExt, Vfs};
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{FsResultExt, Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
dir::SnapshotDir,
|
||||
@@ -18,12 +16,8 @@ use super::{
|
||||
pub struct SnapshotLua;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotLua {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
let file_name = entry.path().file_name().unwrap().to_string_lossy();
|
||||
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.
|
||||
@@ -32,18 +26,20 @@ impl SnapshotMiddleware for SnapshotLua {
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if entry.is_file() {
|
||||
snapshot_lua_file(context, vfs, entry)
|
||||
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, entry, "init.lua")? {
|
||||
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, entry, "init.server.lua")? {
|
||||
} 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, entry, "init.client.lua")? {
|
||||
} 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 {
|
||||
@@ -54,12 +50,8 @@ impl SnapshotMiddleware for SnapshotLua {
|
||||
}
|
||||
|
||||
/// Core routine for turning Lua files into snapshots.
|
||||
fn snapshot_lua_file<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
let file_name = entry.path().file_name().unwrap().to_string_lossy();
|
||||
fn snapshot_lua_file(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
|
||||
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")
|
||||
{
|
||||
@@ -72,15 +64,13 @@ fn snapshot_lua_file<F: VfsFetcher>(
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let contents = entry.contents(vfs)?;
|
||||
let contents = vfs.read(path)?;
|
||||
let contents_str = str::from_utf8(&contents)
|
||||
// TODO: Turn into error type
|
||||
.expect("File content was not valid UTF-8")
|
||||
.to_string();
|
||||
|
||||
let meta_path = entry
|
||||
.path()
|
||||
.with_file_name(format!("{}.meta.json", instance_name));
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
|
||||
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
.name(instance_name)
|
||||
@@ -92,13 +82,12 @@ fn snapshot_lua_file<F: VfsFetcher>(
|
||||
})
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf(), meta_path.clone()])
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
|
||||
.context(context),
|
||||
);
|
||||
|
||||
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
|
||||
let meta_contents = meta_entry.contents(vfs)?;
|
||||
if let Some(meta_contents) = vfs.read(meta_path).with_not_found()? {
|
||||
let mut metadata = AdjacentMetadata::from_slice(&meta_contents);
|
||||
metadata.apply_all(&mut snapshot);
|
||||
}
|
||||
@@ -111,17 +100,17 @@ fn snapshot_lua_file<F: VfsFetcher>(
|
||||
///
|
||||
/// Scripts named `init.lua`, `init.server.lua`, or `init.client.lua` usurp
|
||||
/// their parents, which acts similarly to `__init__.py` from the Python world.
|
||||
fn snapshot_init<F: VfsFetcher>(
|
||||
fn snapshot_init(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
folder_entry: &VfsEntry,
|
||||
vfs: &Vfs,
|
||||
folder_path: &Path,
|
||||
init_name: &str,
|
||||
) -> SnapshotInstanceResult {
|
||||
let init_path = folder_entry.path().join(init_name);
|
||||
let init_path = folder_path.join(init_name);
|
||||
|
||||
if let Some(init_entry) = vfs.get(init_path).with_not_found()? {
|
||||
if let Some(dir_snapshot) = SnapshotDir::from_vfs(context, vfs, folder_entry)? {
|
||||
if let Some(mut init_snapshot) = snapshot_lua_file(context, vfs, &init_entry)? {
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
if let Some(dir_snapshot) = SnapshotDir::from_vfs(context, vfs, folder_path)? {
|
||||
if let Some(mut init_snapshot) = snapshot_lua_file(context, vfs, &init_path)? {
|
||||
if dir_snapshot.class_name != "Folder" {
|
||||
panic!(
|
||||
"init.lua, init.server.lua, and init.client.lua can \
|
||||
@@ -146,149 +135,171 @@ fn snapshot_init<F: VfsFetcher>(
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use insta::{assert_yaml_snapshot, with_settings};
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug, VfsSnapshot};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn module_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.lua", file);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.lua").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.server.lua", file);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.server.lua").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotLua::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.server.lua"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn client_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo.client.lua", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.client.lua", file);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.client.lua").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotLua::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.client.lua"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn init_module_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"init.lua" => VfsSnapshot::file("Hello!"),
|
||||
});
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/root",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"init.lua" => VfsSnapshot::file("Hello!"),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/root", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/root").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/root"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_with_meta() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let meta = VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"ignoreUnknownInstances": true
|
||||
}
|
||||
"#,
|
||||
);
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
imfs.load_snapshot(
|
||||
"/foo.meta.json",
|
||||
VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"ignoreUnknownInstances": true
|
||||
}
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.lua", file);
|
||||
vfs.debug_load_snapshot("/foo.meta.json", meta);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.lua").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_with_meta() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let meta = VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"ignoreUnknownInstances": true
|
||||
}
|
||||
"#,
|
||||
);
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
imfs.load_snapshot(
|
||||
"/foo.meta.json",
|
||||
VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"ignoreUnknownInstances": true
|
||||
}
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.server.lua", file);
|
||||
vfs.debug_load_snapshot("/foo.meta.json", meta);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.server.lua").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotLua::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.server.lua"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_disabled() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let meta = VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"properties": {
|
||||
"Disabled": true
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/bar.server.lua", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
imfs.load_snapshot(
|
||||
"/bar.meta.json",
|
||||
VfsSnapshot::file(
|
||||
r#"
|
||||
{
|
||||
"properties": {
|
||||
"Disabled": true
|
||||
}
|
||||
}
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/bar.server.lua", file);
|
||||
vfs.debug_load_snapshot("/bar.meta.json", meta);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/bar.server.lua").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotLua::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotLua::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/bar.server.lua"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
with_settings!({ sort_maps => true }, {
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::with_settings!({ sort_maps => true }, {
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
use rbx_dom_weak::{RbxId, RbxTree};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceSnapshot},
|
||||
vfs::{Vfs, VfsEntry, VfsFetcher, VfsSnapshot},
|
||||
};
|
||||
use vfs::Vfs;
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceSnapshot};
|
||||
|
||||
use super::error::SnapshotError;
|
||||
|
||||
pub type SnapshotInstanceResult = Result<Option<InstanceSnapshot>, SnapshotError>;
|
||||
pub type SnapshotFileResult = Option<(String, VfsSnapshot)>;
|
||||
|
||||
pub trait SnapshotMiddleware {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult;
|
||||
|
||||
fn from_instance(_tree: &RbxTree, _id: RbxId) -> SnapshotFileResult {
|
||||
None
|
||||
}
|
||||
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult;
|
||||
}
|
||||
|
||||
@@ -15,46 +15,37 @@ mod rbxlx;
|
||||
mod rbxm;
|
||||
mod rbxmx;
|
||||
mod txt;
|
||||
mod user_plugins;
|
||||
mod util;
|
||||
|
||||
pub use self::error::*;
|
||||
|
||||
use rbx_dom_weak::{RbxId, RbxTree};
|
||||
use std::path::Path;
|
||||
|
||||
use vfs::Vfs;
|
||||
|
||||
use self::middleware::{SnapshotInstanceResult, SnapshotMiddleware};
|
||||
use self::{
|
||||
csv::SnapshotCsv,
|
||||
dir::SnapshotDir,
|
||||
json_model::SnapshotJsonModel,
|
||||
lua::SnapshotLua,
|
||||
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
|
||||
project::SnapshotProject,
|
||||
rbxlx::SnapshotRbxlx,
|
||||
rbxm::SnapshotRbxm,
|
||||
rbxmx::SnapshotRbxmx,
|
||||
csv::SnapshotCsv, dir::SnapshotDir, json_model::SnapshotJsonModel, lua::SnapshotLua,
|
||||
project::SnapshotProject, rbxlx::SnapshotRbxlx, rbxm::SnapshotRbxm, rbxmx::SnapshotRbxmx,
|
||||
txt::SnapshotTxt,
|
||||
user_plugins::SnapshotUserPlugins,
|
||||
};
|
||||
use crate::{
|
||||
snapshot::InstanceContext,
|
||||
vfs::{Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use crate::snapshot::InstanceContext;
|
||||
|
||||
pub use self::project::snapshot_project_node;
|
||||
|
||||
macro_rules! middlewares {
|
||||
( $($middleware: ident,)* ) => {
|
||||
/// Generates a snapshot of instances from the given VfsEntry.
|
||||
pub fn snapshot_from_vfs<F: VfsFetcher>(
|
||||
/// Generates a snapshot of instances from the given path.
|
||||
pub fn snapshot_from_vfs(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
) -> SnapshotInstanceResult {
|
||||
$(
|
||||
log::trace!("trying middleware {} on {}", stringify!($middleware), entry.path().display());
|
||||
log::trace!("trying middleware {} on {}", stringify!($middleware), path.display());
|
||||
|
||||
if let Some(snapshot) = $middleware::from_vfs(context, vfs, entry)? {
|
||||
log::trace!("middleware {} success on {}", stringify!($middleware), entry.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));
|
||||
}
|
||||
)*
|
||||
@@ -62,24 +53,11 @@ macro_rules! middlewares {
|
||||
log::trace!("no middleware returned Ok(Some)");
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Generates an in-memory filesystem snapshot of the given Roblox
|
||||
/// instance.
|
||||
pub fn snapshot_from_instance(tree: &RbxTree, id: RbxId) -> SnapshotFileResult {
|
||||
$(
|
||||
if let Some(result) = $middleware::from_instance(tree, id) {
|
||||
return Some(result);
|
||||
}
|
||||
)*
|
||||
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
middlewares! {
|
||||
SnapshotProject,
|
||||
SnapshotUserPlugins,
|
||||
SnapshotJsonModel,
|
||||
SnapshotRbxlx,
|
||||
SnapshotRbxmx,
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use std::{borrow::Cow, collections::HashMap, path::Path};
|
||||
|
||||
use rbx_reflection::try_resolve_value;
|
||||
use vfs::{IoResultExt, Vfs};
|
||||
|
||||
use crate::{
|
||||
project::{Project, ProjectNode},
|
||||
snapshot::{
|
||||
InstanceContext, InstanceMetadata, InstanceSnapshot, InstigatingSource, PathIgnoreRule,
|
||||
},
|
||||
vfs::{FsResultExt, Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
|
||||
use super::{
|
||||
@@ -22,30 +22,28 @@ use super::{
|
||||
pub struct SnapshotProject;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotProject {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
let project_path = entry.path().join("default.project.json");
|
||||
fn from_vfs(context: &InstanceContext, vfs: &Vfs, path: &Path) -> SnapshotInstanceResult {
|
||||
let meta = vfs.metadata(path)?;
|
||||
|
||||
match vfs.get(project_path).with_not_found()? {
|
||||
if meta.is_dir() {
|
||||
let project_path = path.join("default.project.json");
|
||||
|
||||
match vfs.metadata(&project_path).with_not_found()? {
|
||||
// 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(entry) => return SnapshotProject::from_vfs(context, vfs, &entry),
|
||||
Some(_meta) => return SnapshotProject::from_vfs(context, vfs, &project_path),
|
||||
None => return Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
if !entry.path().to_string_lossy().ends_with(".project.json") {
|
||||
if !path.to_string_lossy().ends_with(".project.json") {
|
||||
// This isn't a project file, so it's not our job.
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let project = Project::load_from_slice(&entry.contents(vfs)?, entry.path())
|
||||
.map_err(|err| SnapshotError::malformed_project(err, entry.path()))?;
|
||||
let project = Project::load_from_slice(&vfs.read(path)?, path)
|
||||
.map_err(|err| SnapshotError::malformed_project(err, path))?;
|
||||
|
||||
let mut context = context.clone();
|
||||
|
||||
@@ -75,7 +73,7 @@ impl SnapshotMiddleware for SnapshotProject {
|
||||
// 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(entry.path().to_path_buf().into());
|
||||
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.
|
||||
@@ -83,21 +81,18 @@ impl SnapshotMiddleware for SnapshotProject {
|
||||
// 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(entry.path().to_path_buf());
|
||||
snapshot.metadata.relevant_paths.push(path.to_path_buf());
|
||||
|
||||
Ok(Some(snapshot))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn snapshot_project_node<F: VfsFetcher>(
|
||||
pub fn snapshot_project_node(
|
||||
context: &InstanceContext,
|
||||
project_folder: &Path,
|
||||
instance_name: &str,
|
||||
node: &ProjectNode,
|
||||
vfs: &Vfs<F>,
|
||||
vfs: &Vfs,
|
||||
) -> SnapshotInstanceResult {
|
||||
let name = Cow::Owned(instance_name.to_owned());
|
||||
let mut class_name = node
|
||||
@@ -117,9 +112,7 @@ pub fn snapshot_project_node<F: VfsFetcher>(
|
||||
Cow::Borrowed(path)
|
||||
};
|
||||
|
||||
let entry = vfs.get(path.as_path())?;
|
||||
|
||||
if let Some(snapshot) = snapshot_from_vfs(context, vfs, &entry)? {
|
||||
if let Some(snapshot) = snapshot_from_vfs(context, vfs, &path)? {
|
||||
// If a class name was already specified, then it'll override the
|
||||
// class name of this snapshot ONLY if it's a Folder.
|
||||
//
|
||||
@@ -217,259 +210,284 @@ pub fn snapshot_project_node<F: VfsFetcher>(
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use insta::assert_yaml_snapshot;
|
||||
use maplit::hashmap;
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug, VfsSnapshot};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn project_from_folder() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "indirect-project",
|
||||
"tree": {
|
||||
"$className": "Folder"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "indirect-project",
|
||||
"tree": {
|
||||
"$className": "Folder"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_from_direct_file() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"hello.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "direct-project",
|
||||
"tree": {
|
||||
"$className": "Model"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"hello.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "direct-project",
|
||||
"tree": {
|
||||
"$className": "Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo/hello.project.json").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
let instance_snapshot = SnapshotProject::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo/hello.project.json"),
|
||||
)
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_with_resolved_properties() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "resolved-properties",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": {
|
||||
"Type": "String",
|
||||
"Value": "Hello, world!"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "resolved-properties",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": {
|
||||
"Type": "String",
|
||||
"Value": "Hello, world!"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_with_unresolved_properties() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "unresolved-properties",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": "Hi!"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "unresolved-properties",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": "Hi!"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_with_children() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "children",
|
||||
"tree": {
|
||||
"$className": "Folder",
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "children",
|
||||
"tree": {
|
||||
"$className": "Folder",
|
||||
|
||||
"Child": {
|
||||
"$className": "Model"
|
||||
"Child": {
|
||||
"$className": "Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_with_path_to_txt() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-project",
|
||||
"tree": {
|
||||
"$path": "other.txt"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-project",
|
||||
"tree": {
|
||||
"$path": "other.txt"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
"other.txt" => VfsSnapshot::file("Hello, world!"),
|
||||
});
|
||||
"#),
|
||||
"other.txt" => VfsSnapshot::file("Hello, world!"),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_with_path_to_project() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-project",
|
||||
"tree": {
|
||||
"$path": "other.project.json"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-project",
|
||||
"tree": {
|
||||
"$path": "other.project.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
"other.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "other-project",
|
||||
"tree": {
|
||||
"$className": "Model"
|
||||
"#),
|
||||
"other.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "other-project",
|
||||
"tree": {
|
||||
"$className": "Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_with_path_to_project_with_children() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-child-project",
|
||||
"tree": {
|
||||
"$path": "other.project.json"
|
||||
}
|
||||
}
|
||||
"#),
|
||||
"other.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "other-project",
|
||||
"tree": {
|
||||
"$className": "Folder",
|
||||
|
||||
"SomeChild": {
|
||||
"$className": "Model"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-child-project",
|
||||
"tree": {
|
||||
"$path": "other.project.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
"other.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "other-project",
|
||||
"tree": {
|
||||
"$className": "Folder",
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
"SomeChild": {
|
||||
"$className": "Model"
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
/// Ensures that if a property is defined both in the resulting instance
|
||||
@@ -479,40 +497,43 @@ mod test {
|
||||
fn project_path_property_overrides() {
|
||||
let _ = env_logger::try_init();
|
||||
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let dir = VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-property-override",
|
||||
"tree": {
|
||||
"$path": "other.project.json",
|
||||
"$properties": {
|
||||
"Value": "Changed"
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo",
|
||||
VfsSnapshot::dir(hashmap! {
|
||||
"default.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "path-property-override",
|
||||
"tree": {
|
||||
"$path": "other.project.json",
|
||||
"$properties": {
|
||||
"Value": "Changed"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
"other.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "other-project",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": "Original"
|
||||
"#),
|
||||
"other.project.json" => VfsSnapshot::file(r#"
|
||||
{
|
||||
"name": "other-project",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": "Original"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#),
|
||||
});
|
||||
"#),
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo", dir);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotProject::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo"))
|
||||
.expect("snapshot error")
|
||||
.expect("snapshot returned no instances");
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
use vfs::Vfs;
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
@@ -11,16 +12,14 @@ use super::{
|
||||
pub struct SnapshotRbxlx;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotRbxlx {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
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(entry.path(), ".rbxlx") {
|
||||
let instance_name = match match_file_name(path, ".rbxlx") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
@@ -28,7 +27,7 @@ impl SnapshotMiddleware for SnapshotRbxlx {
|
||||
let options = rbx_xml::DecodeOptions::new()
|
||||
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
|
||||
|
||||
let temp_tree = rbx_xml::from_reader(entry.contents(vfs)?.as_slice(), options)
|
||||
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();
|
||||
@@ -37,8 +36,8 @@ impl SnapshotMiddleware for SnapshotRbxlx {
|
||||
.name(instance_name)
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf()])
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf()])
|
||||
.context(context),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, path::Path};
|
||||
|
||||
use rbx_dom_weak::{RbxInstanceProperties, RbxTree};
|
||||
use vfs::Vfs;
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
@@ -15,16 +13,14 @@ use super::{
|
||||
pub struct SnapshotRbxm;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotRbxm {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
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(entry.path(), ".rbxm") {
|
||||
let instance_name = match match_file_name(path, ".rbxm") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
@@ -36,7 +32,7 @@ impl SnapshotMiddleware for SnapshotRbxm {
|
||||
});
|
||||
|
||||
let root_id = temp_tree.get_root_id();
|
||||
rbx_binary::decode(&mut temp_tree, root_id, entry.contents(vfs)?.as_slice())
|
||||
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();
|
||||
@@ -47,8 +43,8 @@ impl SnapshotMiddleware for SnapshotRbxm {
|
||||
.name(instance_name)
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf()])
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf()])
|
||||
.context(context),
|
||||
);
|
||||
|
||||
@@ -63,20 +59,26 @@ impl SnapshotMiddleware for SnapshotRbxm {
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug, VfsSnapshot};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn model_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file(include_bytes!("../../assets/test-folder.rbxm").to_vec());
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo.rbxm",
|
||||
VfsSnapshot::file(include_bytes!("../../assets/test-folder.rbxm").to_vec()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.rbxm", file);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.rbxm").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotRbxm::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotRbxm::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.rbxm"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(instance_snapshot.name, "foo");
|
||||
assert_eq!(instance_snapshot.class_name, "Folder");
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{Vfs, VfsEntry, VfsFetcher},
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
use vfs::Vfs;
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
@@ -11,16 +12,14 @@ use super::{
|
||||
pub struct SnapshotRbxmx;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotRbxmx {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
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(entry.path(), ".rbxmx") {
|
||||
let instance_name = match match_file_name(path, ".rbxmx") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
@@ -28,7 +27,7 @@ impl SnapshotMiddleware for SnapshotRbxmx {
|
||||
let options = rbx_xml::DecodeOptions::new()
|
||||
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
|
||||
|
||||
let temp_tree = rbx_xml::from_reader(entry.contents(vfs)?.as_slice(), options)
|
||||
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();
|
||||
@@ -39,8 +38,8 @@ impl SnapshotMiddleware for SnapshotRbxmx {
|
||||
.name(instance_name)
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf()])
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf()])
|
||||
.context(context),
|
||||
);
|
||||
|
||||
@@ -55,36 +54,40 @@ impl SnapshotMiddleware for SnapshotRbxmx {
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug, VfsSnapshot};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn model_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file(
|
||||
r#"
|
||||
<roblox version="4">
|
||||
<Item class="Folder" referent="0">
|
||||
<Properties>
|
||||
<string name="Name">THIS NAME IS IGNORED</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
</roblox>
|
||||
"#,
|
||||
);
|
||||
fn plain_folder() {
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot(
|
||||
"/foo.rbxmx",
|
||||
VfsSnapshot::file(
|
||||
r#"
|
||||
<roblox version="4">
|
||||
<Item class="Folder" referent="0">
|
||||
<Properties>
|
||||
<string name="Name">THIS NAME IS IGNORED</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
</roblox>
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.rbxmx", file);
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let entry = vfs.get("/foo.rbxmx").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotRbxmx::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = SnapshotRbxmx::from_vfs(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.rbxmx"),
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(instance_snapshot.name, "foo");
|
||||
assert_eq!(instance_snapshot.class_name, "Folder");
|
||||
assert_eq!(instance_snapshot.properties, HashMap::new());
|
||||
assert_eq!(instance_snapshot.properties, Default::default());
|
||||
assert_eq!(instance_snapshot.children, Vec::new());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,36 @@
|
||||
use std::str;
|
||||
use std::{path::Path, str};
|
||||
|
||||
use maplit::hashmap;
|
||||
use rbx_dom_weak::{RbxId, RbxTree, RbxValue};
|
||||
use rbx_dom_weak::RbxValue;
|
||||
use vfs::{IoResultExt, Vfs};
|
||||
|
||||
use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
vfs::{FileSnapshot, FsResultExt, Vfs, VfsEntry, VfsFetcher, VfsSnapshot},
|
||||
};
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{
|
||||
error::SnapshotError,
|
||||
meta_file::AdjacentMetadata,
|
||||
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
|
||||
middleware::{SnapshotInstanceResult, SnapshotMiddleware},
|
||||
util::match_file_name,
|
||||
};
|
||||
|
||||
pub struct SnapshotTxt;
|
||||
|
||||
impl SnapshotMiddleware for SnapshotTxt {
|
||||
fn from_vfs<F: VfsFetcher>(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs<F>,
|
||||
entry: &VfsEntry,
|
||||
) -> SnapshotInstanceResult {
|
||||
if entry.is_directory() {
|
||||
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(entry.path(), ".txt") {
|
||||
let instance_name = match match_file_name(path, ".txt") {
|
||||
Some(name) => name,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let contents = entry.contents(vfs)?;
|
||||
let contents = vfs.read(path)?;
|
||||
let contents_str = str::from_utf8(&contents)
|
||||
.map_err(|err| SnapshotError::file_contents_bad_unicode(err, entry.path()))?
|
||||
.map_err(|err| SnapshotError::file_contents_bad_unicode(err, path))?
|
||||
.to_string();
|
||||
|
||||
let properties = hashmap! {
|
||||
@@ -43,9 +39,7 @@ impl SnapshotMiddleware for SnapshotTxt {
|
||||
},
|
||||
};
|
||||
|
||||
let meta_path = entry
|
||||
.path()
|
||||
.with_file_name(format!("{}.meta.json", instance_name));
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
|
||||
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
.name(instance_name)
|
||||
@@ -53,99 +47,39 @@ impl SnapshotMiddleware for SnapshotTxt {
|
||||
.properties(properties)
|
||||
.metadata(
|
||||
InstanceMetadata::new()
|
||||
.instigating_source(entry.path())
|
||||
.relevant_paths(vec![entry.path().to_path_buf(), meta_path.clone()])
|
||||
.instigating_source(path)
|
||||
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
|
||||
.context(context),
|
||||
);
|
||||
|
||||
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
|
||||
let meta_contents = meta_entry.contents(vfs)?;
|
||||
if let Some(meta_contents) = vfs.read(meta_path).with_not_found()? {
|
||||
let mut metadata = AdjacentMetadata::from_slice(&meta_contents);
|
||||
metadata.apply_all(&mut snapshot);
|
||||
}
|
||||
|
||||
Ok(Some(snapshot))
|
||||
}
|
||||
|
||||
fn from_instance(tree: &RbxTree, id: RbxId) -> SnapshotFileResult {
|
||||
let instance = tree.get_instance(id).unwrap();
|
||||
|
||||
if instance.class_name != "StringValue" {
|
||||
return None;
|
||||
}
|
||||
|
||||
if !instance.get_children_ids().is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let value = match instance.properties.get("Value") {
|
||||
Some(RbxValue::String { value }) => value.clone(),
|
||||
Some(_) => panic!("wrong type ahh"),
|
||||
None => String::new(),
|
||||
};
|
||||
|
||||
let snapshot = VfsSnapshot::File(FileSnapshot {
|
||||
contents: value.into_bytes(),
|
||||
});
|
||||
|
||||
let mut file_name = instance.name.clone();
|
||||
file_name.push_str(".txt");
|
||||
|
||||
Some((file_name, snapshot))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
use insta::assert_yaml_snapshot;
|
||||
use maplit::hashmap;
|
||||
use rbx_dom_weak::RbxInstanceProperties;
|
||||
|
||||
use crate::vfs::{NoopFetcher, VfsDebug};
|
||||
use vfs::{InMemoryFs, VfsSnapshot};
|
||||
|
||||
#[test]
|
||||
fn instance_from_vfs() {
|
||||
let mut vfs = Vfs::new(NoopFetcher);
|
||||
let file = VfsSnapshot::file("Hello there!");
|
||||
let mut imfs = InMemoryFs::new();
|
||||
imfs.load_snapshot("/foo.txt", VfsSnapshot::file("Hello there!"))
|
||||
.unwrap();
|
||||
|
||||
vfs.debug_load_snapshot("/foo.txt", file);
|
||||
let mut vfs = Vfs::new(imfs.clone());
|
||||
|
||||
let entry = vfs.get("/foo.txt").unwrap();
|
||||
let instance_snapshot =
|
||||
SnapshotTxt::from_vfs(&InstanceContext::default(), &mut vfs, &entry)
|
||||
SnapshotTxt::from_vfs(&InstanceContext::default(), &mut vfs, Path::new("/foo.txt"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vfs_from_instance() {
|
||||
let tree = RbxTree::new(string_value("Root", "Hello, world!"));
|
||||
let root_id = tree.get_root_id();
|
||||
|
||||
let (_file_name, _file) = SnapshotTxt::from_instance(&tree, root_id).unwrap();
|
||||
}
|
||||
|
||||
fn folder(name: impl Into<String>) -> RbxInstanceProperties {
|
||||
RbxInstanceProperties {
|
||||
name: name.into(),
|
||||
class_name: "Folder".to_owned(),
|
||||
properties: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn string_value(name: impl Into<String>, value: impl Into<String>) -> RbxInstanceProperties {
|
||||
RbxInstanceProperties {
|
||||
name: name.into(),
|
||||
class_name: "StringValue".to_owned(),
|
||||
properties: hashmap! {
|
||||
"Value".to_owned() => RbxValue::String {
|
||||
value: value.into(),
|
||||
},
|
||||
},
|
||||
}
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user