Add test model, shore up 'build' command more

This commit is contained in:
Lucien Greathouse
2018-11-27 14:40:19 -08:00
parent c61d6a5804
commit 1e0a7dea73
6 changed files with 38 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
use std::{ use std::{
path::PathBuf, path::PathBuf,
fs::File, fs::File,
io,
fmt, fmt,
}; };
@@ -19,17 +20,12 @@ pub enum OutputKind {
} }
fn detect_output_kind(options: &BuildOptions) -> Option<OutputKind> { fn detect_output_kind(options: &BuildOptions) -> Option<OutputKind> {
match options.output_kind { let extension = options.output_file.extension()?.to_str()?;
Some(output_kind) => Some(output_kind),
None => {
let extension = options.output_file.extension()?.to_str()?;
match extension { match extension {
"rbxlx" => Some(OutputKind::Rbxlx), "rbxlx" => Some(OutputKind::Rbxlx),
"rbxmx" => Some(OutputKind::Rbxmx), "rbxmx" => Some(OutputKind::Rbxmx),
_ => None, _ => None,
}
},
} }
} }
@@ -43,6 +39,7 @@ pub struct BuildOptions {
pub enum BuildError { pub enum BuildError {
UnknownOutputKind, UnknownOutputKind,
ProjectLoadError(ProjectLoadFuzzyError), ProjectLoadError(ProjectLoadFuzzyError),
IoError(io::Error),
} }
impl fmt::Display for BuildError { 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") write!(output, "Could not detect what kind of output file to create")
}, },
BuildError::ProjectLoadError(inner) => write!(output, "{}", inner), BuildError::ProjectLoadError(inner) => write!(output, "{}", inner),
BuildError::IoError(inner) => write!(output, "{}", inner),
} }
} }
} }
@@ -62,8 +60,15 @@ impl From<ProjectLoadFuzzyError> for BuildError {
} }
} }
impl From<io::Error> for BuildError {
fn from(error: io::Error) -> BuildError {
BuildError::IoError(error)
}
}
pub fn build(options: &BuildOptions) -> Result<(), BuildError> { 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)?; .ok_or(BuildError::UnknownOutputKind)?;
info!("Hoping to generate file of type {:?}", output_kind); 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!("Found project at {}", project.file_location.display());
info!("Using project {:#?}", project); info!("Using project {:#?}", project);
let imfs = Imfs::new(&project) let imfs = Imfs::new(&project)?;
.expect("Could not create in-memory filesystem");
let tree = construct_oneoff_tree(&project, &imfs); let tree = construct_oneoff_tree(&project, &imfs);
let mut file = File::create(&options.output_file)?;
let mut file = File::create(&options.output_file)
.expect("Could not open output file for write");
match output_kind { match output_kind {
OutputKind::Rbxmx => { OutputKind::Rbxmx => {
// Model files include the root instance of the tree and all its
// descendants.
let root_id = tree.get_root_id(); let root_id = tree.get_root_id();
rbxmx::encode(&tree, &[root_id], &mut file); rbxmx::encode(&tree, &[root_id], &mut file);
}, },
OutputKind::Rbxlx => { 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 root_id = tree.get_root_id();
let top_level_ids = tree.get_instance(root_id).unwrap().get_children_ids(); let top_level_ids = tree.get_instance(root_id).unwrap().get_children_ids();
rbxmx::encode(&tree, top_level_ids, &mut file); rbxmx::encode(&tree, top_level_ids, &mut file);

View File

@@ -1,5 +1,5 @@
{ {
"name": "empty", "name": "single-sync-point",
"tree": { "tree": {
"$className": "DataModel", "$className": "DataModel",
"ReplicatedStorage": { "ReplicatedStorage": {

View File

@@ -0,0 +1,9 @@
{
"name": "empty",
"tree": {
"$className": "Folder",
"Library": {
"$path": "src"
}
}
}

View File

@@ -0,0 +1,3 @@
local other = require(script.Parent.other)
print(other)

View File

@@ -0,0 +1 @@
return "Hello, world!"