1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::path::Path;

use anyhow::{Context, Result};
use camino::Utf8PathBuf;
use fs_extra::dir::CopyOptions;

pub trait PathBufExt {
    fn resolve_home_dir(self) -> Result<Utf8PathBuf>;

    fn create_dir_all_if_needed(&self) -> Result<()>;

    fn remove_dir_all_if_exists(&self) -> Result<()>;

    fn copy_dir<P: AsRef<Path>>(&self, to: P) -> Result<()>;

    fn copy_dir_contents<P: AsRef<Path>>(&self, to: P) -> Result<()>;
}

impl PathBufExt for Utf8PathBuf {
    fn create_dir_all_if_needed(&self) -> Result<()> {
        if !self.exists() {
            fs_err::create_dir_all(self)?;
        }
        Ok(())
    }

    fn resolve_home_dir(self) -> Result<Utf8PathBuf> {
        if self.starts_with("~") {
            let home = std::env::var("HOME").context("Could not resolve $HOME")?;
            let home = Utf8PathBuf::from(home);
            Ok(home.join(self.strip_prefix("~").unwrap()))
        } else {
            Ok(self)
        }
    }

    fn remove_dir_all_if_exists(&self) -> Result<()> {
        if self.exists() {
            fs_err::remove_dir_all(self)?;
        }
        Ok(())
    }

    fn copy_dir<P: AsRef<Path>>(&self, to: P) -> Result<()> {
        let to_path = to.as_ref();
        if !to_path.exists() {
            fs_err::create_dir_all(to_path)?;
        }
        fs_extra::dir::copy(self, to_path, &CopyOptions::new())?;

        Ok(())
    }

    fn copy_dir_contents<P: AsRef<Path>>(&self, to: P) -> Result<()> {
        let to_path = to.as_ref();
        if !to_path.exists() {
            fs_err::create_dir_all(to_path)?;
        }
        let options = CopyOptions::new().content_only(true);
        fs_extra::dir::copy(self, to_path, &options)?;

        Ok(())
    }
}