use std::{path::Path, str}; use memofs::{IoResultExt, Vfs}; use rbx_dom_weak::{types::Enum, ustr, HashMapExt as _, UstrMap}; use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot}; use super::{ dir::{dir_meta, snapshot_dir_no_meta}, meta_file::AdjacentMetadata, }; #[derive(Debug)] pub enum ScriptType { Server, Client, Module, } /// Core routine for turning Lua files into snapshots. pub fn snapshot_lua( context: &InstanceContext, vfs: &Vfs, path: &Path, name: &str, script_type: ScriptType, ) -> anyhow::Result> { let run_context_enums = &rbx_reflection_database::get() .enums .get("RunContext") .expect("Unable to get RunContext enums!") .items; let (class_name, run_context) = match (context.emit_legacy_scripts, script_type) { (false, ScriptType::Server) => ("Script", run_context_enums.get("Server")), (false, ScriptType::Client) => ("Script", run_context_enums.get("Client")), (true, ScriptType::Server) => ("Script", run_context_enums.get("Legacy")), (true, ScriptType::Client) => ("LocalScript", None), (_, ScriptType::Module) => ("ModuleScript", None), }; let contents = vfs.read_to_string_lf_normalized(path)?; let contents_str = contents.as_str(); let mut properties = UstrMap::with_capacity(2); properties.insert(ustr("Source"), contents_str.into()); if let Some(run_context) = run_context { properties.insert( ustr("RunContext"), Enum::from_u32(run_context.to_owned()).into(), ); } let meta_path = path.with_file_name(format!("{}.meta.json", name)); let mut snapshot = InstanceSnapshot::new() .name(name) .class_name(class_name) .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)) } /// Attempts to snapshot an 'init' Lua script contained inside of a folder with /// the given name. /// /// Scripts named `init.lua`, `init.server.lua`, or `init.client.lua` usurp /// their parents, which acts similarly to `__init__.py` from the Python world. pub fn snapshot_lua_init( context: &InstanceContext, vfs: &Vfs, init_path: &Path, script_type: ScriptType, ) -> anyhow::Result> { let folder_path = init_path.parent().unwrap(); let dir_snapshot = snapshot_dir_no_meta(context, vfs, folder_path)?.unwrap(); if dir_snapshot.class_name != "Folder" { anyhow::bail!( "init.lua, init.server.lua, and init.client.lua can \ only be used if the instance produced by the containing \ directory would be a Folder.\n\ \n\ The directory {} turned into an instance of class {}.", folder_path.display(), dir_snapshot.class_name ); } let mut init_snapshot = snapshot_lua(context, vfs, init_path, &dir_snapshot.name, script_type)?.unwrap(); init_snapshot.children = dir_snapshot.children; init_snapshot.metadata = dir_snapshot.metadata; if let Some(mut meta) = dir_meta(vfs, folder_path)? { meta.apply_all(&mut init_snapshot)?; } Ok(Some(init_snapshot)) } #[cfg(test)] mod test { use super::*; use memofs::{InMemoryFs, VfsSnapshot}; #[test] fn class_module_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!")) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/foo.lua"), "foo", ScriptType::Module, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn runcontext_module_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!")) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(false)), &vfs, Path::new("/foo.lua"), "foo", ScriptType::Module, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn class_server_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!")) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/foo.server.lua"), "foo", ScriptType::Server, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn runcontext_server_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!")) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(false)), &vfs, Path::new("/foo.server.lua"), "foo", ScriptType::Server, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn class_client_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot("/foo.client.lua", VfsSnapshot::file("Hello there!")) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/foo.client.lua"), "foo", ScriptType::Client, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn runcontext_client_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot("/foo.client.lua", VfsSnapshot::file("Hello there!")) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(false)), &vfs, Path::new("/foo.client.lua"), "foo", ScriptType::Client, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[ignore = "init.lua functionality has moved to the root snapshot function"] #[test] fn init_module_from_vfs() { let mut imfs = InMemoryFs::new(); imfs.load_snapshot( "/root", VfsSnapshot::dir([("init.lua", VfsSnapshot::file("Hello!"))]), ) .unwrap(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/root"), "root", ScriptType::Module, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn class_module_with_meta() { 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(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/foo.lua"), "foo", ScriptType::Module, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn runcontext_module_with_meta() { 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(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(false)), &vfs, Path::new("/foo.lua"), "foo", ScriptType::Module, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn class_script_with_meta() { 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(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/foo.server.lua"), "foo", ScriptType::Server, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn runcontext_script_with_meta() { 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(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(false)), &vfs, Path::new("/foo.server.lua"), "foo", ScriptType::Server, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn class_script_disabled() { 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(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(true)), &vfs, Path::new("/bar.server.lua"), "bar", ScriptType::Server, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } #[test] fn runcontext_script_disabled() { 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(); let vfs = Vfs::new(imfs); let instance_snapshot = snapshot_lua( &InstanceContext::with_emit_legacy_scripts(Some(false)), &vfs, Path::new("/bar.server.lua"), "bar", ScriptType::Server, ) .unwrap() .unwrap(); insta::with_settings!({ sort_maps => true }, { insta::assert_yaml_snapshot!(instance_snapshot); }); } }