Skip to content

Commit

Permalink
Defer application of init.meta.json until after init Lua files. (ro…
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy authored Jun 10, 2022
1 parent 135a096 commit 16129d9
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/tests/build.rs
assertion_line: 98
expression: contents
---
<roblox version="4">
<Item class="LocalScript" referent="0">
<Properties>
<string name="Name">issue_546</string>
<bool name="Disabled">true</bool>
<string name="Source">print("Hello, world!")</string>
</Properties>
</Item>
</roblox>
2 changes: 2 additions & 0 deletions rojo-test/build-tests/issue_546/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Issue #546 (https://github.com/rojo-rbx/rojo/issues/546)
Regression from Rojo 6.2.0 to Rojo 7.0.0. Meta files named as init.meta.json should apply after init.client.lua and other init files.
6 changes: 6 additions & 0 deletions rojo-test/build-tests/issue_546/default.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "issue_546",
"tree": {
"$path": "hello"
}
}
1 change: 1 addition & 0 deletions rojo-test/build-tests/issue_546/hello/init.client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello, world!")
5 changes: 5 additions & 0 deletions rojo-test/build-tests/issue_546/hello/init.meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"properties": {
"Disabled": true
}
}
41 changes: 35 additions & 6 deletions src/snapshot_middleware/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ pub fn snapshot_dir(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
) -> anyhow::Result<Option<InstanceSnapshot>> {
let mut snapshot = match snapshot_dir_no_meta(context, vfs, path)? {
Some(snapshot) => snapshot,
None => return Ok(None),
};

if let Some(mut meta) = dir_meta(vfs, path)? {
meta.apply_all(&mut snapshot)?;
}

Ok(Some(snapshot))
}

/// Retrieves the meta file that should be applied for this directory, if it
/// exists.
pub fn dir_meta(vfs: &Vfs, path: &Path) -> anyhow::Result<Option<DirectoryMetadata>> {
let meta_path = path.join("init.meta.json");

if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let metadata = DirectoryMetadata::from_slice(&meta_contents, meta_path)?;
Ok(Some(metadata))
} else {
Ok(None)
}
}

/// Snapshot a directory without applying meta files; useful for if the
/// directory's ClassName will change before metadata should be applied. For
/// example, this can happen if the directory contains an `init.client.lua`
/// file.
pub fn snapshot_dir_no_meta(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
) -> anyhow::Result<Option<InstanceSnapshot>> {
let passes_filter_rules = |child: &DirEntry| {
context
Expand Down Expand Up @@ -52,7 +86,7 @@ pub fn snapshot_dir(
path.join("init.client.lua"),
];

let mut snapshot = InstanceSnapshot::new()
let snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name("Folder")
.children(snapshot_children)
Expand All @@ -63,11 +97,6 @@ pub fn snapshot_dir(
.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))
}

Expand Down
12 changes: 10 additions & 2 deletions src/snapshot_middleware/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use memofs::{IoResultExt, Vfs};

use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};

use super::{dir::snapshot_dir, meta_file::AdjacentMetadata, util::match_trailing};
use super::{
dir::{dir_meta, snapshot_dir_no_meta},
meta_file::AdjacentMetadata,
util::match_trailing,
};

/// Core routine for turning Lua files into snapshots.
pub fn snapshot_lua(
Expand Down Expand Up @@ -66,7 +70,7 @@ pub fn snapshot_lua_init(
init_path: &Path,
) -> anyhow::Result<Option<InstanceSnapshot>> {
let folder_path = init_path.parent().unwrap();
let dir_snapshot = snapshot_dir(context, vfs, folder_path)?.unwrap();
let dir_snapshot = snapshot_dir_no_meta(context, vfs, folder_path)?.unwrap();

if dir_snapshot.class_name != "Folder" {
anyhow::bail!(
Expand All @@ -86,6 +90,10 @@ pub fn snapshot_lua_init(
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))
}

Expand Down
3 changes: 2 additions & 1 deletion tests/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ gen_build_tests! {
init_meta_class_name,
init_meta_properties,
init_with_children,
issue_546,
json_as_lua,
json_model_in_folder,
json_model_legacy_name,
module_in_folder,
module_init,
optional,
project_composed_default,
project_composed_file,
project_root_name,
Expand All @@ -53,7 +55,6 @@ gen_build_tests! {
txt,
txt_in_folder,
unresolved_values,
optional,
weldconstraint,
}

Expand Down

0 comments on commit 16129d9

Please sign in to comment.