Skip to content

Commit

Permalink
perf(cli.rs): improve performance of tauri dir lookup reading .gitign…
Browse files Browse the repository at this point in the history
…ore (#3405)
  • Loading branch information
lucasfernog authored Feb 11, 2022
1 parent 989ee4e commit 9c6c5a8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changes/perf-cli-dir-lookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Respect `.gitignore` configuration when looking for the folder with the `tauri.conf.json` file.
50 changes: 50 additions & 0 deletions tooling/cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ heck = "0.4"
dialoguer = "0.9"
url = { version = "2.2", features = [ "serde" ] }
os_pipe = "1"
ignore = "0.4"

[target."cfg(windows)".dependencies]
encode_unicode = "0.3"
Expand Down
50 changes: 29 additions & 21 deletions tooling/cli/src/helpers/app_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{env::current_dir, path::PathBuf};
use std::{
env::current_dir,
ffi::OsStr,
path::{Path, PathBuf},
};

use ignore::Walk;
use once_cell::sync::Lazy;

fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
for entry in Walk::new(dir).flatten() {
let path = dir.join(entry.path());
if checker(&path) {
return Some(path);
}
}
None
}

fn get_tauri_dir() -> PathBuf {
glob::glob(
&current_dir()
.expect("failed to read cwd")
.join("**/tauri.conf.json")
.to_string_lossy(),
)
.unwrap()
.filter_map(Result::ok)
.last()
lookup(&current_dir().expect("failed to read cwd"), |path| if let Some(file_name) = path.file_name() {
file_name == OsStr::new("tauri.conf.json") || file_name == OsStr::new("tauri.conf.json5")
} else {
false
})
.map(|p| p.parent().unwrap().to_path_buf())
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` file in any subfolder.")
.expect("Couldn't recognize the current folder as a Tauri project. It must contain a `tauri.conf.json` or `tauri.conf.json5` file in any subfolder.")
}

fn get_app_dir() -> Option<PathBuf> {
glob::glob(
&current_dir()
.expect("failed to read cwd")
.join("**/package.json")
.to_string_lossy(),
)
.unwrap()
.filter_map(Result::ok)
.filter(|p| !p.to_string_lossy().into_owned().contains("node_modules"))
.last()
lookup(&current_dir().expect("failed to read cwd"), |path| {
if let Some(file_name) = path.file_name() {
file_name == OsStr::new("package.json")
} else {
false
}
})
.map(|p| p.parent().unwrap().to_path_buf())
}

Expand Down

0 comments on commit 9c6c5a8

Please sign in to comment.