Переглянути джерело

added support for cargo workspaces for `dev` command (#1827)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
crapStone 4 роки тому
батько
коміт
86a23ff30b

+ 5 - 0
.changes/cli.rs-dev-workspaces.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Watch workspace crates on `dev` command.

+ 7 - 1
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 {

+ 37 - 7
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<String>,
 }
 
+fn read_manifest(manifest_path: &Path) -> crate::Result<Document> {
+  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::<Document>()
+    .with_context(|| "failed to parse Cargo.toml")?;
+
+  Ok(manifest)
+}
+
 fn features_to_vec(features: &Array) -> Vec<String> {
   let mut string_features = Vec::new();
   for feat in features.iter() {
@@ -28,13 +43,7 @@ fn features_to_vec(features: &Array) -> Vec<String> {
 
 pub fn rewrite_manifest(config: ConfigHandle) -> crate::Result<Manifest> {
   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::<Document>()
-    .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<Manifest> {
     features: features_to_vec(&features),
   })
 }
+
+pub fn get_workspace_members() -> crate::Result<Vec<String>> {
+  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![]),
+  }
+}