mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-22 05:35:10 +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,
|
||||
|
||||
Reference in New Issue
Block a user