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

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
crapStone 2021-05-13 21:45:46 +02:00 committed by GitHub
parent 754c2e766a
commit 86a23ff30b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 8 deletions

View File

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

View File

@ -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 {

View File

@ -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![]),
}
}