2021-12-24 06:51:35 +01:00
|
|
|
use std::{
|
|
|
|
ffi::{OsStr, OsString},
|
2021-12-29 08:14:29 +01:00
|
|
|
path::MAIN_SEPARATOR,
|
2021-12-24 06:51:35 +01:00
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-12-23 15:37:51 +01:00
|
|
|
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-12-29 07:29:45 +01:00
|
|
|
use watchexec::{
|
|
|
|
error::RuntimeError,
|
|
|
|
event::{
|
|
|
|
filekind::{FileEventKind, ModifyKind},
|
|
|
|
Event, Tag,
|
|
|
|
},
|
|
|
|
filter::{globset::GlobsetFilterer, Filterer},
|
2022-01-16 04:12:50 +01:00
|
|
|
project::ProjectType,
|
2021-12-29 07:29:45 +01:00
|
|
|
};
|
2021-12-23 15:37:51 +01:00
|
|
|
|
2021-12-29 07:29:45 +01:00
|
|
|
pub async fn globset(args: &ArgMatches<'static>) -> Result<Arc<WatchexecFilterer>> {
|
2021-12-24 06:51:35 +01:00
|
|
|
let (project_origin, workdir) = super::common::dirs(args).await?;
|
2022-01-16 04:12:50 +01:00
|
|
|
let vcs_types = super::common::vcs_types(&project_origin).await;
|
|
|
|
let ignore_files = super::common::ignores(args, &vcs_types, &project_origin).await?;
|
2021-12-24 06:51:35 +01:00
|
|
|
|
|
|
|
let mut ignores = Vec::new();
|
2021-12-29 08:14:29 +01:00
|
|
|
|
|
|
|
if !args.is_present("no-default-ignore") {
|
|
|
|
ignores.extend([
|
|
|
|
(format!("**{s}.DS_Store", s = MAIN_SEPARATOR), None),
|
|
|
|
(String::from("*.py[co]"), None),
|
|
|
|
(String::from("#*#"), None),
|
|
|
|
(String::from(".#*"), None),
|
|
|
|
(String::from(".*.kate-swp"), None),
|
|
|
|
(String::from(".*.sw?"), None),
|
|
|
|
(String::from(".*.sw?x"), None),
|
|
|
|
]);
|
2022-01-16 04:12:50 +01:00
|
|
|
|
|
|
|
if vcs_types.contains(&ProjectType::Git) {
|
|
|
|
ignores.push((format!("**{s}.git{s}**", s = MAIN_SEPARATOR), None));
|
|
|
|
}
|
|
|
|
|
|
|
|
if vcs_types.contains(&ProjectType::Mercurial) {
|
|
|
|
ignores.push((format!("**{s}.hg{s}**", s = MAIN_SEPARATOR), None));
|
|
|
|
}
|
2022-01-16 04:13:05 +01:00
|
|
|
|
|
|
|
if vcs_types.contains(&ProjectType::Subversion) {
|
|
|
|
ignores.push((format!("**{s}.svn{s}**", s = MAIN_SEPARATOR), None));
|
|
|
|
}
|
|
|
|
|
2022-01-16 04:12:50 +01:00
|
|
|
if vcs_types.contains(&ProjectType::Bazaar) {
|
|
|
|
ignores.push((format!("**{s}.bzr{s}**", s = MAIN_SEPARATOR), None));
|
|
|
|
}
|
|
|
|
|
|
|
|
if vcs_types.contains(&ProjectType::Darcs) {
|
|
|
|
ignores.push((format!("**{s}_darcs{s}**", s = MAIN_SEPARATOR), None));
|
|
|
|
}
|
|
|
|
|
|
|
|
if vcs_types.contains(&ProjectType::Fossil) {
|
|
|
|
ignores.push((
|
|
|
|
format!("**{s}.fossil-settings{s}**", s = MAIN_SEPARATOR),
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if vcs_types.contains(&ProjectType::Pijul) {
|
|
|
|
ignores.push((format!("**{s}.pijul{s}**", s = MAIN_SEPARATOR), None));
|
|
|
|
}
|
2021-12-29 08:14:29 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 15:37:51 +01:00
|
|
|
let filters = args
|
|
|
|
.values_of("filter")
|
|
|
|
.unwrap_or_default()
|
2021-12-24 06:51:35 +01:00
|
|
|
.map(|f| (f.to_owned(), Some(workdir.clone())));
|
2021-12-23 15:37:51 +01:00
|
|
|
|
2021-12-24 06:51:35 +01:00
|
|
|
ignores.extend(
|
|
|
|
args.values_of("ignore")
|
|
|
|
.unwrap_or_default()
|
|
|
|
.map(|f| (f.to_owned(), Some(workdir.clone()))),
|
|
|
|
);
|
2021-12-23 15:37:51 +01:00
|
|
|
|
|
|
|
let exts = args
|
2021-12-24 06:51:35 +01:00
|
|
|
.values_of_os("extensions")
|
2021-12-23 15:37:51 +01:00
|
|
|
.unwrap_or_default()
|
2021-12-24 06:51:35 +01:00
|
|
|
.map(|s| s.split(b','))
|
2021-12-23 15:37:51 +01:00
|
|
|
.flatten();
|
|
|
|
|
2021-12-29 07:29:45 +01:00
|
|
|
Ok(Arc::new(WatchexecFilterer {
|
2022-01-16 04:09:53 +01:00
|
|
|
inner: GlobsetFilterer::new(project_origin, filters, ignores, ignore_files, exts)
|
|
|
|
.await
|
|
|
|
.into_diagnostic()?,
|
2021-12-29 07:29:45 +01:00
|
|
|
no_meta: args.is_present("no-meta"),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A custom filterer that combines the library's Globset filterer and a switch for --no-meta
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WatchexecFilterer {
|
|
|
|
inner: GlobsetFilterer,
|
|
|
|
no_meta: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Filterer for WatchexecFilterer {
|
|
|
|
fn check_event(&self, event: &Event) -> Result<bool, RuntimeError> {
|
|
|
|
let is_meta = event.tags.iter().any(|tag| {
|
|
|
|
matches!(
|
|
|
|
tag,
|
|
|
|
Tag::FileEventKind(FileEventKind::Modify(ModifyKind::Metadata(_)))
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
if self.no_meta && is_meta {
|
|
|
|
Ok(false)
|
|
|
|
} else {
|
|
|
|
self.inner.check_event(event)
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 15:37:51 +01:00
|
|
|
}
|
2021-12-24 06:51:35 +01:00
|
|
|
|
|
|
|
trait OsStringSplit {
|
|
|
|
fn split(&self, sep: u8) -> OsSplit;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OsStringSplit for OsStr {
|
|
|
|
fn split(&self, sep: u8) -> OsSplit {
|
|
|
|
OsSplit {
|
|
|
|
os: self.to_os_string(),
|
|
|
|
pos: 0,
|
|
|
|
sep,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OsSplit {
|
|
|
|
os: OsString,
|
|
|
|
pos: usize,
|
|
|
|
sep: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl Iterator for OsSplit {
|
|
|
|
type Item = OsString;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
use std::os::unix::ffi::{OsStrExt, OsStringExt};
|
|
|
|
let bytes = self.os.as_bytes();
|
|
|
|
if self.pos >= bytes.len() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let mut pos = self.pos;
|
|
|
|
while pos < bytes.len() && bytes[pos] != self.sep {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = OsString::from_vec(bytes[self.pos..=pos].to_vec());
|
|
|
|
self.pos = pos + 1;
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl Iterator for OsSplit {
|
|
|
|
type Item = OsString;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
use std::os::windows::ffi::{OsStrExt, OsStringExt};
|
2021-12-24 07:07:01 +01:00
|
|
|
let wides = self.os.encode_wide().skip(self.pos);
|
2021-12-24 06:51:35 +01:00
|
|
|
|
|
|
|
let mut cur = Vec::new();
|
|
|
|
for wide in wides {
|
|
|
|
if wide == u16::from(self.sep) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.push(wide);
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = OsString::from_wide(&cur);
|
|
|
|
self.pos = cur.len() + 1;
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
}
|