Skip to content

Commit

Permalink
added support for cargo workspaces for dev command (#1827)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
crapStone and lucasfernog committed May 13, 2021
1 parent 754c2e7 commit 86a23ff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changes/cli.rs-dev-workspaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Watch workspace crates on `dev` command.
8 changes: 7 additions & 1 deletion tooling/cli.rs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 37 additions & 7 deletions tooling/cli.rs/src/helpers/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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")
Expand Down Expand Up @@ -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![]),
}
}

0 comments on commit 86a23ff

Please sign in to comment.