Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions packages/cli/src/cli/autoformat.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(());
Expand Down
30 changes: 2 additions & 28 deletions packages/cli/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 changes: 15 additions & 0 deletions packages/cli/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::error::Error;
use std::{
env,
ffi::OsStr,
fmt::{Display, Formatter},
fs,
path::{Path, PathBuf},
Expand Down Expand Up @@ -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
}
Loading