Add RunContext support for script outputs (#765)

Resolves #667

This PR:

- Introduces a new field in the project file: `scriptType` which has the
default value of `Class` (in parity with previous versions), but can
also be `RunContext`.
- This is then passed to `InstanceContext` from the `Project` struct.
- This then changes the RunContext in the lua `snapshot_middleware`

---------

Co-authored-by: Micah <dekkonot@rocketmail.com>
This commit is contained in:
Sasial
2023-09-24 06:28:09 +10:00
committed by GitHub
parent 539cd0d418
commit bb8dd1402d
56 changed files with 602 additions and 95 deletions

View File

@@ -1,8 +1,8 @@
use std::{path::Path, str};
use std::{collections::HashMap, path::Path, str};
use anyhow::Context;
use maplit::hashmap;
use memofs::{IoResultExt, Vfs};
use rbx_dom_weak::types::Enum;
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
@@ -12,6 +12,13 @@ use super::{
util::match_trailing,
};
#[derive(Debug)]
enum ScriptType {
Server,
Client,
Module,
}
/// Core routine for turning Lua files into snapshots.
pub fn snapshot_lua(
context: &InstanceContext,
@@ -20,36 +27,58 @@ pub fn snapshot_lua(
) -> anyhow::Result<Option<InstanceSnapshot>> {
let file_name = path.file_name().unwrap().to_string_lossy();
let (class_name, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua")
let run_context_enums = &rbx_reflection_database::get()
.enums
.get("RunContext")
.expect("Unable to get RunContext enums!")
.items;
let (script_type, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua")
{
("Script", name)
(ScriptType::Server, name)
} else if let Some(name) = match_trailing(&file_name, ".client.lua") {
("LocalScript", name)
(ScriptType::Client, name)
} else if let Some(name) = match_trailing(&file_name, ".lua") {
("ModuleScript", name)
(ScriptType::Module, name)
} else if let Some(name) = match_trailing(&file_name, ".server.luau") {
("Script", name)
(ScriptType::Server, name)
} else if let Some(name) = match_trailing(&file_name, ".client.luau") {
("LocalScript", name)
(ScriptType::Client, name)
} else if let Some(name) = match_trailing(&file_name, ".luau") {
("ModuleScript", name)
(ScriptType::Module, name)
} else {
return Ok(None);
};
let (class_name, run_context) = match (context.emit_legacy_scripts, script_type) {
(false, ScriptType::Server) => ("Script", run_context_enums.get("Server")),
(false, ScriptType::Client) => ("Script", run_context_enums.get("Client")),
(true, ScriptType::Server) => ("Script", run_context_enums.get("Legacy")),
(true, ScriptType::Client) => ("LocalScript", None),
(_, ScriptType::Module) => ("ModuleScript", None),
};
let contents = vfs.read(path)?;
let contents_str = str::from_utf8(&contents)
.with_context(|| format!("File was not valid UTF-8: {}", path.display()))?
.to_owned();
let mut properties = HashMap::with_capacity(2);
properties.insert("Source".to_owned(), contents_str.into());
if let Some(run_context) = run_context {
properties.insert(
"RunContext".to_owned(),
Enum::from_u32(run_context.to_owned()).into(),
);
}
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name(class_name)
.properties(hashmap! {
"Source".to_owned() => contents_str.into(),
})
.properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
@@ -107,26 +136,53 @@ pub fn snapshot_lua_init(
mod test {
use super::*;
use maplit::hashmap;
use memofs::{InMemoryFs, VfsSnapshot};
#[test]
fn module_from_vfs() {
fn class_module_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot =
snapshot_lua(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua"))
.unwrap()
.unwrap();
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.lua"),
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn server_from_vfs() {
fn runcontext_module_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.lua"),
)
.unwrap()
.unwrap();
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn class_server_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
@@ -134,18 +190,41 @@ mod test {
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::default(),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.server.lua"),
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn client_from_vfs() {
fn runcontext_server_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.server.lua"),
)
.unwrap()
.unwrap();
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn class_client_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.client.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
@@ -153,14 +232,37 @@ mod test {
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::default(),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.client.lua"),
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn runcontext_client_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.client.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.client.lua"),
)
.unwrap()
.unwrap();
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[ignore = "init.lua functionality has moved to the root snapshot function"]
@@ -177,16 +279,21 @@ mod test {
let mut vfs = Vfs::new(imfs);
let instance_snapshot =
snapshot_lua(&InstanceContext::default(), &mut vfs, Path::new("/root"))
.unwrap()
.unwrap();
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/root"),
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn module_with_meta() {
fn class_module_with_meta() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
@@ -204,16 +311,53 @@ mod test {
let mut vfs = Vfs::new(imfs);
let instance_snapshot =
snapshot_lua(&InstanceContext::default(), &mut vfs, Path::new("/foo.lua"))
.unwrap()
.unwrap();
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.lua"),
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn script_with_meta() {
fn runcontext_module_with_meta() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
imfs.load_snapshot(
"/foo.meta.json",
VfsSnapshot::file(
r#"
{
"ignoreUnknownInstances": true
}
"#,
),
)
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.lua"),
)
.unwrap()
.unwrap();
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn class_script_with_meta() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
@@ -232,18 +376,52 @@ mod test {
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::default(),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/foo.server.lua"),
)
.unwrap()
.unwrap();
insta::assert_yaml_snapshot!(instance_snapshot);
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn script_disabled() {
fn runcontext_script_with_meta() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/foo.server.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
imfs.load_snapshot(
"/foo.meta.json",
VfsSnapshot::file(
r#"
{
"ignoreUnknownInstances": true
}
"#,
),
)
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/foo.server.lua"),
)
.unwrap()
.unwrap();
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn class_script_disabled() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/bar.server.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
@@ -264,7 +442,41 @@ mod test {
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::default(),
&InstanceContext::with_emit_legacy_scripts(Some(true)),
&mut vfs,
Path::new("/bar.server.lua"),
)
.unwrap()
.unwrap();
insta::with_settings!({ sort_maps => true }, {
insta::assert_yaml_snapshot!(instance_snapshot);
});
}
#[test]
fn runcontext_script_disabled() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot("/bar.server.lua", VfsSnapshot::file("Hello there!"))
.unwrap();
imfs.load_snapshot(
"/bar.meta.json",
VfsSnapshot::file(
r#"
{
"properties": {
"Disabled": true
}
}
"#,
),
)
.unwrap();
let mut vfs = Vfs::new(imfs);
let instance_snapshot = snapshot_lua(
&InstanceContext::with_emit_legacy_scripts(Some(false)),
&mut vfs,
Path::new("/bar.server.lua"),
)

View File

@@ -38,7 +38,7 @@ use self::{
util::PathExt,
};
pub use self::project::snapshot_project_node;
pub use self::{project::snapshot_project_node, util::emit_legacy_scripts_default};
/// The main entrypoint to the snapshot function. This function can be pointed
/// at any path and will return something if Rojo knows how to deal with it.

View File

@@ -12,7 +12,7 @@ use crate::{
},
};
use super::snapshot_from_vfs;
use super::{emit_legacy_scripts_default, snapshot_from_vfs};
pub fn snapshot_project(
context: &InstanceContext,
@@ -30,6 +30,12 @@ pub fn snapshot_project(
});
context.add_path_ignore_rules(rules);
context.set_emit_legacy_scripts(
project
.emit_legacy_scripts
.or_else(emit_legacy_scripts_default)
.unwrap(),
);
match snapshot_project_node(&context, path, &project.name, &project.tree, vfs, None)? {
Some(found_snapshot) => {
@@ -77,7 +83,7 @@ pub fn snapshot_project_node(
let name = Cow::Owned(instance_name.to_owned());
let mut properties = HashMap::new();
let mut children = Vec::new();
let mut metadata = InstanceMetadata::default();
let mut metadata = InstanceMetadata::new().context(context);
if let Some(path_node) = &node.path {
let path = path_node.path();

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.csv
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: LocalizationTable
properties:

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.csv
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: LocalizationTable
properties:

View File

@@ -17,7 +17,8 @@ metadata:
- /foo/init.client.lua
- /foo/init.client.luau
- /foo/init.csv
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: Folder
properties: {}

View File

@@ -17,7 +17,8 @@ metadata:
- /foo/init.client.lua
- /foo/init.client.luau
- /foo/init.csv
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: Folder
properties: {}
@@ -37,7 +38,8 @@ children:
- /foo/Child/init.client.lua
- /foo/Child/init.client.luau
- /foo/Child/init.csv
context: {}
context:
emit_legacy_scripts: true
name: Child
class_name: Folder
properties: {}

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.json
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: ModuleScript
properties:

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo.model.json
relevant_paths:
- /foo.model.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: IntValue
properties:
@@ -20,7 +21,8 @@ children:
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
name: The Child
class_name: StringValue
properties: {}

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo.model.json
relevant_paths:
- /foo.model.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: IntValue
properties:
@@ -20,7 +21,8 @@ children:
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
name: The Child
class_name: StringValue
properties: {}

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.client.lua
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: LocalScript
properties:

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.lua
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: ModuleScript
properties:

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.lua
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: ModuleScript
properties:

View File

@@ -10,12 +10,15 @@ metadata:
relevant_paths:
- /bar.server.lua
- /bar.meta.json
context: {}
context:
emit_legacy_scripts: true
name: bar
class_name: Script
properties:
Disabled:
Bool: true
RunContext:
Enum: 0
Source:
String: Hello there!
children: []

View File

@@ -10,10 +10,13 @@ metadata:
relevant_paths:
- /foo.server.lua
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: Script
properties:
RunContext:
Enum: 0
Source:
String: Hello there!
children: []

View File

@@ -10,10 +10,13 @@ metadata:
relevant_paths:
- /foo.server.lua
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: Script
properties:
RunContext:
Enum: 0
Source:
String: Hello there!
children: []

View File

@@ -13,7 +13,8 @@ metadata:
- /root/init.lua
- /root/init.server.lua
- /root/init.client.lua
context: {}
context:
script_type: Class
name: root
class_name: ModuleScript
properties:

View File

@@ -0,0 +1,23 @@
---
source: src/snapshot_middleware/lua.rs
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
metadata:
ignore_unknown_instances: false
instigating_source:
Path: /foo.client.lua
relevant_paths:
- /foo.client.lua
- /foo.meta.json
context:
emit_legacy_scripts: false
name: foo
class_name: Script
properties:
RunContext:
Enum: 2
Source:
String: Hello there!
children: []

View File

@@ -0,0 +1,21 @@
---
source: src/snapshot_middleware/lua.rs
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
metadata:
ignore_unknown_instances: false
instigating_source:
Path: /foo.lua
relevant_paths:
- /foo.lua
- /foo.meta.json
context:
emit_legacy_scripts: false
name: foo
class_name: ModuleScript
properties:
Source:
String: Hello there!
children: []

View File

@@ -0,0 +1,21 @@
---
source: src/snapshot_middleware/lua.rs
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
metadata:
ignore_unknown_instances: true
instigating_source:
Path: /foo.lua
relevant_paths:
- /foo.lua
- /foo.meta.json
context:
emit_legacy_scripts: false
name: foo
class_name: ModuleScript
properties:
Source:
String: Hello there!
children: []

View File

@@ -0,0 +1,25 @@
---
source: src/snapshot_middleware/lua.rs
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
metadata:
ignore_unknown_instances: false
instigating_source:
Path: /bar.server.lua
relevant_paths:
- /bar.server.lua
- /bar.meta.json
context:
emit_legacy_scripts: false
name: bar
class_name: Script
properties:
Disabled:
Bool: true
RunContext:
Enum: 1
Source:
String: Hello there!
children: []

View File

@@ -0,0 +1,23 @@
---
source: src/snapshot_middleware/lua.rs
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
metadata:
ignore_unknown_instances: true
instigating_source:
Path: /foo.server.lua
relevant_paths:
- /foo.server.lua
- /foo.meta.json
context:
emit_legacy_scripts: false
name: foo
class_name: Script
properties:
RunContext:
Enum: 1
Source:
String: Hello there!
children: []

View File

@@ -0,0 +1,23 @@
---
source: src/snapshot_middleware/lua.rs
expression: instance_snapshot
---
snapshot_id: "00000000000000000000000000000000"
metadata:
ignore_unknown_instances: false
instigating_source:
Path: /foo.server.lua
relevant_paths:
- /foo.server.lua
- /foo.meta.json
context:
emit_legacy_scripts: false
name: foo
class_name: Script
properties:
RunContext:
Enum: 1
Source:
String: Hello there!
children: []

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo/hello.project.json
relevant_paths:
- /foo/hello.project.json
context: {}
context:
emit_legacy_scripts: true
name: direct-project
class_name: Model
properties: {}

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo/default.project.json
relevant_paths:
- /foo/default.project.json
context: {}
context:
script_type: Class
name: indirect-project
class_name: Folder
properties: {}

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo/other.project.json
- /foo/default.project.json
context: {}
context:
emit_legacy_scripts: true
name: path-property-override
class_name: StringValue
properties:

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo.project.json
relevant_paths:
- /foo.project.json
context: {}
context:
emit_legacy_scripts: true
name: children
class_name: Folder
properties: {}
@@ -24,7 +25,8 @@ children:
- $className: Model
- Folder
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
name: Child
class_name: Model
properties: {}

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo/other.project.json
- /foo/default.project.json
context: {}
context:
emit_legacy_scripts: true
name: path-project
class_name: Model
properties: {}

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo/other.project.json
- /foo/default.project.json
context: {}
context:
emit_legacy_scripts: true
name: path-child-project
class_name: Folder
properties: {}
@@ -25,7 +26,8 @@ children:
- $className: Model
- Folder
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
name: SomeChild
class_name: Model
properties: {}

View File

@@ -11,7 +11,8 @@ metadata:
- /foo/other.txt
- /foo/other.meta.json
- /foo/default.project.json
context: {}
context:
emit_legacy_scripts: true
name: path-project
class_name: StringValue
properties:

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo.project.json
relevant_paths:
- /foo.project.json
context: {}
context:
emit_legacy_scripts: true
name: resolved-properties
class_name: StringValue
properties:

View File

@@ -9,7 +9,8 @@ metadata:
Path: /foo.project.json
relevant_paths:
- /foo.project.json
context: {}
context:
emit_legacy_scripts: true
name: unresolved-properties
class_name: StringValue
properties:

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.toml
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: ModuleScript
properties:

View File

@@ -10,7 +10,8 @@ metadata:
relevant_paths:
- /foo.txt
- /foo.meta.json
context: {}
context:
emit_legacy_scripts: true
name: foo
class_name: StringValue
properties:

View File

@@ -41,3 +41,8 @@ where
.with_context(|| format!("Path did not end in {}: {}", suffix, path.display()))
}
}
// TEMP function until rojo 8.0, when it can be replaced with bool::default (aka false)
pub fn emit_legacy_scripts_default() -> Option<bool> {
Some(true)
}