瀏覽代碼

fix: dx check now respects files to ignore (e.g. .gitignore) (#4038)

closes #3751
silentvoid 2 月之前
父節點
當前提交
74789c4b13
共有 3 個文件被更改,包括 19 次插入44 次删除
  1. 2 16
      packages/cli/src/cli/autoformat.rs
  2. 2 28
      packages/cli/src/cli/check.rs
  3. 15 0
      packages/cli/src/metadata.rs

+ 2 - 16
packages/cli/src/cli/autoformat.rs

@@ -1,5 +1,5 @@
 use super::*;
-use crate::DioxusCrate;
+use crate::{metadata::collect_rs_files, DioxusCrate};
 use anyhow::Context;
 use dioxus_autofmt::{IndentOptions, IndentType};
 use rayon::prelude::*;
@@ -126,20 +126,6 @@ fn refactor_file(
     Ok(())
 }
 
-use std::ffi::OsStr;
-fn get_project_files(dir: impl AsRef<Path>) -> Vec<PathBuf> {
-    let mut files = Vec::new();
-    for result in ignore::Walk::new(dir) {
-        let path = result.unwrap().into_path();
-        if let Some(ext) = path.extension() {
-            if ext == OsStr::new("rs") {
-                files.push(path);
-            }
-        }
-    }
-    files
-}
-
 fn format_file(
     path: impl AsRef<Path>,
     indent: IndentOptions,
@@ -185,7 +171,7 @@ fn autoformat_project(
     format_rust_code: bool,
     dir: impl AsRef<Path>,
 ) -> Result<()> {
-    let files_to_format = get_project_files(dir);
+    let files_to_format = collect_rs_files(dir);
 
     if files_to_format.is_empty() {
         return Ok(());

+ 2 - 28
packages/cli/src/cli/check.rs

@@ -4,10 +4,9 @@
 //! https://github.com/rust-lang/rustfmt/blob/master/src/bin/main.rs
 
 use super::*;
-use crate::DioxusCrate;
+use crate::{metadata::collect_rs_files, DioxusCrate};
 use anyhow::Context;
 use futures_util::{stream::FuturesUnordered, StreamExt};
-use std::path::Path;
 
 /// Check the Rust files in the project for issues.
 #[derive(Clone, Debug, Parser)]
@@ -54,7 +53,7 @@ async fn check_file_and_report(path: PathBuf) -> Result<()> {
 /// Doesn't do mod-descending, so it will still try to check unreachable files. TODO.
 async fn check_project_and_report(dioxus_crate: DioxusCrate) -> Result<()> {
     let mut files_to_check = vec![dioxus_crate.main_source_file()];
-    collect_rs_files(&dioxus_crate.crate_dir(), &mut files_to_check);
+    files_to_check.extend(collect_rs_files(dioxus_crate.crate_dir()));
     check_files_and_report(files_to_check).await
 }
 
@@ -106,28 +105,3 @@ async fn check_files_and_report(files_to_check: Vec<PathBuf>) -> Result<()> {
         _ => Err(format!("{} issues found.", total_issues).into()),
     }
 }
-
-fn collect_rs_files(folder: &Path, files: &mut Vec<PathBuf>) {
-    let Ok(folder) = folder.read_dir() else {
-        return;
-    };
-
-    // load the gitignore
-    for entry in folder {
-        let Ok(entry) = entry else {
-            continue;
-        };
-
-        let path = entry.path();
-
-        if path.is_dir() {
-            collect_rs_files(&path, files);
-        }
-
-        if let Some(ext) = path.extension() {
-            if ext == "rs" {
-                files.push(path);
-            }
-        }
-    }
-}

+ 15 - 0
packages/cli/src/metadata.rs

@@ -2,6 +2,7 @@
 use std::error::Error;
 use std::{
     env,
+    ffi::OsStr,
     fmt::{Display, Formatter},
     fs,
     path::{Path, PathBuf},
@@ -62,3 +63,17 @@ fn contains_manifest(path: &Path) -> bool {
         })
         .unwrap_or(false)
 }
+
+/// Collects all `.rs` files in the provided directory, respecting files to ignore (e.g. `.gitignore`)
+pub(crate) fn collect_rs_files(dir: impl AsRef<Path>) -> Vec<PathBuf> {
+    let mut files = Vec::new();
+    for result in ignore::Walk::new(dir) {
+        let path = result.unwrap().into_path();
+        if let Some(ext) = path.extension() {
+            if ext == OsStr::new("rs") {
+                files.push(path);
+            }
+        }
+    }
+    files
+}