feature init csv (#594)

* init csv feature + test

* fmt fixes
This commit is contained in:
Boegie19
2022-07-30 03:45:19 +02:00
committed by GitHub
parent 62eb4f026f
commit 5cb4cc0d1d
7 changed files with 88 additions and 4 deletions

View File

@@ -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>

View File

@@ -0,0 +1,6 @@
{
"name": "init_csv_with_children",
"tree": {
"$path": "src"
}
}

View File

@@ -0,0 +1,2 @@
Key
init.csv
1 Key
2 init.csv

View File

@@ -0,0 +1,2 @@
Key
other.csv
1 Key
2 other.csv

View File

@@ -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,
@@ -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

View File

@@ -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,
@@ -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
@@ -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") {

View File

@@ -21,6 +21,7 @@ macro_rules! gen_build_tests {
}
gen_build_tests! {
init_csv_with_children,
attributes,
client_in_folder,
client_init,