Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add exclude patterns support #133

Merged
merged 4 commits into from
Aug 1, 2024
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Options:
Maximum width of each line
-t, --tab-spaces <TAB_SPACES>
Number of spaces per tab
-x, --excludes <EXCLUDE_PATTERNS>
A space separated list of file or directory
-c, --config-file <CONFIG_FILE>
Configuration file
-s, --stdin
Expand Down
43 changes: 30 additions & 13 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
use anyhow::Context;
use clap::Parser;
use console::Style;
use glob::{glob, GlobError};
use glob::{glob, GlobError, Pattern};
use leptosfmt_formatter::{format_file_source, FormatterSettings};
use rayon::{iter::ParallelIterator, prelude::IntoParallelIterator};
use similar::{ChangeTag, TextDiff};
Expand All @@ -33,6 +33,10 @@ struct Args {
#[arg(short, long)]
tab_spaces: Option<usize>,

/// A space separated list of file or directory
#[arg(short = 'x', long = "excludes")]
exclude_patterns: Option<Vec<String>>,

/// Configuration file
#[arg(short, long)]
config_file: Option<PathBuf>,
Expand Down Expand Up @@ -142,7 +146,8 @@ fn main() {
};

let input_patterns = args.input_patterns.unwrap();
let file_paths: Vec<_> = get_file_paths(input_patterns).unwrap();
let exclude_patterns = args.exclude_patterns.unwrap_or_default();
let file_paths: Vec<_> = get_file_paths(input_patterns, exclude_patterns).unwrap();

let total_files = file_paths.len();
let start_formatting = Instant::now();
Expand Down Expand Up @@ -184,21 +189,33 @@ fn main() {
}
}

fn get_file_paths(input_patterns: Vec<String>) -> Result<Vec<PathBuf>, GlobError> {
fn as_glob_pattern(pattern: String) -> String {
let is_dir = fs::metadata(&pattern)
.map(|meta| meta.is_dir())
.unwrap_or(false);
if is_dir {
return format!("{}/**/*.rs", &pattern.trim_end_matches('/'));
}
pattern
}

fn get_file_paths(input_patterns: Vec<String>, exclude_patterns: Vec<String>) -> Result<Vec<PathBuf>, GlobError> {
let exclude_patterns = exclude_patterns
.into_iter()
.map(as_glob_pattern)
.map(|p| Pattern::new(&p))
.collect::<Result<Vec<_>, _>>()
.expect("failed to parse exclude glob pattern");

input_patterns
.into_iter()
.flat_map(|input_pattern| {
let is_dir = fs::metadata(&input_pattern)
.map(|meta| meta.is_dir())
.unwrap_or(false);
let glob_pattern = if is_dir {
format!("{}/**/*.rs", &input_pattern)
} else {
input_pattern
};
.map(as_glob_pattern)
.flat_map(|glob_pattern| {
glob(&glob_pattern)
.expect("failed to read glob pattern")
.collect::<Vec<_>>()
.filter(|is_file| {
is_file.as_ref().is_ok_and(|file| !exclude_patterns.iter().any(|pattern| pattern.matches_path(file)))
})
})
.collect()
}
Expand Down