2018-12-11 16:43:26 +02:00
|
|
|
use super::error::{Error, ErrorKind};
|
2018-11-18 11:52:37 +02:00
|
|
|
use log::{debug, error};
|
2018-06-17 14:17:36 +03:00
|
|
|
use std::ffi::OsStr;
|
2019-01-30 16:00:10 +02:00
|
|
|
use std::fmt::{self, Debug};
|
|
|
|
|
use std::path::{Component, Path, PathBuf};
|
2018-08-22 22:18:48 +03:00
|
|
|
use std::process::{ExitStatus, Output};
|
2018-12-09 10:30:41 +02:00
|
|
|
use which_crate;
|
2018-06-17 11:43:25 +03:00
|
|
|
|
|
|
|
|
pub trait Check {
|
|
|
|
|
fn check(self) -> Result<(), Error>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Check for ExitStatus {
|
|
|
|
|
fn check(self) -> Result<(), Error> {
|
|
|
|
|
if self.success() {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
2018-12-11 16:43:26 +02:00
|
|
|
Err(ErrorKind::ProcessFailed(self))?
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 22:18:48 +03:00
|
|
|
impl Check for Output {
|
|
|
|
|
fn check(self) -> Result<(), Error> {
|
|
|
|
|
self.status.check()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 09:18:53 +03:00
|
|
|
pub trait PathExt
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
fn if_exists(self) -> Option<Self>;
|
2018-08-22 10:43:32 +03:00
|
|
|
fn is_descendant_of(&self, ancestor: &Path) -> bool;
|
2019-01-13 23:20:32 +02:00
|
|
|
|
|
|
|
|
/// Returns the path if it exists or ErrorKind::SkipStep otherwise
|
|
|
|
|
fn require(self) -> Result<Self, Error>;
|
2018-07-07 09:18:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PathExt for PathBuf {
|
|
|
|
|
fn if_exists(self) -> Option<Self> {
|
|
|
|
|
if self.exists() {
|
|
|
|
|
Some(self)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
|
|
|
|
|
2018-07-07 09:18:53 +03:00
|
|
|
fn is_descendant_of(&self, ancestor: &Path) -> bool {
|
|
|
|
|
self.iter().zip(ancestor.iter()).all(|(a, b)| a == b)
|
|
|
|
|
}
|
2019-01-13 23:20:32 +02:00
|
|
|
|
|
|
|
|
fn require(self) -> Result<Self, Error> {
|
|
|
|
|
if self.exists() {
|
|
|
|
|
Ok(self)
|
|
|
|
|
} else {
|
|
|
|
|
Err(ErrorKind::SkipStep)?
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-17 11:43:25 +03:00
|
|
|
}
|
2018-06-17 14:17:36 +03:00
|
|
|
|
|
|
|
|
pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
|
2018-12-09 10:30:41 +02:00
|
|
|
match which_crate::which(&binary_name) {
|
2018-06-17 14:17:36 +03:00
|
|
|
Ok(path) => {
|
|
|
|
|
debug!("Detected {:?} as {:?}", &path, &binary_name);
|
|
|
|
|
Some(path)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
match e.kind() {
|
2018-12-09 10:30:41 +02:00
|
|
|
which_crate::ErrorKind::CannotFindBinaryPath => {
|
2018-06-17 14:17:36 +03:00
|
|
|
debug!("Cannot find {:?}", &binary_name);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
error!("Detecting {:?} failed: {}", &binary_name, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-30 16:00:10 +02:00
|
|
|
|
|
|
|
|
/// `std::fmt::Display` implementation for `std::path::Path`.
|
|
|
|
|
///
|
|
|
|
|
/// This struct differs from `std::path::Display` in that in Windows it takes care of printing backslashes
|
|
|
|
|
/// instead of slashes and don't print the `\\?` prefix in long paths.
|
|
|
|
|
pub struct HumanizedPath<'a> {
|
|
|
|
|
path: &'a Path,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> From<&'a Path> for HumanizedPath<'a> {
|
|
|
|
|
fn from(path: &'a Path) -> Self {
|
|
|
|
|
Self { path }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> fmt::Display for HumanizedPath<'a> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
|
let mut iterator = self.path.components().peekable();
|
|
|
|
|
|
|
|
|
|
while let Some(component) = iterator.next() {
|
|
|
|
|
let mut print_seperator = iterator.peek().is_some();
|
|
|
|
|
|
|
|
|
|
match &component {
|
|
|
|
|
Component::Normal(c) if *c == "?" => {
|
|
|
|
|
print_seperator = false;
|
|
|
|
|
}
|
|
|
|
|
Component::RootDir | Component::CurDir => {
|
|
|
|
|
print_seperator = false;
|
|
|
|
|
}
|
|
|
|
|
Component::ParentDir => {
|
|
|
|
|
write!(f, "..")?;
|
|
|
|
|
}
|
|
|
|
|
Component::Prefix(p) => {
|
|
|
|
|
write!(f, "{}", p.as_os_str().to_string_lossy())?;
|
|
|
|
|
print_seperator = true;
|
|
|
|
|
}
|
|
|
|
|
Component::Normal(c) => {
|
|
|
|
|
write!(f, "{}", c.to_string_lossy())?;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if print_seperator {
|
|
|
|
|
write!(f, "{}", std::path::MAIN_SEPARATOR)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "{}", self.path.display())?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
fn humanize<P: AsRef<Path>>(path: P) -> String {
|
|
|
|
|
format!("{}", HumanizedPath::from(path.as_ref()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_just_drive() {
|
|
|
|
|
assert_eq!("C:\\", humanize("C:\\"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_path() {
|
|
|
|
|
assert_eq!("C:\\hi", humanize("C:\\hi"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unc() {
|
|
|
|
|
assert_eq!("\\\\server\\share\\", humanize("\\\\server\\share"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_long_path() {
|
|
|
|
|
assert_eq!("C:\\hi", humanize("//?/C:/hi"));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-13 23:20:32 +02:00
|
|
|
|
|
|
|
|
pub fn require<T: AsRef<OsStr> + Debug>(binary_name: T) -> Result<PathBuf, Error> {
|
|
|
|
|
match which_crate::which(&binary_name) {
|
|
|
|
|
Ok(path) => {
|
|
|
|
|
debug!("Detected {:?} as {:?}", &path, &binary_name);
|
|
|
|
|
Ok(path)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
|
which_crate::ErrorKind::CannotFindBinaryPath => {
|
|
|
|
|
debug!("Cannot find {:?}", &binary_name);
|
|
|
|
|
Err(ErrorKind::SkipStep)?
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
panic!("Detecting {:?} failed: {}", &binary_name, e);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-11 14:10:06 +02:00
|
|
|
|
2019-02-11 14:15:35 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
2019-02-11 14:10:06 +02:00
|
|
|
pub fn require_option<T>(option: Option<T>) -> Result<T, Error> {
|
|
|
|
|
if let Some(value) = option {
|
|
|
|
|
Ok(value)
|
|
|
|
|
} else {
|
|
|
|
|
Err(ErrorKind::SkipStep)?
|
|
|
|
|
}
|
|
|
|
|
}
|