Skip to content

Commit 45c8a05

Browse files
Add --watch to sourcemap generation (rojo-rbx#602)
* Implement watch argument * Add forget call * Clippy fixes * Update changelog
1 parent 4027072 commit 45c8a05

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Rojo Changelog
22

33
## Unreleased Changes
4+
* Added `--watch` flag to `rojo sourcemap` ([#602])
5+
6+
[#602]: https://github.com/rojo-rbx/rojo/pull/602
47

58
## [7.2.1] - July 8, 2022
69
* Fixed notification sound by changing it to a generic sound. ([#566])

src/cli/sourcemap.rs

+50-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
io::{BufWriter, Write},
3+
mem::forget,
34
path::{Path, PathBuf},
45
};
56

@@ -8,6 +9,7 @@ use fs_err::File;
89
use memofs::Vfs;
910
use rbx_dom_weak::types::Ref;
1011
use serde::Serialize;
12+
use tokio::runtime::Runtime;
1113

1214
use crate::{
1315
serve_session::ServeSession,
@@ -50,6 +52,10 @@ pub struct SourcemapCommand {
5052
/// If non-script files should be included or not. Defaults to false.
5153
#[clap(long)]
5254
pub include_non_scripts: bool,
55+
56+
/// Whether to automatically recreate a snapshot when any input files change.
57+
#[clap(long)]
58+
pub watch: bool,
5359
}
5460

5561
impl SourcemapCommand {
@@ -58,29 +64,35 @@ impl SourcemapCommand {
5864

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

6269
let session = ServeSession::new(vfs, &project_path)?;
63-
let tree = session.tree();
70+
let mut cursor = session.message_queue().cursor();
6471

6572
let filter = if self.include_non_scripts {
6673
filter_nothing
6774
} else {
6875
filter_non_scripts
6976
};
7077

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

73-
if let Some(output_path) = self.output {
74-
let mut file = BufWriter::new(File::create(&output_path)?);
75-
serde_json::to_writer(&mut file, &root_node)?;
76-
file.flush()?;
80+
if self.watch {
81+
let rt = Runtime::new().unwrap();
7782

78-
println!("Created sourcemap at {}", output_path.display());
79-
} else {
80-
let output = serde_json::to_string(&root_node)?;
81-
println!("{}", output);
83+
loop {
84+
let receiver = session.message_queue().subscribe(cursor);
85+
let (new_cursor, _patch_set) = rt.block_on(receiver).unwrap();
86+
cursor = new_cursor;
87+
88+
write_sourcemap(&session, self.output.as_deref(), filter)?;
89+
}
8290
}
8391

92+
// Avoid dropping ServeSession: it's potentially VERY expensive to drop
93+
// and we're about to exit anyways.
94+
forget(session);
95+
8496
Ok(())
8597
}
8698
}
@@ -90,10 +102,10 @@ fn filter_nothing(_instance: &InstanceWithMeta) -> bool {
90102
}
91103

92104
fn filter_non_scripts(instance: &InstanceWithMeta) -> bool {
93-
match instance.class_name() {
94-
"Script" | "LocalScript" | "ModuleScript" => true,
95-
_ => false,
96-
}
105+
matches!(
106+
instance.class_name(),
107+
"Script" | "LocalScript" | "ModuleScript"
108+
)
97109
}
98110

99111
fn recurse_create_node(
@@ -106,7 +118,7 @@ fn recurse_create_node(
106118

107119
let mut children = Vec::new();
108120
for &child_id in instance.children() {
109-
if let Some(child_node) = recurse_create_node(tree, child_id, &project_dir, filter) {
121+
if let Some(child_node) = recurse_create_node(tree, child_id, project_dir, filter) {
110122
children.push(child_node);
111123
}
112124
}
@@ -134,3 +146,26 @@ fn recurse_create_node(
134146
children,
135147
})
136148
}
149+
150+
fn write_sourcemap(
151+
session: &ServeSession,
152+
output: Option<&Path>,
153+
filter: fn(&InstanceWithMeta) -> bool,
154+
) -> anyhow::Result<()> {
155+
let tree = session.tree();
156+
157+
let root_node = recurse_create_node(&tree, tree.get_root_id(), session.root_dir(), filter);
158+
159+
if let Some(output_path) = output {
160+
let mut file = BufWriter::new(File::create(&output_path)?);
161+
serde_json::to_writer(&mut file, &root_node)?;
162+
file.flush()?;
163+
164+
println!("Created sourcemap at {}", output_path.display());
165+
} else {
166+
let output = serde_json::to_string(&root_node)?;
167+
println!("{}", output);
168+
}
169+
170+
Ok(())
171+
}

0 commit comments

Comments
 (0)