Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature init csv #594

Merged
merged 2 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: tests/tests/build.rs
assertion_line: 100
expression: contents
---
<roblox version="4">
<Item class="LocalizationTable" referent="0">
<Properties>
<string name="Name">init_csv_with_children</string>
<string name="Contents">[{"key":"init.csv","values":{}}]</string>
</Properties>
<Item class="LocalizationTable" referent="1">
<Properties>
<string name="Name">other</string>
<string name="Contents">[{"key":"other.csv","values":{}}]</string>
</Properties>
</Item>
</Item>
</roblox>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "init_csv_with_children",
"tree": {
"$path": "src"
}
}
2 changes: 2 additions & 0 deletions rojo-test/build-tests/init_csv_with_children/src/init.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Key
init.csv
2 changes: 2 additions & 0 deletions rojo-test/build-tests/init_csv_with_children/src/other.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Key
other.csv
43 changes: 42 additions & 1 deletion src/snapshot_middleware/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use serde::Serialize;

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

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

pub fn snapshot_csv(
_context: &InstanceContext,
Expand Down Expand Up @@ -46,6 +50,43 @@ pub fn snapshot_csv(
Ok(Some(snapshot))
}

/// Attempts to snapshot an 'init' csv contained inside of a folder with
/// the given name.
///
/// csv named `init.csv`
/// their parents, which acts similarly to `__init__.py` from the Python world.
pub fn snapshot_csv_init(
context: &InstanceContext,
vfs: &Vfs,
init_path: &Path,
) -> anyhow::Result<Option<InstanceSnapshot>> {
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.csv 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_csv(context, vfs, init_path)?.unwrap();

init_snapshot.name = dir_snapshot.name;
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))
}

/// Struct that holds any valid row from a Roblox CSV translation table.
///
/// We manually deserialize into this table from CSV, but let serde_json handle
Expand Down
19 changes: 16 additions & 3 deletions src/snapshot_middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use memofs::{IoResultExt, Vfs};
use crate::snapshot::{InstanceContext, InstanceSnapshot};

use self::{
csv::snapshot_csv,
csv::{snapshot_csv, snapshot_csv_init},
dir::snapshot_dir,
json::snapshot_json,
json_model::snapshot_json_model,
Expand Down Expand Up @@ -87,12 +87,19 @@ pub fn snapshot_from_vfs(
return snapshot_lua_init(context, vfs, &init_path);
}

let init_path = path.join("init.csv");
if vfs.metadata(&init_path).with_not_found()?.is_some() {
return snapshot_csv_init(context, vfs, &init_path);
}

snapshot_dir(context, vfs, path)
} else {
let script_name = path
.file_name_trim_end(".lua")
.or_else(|_| path.file_name_trim_end(".luau"));

let csv_name = path.file_name_trim_end(".csv");

if let Ok(name) = script_name {
match name {
// init scripts are handled elsewhere and should not turn into
Expand All @@ -110,8 +117,14 @@ pub fn snapshot_from_vfs(
return Ok(None);
} else if path.file_name_ends_with(".json") {
return snapshot_json(context, vfs, path);
} else if path.file_name_ends_with(".csv") {
return snapshot_csv(context, vfs, path);
} else if let Ok(name) = csv_name {
match name {
// init csv are handled elsewhere and should not turn into
// their own children.
"init" => return Ok(None),

_ => return snapshot_csv(context, vfs, path),
}
} else if path.file_name_ends_with(".txt") {
return snapshot_txt(context, vfs, path);
} else if path.file_name_ends_with(".rbxmx") {
Expand Down
1 change: 1 addition & 0 deletions tests/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ macro_rules! gen_build_tests {
}

gen_build_tests! {
init_csv_with_children,
attributes,
client_in_folder,
client_init,
Expand Down