Skip to content

Commit

Permalink
feat(cli): watch Cargo workspaces in the dev command, closes #4222 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jul 3, 2022
1 parent edb9ab2 commit dbb8c87
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changes/watch-cargo-workspace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Watch for Cargo workspace members in the `dev` file watcher.
48 changes: 36 additions & 12 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use anyhow::Context;
#[cfg(target_os = "linux")]
use heck::ToKebabCase;
use log::warn;
use log::{debug, info};
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
use serde::Deserialize;
use shared_child::SharedChild;
Expand Down Expand Up @@ -461,20 +462,43 @@ impl Rust {
let process = Arc::new(Mutex::new(child));
let (tx, rx) = channel();
let tauri_path = tauri_dir();
let workspace_path = get_workspace_dir(&tauri_path);

let watch_folders = if tauri_path == workspace_path {
vec![tauri_path]
} else {
let cargo_settings = CargoSettings::load(&workspace_path)?;
cargo_settings
.workspace
.as_ref()
.map(|w| {
w.members
.clone()
.unwrap_or_default()
.into_iter()
.map(|p| workspace_path.join(p))
.collect()
})
.unwrap_or_else(|| vec![tauri_path])
};

let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
lookup(&tauri_path, |file_type, path| {
if path != tauri_path {
let _ = watcher.watch(
path,
if file_type.is_dir() {
RecursiveMode::Recursive
} else {
RecursiveMode::NonRecursive
},
);
}
});
for path in watch_folders {
info!("Watching {} for changes...", path.display());
lookup(&path, |file_type, p| {
if p != path {
debug!("Watching {} for changes...", p.display());
let _ = watcher.watch(
p,
if file_type.is_dir() {
RecursiveMode::Recursive
} else {
RecursiveMode::NonRecursive
},
);
}
});
}

loop {
let on_exit = on_exit.clone();
Expand Down

0 comments on commit dbb8c87

Please sign in to comment.