mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-20 20:55:50 +00:00
Do a bit of tinkering with instance names relative to files and partitions
This commit is contained in:
@@ -13,6 +13,7 @@ pub struct FileRoute {
|
||||
impl FileRoute {
|
||||
pub fn from_path(path: &Path, partition: &Partition) -> Option<FileRoute> {
|
||||
assert!(path.is_absolute());
|
||||
assert!(path.starts_with(&partition.path));
|
||||
|
||||
let relative_path = path.strip_prefix(&partition.path).ok()?;
|
||||
let mut route = Vec::new();
|
||||
@@ -70,20 +71,96 @@ impl FileRoute {
|
||||
result
|
||||
}
|
||||
|
||||
/// This function is totally wrong and should be handled by middleware, heh.
|
||||
pub fn name(&self, partition: &Partition) -> String { // I guess??
|
||||
pub fn file_name(&self, partition: &Partition) -> String {
|
||||
if self.route.len() == 0 {
|
||||
// This FileRoute refers to the partition itself
|
||||
|
||||
if partition.target.len() == 0 {
|
||||
// We're targeting the game!
|
||||
"game".to_string()
|
||||
} else {
|
||||
partition.target.last().unwrap().clone()
|
||||
}
|
||||
partition.path.file_name().unwrap().to_str().unwrap().to_string()
|
||||
} else {
|
||||
// This FileRoute refers to an item in a partition
|
||||
self.route.last().unwrap().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg(windows)]
|
||||
const ROOT_PATH: &'static str = "C:\\";
|
||||
|
||||
#[cfg(not(windows))]
|
||||
const ROOT_PATH: &'static str = "/";
|
||||
|
||||
#[test]
|
||||
fn from_path_empty() {
|
||||
let path = Path::new(ROOT_PATH).join("a/b/c");
|
||||
|
||||
let partition = Partition {
|
||||
name: "foo".to_string(),
|
||||
path: path.clone(),
|
||||
target: vec![],
|
||||
};
|
||||
|
||||
let route = FileRoute::from_path(&path, &partition);
|
||||
|
||||
assert_eq!(route, Some(FileRoute {
|
||||
partition: "foo".to_string(),
|
||||
route: vec![],
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_path_non_empty() {
|
||||
let partition_path = Path::new(ROOT_PATH).join("a/b/c");
|
||||
|
||||
let inside_path = partition_path.join("d");
|
||||
|
||||
let partition = Partition {
|
||||
name: "bar".to_string(),
|
||||
path: partition_path,
|
||||
target: vec![],
|
||||
};
|
||||
|
||||
let route = FileRoute::from_path(&inside_path, &partition);
|
||||
|
||||
assert_eq!(route, Some(FileRoute {
|
||||
partition: "bar".to_string(),
|
||||
route: vec!["d".to_string()],
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_name_empty_route() {
|
||||
let partition_path = Path::new(ROOT_PATH).join("a/b/c");
|
||||
|
||||
let partition = Partition {
|
||||
name: "bar".to_string(),
|
||||
path: partition_path,
|
||||
target: vec![],
|
||||
};
|
||||
|
||||
let route = FileRoute {
|
||||
partition: "bar".to_string(),
|
||||
route: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(route.file_name(&partition), "c");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_name_non_empty_route() {
|
||||
let partition_path = Path::new(ROOT_PATH).join("a/b/c");
|
||||
|
||||
let partition = Partition {
|
||||
name: "bar".to_string(),
|
||||
path: partition_path,
|
||||
target: vec![],
|
||||
};
|
||||
|
||||
let route = FileRoute {
|
||||
partition: "bar".to_string(),
|
||||
route: vec!["foo".to_string(), "hello.lua".to_string()],
|
||||
};
|
||||
|
||||
assert_eq!(route.file_name(&partition), "hello.lua");
|
||||
}
|
||||
}
|
||||
@@ -74,19 +74,47 @@ fn file_to_instances(
|
||||
None => partition.path.file_name().unwrap().to_str().unwrap().to_string()
|
||||
};
|
||||
|
||||
let use_partition_name = route.route.len() == 0;
|
||||
|
||||
let partition_name = partition.target.last().unwrap();
|
||||
|
||||
fn strip_suffix<'a>(source: &'a str, suffix: &'static str) -> String {
|
||||
source[..source.len() - suffix.len()].to_string()
|
||||
}
|
||||
|
||||
if file_name.ends_with(".client.lua") {
|
||||
("LocalScript", "Source", strip_suffix(&file_name, ".client.lua"))
|
||||
let name = if use_partition_name {
|
||||
partition_name.clone()
|
||||
} else {
|
||||
strip_suffix(&file_name, ".client.lua")
|
||||
};
|
||||
|
||||
("LocalScript", "Source", name)
|
||||
} else if file_name.ends_with(".server.lua") {
|
||||
("Script", "Source", strip_suffix(&file_name, ".server.lua"))
|
||||
let name = if use_partition_name {
|
||||
partition_name.clone()
|
||||
} else {
|
||||
strip_suffix(&file_name, ".server.lua")
|
||||
};
|
||||
|
||||
("Script", "Source", name)
|
||||
} else if file_name.ends_with(".lua") {
|
||||
("ModuleScript", "Source", strip_suffix(&file_name, ".lua"))
|
||||
let name = if use_partition_name {
|
||||
partition_name.clone()
|
||||
} else {
|
||||
strip_suffix(&file_name, ".lua")
|
||||
};
|
||||
|
||||
("ModuleScript", "Source", name)
|
||||
} else {
|
||||
let name = if use_partition_name {
|
||||
partition_name.clone()
|
||||
} else {
|
||||
file_name
|
||||
};
|
||||
|
||||
// TODO: Error/warn/skip instead of falling back
|
||||
("StringValue", "Value", file_name)
|
||||
("StringValue", "Value", name)
|
||||
}
|
||||
};
|
||||
|
||||
@@ -132,7 +160,11 @@ fn file_to_instances(
|
||||
|
||||
let class_name = get_partition_target_class_name(&route.route).to_string();
|
||||
|
||||
let name = route.name(partition);
|
||||
let name = if route.route.len() == 0 {
|
||||
partition.target.last().unwrap().clone()
|
||||
} else {
|
||||
route.file_name(partition)
|
||||
};
|
||||
|
||||
tree.insert_instance(primary_id, RbxInstance {
|
||||
name,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
-- foo.lua
|
||||
@@ -4,7 +4,7 @@
|
||||
"partitions": {
|
||||
"lib": {
|
||||
"path": "foo.lua",
|
||||
"target": "ReplicatedStorage.foo"
|
||||
"target": "ReplicatedStorage.bar"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -390,4 +390,62 @@ fn one_partition() {
|
||||
}
|
||||
|
||||
// TODO: Test to change existing instance
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partition_to_file() {
|
||||
let original_project_path = {
|
||||
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
path.push("test-projects/partition-to-file");
|
||||
path
|
||||
};
|
||||
|
||||
let project_tempdir = tempfile::tempdir().unwrap();
|
||||
let project_path = project_tempdir.path();
|
||||
|
||||
copy_recursive(&original_project_path, &project_path).unwrap();
|
||||
|
||||
let project = Project::load(&project_path).unwrap();
|
||||
let mut session = Session::new(project.clone());
|
||||
session.start();
|
||||
|
||||
let web_config = WebConfig::from_session(0, project.serve_port, &session);
|
||||
let server = Server::new(web_config);
|
||||
|
||||
{
|
||||
let body = server.get_string("/api/rojo");
|
||||
let response = serde_json::from_str::<ServerInfoResponse>(&body).unwrap();
|
||||
|
||||
assert_eq!(response.server_id, "0");
|
||||
assert_eq!(response.protocol_version, 2);
|
||||
assert_eq!(response.partitions.len(), 1);
|
||||
}
|
||||
|
||||
{
|
||||
let body = server.get_string("/api/read_all");
|
||||
let response = serde_json::from_str::<ReadAllResponse>(&body).unwrap();
|
||||
|
||||
assert_eq!(response.server_id, "0");
|
||||
assert_eq!(response.message_cursor, -1);
|
||||
assert_eq!(response.instances.len(), 1);
|
||||
|
||||
let instance = response.instances.values().next().unwrap();
|
||||
|
||||
assert_eq!(instance.name, "bar");
|
||||
assert_eq!(instance.class_name, "ModuleScript");
|
||||
assert_eq!(instance.properties.get("Source"), Some(&RbxValue::String { value: "-- foo.lua".to_string() }));
|
||||
assert_eq!(instance.children.len(), 0);
|
||||
assert_eq!(instance.parent, None);
|
||||
|
||||
let body = server.get_string("/api/read/0");
|
||||
let response = serde_json::from_str::<ReadResponse>(&body).unwrap();
|
||||
|
||||
assert_eq!(response.server_id, "0");
|
||||
assert_eq!(response.message_cursor, -1);
|
||||
assert_eq!(response.instances.len(), 1);
|
||||
|
||||
let single_instance = response.instances.values().next().unwrap();
|
||||
|
||||
assert_eq!(&Cow::Borrowed(instance), single_instance);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user