mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-20 12:45:05 +00:00
Add a new syncRules field project files to allow users to specify middleware to use for files (#813)
This commit is contained in:
49
CHANGELOG.md
49
CHANGELOG.md
@@ -2,6 +2,55 @@
|
||||
|
||||
## Unreleased Changes
|
||||
|
||||
* Projects may now specify rules for syncing files as if they had a different file extension. ([#813])
|
||||
This is specified via a new field on project files, `syncRules`:
|
||||
|
||||
```json
|
||||
{
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.foo",
|
||||
"use": "text",
|
||||
"exclude": "*.exclude.foo",
|
||||
},
|
||||
{
|
||||
"pattern": "*.bar.baz",
|
||||
"use": "json",
|
||||
"suffix": ".bar.baz",
|
||||
},
|
||||
],
|
||||
"name": "SyncRulesAreCool",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `pattern` field is a glob used to match the sync rule to files. If present, the `suffix` field allows you to specify parts of a file's name get cut off by Rojo to name the Instance, including the file extension. If it isn't specified, Rojo will only cut off the first part of the file extension, up to the first dot.
|
||||
|
||||
Additionally, the `exclude` field allows files to be excluded from the sync rule if they match a pattern specified by it. If it's not present, all files that match `pattern` will be modified using the sync rule.
|
||||
|
||||
The `use` field corresponds to one of the potential file type that Rojo will currently include in a project. Files that match the provided pattern will be treated as if they had the file extension for that file type. A full list is below:
|
||||
|
||||
| `use` value | file extension |
|
||||
|:---------------|:----------------|
|
||||
| `serverScript` | `.server.lua` |
|
||||
| `clientScript` | `.client.lua` |
|
||||
| `moduleScript` | `.lua` |
|
||||
| `json` | `.json` |
|
||||
| `toml` | `.toml` |
|
||||
| `csv` | `.csv` |
|
||||
| `text` | `.txt` |
|
||||
| `jsonModel` | `.model.json` |
|
||||
| `rbxm` | `.rbxm` |
|
||||
| `rbxmx` | `.rbxmx` |
|
||||
| `project` | `.project.json` |
|
||||
| `ignore` | None! |
|
||||
|
||||
**All** sync rules are reset between project files, so they must be specified in each one when nesting them. This is to ensure that nothing can break other projects by changing how files are synced!
|
||||
|
||||
[#813]: https://github.com/rojo-rbx/rojo/pull/813
|
||||
|
||||
## [7.4.0] - January 16, 2024
|
||||
* Improved the visualization for array properties like Tags ([#829])
|
||||
* Significantly improved performance of `rojo serve`, `rojo build --watch`, and `rojo sourcemap --watch` on macOS. ([#830])
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
source: tests/tests/build.rs
|
||||
assertion_line: 102
|
||||
expression: contents
|
||||
---
|
||||
<roblox version="4">
|
||||
<Item class="Folder" referent="0">
|
||||
<Properties>
|
||||
<string name="Name">sync_rule_alone</string>
|
||||
</Properties>
|
||||
<Item class="StringValue" referent="1">
|
||||
<Properties>
|
||||
<string name="Name">foo</string>
|
||||
<string name="Value">Hello, world!</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
</Item>
|
||||
</roblox>
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
source: tests/tests/build.rs
|
||||
assertion_line: 104
|
||||
expression: contents
|
||||
---
|
||||
<roblox version="4">
|
||||
<Item class="Folder" referent="0">
|
||||
<Properties>
|
||||
<string name="Name">sync_rule_complex</string>
|
||||
</Properties>
|
||||
<Item class="Script" referent="1">
|
||||
<Properties>
|
||||
<string name="Name">bar</string>
|
||||
<token name="RunContext">0</token>
|
||||
<string name="Source">-- Hello, from bar (a Script)!</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
<Item class="LocalScript" referent="2">
|
||||
<Properties>
|
||||
<string name="Name">baz</string>
|
||||
<string name="Source">-- Hello, from baz (a LocalScript)!</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
<Item class="StringValue" referent="3">
|
||||
<Properties>
|
||||
<string name="Name">cat</string>
|
||||
<string name="Value">Hello, from cat (a StringValue)!</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
<Item class="ModuleScript" referent="4">
|
||||
<Properties>
|
||||
<string name="Name">foo</string>
|
||||
<string name="Source">-- Hello, from foo (a ModuleScript)!</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
<Item class="StringValue" referent="5">
|
||||
<Properties>
|
||||
<string name="Name">qux</string>
|
||||
<string name="Value">Hello, from qux (a .rojo file that's turned into a StringValue)!</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
</Item>
|
||||
</roblox>
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: tests/tests/build.rs
|
||||
assertion_line: 104
|
||||
expression: contents
|
||||
---
|
||||
<roblox version="4">
|
||||
<Item class="Folder" referent="0">
|
||||
<Properties>
|
||||
<string name="Name">sync_rule_nested_projects</string>
|
||||
</Properties>
|
||||
</Item>
|
||||
</roblox>
|
||||
12
rojo-test/build-tests/sync_rule_alone/default.project.json
Normal file
12
rojo-test/build-tests/sync_rule_alone/default.project.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "sync_rule_alone",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.nothing",
|
||||
"use": "text"
|
||||
}
|
||||
]
|
||||
}
|
||||
1
rojo-test/build-tests/sync_rule_alone/src/foo.nothing
Normal file
1
rojo-test/build-tests/sync_rule_alone/src/foo.nothing
Normal file
@@ -0,0 +1 @@
|
||||
Hello, world!
|
||||
30
rojo-test/build-tests/sync_rule_complex/default.project.json
Normal file
30
rojo-test/build-tests/sync_rule_complex/default.project.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "sync_rule_complex",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.module",
|
||||
"use": "moduleScript"
|
||||
},
|
||||
{
|
||||
"pattern": "*.server",
|
||||
"use": "serverScript"
|
||||
},
|
||||
{
|
||||
"pattern": "*.client",
|
||||
"use": "clientScript"
|
||||
},
|
||||
{
|
||||
"pattern": "*.rojo",
|
||||
"exclude": "*.ignore.rojo",
|
||||
"use": "project"
|
||||
},
|
||||
{
|
||||
"pattern": "*.dog.rojo2",
|
||||
"use": "text",
|
||||
"suffix": ".dog.rojo2"
|
||||
}
|
||||
]
|
||||
}
|
||||
1
rojo-test/build-tests/sync_rule_complex/src/bar.server
Normal file
1
rojo-test/build-tests/sync_rule_complex/src/bar.server
Normal file
@@ -0,0 +1 @@
|
||||
-- Hello, from bar (a Script)!
|
||||
1
rojo-test/build-tests/sync_rule_complex/src/baz.client
Normal file
1
rojo-test/build-tests/sync_rule_complex/src/baz.client
Normal file
@@ -0,0 +1 @@
|
||||
-- Hello, from baz (a LocalScript)!
|
||||
@@ -0,0 +1 @@
|
||||
Hello, from cat (a StringValue)!
|
||||
1
rojo-test/build-tests/sync_rule_complex/src/foo.module
Normal file
1
rojo-test/build-tests/sync_rule_complex/src/foo.module
Normal file
@@ -0,0 +1 @@
|
||||
-- Hello, from foo (a ModuleScript)!
|
||||
9
rojo-test/build-tests/sync_rule_complex/src/qux.rojo
Normal file
9
rojo-test/build-tests/sync_rule_complex/src/qux.rojo
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "qux",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": "Hello, from qux (a .rojo file that's turned into a StringValue)!"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This file should be ignored!
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "sync_rule_nested_projects",
|
||||
"tree": {
|
||||
"$path": "nested.project.json"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.rojo",
|
||||
"use": "text"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "nested",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.txt",
|
||||
"use": "ignore"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This shouldn't be in the built file. If it is, something is wrong.
|
||||
@@ -0,0 +1 @@
|
||||
This shouldn't be in the built file. If it is, something is wrong.
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
source: tests/tests/serve.rs
|
||||
assertion_line: 268
|
||||
expression: "read_response.intern_and_redact(&mut redactions, root_id)"
|
||||
---
|
||||
instances:
|
||||
id-2:
|
||||
Children:
|
||||
- id-3
|
||||
ClassName: Folder
|
||||
Id: id-2
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: sync_rule_alone
|
||||
Parent: "00000000000000000000000000000000"
|
||||
Properties: {}
|
||||
id-3:
|
||||
Children: []
|
||||
ClassName: StringValue
|
||||
Id: id-3
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: foo
|
||||
Parent: id-2
|
||||
Properties:
|
||||
Value:
|
||||
String: "Hello, world!"
|
||||
messageCursor: 0
|
||||
sessionId: id-1
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
source: tests/tests/serve.rs
|
||||
assertion_line: 265
|
||||
expression: redactions.redacted_yaml(info)
|
||||
---
|
||||
expectedPlaceIds: ~
|
||||
gameId: ~
|
||||
placeId: ~
|
||||
projectName: sync_rule_alone
|
||||
protocolVersion: 4
|
||||
rootInstanceId: id-2
|
||||
serverVersion: "[server-version]"
|
||||
sessionId: id-1
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
source: tests/tests/serve.rs
|
||||
assertion_line: 284
|
||||
expression: "read_response.intern_and_redact(&mut redactions, root_id)"
|
||||
---
|
||||
instances:
|
||||
id-2:
|
||||
Children:
|
||||
- id-3
|
||||
- id-4
|
||||
- id-5
|
||||
- id-6
|
||||
- id-7
|
||||
ClassName: Folder
|
||||
Id: id-2
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: sync_rule_complex
|
||||
Parent: "00000000000000000000000000000000"
|
||||
Properties: {}
|
||||
id-3:
|
||||
Children: []
|
||||
ClassName: Script
|
||||
Id: id-3
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: bar
|
||||
Parent: id-2
|
||||
Properties:
|
||||
RunContext:
|
||||
Enum: 0
|
||||
Source:
|
||||
String: "-- Hello, from bar (a Script)!"
|
||||
id-4:
|
||||
Children: []
|
||||
ClassName: LocalScript
|
||||
Id: id-4
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: baz
|
||||
Parent: id-2
|
||||
Properties:
|
||||
Source:
|
||||
String: "-- Hello, from baz (a LocalScript)!"
|
||||
id-5:
|
||||
Children: []
|
||||
ClassName: StringValue
|
||||
Id: id-5
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: cat
|
||||
Parent: id-2
|
||||
Properties:
|
||||
Value:
|
||||
String: "Hello, from cat (a StringValue)!"
|
||||
id-6:
|
||||
Children: []
|
||||
ClassName: ModuleScript
|
||||
Id: id-6
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: foo
|
||||
Parent: id-2
|
||||
Properties:
|
||||
Source:
|
||||
String: "-- Hello, from foo (a ModuleScript)!"
|
||||
id-7:
|
||||
Children: []
|
||||
ClassName: StringValue
|
||||
Id: id-7
|
||||
Metadata:
|
||||
ignoreUnknownInstances: true
|
||||
Name: qux
|
||||
Parent: id-2
|
||||
Properties:
|
||||
Value:
|
||||
String: "Hello, from qux (a .rojo file that's turned into a StringValue)!"
|
||||
messageCursor: 0
|
||||
sessionId: id-1
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
source: tests/tests/serve.rs
|
||||
assertion_line: 281
|
||||
expression: redactions.redacted_yaml(info)
|
||||
---
|
||||
expectedPlaceIds: ~
|
||||
gameId: ~
|
||||
placeId: ~
|
||||
projectName: sync_rule_complex
|
||||
protocolVersion: 4
|
||||
rootInstanceId: id-2
|
||||
serverVersion: "[server-version]"
|
||||
sessionId: id-1
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
source: tests/tests/serve.rs
|
||||
assertion_line: 303
|
||||
expression: "read_response.intern_and_redact(&mut redactions, root_id)"
|
||||
---
|
||||
instances:
|
||||
id-2:
|
||||
Children:
|
||||
- id-3
|
||||
ClassName: Folder
|
||||
Id: id-2
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: sync_rule_no_extension
|
||||
Parent: "00000000000000000000000000000000"
|
||||
Properties: {}
|
||||
id-3:
|
||||
Children: []
|
||||
ClassName: ModuleScript
|
||||
Id: id-3
|
||||
Metadata:
|
||||
ignoreUnknownInstances: false
|
||||
Name: no_extension
|
||||
Parent: id-2
|
||||
Properties:
|
||||
Source:
|
||||
String: "return {\"This file has no extension but should be a ModuleScript named `no_extension`\"}"
|
||||
messageCursor: 0
|
||||
sessionId: id-1
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
source: tests/tests/serve.rs
|
||||
assertion_line: 297
|
||||
expression: redactions.redacted_yaml(info)
|
||||
---
|
||||
expectedPlaceIds: ~
|
||||
gameId: ~
|
||||
placeId: ~
|
||||
projectName: sync_rule_no_extension
|
||||
protocolVersion: 4
|
||||
rootInstanceId: id-2
|
||||
serverVersion: "[server-version]"
|
||||
sessionId: id-1
|
||||
|
||||
12
rojo-test/serve-tests/sync_rule_alone/default.project.json
Normal file
12
rojo-test/serve-tests/sync_rule_alone/default.project.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "sync_rule_alone",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.nothing",
|
||||
"use": "text"
|
||||
}
|
||||
]
|
||||
}
|
||||
1
rojo-test/serve-tests/sync_rule_alone/src/foo.nothing
Normal file
1
rojo-test/serve-tests/sync_rule_alone/src/foo.nothing
Normal file
@@ -0,0 +1 @@
|
||||
Hello, world!
|
||||
30
rojo-test/serve-tests/sync_rule_complex/default.project.json
Normal file
30
rojo-test/serve-tests/sync_rule_complex/default.project.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "sync_rule_complex",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "*.module",
|
||||
"use": "moduleScript"
|
||||
},
|
||||
{
|
||||
"pattern": "*.server",
|
||||
"use": "serverScript"
|
||||
},
|
||||
{
|
||||
"pattern": "*.client",
|
||||
"use": "clientScript"
|
||||
},
|
||||
{
|
||||
"pattern": "*.rojo",
|
||||
"exclude": "*.ignore.rojo",
|
||||
"use": "project"
|
||||
},
|
||||
{
|
||||
"pattern": "*.dog.rojo2",
|
||||
"use": "text",
|
||||
"suffix": ".dog.rojo2"
|
||||
}
|
||||
]
|
||||
}
|
||||
1
rojo-test/serve-tests/sync_rule_complex/src/bar.server
Normal file
1
rojo-test/serve-tests/sync_rule_complex/src/bar.server
Normal file
@@ -0,0 +1 @@
|
||||
-- Hello, from bar (a Script)!
|
||||
1
rojo-test/serve-tests/sync_rule_complex/src/baz.client
Normal file
1
rojo-test/serve-tests/sync_rule_complex/src/baz.client
Normal file
@@ -0,0 +1 @@
|
||||
-- Hello, from baz (a LocalScript)!
|
||||
@@ -0,0 +1 @@
|
||||
Hello, from cat (a StringValue)!
|
||||
1
rojo-test/serve-tests/sync_rule_complex/src/foo.module
Normal file
1
rojo-test/serve-tests/sync_rule_complex/src/foo.module
Normal file
@@ -0,0 +1 @@
|
||||
-- Hello, from foo (a ModuleScript)!
|
||||
9
rojo-test/serve-tests/sync_rule_complex/src/qux.rojo
Normal file
9
rojo-test/serve-tests/sync_rule_complex/src/qux.rojo
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "qux",
|
||||
"tree": {
|
||||
"$className": "StringValue",
|
||||
"$properties": {
|
||||
"Value": "Hello, from qux (a .rojo file that's turned into a StringValue)!"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This file should be ignored!
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "sync_rule_no_extension",
|
||||
"tree": {
|
||||
"$path": "src"
|
||||
},
|
||||
"syncRules": [
|
||||
{
|
||||
"pattern": "src/**",
|
||||
"use": "json"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
["This file has no extension but should be a ModuleScript named `no_extension`"]
|
||||
@@ -8,7 +8,7 @@ use std::{
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{glob::Glob, resolution::UnresolvedValue};
|
||||
use crate::{glob::Glob, resolution::UnresolvedValue, snapshot::SyncRule};
|
||||
|
||||
static PROJECT_FILENAME: &str = "default.project.json";
|
||||
|
||||
@@ -84,6 +84,12 @@ pub struct Project {
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub glob_ignore_paths: Vec<Glob>,
|
||||
|
||||
/// A list of mappings of globs to syncing rules. If a file matches a glob,
|
||||
/// it will be 'transformed' into an Instance following the rule provided.
|
||||
/// Globs are relative to the folder the project file is in.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub sync_rules: Vec<SyncRule>,
|
||||
|
||||
/// The path to the file that this project came from. Relative paths in the
|
||||
/// project should be considered relative to the parent of this field, also
|
||||
/// given by `Project::folder_location`.
|
||||
|
||||
@@ -4,11 +4,14 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
glob::Glob, path_serializer, project::ProjectNode,
|
||||
snapshot_middleware::emit_legacy_scripts_default,
|
||||
glob::Glob,
|
||||
path_serializer,
|
||||
project::ProjectNode,
|
||||
snapshot_middleware::{emit_legacy_scripts_default, Middleware},
|
||||
};
|
||||
|
||||
/// Rojo-specific metadata that can be associated with an instance or a snapshot
|
||||
@@ -107,6 +110,8 @@ pub struct InstanceContext {
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub path_ignore_rules: Arc<Vec<PathIgnoreRule>>,
|
||||
pub emit_legacy_scripts: bool,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub sync_rules: Vec<SyncRule>,
|
||||
}
|
||||
|
||||
impl InstanceContext {
|
||||
@@ -114,6 +119,7 @@ impl InstanceContext {
|
||||
Self {
|
||||
path_ignore_rules: Arc::new(Vec::new()),
|
||||
emit_legacy_scripts: emit_legacy_scripts_default().unwrap(),
|
||||
sync_rules: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,9 +150,28 @@ impl InstanceContext {
|
||||
rules.extend(new_rules);
|
||||
}
|
||||
|
||||
/// Extend the list of syncing rules in the context with the given new rules.
|
||||
pub fn add_sync_rules<I>(&mut self, new_rules: I)
|
||||
where
|
||||
I: IntoIterator<Item = SyncRule>,
|
||||
{
|
||||
self.sync_rules.extend(new_rules);
|
||||
}
|
||||
|
||||
/// Clears all sync rules for this InstanceContext
|
||||
pub fn clear_sync_rules(&mut self) {
|
||||
self.sync_rules.clear();
|
||||
}
|
||||
|
||||
pub fn set_emit_legacy_scripts(&mut self, emit_legacy_scripts: bool) {
|
||||
self.emit_legacy_scripts = emit_legacy_scripts;
|
||||
}
|
||||
|
||||
/// Returns the middleware specified by the first sync rule that
|
||||
/// matches the provided path. This does not handle default syncing rules.
|
||||
pub fn get_user_sync_rule(&self, path: &Path) -> Option<&SyncRule> {
|
||||
self.sync_rules.iter().find(|&rule| rule.matches(path))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InstanceContext {
|
||||
@@ -216,3 +241,64 @@ impl From<&Path> for InstigatingSource {
|
||||
InstigatingSource::Path(path.to_path_buf())
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents an user-specified rule for transforming files
|
||||
/// into Instances using a given middleware.
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
|
||||
pub struct SyncRule {
|
||||
/// A pattern used to determine if a file is included in this SyncRule
|
||||
#[serde(rename = "pattern")]
|
||||
pub include: Glob,
|
||||
/// A pattern used to determine if a file is excluded from this SyncRule.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub exclude: Option<Glob>,
|
||||
/// The middleware specified by the user for this SyncRule
|
||||
#[serde(rename = "use")]
|
||||
pub middleware: Middleware,
|
||||
/// A suffix to trim off of file names, including the file extension.
|
||||
/// If not specified, the file extension is the only thing cut off.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub suffix: Option<String>,
|
||||
/// The 'base' of the glob above, allowing it to be used
|
||||
/// relative to a path instead of absolute.
|
||||
#[serde(skip)]
|
||||
pub base_path: PathBuf,
|
||||
}
|
||||
|
||||
impl SyncRule {
|
||||
/// Returns whether the given path matches this rule.
|
||||
pub fn matches(&self, path: &Path) -> bool {
|
||||
match path.strip_prefix(&self.base_path) {
|
||||
Ok(suffix) => {
|
||||
if let Some(pattern) = &self.exclude {
|
||||
if pattern.is_match(suffix) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self.include.is_match(suffix)
|
||||
}
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_name_for_path<'a>(&self, path: &'a Path) -> anyhow::Result<&'a str> {
|
||||
if let Some(suffix) = &self.suffix {
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.with_context(|| format!("file name of {} is invalid", path.display()))?;
|
||||
if file_name.ends_with(suffix) {
|
||||
let end = file_name.len().saturating_sub(suffix.len());
|
||||
Ok(&file_name[..end])
|
||||
} else {
|
||||
Ok(file_name)
|
||||
}
|
||||
} else {
|
||||
// If the user doesn't specify a suffix, we assume they just want
|
||||
// the name of the file (the file_stem)
|
||||
path.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.with_context(|| format!("file name of {} is invalid", path.display()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,16 +10,14 @@ use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
use super::{
|
||||
dir::{dir_meta, snapshot_dir_no_meta},
|
||||
meta_file::AdjacentMetadata,
|
||||
util::PathExt,
|
||||
};
|
||||
|
||||
pub fn snapshot_csv(
|
||||
_context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".csv")?;
|
||||
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", name));
|
||||
let contents = vfs.read(path)?;
|
||||
|
||||
@@ -74,9 +72,8 @@ pub fn snapshot_csv_init(
|
||||
);
|
||||
}
|
||||
|
||||
let mut init_snapshot = snapshot_csv(context, vfs, init_path)?.unwrap();
|
||||
let mut init_snapshot = snapshot_csv(context, vfs, init_path, &dir_snapshot.name)?.unwrap();
|
||||
|
||||
init_snapshot.name = dir_snapshot.name;
|
||||
init_snapshot.children = dir_snapshot.children;
|
||||
init_snapshot.metadata = dir_snapshot.metadata;
|
||||
|
||||
@@ -185,10 +182,14 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
|
||||
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let instance_snapshot =
|
||||
snapshot_csv(&InstanceContext::default(), &mut vfs, Path::new("/foo.csv"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = snapshot_csv(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.csv"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
@@ -213,10 +214,14 @@ Ack,Ack!,,An exclamation of despair,¡Ay!"#,
|
||||
|
||||
let mut vfs = Vfs::new(imfs);
|
||||
|
||||
let instance_snapshot =
|
||||
snapshot_csv(&InstanceContext::default(), &mut vfs, Path::new("/foo.csv"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = snapshot_csv(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.csv"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
};
|
||||
|
||||
use super::{meta_file::AdjacentMetadata, util::PathExt};
|
||||
use super::meta_file::AdjacentMetadata;
|
||||
|
||||
pub fn snapshot_json(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".json")?;
|
||||
let contents = vfs.read(path)?;
|
||||
|
||||
let value: serde_json::Value = serde_json::from_slice(&contents)
|
||||
@@ -107,6 +107,7 @@ mod test {
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.json"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
@@ -10,15 +10,12 @@ use crate::{
|
||||
snapshot::{InstanceContext, InstanceSnapshot},
|
||||
};
|
||||
|
||||
use super::util::PathExt;
|
||||
|
||||
pub fn snapshot_json_model(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".model.json")?;
|
||||
|
||||
let contents = vfs.read(path)?;
|
||||
let contents_str = str::from_utf8(&contents)
|
||||
.with_context(|| format!("File was not valid UTF-8: {}", path.display()))?;
|
||||
@@ -158,6 +155,7 @@ mod test {
|
||||
&InstanceContext::default(),
|
||||
&vfs,
|
||||
Path::new("/foo.model.json"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -195,6 +193,7 @@ mod test {
|
||||
&InstanceContext::default(),
|
||||
&vfs,
|
||||
Path::new("/foo.model.json"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
@@ -9,11 +9,10 @@ use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
use super::{
|
||||
dir::{dir_meta, snapshot_dir_no_meta},
|
||||
meta_file::AdjacentMetadata,
|
||||
util::match_trailing,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ScriptType {
|
||||
pub enum ScriptType {
|
||||
Server,
|
||||
Client,
|
||||
Module,
|
||||
@@ -24,32 +23,15 @@ pub fn snapshot_lua(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
script_type: ScriptType,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let file_name = path.file_name().unwrap().to_string_lossy();
|
||||
|
||||
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")
|
||||
{
|
||||
(ScriptType::Server, name)
|
||||
} else if let Some(name) = match_trailing(&file_name, ".client.lua") {
|
||||
(ScriptType::Client, name)
|
||||
} else if let Some(name) = match_trailing(&file_name, ".lua") {
|
||||
(ScriptType::Module, name)
|
||||
} else if let Some(name) = match_trailing(&file_name, ".server.luau") {
|
||||
(ScriptType::Server, name)
|
||||
} else if let Some(name) = match_trailing(&file_name, ".client.luau") {
|
||||
(ScriptType::Client, name)
|
||||
} else if let Some(name) = match_trailing(&file_name, ".luau") {
|
||||
(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")),
|
||||
@@ -73,10 +55,10 @@ pub fn snapshot_lua(
|
||||
);
|
||||
}
|
||||
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));
|
||||
let meta_path = path.with_file_name(format!("{}.meta.json", name));
|
||||
|
||||
let mut snapshot = InstanceSnapshot::new()
|
||||
.name(instance_name)
|
||||
.name(name)
|
||||
.class_name(class_name)
|
||||
.properties(properties)
|
||||
.metadata(
|
||||
@@ -103,6 +85,7 @@ pub fn snapshot_lua_init(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
init_path: &Path,
|
||||
script_type: ScriptType,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let folder_path = init_path.parent().unwrap();
|
||||
let dir_snapshot = snapshot_dir_no_meta(context, vfs, folder_path)?.unwrap();
|
||||
@@ -119,9 +102,9 @@ pub fn snapshot_lua_init(
|
||||
);
|
||||
}
|
||||
|
||||
let mut init_snapshot = snapshot_lua(context, vfs, init_path)?.unwrap();
|
||||
let mut init_snapshot =
|
||||
snapshot_lua(context, vfs, init_path, &dir_snapshot.name, script_type)?.unwrap();
|
||||
|
||||
init_snapshot.name = dir_snapshot.name;
|
||||
init_snapshot.children = dir_snapshot.children;
|
||||
init_snapshot.metadata = dir_snapshot.metadata;
|
||||
|
||||
@@ -151,6 +134,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.lua"),
|
||||
"foo",
|
||||
ScriptType::Module,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -172,6 +157,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(false)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.lua"),
|
||||
"foo",
|
||||
ScriptType::Module,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -193,6 +180,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.server.lua"),
|
||||
"foo",
|
||||
ScriptType::Server,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -214,6 +203,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(false)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.server.lua"),
|
||||
"foo",
|
||||
ScriptType::Server,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -235,6 +226,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.client.lua"),
|
||||
"foo",
|
||||
ScriptType::Client,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -256,6 +249,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(false)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.client.lua"),
|
||||
"foo",
|
||||
ScriptType::Client,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -283,6 +278,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/root"),
|
||||
"root",
|
||||
ScriptType::Module,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -315,6 +312,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.lua"),
|
||||
"foo",
|
||||
ScriptType::Module,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -347,6 +346,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(false)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.lua"),
|
||||
"foo",
|
||||
ScriptType::Module,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -379,6 +380,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.server.lua"),
|
||||
"foo",
|
||||
ScriptType::Server,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -411,6 +414,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(false)),
|
||||
&mut vfs,
|
||||
Path::new("/foo.server.lua"),
|
||||
"foo",
|
||||
ScriptType::Server,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -445,6 +450,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(true)),
|
||||
&mut vfs,
|
||||
Path::new("/bar.server.lua"),
|
||||
"bar",
|
||||
ScriptType::Server,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -479,6 +486,8 @@ mod test {
|
||||
&InstanceContext::with_emit_legacy_scripts(Some(false)),
|
||||
&mut vfs,
|
||||
Path::new("/bar.server.lua"),
|
||||
"bar",
|
||||
ScriptType::Server,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
@@ -18,30 +18,37 @@ mod toml;
|
||||
mod txt;
|
||||
mod util;
|
||||
|
||||
use std::path::Path;
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::OnceLock,
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use memofs::{IoResultExt, Vfs};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceSnapshot};
|
||||
use crate::glob::Glob;
|
||||
use crate::snapshot::{InstanceContext, InstanceSnapshot, SyncRule};
|
||||
|
||||
use self::{
|
||||
csv::{snapshot_csv, snapshot_csv_init},
|
||||
dir::snapshot_dir,
|
||||
json::snapshot_json,
|
||||
json_model::snapshot_json_model,
|
||||
lua::{snapshot_lua, snapshot_lua_init},
|
||||
lua::{snapshot_lua, snapshot_lua_init, ScriptType},
|
||||
project::snapshot_project,
|
||||
rbxm::snapshot_rbxm,
|
||||
rbxmx::snapshot_rbxmx,
|
||||
toml::snapshot_toml,
|
||||
txt::snapshot_txt,
|
||||
util::PathExt,
|
||||
};
|
||||
|
||||
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.
|
||||
/// Returns an `InstanceSnapshot` for the provided path.
|
||||
/// This will inspect the path and find the appropriate middleware for it,
|
||||
/// taking user-written rules into account. Then, it will attempt to convert
|
||||
/// the path into an InstanceSnapshot using that middleware.
|
||||
#[profiling::function]
|
||||
pub fn snapshot_from_vfs(
|
||||
context: &InstanceContext,
|
||||
@@ -54,89 +61,234 @@ pub fn snapshot_from_vfs(
|
||||
};
|
||||
|
||||
if meta.is_dir() {
|
||||
let project_path = path.join("default.project.json");
|
||||
if vfs.metadata(&project_path).with_not_found()?.is_some() {
|
||||
return snapshot_project(context, vfs, &project_path);
|
||||
}
|
||||
if let Some(init_path) = get_init_path(vfs, path)? {
|
||||
// TODO: support user-defined init paths
|
||||
for rule in default_sync_rules() {
|
||||
if rule.matches(&init_path) {
|
||||
return match rule.middleware {
|
||||
Middleware::Project => snapshot_project(context, vfs, &init_path),
|
||||
|
||||
let init_path = path.join("init.luau");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_lua_init(context, vfs, &init_path);
|
||||
}
|
||||
Middleware::ModuleScript => {
|
||||
snapshot_lua_init(context, vfs, &init_path, ScriptType::Module)
|
||||
}
|
||||
Middleware::ServerScript => {
|
||||
snapshot_lua_init(context, vfs, &init_path, ScriptType::Server)
|
||||
}
|
||||
Middleware::ClientScript => {
|
||||
snapshot_lua_init(context, vfs, &init_path, ScriptType::Client)
|
||||
}
|
||||
|
||||
let init_path = path.join("init.lua");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_lua_init(context, vfs, &init_path);
|
||||
}
|
||||
Middleware::Csv => snapshot_csv_init(context, vfs, &init_path),
|
||||
|
||||
let init_path = path.join("init.server.luau");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_lua_init(context, vfs, &init_path);
|
||||
_ => snapshot_dir(context, vfs, path),
|
||||
};
|
||||
}
|
||||
}
|
||||
snapshot_dir(context, vfs, path)
|
||||
} else {
|
||||
snapshot_dir(context, vfs, path)
|
||||
}
|
||||
|
||||
let init_path = path.join("init.server.lua");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_lua_init(context, vfs, &init_path);
|
||||
}
|
||||
|
||||
let init_path = path.join("init.client.luau");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_lua_init(context, vfs, &init_path);
|
||||
}
|
||||
|
||||
let init_path = path.join("init.client.lua");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_lua_init(context, vfs, &init_path);
|
||||
}
|
||||
|
||||
let init_path = path.join("init.csv");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return snapshot_csv_init(context, vfs, &init_path);
|
||||
}
|
||||
|
||||
snapshot_dir(context, vfs, path)
|
||||
} else {
|
||||
let script_name = path
|
||||
.file_name_trim_end(".lua")
|
||||
.or_else(|_| path.file_name_trim_end(".luau"));
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.with_context(|| format!("file name of {} is invalid", path.display()))?;
|
||||
|
||||
let csv_name = path.file_name_trim_end(".csv");
|
||||
|
||||
if let Ok(name) = script_name {
|
||||
match name {
|
||||
// init scripts are handled elsewhere and should not turn into
|
||||
// their own children.
|
||||
"init" | "init.client" | "init.server" => return Ok(None),
|
||||
|
||||
_ => return snapshot_lua(context, vfs, path),
|
||||
}
|
||||
} else if path.file_name_ends_with(".project.json") {
|
||||
return snapshot_project(context, vfs, path);
|
||||
} else if path.file_name_ends_with(".model.json") {
|
||||
return snapshot_json_model(context, vfs, path);
|
||||
} else if path.file_name_ends_with(".meta.json") {
|
||||
// .meta.json files do not turn into their own instances.
|
||||
return Ok(None);
|
||||
} else if path.file_name_ends_with(".json") {
|
||||
return snapshot_json(context, vfs, path);
|
||||
} else if path.file_name_ends_with(".toml") {
|
||||
return snapshot_toml(context, vfs, path);
|
||||
} else if let Ok(name) = csv_name {
|
||||
match name {
|
||||
// init csv are handled elsewhere and should not turn into
|
||||
// their own children.
|
||||
"init" => return Ok(None),
|
||||
|
||||
_ => return snapshot_csv(context, vfs, path),
|
||||
}
|
||||
} else if path.file_name_ends_with(".txt") {
|
||||
return snapshot_txt(context, vfs, path);
|
||||
} else if path.file_name_ends_with(".rbxmx") {
|
||||
return snapshot_rbxmx(context, vfs, path);
|
||||
} else if path.file_name_ends_with(".rbxm") {
|
||||
return snapshot_rbxm(context, vfs, path);
|
||||
// TODO: Is this even necessary anymore?
|
||||
match file_name {
|
||||
"init.server.luau" | "init.server.lua" | "init.client.luau" | "init.client.lua"
|
||||
| "init.luau" | "init.lua" | "init.csv" => return Ok(None),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
snapshot_from_path(context, vfs, path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets an `init` path for the given directory.
|
||||
/// This uses an intrinsic priority list and for compatibility,
|
||||
/// it should not be changed.
|
||||
fn get_init_path<P: AsRef<Path>>(vfs: &Vfs, dir: P) -> anyhow::Result<Option<PathBuf>> {
|
||||
let path = dir.as_ref();
|
||||
|
||||
let project_path = path.join("default.project.json");
|
||||
if vfs.metadata(&project_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(project_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.luau");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.lua");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.server.luau");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.server.lua");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.client.luau");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.client.lua");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
let init_path = path.join("init.csv");
|
||||
if vfs.metadata(&init_path).with_not_found()?.is_some() {
|
||||
return Ok(Some(init_path));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Gets a snapshot for a path given an InstanceContext and Vfs, taking
|
||||
/// user specified sync rules into account.
|
||||
fn snapshot_from_path(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
if let Some(rule) = context.get_user_sync_rule(path) {
|
||||
return rule
|
||||
.middleware
|
||||
.snapshot(context, vfs, path, rule.file_name_for_path(path)?);
|
||||
} else {
|
||||
for rule in default_sync_rules() {
|
||||
if rule.matches(path) {
|
||||
return rule.middleware.snapshot(
|
||||
context,
|
||||
vfs,
|
||||
path,
|
||||
rule.file_name_for_path(path)?,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Represents a possible 'transformer' used by Rojo to turn a file system
|
||||
/// item into a Roblox Instance. Missing from this list are directories and
|
||||
/// metadata. This is deliberate, as metadata is not a snapshot middleware
|
||||
/// and directories do not make sense to turn into files.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum Middleware {
|
||||
Csv,
|
||||
JsonModel,
|
||||
Json,
|
||||
ServerScript,
|
||||
ClientScript,
|
||||
ModuleScript,
|
||||
Project,
|
||||
Rbxm,
|
||||
Rbxmx,
|
||||
Toml,
|
||||
Text,
|
||||
Ignore,
|
||||
}
|
||||
|
||||
impl Middleware {
|
||||
/// Creates a snapshot for the given path from the Middleware with
|
||||
/// the provided name.
|
||||
fn snapshot(
|
||||
&self,
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
match self {
|
||||
Self::Csv => snapshot_csv(context, vfs, path, name),
|
||||
Self::JsonModel => snapshot_json_model(context, vfs, path, name),
|
||||
Self::Json => snapshot_json(context, vfs, path, name),
|
||||
Self::ServerScript => snapshot_lua(context, vfs, path, name, ScriptType::Server),
|
||||
Self::ClientScript => snapshot_lua(context, vfs, path, name, ScriptType::Client),
|
||||
Self::ModuleScript => snapshot_lua(context, vfs, path, name, ScriptType::Module),
|
||||
// At the moment, snapshot_project does not use `name` so we
|
||||
// don't provide it.
|
||||
Self::Project => snapshot_project(context, vfs, path),
|
||||
Self::Rbxm => snapshot_rbxm(context, vfs, path, name),
|
||||
Self::Rbxmx => snapshot_rbxmx(context, vfs, path, name),
|
||||
Self::Toml => snapshot_toml(context, vfs, path, name),
|
||||
Self::Text => snapshot_txt(context, vfs, path, name),
|
||||
Self::Ignore => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper for easily defining a SyncRule. Arguments are passed literally
|
||||
/// to this macro in the order `include`, `middleware`, `suffix`,
|
||||
/// and `exclude`. Both `suffix` and `exclude` are optional.
|
||||
///
|
||||
/// All arguments except `middleware` are expected to be strings.
|
||||
/// The `middleware` parameter is expected to be a variant of `Middleware`,
|
||||
/// not including the enum name itself.
|
||||
macro_rules! sync_rule {
|
||||
($pattern:expr, $middleware:ident) => {
|
||||
SyncRule {
|
||||
middleware: Middleware::$middleware,
|
||||
include: Glob::new($pattern).unwrap(),
|
||||
exclude: None,
|
||||
suffix: None,
|
||||
base_path: PathBuf::new(),
|
||||
}
|
||||
};
|
||||
($pattern:expr, $middleware:ident, $suffix:expr) => {
|
||||
SyncRule {
|
||||
middleware: Middleware::$middleware,
|
||||
include: Glob::new($pattern).unwrap(),
|
||||
exclude: None,
|
||||
suffix: Some($suffix.into()),
|
||||
base_path: PathBuf::new(),
|
||||
}
|
||||
};
|
||||
($pattern:expr, $middleware:ident, $suffix:expr, $exclude:expr) => {
|
||||
SyncRule {
|
||||
middleware: Middleware::$middleware,
|
||||
include: Glob::new($pattern).unwrap(),
|
||||
exclude: Some(Glob::new($exclude).unwrap()),
|
||||
suffix: Some($suffix.into()),
|
||||
base_path: PathBuf::new(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Defines the 'default' syncing rules that Rojo uses.
|
||||
/// These do not broadly overlap, but the order matters for some in the case of
|
||||
/// e.g. JSON models.
|
||||
fn default_sync_rules() -> &'static [SyncRule] {
|
||||
static DEFAULT_SYNC_RULES: OnceLock<Vec<SyncRule>> = OnceLock::new();
|
||||
|
||||
DEFAULT_SYNC_RULES.get_or_init(|| {
|
||||
vec![
|
||||
sync_rule!("*.server.lua", ServerScript, ".server.lua"),
|
||||
sync_rule!("*.server.luau", ServerScript, ".server.luau"),
|
||||
sync_rule!("*.client.lua", ClientScript, ".client.lua"),
|
||||
sync_rule!("*.client.luau", ClientScript, ".client.luau"),
|
||||
sync_rule!("*.{lua,luau}", ModuleScript),
|
||||
// Project middleware doesn't use the file name.
|
||||
sync_rule!("*.project.json", Project),
|
||||
sync_rule!("*.model.json", JsonModel, ".model.json"),
|
||||
sync_rule!("*.json", Json, ".json", "*.meta.json"),
|
||||
sync_rule!("*.toml", Toml),
|
||||
sync_rule!("*.csv", Csv),
|
||||
sync_rule!("*.txt", Text),
|
||||
sync_rule!("*.rbxmx", Rbxmx),
|
||||
sync_rule!("*.rbxm", Rbxm),
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use crate::{
|
||||
project::{PathNode, Project, ProjectNode},
|
||||
snapshot::{
|
||||
InstanceContext, InstanceMetadata, InstanceSnapshot, InstigatingSource, PathIgnoreRule,
|
||||
SyncRule,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -23,12 +24,19 @@ pub fn snapshot_project(
|
||||
.with_context(|| format!("File was not a valid Rojo project: {}", path.display()))?;
|
||||
|
||||
let mut context = context.clone();
|
||||
context.clear_sync_rules();
|
||||
|
||||
let rules = project.glob_ignore_paths.iter().map(|glob| PathIgnoreRule {
|
||||
glob: glob.clone(),
|
||||
base_path: project.folder_location().to_path_buf(),
|
||||
});
|
||||
|
||||
let sync_rules = project.sync_rules.iter().map(|rule| SyncRule {
|
||||
base_path: project.folder_location().to_path_buf(),
|
||||
..rule.clone()
|
||||
});
|
||||
|
||||
context.add_sync_rules(sync_rules);
|
||||
context.add_path_ignore_rules(rules);
|
||||
context.set_emit_legacy_scripts(
|
||||
project
|
||||
|
||||
@@ -5,16 +5,13 @@ use memofs::Vfs;
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::util::PathExt;
|
||||
|
||||
#[profiling::function]
|
||||
pub fn snapshot_rbxm(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".rbxm")?;
|
||||
|
||||
let temp_tree = rbx_binary::from_reader(vfs.read(path)?.as_slice())
|
||||
.with_context(|| format!("Malformed rbxm file: {}", path.display()))?;
|
||||
|
||||
@@ -63,6 +60,7 @@ mod test {
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.rbxm"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
@@ -5,15 +5,12 @@ use memofs::Vfs;
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::util::PathExt;
|
||||
|
||||
pub fn snapshot_rbxmx(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".rbxmx")?;
|
||||
|
||||
let options = rbx_xml::DecodeOptions::new()
|
||||
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown);
|
||||
|
||||
@@ -75,6 +72,7 @@ mod test {
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.rbxmx"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
@@ -9,14 +9,14 @@ use crate::{
|
||||
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
|
||||
};
|
||||
|
||||
use super::{meta_file::AdjacentMetadata, util::PathExt};
|
||||
use super::meta_file::AdjacentMetadata;
|
||||
|
||||
pub fn snapshot_toml(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".toml")?;
|
||||
let contents = vfs.read(path)?;
|
||||
|
||||
let value: toml::Value = toml::from_slice(&contents)
|
||||
@@ -114,6 +114,7 @@ mod test {
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.toml"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
@@ -6,15 +6,14 @@ use memofs::{IoResultExt, Vfs};
|
||||
|
||||
use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};
|
||||
|
||||
use super::{meta_file::AdjacentMetadata, util::PathExt};
|
||||
use super::meta_file::AdjacentMetadata;
|
||||
|
||||
pub fn snapshot_txt(
|
||||
context: &InstanceContext,
|
||||
vfs: &Vfs,
|
||||
path: &Path,
|
||||
name: &str,
|
||||
) -> anyhow::Result<Option<InstanceSnapshot>> {
|
||||
let name = path.file_name_trim_end(".txt")?;
|
||||
|
||||
let contents = vfs.read(path)?;
|
||||
let contents_str = str::from_utf8(&contents)
|
||||
.with_context(|| format!("File was not valid UTF-8: {}", path.display()))?
|
||||
@@ -59,10 +58,14 @@ mod test {
|
||||
|
||||
let mut vfs = Vfs::new(imfs.clone());
|
||||
|
||||
let instance_snapshot =
|
||||
snapshot_txt(&InstanceContext::default(), &mut vfs, Path::new("/foo.txt"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let instance_snapshot = snapshot_txt(
|
||||
&InstanceContext::default(),
|
||||
&mut vfs,
|
||||
Path::new("/foo.txt"),
|
||||
"foo",
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
insta::assert_yaml_snapshot!(instance_snapshot);
|
||||
}
|
||||
|
||||
@@ -59,6 +59,9 @@ gen_build_tests! {
|
||||
txt_in_folder,
|
||||
unresolved_values,
|
||||
weldconstraint,
|
||||
sync_rule_alone,
|
||||
sync_rule_complex,
|
||||
sync_rule_nested_projects,
|
||||
}
|
||||
|
||||
fn run_build_test(test_name: &str) {
|
||||
|
||||
@@ -255,3 +255,54 @@ fn add_optional_folder() {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_rule_alone() {
|
||||
run_serve_test("sync_rule_alone", |session, mut redactions| {
|
||||
let info = session.get_api_rojo().unwrap();
|
||||
let root_id = info.root_instance_id;
|
||||
|
||||
assert_yaml_snapshot!("sync_rule_alone_info", redactions.redacted_yaml(info));
|
||||
|
||||
let read_response = session.get_api_read(root_id).unwrap();
|
||||
assert_yaml_snapshot!(
|
||||
"sync_rule_alone_all",
|
||||
read_response.intern_and_redact(&mut redactions, root_id)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_rule_complex() {
|
||||
run_serve_test("sync_rule_complex", |session, mut redactions| {
|
||||
let info = session.get_api_rojo().unwrap();
|
||||
let root_id = info.root_instance_id;
|
||||
|
||||
assert_yaml_snapshot!("sync_rule_complex_info", redactions.redacted_yaml(info));
|
||||
|
||||
let read_response = session.get_api_read(root_id).unwrap();
|
||||
assert_yaml_snapshot!(
|
||||
"sync_rule_complex_all",
|
||||
read_response.intern_and_redact(&mut redactions, root_id)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_rule_no_extension() {
|
||||
run_serve_test("sync_rule_no_extension", |session, mut redactions| {
|
||||
let info = session.get_api_rojo().unwrap();
|
||||
let root_id = info.root_instance_id;
|
||||
|
||||
assert_yaml_snapshot!(
|
||||
"sync_rule_no_extension_info",
|
||||
redactions.redacted_yaml(info)
|
||||
);
|
||||
|
||||
let read_response = session.get_api_read(root_id).unwrap();
|
||||
assert_yaml_snapshot!(
|
||||
"sync_rule_no_extension_all",
|
||||
read_response.intern_and_redact(&mut redactions, root_id)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user