From 083c1e2f52a5b6f616dcb3a5dd92aad824bd6aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Sat, 16 Oct 2021 13:55:20 +1300 Subject: [PATCH] Move common_prefix to its own mod --- cli/src/config.rs | 22 +++++----------- cli/src/main.rs | 3 ++- lib/src/action/workingdata.rs | 2 -- lib/src/lib.rs | 1 + lib/src/paths.rs | 47 +++++++++++++++++++++++++++++++++++ lib/src/project.rs | 44 -------------------------------- 6 files changed, 56 insertions(+), 63 deletions(-) create mode 100644 lib/src/paths.rs diff --git a/cli/src/config.rs b/cli/src/config.rs index 64a40ee..96eaaef 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -6,13 +6,13 @@ use std::{ use clap::ArgMatches; use miette::{IntoDiagnostic, Result}; use watchexec::{ - action::{Action, Outcome, Signal}, + action::{Action, Outcome}, command::Shell, config::{InitConfig, RuntimeConfig}, filter::tagged::TaggedFilterer, fs::Watcher, handler::PrintDisplay, - signal::source::MainSignal, + signal::{process::SubSignal, source::MainSignal}, }; pub fn new(args: &ArgMatches<'static>) -> Result<(InitConfig, RuntimeConfig, Arc)> { @@ -85,13 +85,13 @@ fn runtime(args: &ArgMatches<'static>) -> Result<(RuntimeConfig, Arc) -> Result<(RuntimeConfig, Arc Signal::SIGHUP, - MainSignal::Interrupt => Signal::SIGINT, - MainSignal::Quit => Signal::SIGQUIT, - MainSignal::Terminate => Signal::SIGTERM, - MainSignal::User1 => Signal::SIGUSR1, - MainSignal::User2 => Signal::SIGUSR2, - }), - ); + out = Outcome::both(out, Outcome::Signal(sig.into())); } action.outcome(out); diff --git a/cli/src/main.rs b/cli/src/main.rs index 9ea98d5..a871b68 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -11,6 +11,7 @@ use watchexec::{ event::Event, filter::tagged::{Filter, Matcher, Op, Pattern, Regex}, ignore_files::{self, IgnoreFile}, + paths::common_prefix, project::{self, ProjectType}, Watchexec, }; @@ -51,7 +52,7 @@ async fn main() -> Result<()> { debug!(?origins, "resolved all project origins"); - let project_origin = project::common_prefix(&origins).unwrap_or_else(|| PathBuf::from(".")); + let project_origin = common_prefix(&origins).unwrap_or_else(|| PathBuf::from(".")); debug!(?project_origin, "resolved common/project origin"); let vcs_types = project::types(&project_origin) diff --git a/lib/src/action/workingdata.rs b/lib/src/action/workingdata.rs index d512bab..ffdf77b 100644 --- a/lib/src/action/workingdata.rs +++ b/lib/src/action/workingdata.rs @@ -11,8 +11,6 @@ use tokio::{ sync::{Mutex, OwnedMutexGuard}, }; -pub use command_group::Signal; - use crate::{command::Shell, event::Event, filter::Filterer, handler::Handler}; use super::Outcome; diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 01f794f..9a1f170 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -93,6 +93,7 @@ pub mod event; pub mod filter; pub mod fs; pub mod ignore_files; +pub mod paths; pub mod project; pub mod signal; diff --git a/lib/src/paths.rs b/lib/src/paths.rs new file mode 100644 index 0000000..3bea64e --- /dev/null +++ b/lib/src/paths.rs @@ -0,0 +1,47 @@ +//! Utilities for paths and sets of paths. + +use std::path::{Path, PathBuf}; + +/// Returns the longest common prefix of all given paths. +/// +/// This is a utility function which is useful for finding the common root of a set of origins. +/// +/// Returns `None` if zero paths are given or paths share no common prefix. +pub fn common_prefix(paths: I) -> Option +where + I: IntoIterator, + P: AsRef, +{ + let mut paths = paths.into_iter(); + let first_path = paths.next().map(|p| p.as_ref().to_owned()); + let mut longest_path = if let Some(ref p) = first_path { + p.components().collect::>() + } else { + return None; + }; + + for path in paths { + let mut greatest_distance = 0; + for component_pair in path.as_ref().components().zip(longest_path.iter()) { + if component_pair.0 != *component_pair.1 { + break; + } + + greatest_distance += 1; + } + + if greatest_distance != longest_path.len() { + longest_path.truncate(greatest_distance); + } + } + + if longest_path.is_empty() { + None + } else { + let mut result = PathBuf::new(); + for component in longest_path { + result.push(component.as_os_str()); + } + Some(result) + } +} diff --git a/lib/src/project.rs b/lib/src/project.rs index 5ac8cf5..d655a30 100644 --- a/lib/src/project.rs +++ b/lib/src/project.rs @@ -190,50 +190,6 @@ pub async fn types(path: impl AsRef) -> HashSet { .collect() } -/// Returns the longest common prefix of all given paths. -/// -/// This is a utility function which is useful for finding the common root of a set of origins. -/// -/// Returns `None` if zero paths are given or paths share no common prefix. -pub fn common_prefix(paths: I) -> Option -where - I: IntoIterator, - P: AsRef, -{ - let mut paths = paths.into_iter(); - let first_path = paths.next().map(|p| p.as_ref().to_owned()); - let mut longest_path = if let Some(ref p) = first_path { - p.components().collect::>() - } else { - return None; - }; - - for path in paths { - let mut greatest_distance = 0; - for component_pair in path.as_ref().components().zip(longest_path.iter()) { - if component_pair.0 != *component_pair.1 { - break; - } - - greatest_distance += 1; - } - - if greatest_distance != longest_path.len() { - longest_path.truncate(greatest_distance); - } - } - - if longest_path.is_empty() { - None - } else { - let mut result = PathBuf::new(); - for component in longest_path { - result.push(component.as_os_str()); - } - Some(result) - } -} - #[derive(Debug, Default)] struct DirList(HashMap); impl DirList {