Skip to content

Commit

Permalink
Add --watch to sourcemap generation (rojo-rbx#602)
Browse files Browse the repository at this point in the history
* Implement watch argument

* Add forget call

* Clippy fixes

* Update changelog
  • Loading branch information
JohnnyMorganz authored Aug 1, 2022
1 parent 4027072 commit 45c8a05
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Rojo Changelog

## Unreleased Changes
* Added `--watch` flag to `rojo sourcemap` ([#602])

[#602]: https://github.com/rojo-rbx/rojo/pull/602

## [7.2.1] - July 8, 2022
* Fixed notification sound by changing it to a generic sound. ([#566])
Expand Down
65 changes: 50 additions & 15 deletions src/cli/sourcemap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io::{BufWriter, Write},
mem::forget,
path::{Path, PathBuf},
};

Expand All @@ -8,6 +9,7 @@ use fs_err::File;
use memofs::Vfs;
use rbx_dom_weak::types::Ref;
use serde::Serialize;
use tokio::runtime::Runtime;

use crate::{
serve_session::ServeSession,
Expand Down Expand Up @@ -50,6 +52,10 @@ pub struct SourcemapCommand {
/// If non-script files should be included or not. Defaults to false.
#[clap(long)]
pub include_non_scripts: bool,

/// Whether to automatically recreate a snapshot when any input files change.
#[clap(long)]
pub watch: bool,
}

impl SourcemapCommand {
Expand All @@ -58,29 +64,35 @@ impl SourcemapCommand {

log::trace!("Constructing in-memory filesystem");
let vfs = Vfs::new_default();
vfs.set_watch_enabled(self.watch);

let session = ServeSession::new(vfs, &project_path)?;
let tree = session.tree();
let mut cursor = session.message_queue().cursor();

let filter = if self.include_non_scripts {
filter_nothing
} else {
filter_non_scripts
};

let root_node = recurse_create_node(&tree, tree.get_root_id(), session.root_dir(), filter);
write_sourcemap(&session, self.output.as_deref(), filter)?;

if let Some(output_path) = self.output {
let mut file = BufWriter::new(File::create(&output_path)?);
serde_json::to_writer(&mut file, &root_node)?;
file.flush()?;
if self.watch {
let rt = Runtime::new().unwrap();

println!("Created sourcemap at {}", output_path.display());
} else {
let output = serde_json::to_string(&root_node)?;
println!("{}", output);
loop {
let receiver = session.message_queue().subscribe(cursor);
let (new_cursor, _patch_set) = rt.block_on(receiver).unwrap();
cursor = new_cursor;

write_sourcemap(&session, self.output.as_deref(), filter)?;
}
}

// Avoid dropping ServeSession: it's potentially VERY expensive to drop
// and we're about to exit anyways.
forget(session);

Ok(())
}
}
Expand All @@ -90,10 +102,10 @@ fn filter_nothing(_instance: &InstanceWithMeta) -> bool {
}

fn filter_non_scripts(instance: &InstanceWithMeta) -> bool {
match instance.class_name() {
"Script" | "LocalScript" | "ModuleScript" => true,
_ => false,
}
matches!(
instance.class_name(),
"Script" | "LocalScript" | "ModuleScript"
)
}

fn recurse_create_node(
Expand All @@ -106,7 +118,7 @@ fn recurse_create_node(

let mut children = Vec::new();
for &child_id in instance.children() {
if let Some(child_node) = recurse_create_node(tree, child_id, &project_dir, filter) {
if let Some(child_node) = recurse_create_node(tree, child_id, project_dir, filter) {
children.push(child_node);
}
}
Expand Down Expand Up @@ -134,3 +146,26 @@ fn recurse_create_node(
children,
})
}

fn write_sourcemap(
session: &ServeSession,
output: Option<&Path>,
filter: fn(&InstanceWithMeta) -> bool,
) -> anyhow::Result<()> {
let tree = session.tree();

let root_node = recurse_create_node(&tree, tree.get_root_id(), session.root_dir(), filter);

if let Some(output_path) = output {
let mut file = BufWriter::new(File::create(&output_path)?);
serde_json::to_writer(&mut file, &root_node)?;
file.flush()?;

println!("Created sourcemap at {}", output_path.display());
} else {
let output = serde_json::to_string(&root_node)?;
println!("{}", output);
}

Ok(())
}

0 comments on commit 45c8a05

Please sign in to comment.