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::{
path::PathBuf,
fs::File,
io,
fmt,
};
@@ -19,17 +20,12 @@ pub enum OutputKind {
}
fn detect_output_kind(options: &BuildOptions) -> Option<OutputKind> {
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<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> {
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);

View File

@@ -1,5 +1,5 @@
{
"name": "empty",
"name": "single-sync-point",
"tree": {
"$className": "DataModel",
"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!"