소스 검색

perf(cli.rs): improve performance of tauri dir lookup reading .gitignore (#3405)

Lucas Fernandes Nogueira 3 년 전
부모
커밋
9c6c5a8c52
4개의 변경된 파일85개의 추가작업 그리고 21개의 파일을 삭제
  1. 5 0
      .changes/perf-cli-dir-lookup.md
  2. 50 0
      tooling/cli/Cargo.lock
  3. 1 0
      tooling/cli/Cargo.toml
  4. 29 21
      tooling/cli/src/helpers/app_paths.rs

+ 5 - 0
.changes/perf-cli-dir-lookup.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Respect `.gitignore` configuration when looking for the folder with the `tauri.conf.json` file.

+ 50 - 0
tooling/cli/Cargo.lock

@@ -162,6 +162,15 @@ dependencies = [
  "byte-tools",
 ]
 
+[[package]]
+name = "bstr"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
+dependencies = [
+ "memchr",
+]
+
 [[package]]
 name = "bumpalo"
 version = "3.9.1"
@@ -936,6 +945,19 @@ version = "0.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
 
+[[package]]
+name = "globset"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "10463d9ff00a2a068db14231982f5132edebad0d7660cd956a1c30292dbcbfbd"
+dependencies = [
+ "aho-corasick",
+ "bstr",
+ "fnv",
+ "log",
+ "regex",
+]
+
 [[package]]
 name = "half"
 version = "1.8.2"
@@ -1050,6 +1072,24 @@ dependencies = [
  "unicode-normalization",
 ]
 
+[[package]]
+name = "ignore"
+version = "0.4.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "713f1b139373f96a2e0ce3ac931cd01ee973c3c5dd7c40c0c2efe96ad2b6751d"
+dependencies = [
+ "crossbeam-utils",
+ "globset",
+ "lazy_static",
+ "log",
+ "memchr",
+ "regex",
+ "same-file",
+ "thread_local",
+ "walkdir",
+ "winapi-util",
+]
+
 [[package]]
 name = "image"
 version = "0.24.0"
@@ -2699,6 +2739,7 @@ dependencies = [
  "handlebars",
  "heck",
  "humansize",
+ "ignore",
  "include_dir",
  "json-patch",
  "lazy_static",
@@ -2839,6 +2880,15 @@ dependencies = [
  "syn",
 ]
 
+[[package]]
+name = "thread_local"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
+dependencies = [
+ "once_cell",
+]
+
 [[package]]
 name = "threadpool"
 version = "1.8.1"

+ 1 - 0
tooling/cli/Cargo.toml

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

+ 29 - 21
tooling/cli/src/helpers/app_paths.rs

@@ -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())
 }