Support setting referent properties via attributes (#843)

Co-authored-by: Kenneth Loeffler <kenloef@gmail.com>
This commit is contained in:
Micah
2024-06-20 15:48:52 -07:00
committed by GitHub
parent a7b45ee859
commit 7e2bab921a
64 changed files with 942 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ use serde::Deserialize;
use crate::{
resolution::UnresolvedValue,
snapshot::{InstanceContext, InstanceSnapshot},
RojoRef,
};
pub fn snapshot_json_model(
@@ -41,6 +42,8 @@ pub fn snapshot_json_model(
instance.name = Some(name.to_owned());
let id = instance.id.take().map(RojoRef::new);
let mut snapshot = instance
.into_snapshot()
.with_context(|| format!("Could not load JSON model: {}", path.display()))?;
@@ -49,7 +52,8 @@ pub fn snapshot_json_model(
.metadata
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf()])
.context(context);
.context(context)
.specified_id(id);
Ok(Some(snapshot))
}
@@ -63,6 +67,9 @@ struct JsonModel {
#[serde(alias = "ClassName")]
class_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
#[serde(
alias = "Children",
default = "Vec::new",

View File

@@ -4,7 +4,7 @@ use anyhow::{format_err, Context};
use rbx_dom_weak::types::Attributes;
use serde::{Deserialize, Serialize};
use crate::{resolution::UnresolvedValue, snapshot::InstanceSnapshot};
use crate::{resolution::UnresolvedValue, snapshot::InstanceSnapshot, RojoRef};
/// Represents metadata in a sibling file with the same basename.
///
@@ -13,6 +13,9 @@ use crate::{resolution::UnresolvedValue, snapshot::InstanceSnapshot};
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AdjacentMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ignore_unknown_instances: Option<bool>,
@@ -72,9 +75,21 @@ impl AdjacentMetadata {
Ok(())
}
fn apply_id(&mut self, snapshot: &mut InstanceSnapshot) -> anyhow::Result<()> {
if self.id.is_some() && snapshot.metadata.specified_id.is_some() {
anyhow::bail!(
"cannot specify an ID using {} (instance has an ID from somewhere else)",
self.path.display()
);
}
snapshot.metadata.specified_id = self.id.take().map(RojoRef::new);
Ok(())
}
pub fn apply_all(&mut self, snapshot: &mut InstanceSnapshot) -> anyhow::Result<()> {
self.apply_ignore_unknown_instances(snapshot);
self.apply_properties(snapshot)?;
self.apply_id(snapshot)?;
Ok(())
}
@@ -89,6 +104,9 @@ impl AdjacentMetadata {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DirectoryMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ignore_unknown_instances: Option<bool>,
@@ -122,6 +140,7 @@ impl DirectoryMetadata {
self.apply_ignore_unknown_instances(snapshot);
self.apply_class_name(snapshot)?;
self.apply_properties(snapshot)?;
self.apply_id(snapshot)?;
Ok(())
}
@@ -174,4 +193,15 @@ impl DirectoryMetadata {
Ok(())
}
fn apply_id(&mut self, snapshot: &mut InstanceSnapshot) -> anyhow::Result<()> {
if self.id.is_some() && snapshot.metadata.specified_id.is_some() {
anyhow::bail!(
"cannot specify an ID using {} (instance has an ID from somewhere else)",
self.path.display()
);
}
snapshot.metadata.specified_id = self.id.take().map(RojoRef::new);
Ok(())
}
}

View File

@@ -11,6 +11,7 @@ use crate::{
InstanceContext, InstanceMetadata, InstanceSnapshot, InstigatingSource, PathIgnoreRule,
SyncRule,
},
RojoRef,
};
use super::{emit_legacy_scripts_default, snapshot_from_vfs};
@@ -279,6 +280,10 @@ pub fn snapshot_project_node(
metadata.ignore_unknown_instances = true;
}
if let Some(id) = &node.id {
metadata.specified_id = Some(RojoRef::new(id.clone()))
}
metadata.instigating_source = Some(InstigatingSource::ProjectNode(
project_path.to_path_buf(),
instance_name.to_string(),

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: LocalizationTable
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: LocalizationTable
properties:

View File

@@ -19,6 +19,7 @@ metadata:
- /foo/init.csv
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: Folder
properties: {}

View File

@@ -19,6 +19,7 @@ metadata:
- /foo/init.csv
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: Folder
properties: {}
@@ -40,6 +41,7 @@ children:
- /foo/Child/init.csv
context:
emit_legacy_scripts: true
specified_id: ~
name: Child
class_name: Folder
properties: {}

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: ModuleScript
properties:

View File

@@ -11,6 +11,7 @@ metadata:
- /foo.model.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: IntValue
properties:
@@ -23,6 +24,7 @@ children:
relevant_paths: []
context:
emit_legacy_scripts: true
specified_id: ~
name: The Child
class_name: StringValue
properties: {}

View File

@@ -11,6 +11,7 @@ metadata:
- /foo.model.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: IntValue
properties:
@@ -23,6 +24,7 @@ children:
relevant_paths: []
context:
emit_legacy_scripts: true
specified_id: ~
name: The Child
class_name: StringValue
properties: {}

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: LocalScript
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: ModuleScript
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: ModuleScript
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /bar.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: bar
class_name: Script
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: Script
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: Script
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: false
specified_id: ~
name: foo
class_name: Script
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: false
specified_id: ~
name: foo
class_name: ModuleScript
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: false
specified_id: ~
name: foo
class_name: ModuleScript
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /bar.meta.json
context:
emit_legacy_scripts: false
specified_id: ~
name: bar
class_name: Script
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: false
specified_id: ~
name: foo
class_name: Script
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: false
specified_id: ~
name: foo
class_name: Script
properties:

View File

@@ -1,6 +1,6 @@
---
source: src/snapshot_middleware/project.rs
assertion_line: 725
assertion_line: 730
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
@@ -12,8 +12,8 @@ metadata:
- /foo/default.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: no_name_project
class_name: Model
properties: {}
children: []

View File

@@ -11,6 +11,7 @@ metadata:
- /foo/hello.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: direct-project
class_name: Model
properties: {}

View File

@@ -12,6 +12,7 @@ metadata:
- /foo/default.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: path-property-override
class_name: StringValue
properties:

View File

@@ -11,6 +11,7 @@ metadata:
- /foo.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: children
class_name: Folder
properties: {}
@@ -27,6 +28,7 @@ children:
relevant_paths: []
context:
emit_legacy_scripts: true
specified_id: ~
name: Child
class_name: Model
properties: {}

View File

@@ -12,6 +12,7 @@ metadata:
- /foo/default.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: path-project
class_name: Model
properties: {}

View File

@@ -12,6 +12,7 @@ metadata:
- /foo/default.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: path-child-project
class_name: Folder
properties: {}
@@ -28,6 +29,7 @@ children:
relevant_paths: []
context:
emit_legacy_scripts: true
specified_id: ~
name: SomeChild
class_name: Model
properties: {}

View File

@@ -13,6 +13,7 @@ metadata:
- /foo/default.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: path-project
class_name: StringValue
properties:

View File

@@ -11,6 +11,7 @@ metadata:
- /foo.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: resolved-properties
class_name: StringValue
properties:

View File

@@ -11,6 +11,7 @@ metadata:
- /foo.project.json
context:
emit_legacy_scripts: true
specified_id: ~
name: unresolved-properties
class_name: StringValue
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: ModuleScript
properties:

View File

@@ -12,6 +12,7 @@ metadata:
- /foo.meta.json
context:
emit_legacy_scripts: true
specified_id: ~
name: foo
class_name: StringValue
properties: