mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-25 15:16:07 +00:00
Fix Lua snapshot code to work with children.
It's also way easier to read now
This commit is contained in:
1
rojo-test/build-tests/init_with_children/init.lua
Normal file
1
rojo-test/build-tests/init_with_children/init.lua
Normal file
@@ -0,0 +1 @@
|
|||||||
|
-- init.lua
|
||||||
1
rojo-test/build-tests/init_with_children/other.lua
Normal file
1
rojo-test/build-tests/init_with_children/other.lua
Normal file
@@ -0,0 +1 @@
|
|||||||
|
-- other.lua
|
||||||
@@ -24,6 +24,7 @@ gen_build_tests! {
|
|||||||
csv_in_folder,
|
csv_in_folder,
|
||||||
deep_nesting,
|
deep_nesting,
|
||||||
gitkeep,
|
gitkeep,
|
||||||
|
init_with_children,
|
||||||
json_model_in_folder,
|
json_model_in_folder,
|
||||||
json_model_legacy_name,
|
json_model_legacy_name,
|
||||||
module_in_folder,
|
module_in_folder,
|
||||||
|
|||||||
20
rojo-test/src/snapshots/build_test__init_with_children.snap
Normal file
20
rojo-test/src/snapshots/build_test__init_with_children.snap
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
created: "2019-08-30T00:12:56.686707700Z"
|
||||||
|
creator: insta@0.10.1
|
||||||
|
source: rojo-test/src/build_test.rs
|
||||||
|
expression: contents
|
||||||
|
---
|
||||||
|
<roblox version="4">
|
||||||
|
<Item class="ModuleScript" referent="0">
|
||||||
|
<Properties>
|
||||||
|
<string name="Name">init_with_children</string>
|
||||||
|
<string name="Source">-- init.lua</string>
|
||||||
|
</Properties>
|
||||||
|
<Item class="ModuleScript" referent="1">
|
||||||
|
<Properties>
|
||||||
|
<string name="Name">other</string>
|
||||||
|
<string name="Source">-- other.lua</string>
|
||||||
|
</Properties>
|
||||||
|
</Item>
|
||||||
|
</Item>
|
||||||
|
</roblox>
|
||||||
@@ -8,7 +8,10 @@ use crate::{
|
|||||||
snapshot::InstanceSnapshot,
|
snapshot::InstanceSnapshot,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware};
|
use super::{
|
||||||
|
dir::SnapshotDir,
|
||||||
|
middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware},
|
||||||
|
};
|
||||||
|
|
||||||
pub struct SnapshotLua;
|
pub struct SnapshotLua;
|
||||||
|
|
||||||
@@ -19,64 +22,29 @@ impl SnapshotMiddleware for SnapshotLua {
|
|||||||
) -> SnapshotInstanceResult<'static> {
|
) -> SnapshotInstanceResult<'static> {
|
||||||
let file_name = entry.path().file_name().unwrap().to_string_lossy();
|
let file_name = entry.path().file_name().unwrap().to_string_lossy();
|
||||||
|
|
||||||
if entry.is_directory() {
|
// These paths alter their parent instance, so we don't need to turn
|
||||||
let module_init_path = entry.path().join("init.lua");
|
// them into a script instance here.
|
||||||
if let Some(init_entry) = imfs.get(module_init_path).with_not_found()? {
|
match &*file_name {
|
||||||
if let Some(mut snapshot) = SnapshotLua::from_imfs(imfs, &init_entry)? {
|
"init.lua" | "init.server.lua" | "init.client.lua" => return Ok(None),
|
||||||
snapshot.name = Cow::Owned(file_name.into_owned());
|
_ => {}
|
||||||
|
|
||||||
return Ok(Some(snapshot));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let server_init_path = entry.path().join("init.server.lua");
|
|
||||||
if let Some(init_entry) = imfs.get(server_init_path).with_not_found()? {
|
|
||||||
if let Some(mut snapshot) = SnapshotLua::from_imfs(imfs, &init_entry)? {
|
|
||||||
snapshot.name = Cow::Owned(file_name.into_owned());
|
|
||||||
|
|
||||||
return Ok(Some(snapshot));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let client_init_path = entry.path().join("init.client.lua");
|
|
||||||
if let Some(init_entry) = imfs.get(client_init_path).with_not_found()? {
|
|
||||||
if let Some(mut snapshot) = SnapshotLua::from_imfs(imfs, &init_entry)? {
|
|
||||||
snapshot.name = Cow::Owned(file_name.into_owned());
|
|
||||||
|
|
||||||
return Ok(Some(snapshot));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (class_name, instance_name) =
|
if entry.is_file() {
|
||||||
if let Some(name) = match_trailing(&file_name, ".server.lua") {
|
snapshot_lua_file(imfs, entry)
|
||||||
("Script", name)
|
} else {
|
||||||
} else if let Some(name) = match_trailing(&file_name, ".client.lua") {
|
if let Some(snapshot) = snapshot_init(imfs, entry, "init.lua")? {
|
||||||
("LocalScript", name)
|
// An `init.lua` file turns its parent into a ModuleScript
|
||||||
} else if let Some(name) = match_trailing(&file_name, ".lua") {
|
Ok(Some(snapshot))
|
||||||
("ModuleScript", name)
|
} else if let Some(snapshot) = snapshot_init(imfs, entry, "init.server.lua")? {
|
||||||
|
// An `init.server.lua` file turns its parent into a Script
|
||||||
|
Ok(Some(snapshot))
|
||||||
|
} else if let Some(snapshot) = snapshot_init(imfs, entry, "init.client.lua")? {
|
||||||
|
// An `init.client.lua` file turns its parent into a LocalScript
|
||||||
|
Ok(Some(snapshot))
|
||||||
} else {
|
} else {
|
||||||
return Ok(None);
|
Ok(None)
|
||||||
};
|
}
|
||||||
|
}
|
||||||
let contents = entry.contents(imfs)?;
|
|
||||||
let contents_str = str::from_utf8(contents)
|
|
||||||
.expect("File content was not valid UTF-8")
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let properties = hashmap! {
|
|
||||||
"Source".to_owned() => RbxValue::String {
|
|
||||||
value: contents_str,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Some(InstanceSnapshot {
|
|
||||||
snapshot_id: None,
|
|
||||||
name: Cow::Owned(instance_name.to_owned()),
|
|
||||||
class_name: Cow::Borrowed(class_name),
|
|
||||||
properties,
|
|
||||||
children: Vec::new(),
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_instance(tree: &RbxTree, id: RbxId) -> SnapshotFileResult {
|
fn from_instance(tree: &RbxTree, id: RbxId) -> SnapshotFileResult {
|
||||||
@@ -91,6 +59,70 @@ impl SnapshotMiddleware for SnapshotLua {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Core routine for turning Lua files into snapshots.
|
||||||
|
fn snapshot_lua_file<F: ImfsFetcher>(
|
||||||
|
imfs: &mut Imfs<F>,
|
||||||
|
entry: &ImfsEntry,
|
||||||
|
) -> SnapshotInstanceResult<'static> {
|
||||||
|
let file_name = entry.path().file_name().unwrap().to_string_lossy();
|
||||||
|
|
||||||
|
let (class_name, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua")
|
||||||
|
{
|
||||||
|
("Script", name)
|
||||||
|
} else if let Some(name) = match_trailing(&file_name, ".client.lua") {
|
||||||
|
("LocalScript", name)
|
||||||
|
} else if let Some(name) = match_trailing(&file_name, ".lua") {
|
||||||
|
("ModuleScript", name)
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
let contents = entry.contents(imfs)?;
|
||||||
|
let contents_str = str::from_utf8(contents)
|
||||||
|
.expect("File content was not valid UTF-8")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let properties = hashmap! {
|
||||||
|
"Source".to_owned() => RbxValue::String {
|
||||||
|
value: contents_str,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(InstanceSnapshot {
|
||||||
|
snapshot_id: None,
|
||||||
|
name: Cow::Owned(instance_name.to_owned()),
|
||||||
|
class_name: Cow::Borrowed(class_name),
|
||||||
|
properties,
|
||||||
|
children: Vec::new(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
fn snapshot_init<F: ImfsFetcher>(
|
||||||
|
imfs: &mut Imfs<F>,
|
||||||
|
folder_entry: &ImfsEntry,
|
||||||
|
init_name: &str,
|
||||||
|
) -> SnapshotInstanceResult<'static> {
|
||||||
|
let init_path = folder_entry.path().join(init_name);
|
||||||
|
|
||||||
|
if let Some(init_entry) = imfs.get(init_path).with_not_found()? {
|
||||||
|
if let Some(mut dir_snapshot) = SnapshotDir::from_imfs(imfs, folder_entry)? {
|
||||||
|
if let Some(mut init_snapshot) = snapshot_lua_file(imfs, &init_entry)? {
|
||||||
|
init_snapshot.name = dir_snapshot.name;
|
||||||
|
init_snapshot.children = dir_snapshot.children;
|
||||||
|
|
||||||
|
return Ok(Some(init_snapshot));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
fn match_trailing<'a>(input: &'a str, trailer: &str) -> Option<&'a str> {
|
fn match_trailing<'a>(input: &'a str, trailer: &str) -> Option<&'a str> {
|
||||||
if input.ends_with(trailer) {
|
if input.ends_with(trailer) {
|
||||||
let end = input.len().saturating_sub(trailer.len());
|
let end = input.len().saturating_sub(trailer.len());
|
||||||
|
|||||||
Reference in New Issue
Block a user