mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-25 07:06:12 +00:00
init.meta.json support (#183)
* A minimum viable product for init.meta.json * Properties support * Add ignoreUnknownChildren support * Apply requested changes * Use reflection guiding * Add a script to the test * Change to ignoreUnknownInstances * Apply requested changes
This commit is contained in:
committed by
Lucien Greathouse
parent
983d44947e
commit
0ed6c57c7f
@@ -39,6 +39,7 @@ use crate::{
|
|||||||
const INIT_MODULE_NAME: &str = "init.lua";
|
const INIT_MODULE_NAME: &str = "init.lua";
|
||||||
const INIT_SERVER_NAME: &str = "init.server.lua";
|
const INIT_SERVER_NAME: &str = "init.server.lua";
|
||||||
const INIT_CLIENT_NAME: &str = "init.client.lua";
|
const INIT_CLIENT_NAME: &str = "init.client.lua";
|
||||||
|
const INIT_META_NAME: &str = "init.meta.json";
|
||||||
|
|
||||||
pub struct SnapshotContext {
|
pub struct SnapshotContext {
|
||||||
pub plugin_context: Option<SnapshotPluginContext>,
|
pub plugin_context: Option<SnapshotPluginContext>,
|
||||||
@@ -105,6 +106,12 @@ pub enum SnapshotError {
|
|||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
InitMetaError {
|
||||||
|
#[fail(cause)]
|
||||||
|
inner: serde_json::Error,
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
|
|
||||||
XmlModelDecodeError {
|
XmlModelDecodeError {
|
||||||
#[fail(cause)]
|
#[fail(cause)]
|
||||||
inner: rbx_xml::DecodeError,
|
inner: rbx_xml::DecodeError,
|
||||||
@@ -152,6 +159,9 @@ impl fmt::Display for SnapshotError {
|
|||||||
SnapshotError::JsonModelDecodeError { inner, path } => {
|
SnapshotError::JsonModelDecodeError { inner, path } => {
|
||||||
write!(output, "Malformed .model.json model: {} in path {}", inner, path.display())
|
write!(output, "Malformed .model.json model: {} in path {}", inner, path.display())
|
||||||
},
|
},
|
||||||
|
SnapshotError::InitMetaError { inner, path } => {
|
||||||
|
write!(output, "Malformed init.meta.json: {} in path {}", inner, path.display())
|
||||||
|
},
|
||||||
SnapshotError::XmlModelDecodeError { inner, path } => {
|
SnapshotError::XmlModelDecodeError { inner, path } => {
|
||||||
write!(output, "Malformed rbxmx model: {} in path {}", inner, path.display())
|
write!(output, "Malformed rbxmx model: {} in path {}", inner, path.display())
|
||||||
},
|
},
|
||||||
@@ -299,6 +309,7 @@ fn snapshot_imfs_directory<'source>(
|
|||||||
let init_path = directory.path.join(INIT_MODULE_NAME);
|
let init_path = directory.path.join(INIT_MODULE_NAME);
|
||||||
let init_server_path = directory.path.join(INIT_SERVER_NAME);
|
let init_server_path = directory.path.join(INIT_SERVER_NAME);
|
||||||
let init_client_path = directory.path.join(INIT_CLIENT_NAME);
|
let init_client_path = directory.path.join(INIT_CLIENT_NAME);
|
||||||
|
let init_meta_path = directory.path.join(INIT_META_NAME);
|
||||||
|
|
||||||
let snapshot_name = instance_name
|
let snapshot_name = instance_name
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
@@ -327,6 +338,27 @@ fn snapshot_imfs_directory<'source>(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Some(ImfsItem::File(file)) = imfs.get(&init_meta_path) {
|
||||||
|
let meta: InitMeta = serde_json::from_slice(&file.contents)
|
||||||
|
.map_err(|inner| SnapshotError::InitMetaError {
|
||||||
|
inner,
|
||||||
|
path: file.path.to_path_buf(),
|
||||||
|
})?;
|
||||||
|
|
||||||
|
if let Some(meta_class) = meta.class_name {
|
||||||
|
snapshot.class_name = Cow::Owned(meta_class);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(meta_ignore_instances) = meta.ignore_unknown_instances {
|
||||||
|
snapshot.metadata.ignore_unknown_instances = meta_ignore_instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (key, value) in meta.properties {
|
||||||
|
let resolved_value = try_resolve_value(&snapshot.class_name, &key, &value)?;
|
||||||
|
snapshot.properties.insert(key, resolved_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
snapshot.metadata.source_path = Some(directory.path.to_owned());
|
snapshot.metadata.source_path = Some(directory.path.to_owned());
|
||||||
|
|
||||||
for child_path in &directory.children {
|
for child_path in &directory.children {
|
||||||
@@ -351,6 +383,16 @@ fn snapshot_imfs_directory<'source>(
|
|||||||
Ok(Some(snapshot))
|
Ok(Some(snapshot))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct InitMeta {
|
||||||
|
class_name: Option<String>,
|
||||||
|
ignore_unknown_instances: Option<bool>,
|
||||||
|
|
||||||
|
#[serde(default = "HashMap::new")]
|
||||||
|
properties: HashMap<String, UnresolvedRbxValue>,
|
||||||
|
}
|
||||||
|
|
||||||
fn snapshot_imfs_file<'source>(
|
fn snapshot_imfs_file<'source>(
|
||||||
context: &SnapshotContext,
|
context: &SnapshotContext,
|
||||||
file: &'source ImfsFile,
|
file: &'source ImfsFile,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ generate_snapshot_tests!(
|
|||||||
empty,
|
empty,
|
||||||
json_model,
|
json_model,
|
||||||
localization,
|
localization,
|
||||||
|
meta_files,
|
||||||
multi_partition_game,
|
multi_partition_game,
|
||||||
nested_partitions,
|
nested_partitions,
|
||||||
single_partition_game,
|
single_partition_game,
|
||||||
|
|||||||
6
test-projects/meta_files/default.project.json
Normal file
6
test-projects/meta_files/default.project.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "test-model",
|
||||||
|
"tree": {
|
||||||
|
"$path": "src"
|
||||||
|
}
|
||||||
|
}
|
||||||
57
test-projects/meta_files/expected-snapshot.json
Normal file
57
test-projects/meta_files/expected-snapshot.json
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
{
|
||||||
|
"name": "test-model",
|
||||||
|
"class_name": "Tool",
|
||||||
|
"properties": {
|
||||||
|
"Enabled": {
|
||||||
|
"Type": "Bool",
|
||||||
|
"Value": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "A",
|
||||||
|
"class_name": "Folder",
|
||||||
|
"properties": {},
|
||||||
|
"children": [],
|
||||||
|
"metadata": {
|
||||||
|
"ignore_unknown_instances": true,
|
||||||
|
"source_path": "src/A",
|
||||||
|
"project_definition": null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Script",
|
||||||
|
"class_name": "Script",
|
||||||
|
"properties": {
|
||||||
|
"Disabled": {
|
||||||
|
"Type": "Bool",
|
||||||
|
"Value": true
|
||||||
|
},
|
||||||
|
"Source": {
|
||||||
|
"Type": "String",
|
||||||
|
"Value": "print(\"Hello, world\")"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"children": [],
|
||||||
|
"metadata": {
|
||||||
|
"ignore_unknown_instances": false,
|
||||||
|
"source_path": "src/Script",
|
||||||
|
"project_definition": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"ignore_unknown_instances": false,
|
||||||
|
"source_path": "src",
|
||||||
|
"project_definition": [
|
||||||
|
"test-model",
|
||||||
|
{
|
||||||
|
"class_name": null,
|
||||||
|
"children": {},
|
||||||
|
"properties": {},
|
||||||
|
"ignore_unknown_instances": null,
|
||||||
|
"path": "src"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
3
test-projects/meta_files/src/A/init.meta.json
Normal file
3
test-projects/meta_files/src/A/init.meta.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"ignoreUnknownInstances": true
|
||||||
|
}
|
||||||
5
test-projects/meta_files/src/Script/init.meta.json
Normal file
5
test-projects/meta_files/src/Script/init.meta.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"Disabled": true
|
||||||
|
}
|
||||||
|
}
|
||||||
1
test-projects/meta_files/src/Script/init.server.lua
Normal file
1
test-projects/meta_files/src/Script/init.server.lua
Normal file
@@ -0,0 +1 @@
|
|||||||
|
print("Hello, world")
|
||||||
6
test-projects/meta_files/src/init.meta.json
Normal file
6
test-projects/meta_files/src/init.meta.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"className": "Tool",
|
||||||
|
"properties": {
|
||||||
|
"Enabled": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user