Add --plugin_autostart flag to rojo build, adds special marker to output

This commit is contained in:
Lucien Greathouse 2019-04-22 18:02:08 -07:00
parent 1d2d2a85d1
commit 9e682c7f37
2 changed files with 30 additions and 1 deletions

View file

@ -50,6 +50,7 @@ fn main() {
(about: "Generates an rbxmx model file from the project.") (about: "Generates an rbxmx model file from the project.")
(@arg PROJECT: "Path to the project to serve. Defaults to the current directory.") (@arg PROJECT: "Path to the project to serve. Defaults to the current directory.")
(@arg output: --output -o +takes_value +required "Where to output the result.") (@arg output: --output -o +takes_value +required "Where to output the result.")
(@arg plugin_autostart: --plugin_autostart "Makes the output place automatically connect to Rojo on open.")
) )
(@subcommand upload => (@subcommand upload =>
@ -150,11 +151,13 @@ fn start_build(sub_matches: &ArgMatches) {
}; };
let output_file = make_path_absolute(Path::new(sub_matches.value_of("output").unwrap())); let output_file = make_path_absolute(Path::new(sub_matches.value_of("output").unwrap()));
let plugin_autostart = sub_matches.is_present("plugin_autostart");
let options = commands::BuildOptions { let options = commands::BuildOptions {
fuzzy_project_path, fuzzy_project_path,
output_file, output_file,
output_kind: None, // TODO: Accept from argument output_kind: None, // TODO: Accept from argument
plugin_autostart,
}; };
match commands::build(&options) { match commands::build(&options) {

View file

@ -1,9 +1,11 @@
use std::{ use std::{
collections::HashMap,
path::PathBuf, path::PathBuf,
fs::File, fs::File,
io::{self, Write, BufWriter}, io::{self, Write, BufWriter},
}; };
use rbx_dom_weak::{RbxTree, RbxInstanceProperties, RbxValue};
use log::info; use log::info;
use failure::Fail; use failure::Fail;
@ -39,6 +41,7 @@ pub struct BuildOptions {
pub fuzzy_project_path: PathBuf, pub fuzzy_project_path: PathBuf,
pub output_file: PathBuf, pub output_file: PathBuf,
pub output_kind: Option<OutputKind>, pub output_kind: Option<OutputKind>,
pub plugin_autostart: bool,
} }
#[derive(Debug, Fail)] #[derive(Debug, Fail)]
@ -91,7 +94,7 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
let mut imfs = Imfs::new(); let mut imfs = Imfs::new();
imfs.add_roots_from_project(&project)?; imfs.add_roots_from_project(&project)?;
let tree = construct_oneoff_tree(&project, &imfs)?; let mut tree = construct_oneoff_tree(&project, &imfs)?;
let mut file = BufWriter::new(File::create(&options.output_file)?); let mut file = BufWriter::new(File::create(&options.output_file)?);
match output_kind { match output_kind {
@ -106,6 +109,10 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
// Place files don't contain an entry for the DataModel, but our // Place files don't contain an entry for the DataModel, but our
// RbxTree representation does. // RbxTree representation does.
if options.plugin_autostart {
inject_autostart_marker(&mut tree);
}
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();
rbx_xml::encode(&tree, top_level_ids, &mut file)?; rbx_xml::encode(&tree, top_level_ids, &mut file)?;
@ -115,6 +122,10 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
rbx_binary::encode(&tree, &[root_id], &mut file)?; rbx_binary::encode(&tree, &[root_id], &mut file)?;
}, },
OutputKind::Rbxl => { OutputKind::Rbxl => {
if options.plugin_autostart {
inject_autostart_marker(&mut tree);
}
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();
rbx_binary::encode(&tree, top_level_ids, &mut file)?; rbx_binary::encode(&tree, top_level_ids, &mut file)?;
@ -125,3 +136,18 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
Ok(()) Ok(())
} }
fn inject_autostart_marker(tree: &mut RbxTree) {
let root_id = tree.get_root_id();
let mut properties = HashMap::new();
properties.insert(String::from("Value"), RbxValue::Int64 { value: 34872 });
let marker = RbxInstanceProperties {
class_name: String::from("IntValue"),
name: String::from("ROJO_AUTOSTART_PORT"),
properties,
};
tree.insert_instance(marker, root_id);
}