forked from rojo-rbx/rojo
* Mostly mechanical port bits * Almost there * It builds again! * Turn on all the code again * Tests compiling but not passing * Stub work for value resolution * Implement resolution minus enums and derived properties * Implement property descriptor resolution * Update referent snapshots * Update unions test project Using a place file instead of a model yields better error messages in Roblox Studio. * Add easy shortcut to testing with local rbx-dom * Update rbx-dom * Add enum resolution * Update init.meta.json to use UnresolvedValue * Expand value resolution support, add test * Filter SharedString values from web API * Add 'property' builder method to InstanceSnapshot * Change InstanceSnapshot/InstanceBuilder boundary * Fix remove_file crash * rustfmt * Update to latest rbx_dom_lua * Update dependencies, including rbx_dom_weak * Update to latest rbx-dom * Update dependencies * Update rbx-dom, fixing more bugs * Remove experimental warning on binary place builds * Remove unused imports
73 lines
1.9 KiB
Rust
73 lines
1.9 KiB
Rust
use std::{path::Path, str};
|
|
|
|
use anyhow::Context;
|
|
use maplit::hashmap;
|
|
use memofs::{IoResultExt, Vfs};
|
|
|
|
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
|
|
|
use super::{meta_file::AdjacentMetadata, middleware::SnapshotInstanceResult};
|
|
|
|
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)
|
|
.with_context(|| format!("File was not valid UTF-8: {}", path.display()))?
|
|
.to_owned();
|
|
|
|
let properties = hashmap! {
|
|
"Value".to_owned() => contents_str.into(),
|
|
};
|
|
|
|
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))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
use memofs::{InMemoryFs, VfsSnapshot};
|
|
|
|
#[test]
|
|
fn instance_from_vfs() {
|
|
let mut imfs = InMemoryFs::new();
|
|
imfs.load_snapshot("/foo.txt", VfsSnapshot::file("Hello there!"))
|
|
.unwrap();
|
|
|
|
let mut vfs = Vfs::new(imfs.clone());
|
|
|
|
let instance_snapshot = snapshot_txt(
|
|
&InstanceContext::default(),
|
|
&mut vfs,
|
|
Path::new("/foo.txt"),
|
|
"foo",
|
|
)
|
|
.unwrap()
|
|
.unwrap();
|
|
|
|
insta::assert_yaml_snapshot!(instance_snapshot);
|
|
}
|
|
}
|