Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'rojo build --watch' #284

Merged
merged 3 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ serde_json = "1.0"
snafu = "0.6.0"
structopt = "0.3.5"
termcolor = "1.0.5"
tokio = "0.1.22"
uuid = { version = "0.8.1", features = ["v4", "serde"] }

[target.'cfg(windows)'.dependencies]
Expand All @@ -89,5 +90,4 @@ paste = "0.1"
pretty_assertions = "0.6.1"
serde_yaml = "0.8.9"
tempfile = "3.0"
tokio = "0.1.22"
walkdir = "2.1"
47 changes: 38 additions & 9 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use std::{
};

use snafu::{ResultExt, Snafu};
use tokio::runtime::Runtime;

use crate::{
cli::BuildCommand,
common_setup,
project::ProjectError,
serve_session::ServeSession,
snapshot::RojoTree,
vfs::{RealFetcher, Vfs, WatchMode},
};

Expand Down Expand Up @@ -71,18 +73,47 @@ pub fn build(options: BuildCommand) -> Result<(), BuildError> {
}

fn build_inner(options: BuildCommand) -> Result<(), Error> {
let output_kind = detect_output_kind(&options).ok_or(Error::UnknownOutputKind)?;
log::trace!("Constructing in-memory filesystem");

log::debug!("Hoping to generate file of type {:?}", output_kind);
let watch_mode = if options.watch {
WatchMode::Enabled
} else {
WatchMode::Disabled
};

log::trace!("Constructing in-memory filesystem");
let vfs = Vfs::new(RealFetcher::new(WatchMode::Disabled));
let vfs = Vfs::new(RealFetcher::new(watch_mode));

let session = ServeSession::new(vfs, &options.absolute_project());
let mut cursor = session.message_queue().cursor();

{
let tree = session.tree();
write_model(&tree, &options)?;
}

if options.watch {
let mut rt = Runtime::new().unwrap();

loop {
let receiver = session.message_queue().subscribe(cursor);
let (new_cursor, _patch_set) = rt.block_on(receiver).unwrap();
cursor = new_cursor;

let tree = session.tree();
write_model(&tree, &options)?;
}
}

Ok(())
}

fn write_model(tree: &RojoTree, options: &BuildCommand) -> Result<(), Error> {
let output_kind = detect_output_kind(&options).ok_or(Error::UnknownOutputKind)?;
log::debug!("Hoping to generate file of type {:?}", output_kind);

let (_maybe_project, tree) = common_setup::start(&options.absolute_project(), &vfs);
let root_id = tree.get_root_id();

log::trace!("Opening output file for write");

let file = File::create(&options.output).context(Io)?;
let mut file = BufWriter::new(file);

Expand Down Expand Up @@ -121,7 +152,5 @@ fn build_inner(options: BuildCommand) -> Result<(), Error> {

file.flush().context(Io)?;

log::trace!("Done!");

Ok(())
}
4 changes: 4 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ pub struct BuildCommand {
/// Where to output the result.
#[structopt(long, short)]
pub output: PathBuf,

/// Whether to automatically rebuild when any input files change.
#[structopt(long)]
pub watch: bool,
}

impl BuildCommand {
Expand Down