veryl_path/
lib.rs

1use directories::ProjectDirs;
2#[cfg(not(target_family = "wasm"))]
3use fs4::fs_std::FileExt;
4use log::debug;
5#[cfg(not(target_family = "wasm"))]
6use std::fs::File;
7use std::path::{Path, PathBuf};
8use walkdir::WalkDir;
9
10mod path_error;
11pub use path_error::PathError;
12
13#[derive(Clone, Debug)]
14pub struct PathSet {
15    pub prj: String,
16    pub src: PathBuf,
17    pub dst: PathBuf,
18    pub map: PathBuf,
19}
20
21pub fn cache_path() -> PathBuf {
22    let project_dir = ProjectDirs::from("org", "veryl-lang", "veryl").unwrap();
23    project_dir.cache_dir().to_path_buf()
24}
25
26pub fn gather_files_with_extension<T: AsRef<Path>>(
27    base_dir: T,
28    ext: &str,
29    symlink: bool,
30) -> Result<Vec<PathBuf>, PathError> {
31    let mut inner_prj = Vec::new();
32    for entry in WalkDir::new(base_dir.as_ref())
33        .follow_links(symlink)
34        .into_iter()
35        .flatten()
36    {
37        if entry.file_type().is_file() {
38            if let Some(x) = entry.path().file_name() {
39                if x == "Veryl.toml" {
40                    let prj_dir = entry.path().parent().unwrap();
41                    if prj_dir != base_dir.as_ref() {
42                        debug!("Found inner project ({})", prj_dir.to_string_lossy());
43                        inner_prj.push(prj_dir.to_path_buf());
44                    }
45                }
46            }
47        }
48    }
49
50    let mut ret = Vec::new();
51    for entry in WalkDir::new(base_dir.as_ref())
52        .follow_links(symlink)
53        .sort_by_file_name()
54        .into_iter()
55        .flatten()
56    {
57        if entry.file_type().is_file() {
58            if let Some(x) = entry.path().extension() {
59                if x == ext {
60                    let is_inner = inner_prj.iter().any(|x| entry.path().starts_with(x));
61
62                    if !is_inner {
63                        debug!("Found file ({})", entry.path().to_string_lossy());
64                        ret.push(entry.path().to_path_buf());
65                    }
66                }
67            }
68        }
69    }
70    Ok(ret)
71}
72
73#[cfg(not(target_family = "wasm"))]
74pub fn lock_dir<T: AsRef<Path>>(path: T) -> Result<File, PathError> {
75    let base_dir = cache_path().join(path);
76    let lock = base_dir.join("lock");
77    let lock = File::create(lock)?;
78    lock.lock_exclusive()?;
79    Ok(lock)
80}
81
82#[cfg(not(target_family = "wasm"))]
83pub fn unlock_dir(lock: File) -> Result<(), PathError> {
84    fs4::fs_std::FileExt::unlock(&lock)?;
85    Ok(())
86}
87
88#[cfg(target_family = "wasm")]
89pub fn lock_dir<T: AsRef<Path>>(_path: T) -> Result<(), PathError> {
90    Ok(())
91}
92
93#[cfg(target_family = "wasm")]
94pub fn unlock_dir(_lock: ()) -> Result<(), PathError> {
95    Ok(())
96}
97
98pub fn ignore_already_exists(x: Result<(), std::io::Error>) -> Result<(), std::io::Error> {
99    if let Err(x) = x {
100        if x.kind() != std::io::ErrorKind::AlreadyExists {
101            return Err(x);
102        }
103    }
104    Ok(())
105}
106
107pub fn ignore_directory_not_empty(x: Result<(), std::io::Error>) -> Result<(), std::io::Error> {
108    if let Err(x) = x {
109        if x.kind() != std::io::ErrorKind::DirectoryNotEmpty {
110            return Err(x);
111        }
112    }
113    Ok(())
114}