Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RunContext support for script outputs #765

Merged
merged 24 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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). |
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

[#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
13 changes: 12 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,15 @@ pub struct Project {
#[serde(skip_serializing_if = "Option::is_none")]
pub serve_address: Option<IpAddr>,

/// The mode to use when mapping scripts into Roblox.
/// Can be either `Class` or `RunContext` and determines whether script
Dekkonot marked this conversation as resolved.
Show resolved Hide resolved
/// behavior is set using the `RunContext` property or the script's `ClassName`.
#[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
2 changes: 1 addition & 1 deletion src/serve_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl ServeSession {

let root_id = tree.get_root_id();

let instance_context = InstanceContext::default();
let instance_context = InstanceContext::from(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
42 changes: 38 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,17 @@ 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(),
}
}

/// 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 +134,36 @@ 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
}
}

// serve_session always passes an option from the config file, but tests want it to be explict
#[cfg(test)]
impl From<bool> for InstanceContext {
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
fn from(emit_legacy_scripts: bool) -> Self {
Self {
emit_legacy_scripts,
..Self::new()
}
}
}

impl From<Option<bool>> for InstanceContext {
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
fn from(emit_legacy_scripts: Option<bool>) -> Self {
Self {
emit_legacy_scripts: emit_legacy_scripts
.unwrap_or_else(|| emit_legacy_scripts_default().unwrap()),
..Self::new()
}
}
}

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