mirror of https://github.com/astral-sh/ruff
Support multiple files
This commit is contained in:
parent
92238b4138
commit
6257dd00c7
|
|
@ -5,9 +5,20 @@ use anyhow::Result;
|
||||||
use clap::{Parser, ValueHint};
|
use clap::{Parser, ValueHint};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
use rust_python_linter::fs::collect_python_files;
|
||||||
use rust_python_linter::linter::check_path;
|
use rust_python_linter::linter::check_path;
|
||||||
use rust_python_linter::message::Message;
|
use rust_python_linter::message::Message;
|
||||||
use walkdir::{DirEntry, WalkDir};
|
use walkdir::DirEntry;
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
#[clap(name = "rust-python-linter")]
|
||||||
|
#[clap(about = "A bare-bones Python linter written in Rust", long_about = None)]
|
||||||
|
struct Cli {
|
||||||
|
#[clap(parse(from_os_str), value_hint = ValueHint::AnyPath, required = true)]
|
||||||
|
files: Vec<PathBuf>,
|
||||||
|
#[clap(short, long, action)]
|
||||||
|
verbose: bool,
|
||||||
|
}
|
||||||
|
|
||||||
fn set_up_logging(verbose: bool) -> Result<()> {
|
fn set_up_logging(verbose: bool) -> Result<()> {
|
||||||
fern::Dispatch::new()
|
fern::Dispatch::new()
|
||||||
|
|
@ -31,27 +42,6 @@ fn set_up_logging(verbose: bool) -> Result<()> {
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
|
||||||
#[clap(name = "rust-python-linter")]
|
|
||||||
#[clap(about = "A bare-bones Python linter written in Rust", long_about = None)]
|
|
||||||
struct Cli {
|
|
||||||
#[clap(name = "filename", parse(from_os_str), value_hint = ValueHint::DirPath)]
|
|
||||||
filename: PathBuf,
|
|
||||||
#[clap(short, long, action)]
|
|
||||||
verbose: bool,
|
|
||||||
// /// Files to process
|
|
||||||
// #[clap(name = "FILE", parse(from_os_str), value_hint = ValueHint::AnyPath)]
|
|
||||||
// files: Vec<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_not_hidden(entry: &DirEntry) -> bool {
|
|
||||||
entry
|
|
||||||
.file_name()
|
|
||||||
.to_str()
|
|
||||||
.map(|s| entry.depth() == 0 || !s.starts_with('.'))
|
|
||||||
.unwrap_or(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
|
@ -59,13 +49,7 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
// Collect all the files to check.
|
// Collect all the files to check.
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let files: Vec<DirEntry> = WalkDir::new(cli.filename)
|
let files: Vec<DirEntry> = cli.files.iter().flat_map(collect_python_files).collect();
|
||||||
.follow_links(true)
|
|
||||||
.into_iter()
|
|
||||||
.filter_entry(is_not_hidden)
|
|
||||||
.filter_map(|entry| entry.ok())
|
|
||||||
.filter(|entry| entry.path().to_string_lossy().ends_with(".py"))
|
|
||||||
.collect();
|
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
info!("Identified files to lint in: {:?}", duration);
|
info!("Identified files to lint in: {:?}", duration);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
|
fn is_not_hidden(entry: &DirEntry) -> bool {
|
||||||
|
entry
|
||||||
|
.file_name()
|
||||||
|
.to_str()
|
||||||
|
.map(|s| entry.depth() == 0 || !s.starts_with('.'))
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn collect_python_files(path: &PathBuf) -> Vec<DirEntry> {
|
||||||
|
WalkDir::new(path)
|
||||||
|
.follow_links(true)
|
||||||
|
.into_iter()
|
||||||
|
.filter_entry(is_not_hidden)
|
||||||
|
.filter_map(|entry| entry.ok())
|
||||||
|
.filter(|entry| entry.path().to_string_lossy().ends_with(".py"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
mod cache;
|
mod cache;
|
||||||
mod check;
|
mod check;
|
||||||
|
pub mod fs;
|
||||||
pub mod linter;
|
pub mod linter;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue