Skip to content

Commit

Permalink
Add RunContext support for script outputs (#765)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sasial-dev and Dekkonot authored Sep 23, 2023
1 parent 539cd0d commit bb8dd14
Show file tree
Hide file tree
Showing 56 changed files with 602 additions and 95 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
* Added support for `Terrain.MaterialColors` ([#770])
* Allow `Terrain` to be specified without a classname ([#771])
* Add Confirmation Behavior setting ([#774])
* Added the `emitLegacyScripts` field to the project format ([#765]). The behavior is outlined below:

| `emitLegacyScripts` Value | Action Taken by Rojo |
|----------------------------|--------------------------------------------------------------------------------------------------------------------|
| false | Rojo emits Scripts with the appropriate `RunContext` for `*.client.lua` and `*.server.lua` files in the project. |
| true (default) | Rojo emits LocalScripts and Scripts with legacy `RunContext` (same behavior as previously). |

[#761]: https://github.com/rojo-rbx/rojo/pull/761
[#745]: https://github.com/rojo-rbx/rojo/pull/745
Expand All @@ -53,6 +59,7 @@
[#748]: https://github.com/rojo-rbx/rojo/pull/748
[#755]: https://github.com/rojo-rbx/rojo/pull/755
[#760]: https://github.com/rojo-rbx/rojo/pull/760
[#765]: https://github.com/rojo-rbx/rojo/pull/765
[#770]: https://github.com/rojo-rbx/rojo/pull/770
[#771]: https://github.com/rojo-rbx/rojo/pull/771
[#774]: https://github.com/rojo-rbx/rojo/pull/774
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: tests/tests/build.rs
expression: contents
---
<roblox version="4">
<Item class="Folder" referent="0">
<Properties>
<string name="Name">nested_runcontext</string>
</Properties>
<Item class="Folder" referent="1">
<Properties>
<string name="Name">folder1</string>
</Properties>
<Item class="Script" referent="2">
<Properties>
<string name="Name">test</string>
<token name="RunContext">1</token>
<string name="Source"></string>
</Properties>
</Item>
</Item>
<Item class="Folder" referent="3">
<Properties>
<string name="Name">folder2</string>
</Properties>
<Item class="Script" referent="4">
<Properties>
<string name="Name">test</string>
<token name="RunContext">0</token>
<string name="Source"></string>
</Properties>
</Item>
</Item>
</Item>
</roblox>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ expression: contents
<Properties>
<string name="Name">hello</string>
<bool name="Disabled">true</bool>
<token name="RunContext">0</token>
<string name="Source">-- This script should be marked 'Disabled'</string>
</Properties>
</Item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ expression: contents
<Item class="Script" referent="1">
<Properties>
<string name="Name">serverScript</string>
<token name="RunContext">0</token>
<string name="Source">-- This is a Lua server script</string>
</Properties>
</Item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expression: contents
<Item class="Script" referent="0">
<Properties>
<string name="Name">server_init</string>
<token name="RunContext">0</token>
<string name="Source">return "From folder/init.server.lua"</string>
</Properties>
</Item>
Expand Down
13 changes: 13 additions & 0 deletions rojo-test/build-tests/nested_runcontext/default.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "nested_runcontext",
"emitLegacyScripts": false,
"tree": {
"$className": "Folder",
"folder1": {
"$path": "folder1"
},
"folder2": {
"$path": "folder2"
}
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "nested_runcontext",
"emitLegacyScripts": true,
"tree": {
"$path": "folder3"
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ instances:
Name: bar
Parent: id-2
Properties:
RunContext:
Enum: 0
Source:
String: "-- Hello, from bar!"
id-4:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ instances:
Name: bar
Parent: id-2
Properties:
RunContext:
Enum: 0
Source:
String: "-- Hello, from bar!"
id-4:
Expand Down
12 changes: 11 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use std::{
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{glob::Glob, resolution::UnresolvedValue};
use crate::{
glob::Glob, resolution::UnresolvedValue, snapshot_middleware::emit_legacy_scripts_default,
};

static PROJECT_FILENAME: &str = "default.project.json";

Expand Down Expand Up @@ -73,6 +75,14 @@ pub struct Project {
#[serde(skip_serializing_if = "Option::is_none")]
pub serve_address: Option<IpAddr>,

/// Determines if rojo should emit scripts with the appropriate `RunContext` for `*.client.lua` and `*.server.lua` files in the project.
/// Or, if rojo should keep the legacy behavior of emitting LocalScripts and Scripts with legacy Runcontext
#[serde(
default = "emit_legacy_scripts_default",
skip_serializing_if = "Option::is_none"
)]
pub emit_legacy_scripts: Option<bool>,

/// A list of globs, relative to the folder the project file is in, that
/// match files that should be excluded if Rojo encounters them.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand Down
3 changes: 2 additions & 1 deletion src/serve_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ impl ServeSession {

let root_id = tree.get_root_id();

let instance_context = InstanceContext::default();
let instance_context =
InstanceContext::with_emit_legacy_scripts(root_project.emit_legacy_scripts);

log::trace!("Generating snapshot of instances from VFS");
let snapshot = snapshot_from_vfs(&instance_context, &vfs, start_path)?;
Expand Down
30 changes: 26 additions & 4 deletions src/snapshot/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{

use serde::{Deserialize, Serialize};

use crate::{glob::Glob, path_serializer, project::ProjectNode};
use crate::{
glob::Glob, path_serializer, project::ProjectNode,
snapshot_middleware::emit_legacy_scripts_default,
};

/// Rojo-specific metadata that can be associated with an instance or a snapshot
/// of an instance.
Expand Down Expand Up @@ -103,9 +106,26 @@ impl Default for InstanceMetadata {
pub struct InstanceContext {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub path_ignore_rules: Arc<Vec<PathIgnoreRule>>,
pub emit_legacy_scripts: bool,
}

impl InstanceContext {
pub fn new() -> Self {
Self {
path_ignore_rules: Arc::new(Vec::new()),
emit_legacy_scripts: emit_legacy_scripts_default().unwrap(),
}
}

pub fn with_emit_legacy_scripts(emit_legacy_scripts: Option<bool>) -> Self {
Self {
emit_legacy_scripts: emit_legacy_scripts
.or_else(emit_legacy_scripts_default)
.unwrap(),
..Self::new()
}
}

/// Extend the list of ignore rules in the context with the given new rules.
pub fn add_path_ignore_rules<I>(&mut self, new_rules: I)
where
Expand All @@ -123,13 +143,15 @@ impl InstanceContext {
let rules = Arc::make_mut(&mut self.path_ignore_rules);
rules.extend(new_rules);
}

pub fn set_emit_legacy_scripts(&mut self, emit_legacy_scripts: bool) {
self.emit_legacy_scripts = emit_legacy_scripts;
}
}

impl Default for InstanceContext {
fn default() -> Self {
InstanceContext {
path_ignore_rules: Arc::new(Vec::new()),
}
Self::new()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: src/snapshot/tests/apply.rs
expression: tree_view

---
id: id-1
name: ROOT
Expand All @@ -12,6 +11,7 @@ properties:
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
children: []

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ properties: {}
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
children: []

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: src/snapshot/tests/apply.rs
expression: tree_view

---
id: id-1
name: ROOT
Expand All @@ -12,6 +11,7 @@ properties:
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
children: []

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ properties: {}
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
children: []

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ added_instances:
metadata:
ignore_unknown_instances: false
relevant_paths: []
context: {}
context:
emit_legacy_scripts: true
name: New
class_name: Folder
properties: {}
Expand Down
Loading

0 comments on commit bb8dd14

Please sign in to comment.