mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-30 03:01:15 -03:00
xtasks: make files_with_extension more accessible
This function is useful beyond the formatting module, so put it into the top level of the library. Closes #12483
This commit is contained in:
committed by
Johannes Altmanninger
parent
2ba031677f
commit
d6ac8a48c0
@@ -2,10 +2,11 @@
|
|||||||
use clap::Args;
|
use clap::Args;
|
||||||
use std::{
|
use std::{
|
||||||
io::{ErrorKind, Write},
|
io::{ErrorKind, Write},
|
||||||
path::{Path, PathBuf},
|
path::PathBuf,
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
use crate::files_with_extension;
|
||||||
|
|
||||||
const GREEN: Style = AnsiColor::Green.on_default();
|
const GREEN: Style = AnsiColor::Green.on_default();
|
||||||
const YELLOW: Style = AnsiColor::Yellow.on_default();
|
const YELLOW: Style = AnsiColor::Yellow.on_default();
|
||||||
@@ -67,32 +68,6 @@ pub fn format(args: FormatArgs) {
|
|||||||
format_rust(&args);
|
format_rust(&args);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_matching_files<P: AsRef<Path>, I: IntoIterator<Item = P>, M: Fn(&Path) -> bool>(
|
|
||||||
all_paths: I,
|
|
||||||
matcher: M,
|
|
||||||
) -> Vec<PathBuf> {
|
|
||||||
all_paths
|
|
||||||
.into_iter()
|
|
||||||
.flat_map(WalkDir::new)
|
|
||||||
.filter_map(|res| {
|
|
||||||
let entry = res.unwrap();
|
|
||||||
let path = entry.path();
|
|
||||||
if entry.file_type().is_file() && matcher(path) {
|
|
||||||
Some(path.to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
fn files_with_extension<P: AsRef<Path>, I: IntoIterator<Item = P>>(
|
|
||||||
all_paths: I,
|
|
||||||
extension: &str,
|
|
||||||
) -> Vec<PathBuf> {
|
|
||||||
let matcher = |p: &Path| p.extension().is_some_and(|e| e == extension);
|
|
||||||
get_matching_files(all_paths, matcher)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_formatter(formatter: &mut Command, name: &str) {
|
fn run_formatter(formatter: &mut Command, name: &str) {
|
||||||
println!("=== Running {GREEN}{name}{GREEN:#}");
|
println!("=== Running {GREEN}{name}{GREEN:#}");
|
||||||
match formatter.status() {
|
match formatter.status() {
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
use std::{ffi::OsStr, process::Command};
|
use std::{
|
||||||
|
ffi::OsStr,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
process::Command,
|
||||||
|
};
|
||||||
|
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
macro_rules! fail {
|
macro_rules! fail {
|
||||||
($($arg:tt)+) => {{
|
($($arg:tt)+) => {{
|
||||||
@@ -35,3 +41,30 @@ pub fn cargo<I, S>(cargo_args: I)
|
|||||||
{
|
{
|
||||||
Command::new(env!("CARGO")).args(cargo_args).run_or_fail();
|
Command::new(env!("CARGO")).args(cargo_args).run_or_fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_matching_files<P: AsRef<Path>, I: IntoIterator<Item = P>, M: Fn(&Path) -> bool>(
|
||||||
|
all_paths: I,
|
||||||
|
matcher: M,
|
||||||
|
) -> Vec<PathBuf> {
|
||||||
|
all_paths
|
||||||
|
.into_iter()
|
||||||
|
.flat_map(WalkDir::new)
|
||||||
|
.filter_map(|res| {
|
||||||
|
let entry = res.unwrap();
|
||||||
|
let path = entry.path();
|
||||||
|
if entry.file_type().is_file() && matcher(path) {
|
||||||
|
Some(path.to_owned())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn files_with_extension<P: AsRef<Path>, I: IntoIterator<Item = P>>(
|
||||||
|
all_paths: I,
|
||||||
|
extension: &str,
|
||||||
|
) -> Vec<PathBuf> {
|
||||||
|
let matcher = |p: &Path| p.extension().is_some_and(|e| e == extension);
|
||||||
|
get_matching_files(all_paths, matcher)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user