From 1e0a7dea734f61e9f916d86cbf21086893bebd45 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 27 Nov 2018 14:40:19 -0800 Subject: [PATCH] Add test model, shore up 'build' command more --- server/src/commands/build.rs | 41 +++++++++++-------- test-projects/{foo.json => example.json} | 0 .../single-sync-point/roblox-project.json | 2 +- test-projects/test-model/roblox-project.json | 9 ++++ test-projects/test-model/src/main.lua | 3 ++ test-projects/test-model/src/other.lua | 1 + 6 files changed, 38 insertions(+), 18 deletions(-) rename test-projects/{foo.json => example.json} (100%) create mode 100644 test-projects/test-model/roblox-project.json create mode 100644 test-projects/test-model/src/main.lua create mode 100644 test-projects/test-model/src/other.lua diff --git a/server/src/commands/build.rs b/server/src/commands/build.rs index 9ac990f0..ab57fb6b 100644 --- a/server/src/commands/build.rs +++ b/server/src/commands/build.rs @@ -1,6 +1,7 @@ use std::{ path::PathBuf, fs::File, + io, fmt, }; @@ -19,17 +20,12 @@ pub enum OutputKind { } fn detect_output_kind(options: &BuildOptions) -> Option { - match options.output_kind { - Some(output_kind) => Some(output_kind), - None => { - let extension = options.output_file.extension()?.to_str()?; + let extension = options.output_file.extension()?.to_str()?; - match extension { - "rbxlx" => Some(OutputKind::Rbxlx), - "rbxmx" => Some(OutputKind::Rbxmx), - _ => None, - } - }, + match extension { + "rbxlx" => Some(OutputKind::Rbxlx), + "rbxmx" => Some(OutputKind::Rbxmx), + _ => None, } } @@ -43,6 +39,7 @@ pub struct BuildOptions { pub enum BuildError { UnknownOutputKind, ProjectLoadError(ProjectLoadFuzzyError), + IoError(io::Error), } impl fmt::Display for BuildError { @@ -52,6 +49,7 @@ impl fmt::Display for BuildError { write!(output, "Could not detect what kind of output file to create") }, BuildError::ProjectLoadError(inner) => write!(output, "{}", inner), + BuildError::IoError(inner) => write!(output, "{}", inner), } } } @@ -62,8 +60,15 @@ impl From for BuildError { } } +impl From for BuildError { + fn from(error: io::Error) -> BuildError { + BuildError::IoError(error) + } +} + pub fn build(options: &BuildOptions) -> Result<(), BuildError> { - let output_kind = detect_output_kind(options) + let output_kind = options.output_kind + .or_else(|| detect_output_kind(options)) .ok_or(BuildError::UnknownOutputKind)?; info!("Hoping to generate file of type {:?}", output_kind); @@ -75,20 +80,22 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> { info!("Found project at {}", project.file_location.display()); info!("Using project {:#?}", project); - let imfs = Imfs::new(&project) - .expect("Could not create in-memory filesystem"); - + let imfs = Imfs::new(&project)?; let tree = construct_oneoff_tree(&project, &imfs); - - let mut file = File::create(&options.output_file) - .expect("Could not open output file for write"); + let mut file = File::create(&options.output_file)?; match output_kind { OutputKind::Rbxmx => { + // Model files include the root instance of the tree and all its + // descendants. + let root_id = tree.get_root_id(); rbxmx::encode(&tree, &[root_id], &mut file); }, OutputKind::Rbxlx => { + // Place files don't contain an entry for the DataModel, but our + // RbxTree representation does. + let root_id = tree.get_root_id(); let top_level_ids = tree.get_instance(root_id).unwrap().get_children_ids(); rbxmx::encode(&tree, top_level_ids, &mut file); diff --git a/test-projects/foo.json b/test-projects/example.json similarity index 100% rename from test-projects/foo.json rename to test-projects/example.json diff --git a/test-projects/single-sync-point/roblox-project.json b/test-projects/single-sync-point/roblox-project.json index 2ec428cc..d3970b9c 100644 --- a/test-projects/single-sync-point/roblox-project.json +++ b/test-projects/single-sync-point/roblox-project.json @@ -1,5 +1,5 @@ { - "name": "empty", + "name": "single-sync-point", "tree": { "$className": "DataModel", "ReplicatedStorage": { diff --git a/test-projects/test-model/roblox-project.json b/test-projects/test-model/roblox-project.json new file mode 100644 index 00000000..503a7d25 --- /dev/null +++ b/test-projects/test-model/roblox-project.json @@ -0,0 +1,9 @@ +{ + "name": "empty", + "tree": { + "$className": "Folder", + "Library": { + "$path": "src" + } + } +} \ No newline at end of file diff --git a/test-projects/test-model/src/main.lua b/test-projects/test-model/src/main.lua new file mode 100644 index 00000000..506c4701 --- /dev/null +++ b/test-projects/test-model/src/main.lua @@ -0,0 +1,3 @@ +local other = require(script.Parent.other) + +print(other) \ No newline at end of file diff --git a/test-projects/test-model/src/other.lua b/test-projects/test-model/src/other.lua new file mode 100644 index 00000000..4c06176d --- /dev/null +++ b/test-projects/test-model/src/other.lua @@ -0,0 +1 @@ +return "Hello, world!" \ No newline at end of file