From 86a23ff30b4f18effa39c87b7cae6b7e324d131c Mon Sep 17 00:00:00 2001 From: crapStone Date: Thu, 13 May 2021 21:45:46 +0200 Subject: [PATCH] added support for cargo workspaces for `dev` command (#1827) Co-authored-by: Lucas Nogueira --- .changes/cli.rs-dev-workspaces.md | 5 +++ tooling/cli.rs/src/dev.rs | 8 ++++- tooling/cli.rs/src/helpers/manifest.rs | 44 ++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 .changes/cli.rs-dev-workspaces.md diff --git a/.changes/cli.rs-dev-workspaces.md b/.changes/cli.rs-dev-workspaces.md new file mode 100644 index 000000000..0e84c549d --- /dev/null +++ b/.changes/cli.rs-dev-workspaces.md @@ -0,0 +1,5 @@ +--- +"cli.rs": patch +--- + +Watch workspace crates on `dev` command. diff --git a/tooling/cli.rs/src/dev.rs b/tooling/cli.rs/src/dev.rs index d852b008d..ea8ab1af4 100644 --- a/tooling/cli.rs/src/dev.rs +++ b/tooling/cli.rs/src/dev.rs @@ -5,7 +5,7 @@ use crate::helpers::{ app_paths::{app_dir, tauri_dir}, config::{get as get_config, reload as reload_config}, - manifest::rewrite_manifest, + manifest::{get_workspace_members, rewrite_manifest}, Logger, }; @@ -157,6 +157,12 @@ impl Dev { watcher.watch(tauri_path.join("Cargo.toml"), RecursiveMode::Recursive)?; watcher.watch(tauri_path.join("tauri.conf.json"), RecursiveMode::Recursive)?; + for member in get_workspace_members()? { + let workspace_path = tauri_path.join(member); + watcher.watch(workspace_path.join("src"), RecursiveMode::Recursive)?; + watcher.watch(workspace_path.join("Cargo.toml"), RecursiveMode::Recursive)?; + } + loop { if let Ok(event) = rx.recv() { let event_path = match event { diff --git a/tooling/cli.rs/src/helpers/manifest.rs b/tooling/cli.rs/src/helpers/manifest.rs index 4ce2048de..45a69ab88 100644 --- a/tooling/cli.rs/src/helpers/manifest.rs +++ b/tooling/cli.rs/src/helpers/manifest.rs @@ -10,12 +10,27 @@ use toml_edit::{Array, Document, InlineTable, Item, Value}; use std::{ fs::File, io::{Read, Write}, + path::Path, }; pub struct Manifest { pub features: Vec, } +fn read_manifest(manifest_path: &Path) -> crate::Result { + let mut manifest_str = String::new(); + + let mut manifest_file = File::open(manifest_path) + .with_context(|| format!("failed to open `{:?}` file", manifest_path))?; + manifest_file.read_to_string(&mut manifest_str)?; + + let manifest: Document = manifest_str + .parse::() + .with_context(|| "failed to parse Cargo.toml")?; + + Ok(manifest) +} + fn features_to_vec(features: &Array) -> Vec { let mut string_features = Vec::new(); for feat in features.iter() { @@ -28,13 +43,7 @@ fn features_to_vec(features: &Array) -> Vec { pub fn rewrite_manifest(config: ConfigHandle) -> crate::Result { let manifest_path = tauri_dir().join("Cargo.toml"); - let mut manifest_str = String::new(); - let mut manifest_file = File::open(&manifest_path) - .with_context(|| format!("failed to open `{:?}` file", manifest_path))?; - manifest_file.read_to_string(&mut manifest_str)?; - let mut manifest: Document = manifest_str - .parse::() - .with_context(|| "failed to parse Cargo.toml")?; + let mut manifest = read_manifest(&manifest_path)?; let dependencies = manifest .as_table_mut() .entry("dependencies") @@ -127,3 +136,24 @@ pub fn rewrite_manifest(config: ConfigHandle) -> crate::Result { features: features_to_vec(&features), }) } + +pub fn get_workspace_members() -> crate::Result> { + let mut manifest = read_manifest(&tauri_dir().join("Cargo.toml"))?; + let workspace = manifest.as_table_mut().entry("workspace").as_table_mut(); + + match workspace { + Some(workspace) => { + let members = workspace + .entry("members") + .as_array() + .expect("workspace members aren't an array"); + Ok( + members + .iter() + .map(|v| v.as_str().unwrap().to_string()) + .collect(), + ) + } + None => Ok(vec![]), + } +}