]>
Commit | Line | Data |
---|---|---|
9c376795 FG |
1 | use std::fs; |
2 | use std::path::PathBuf; | |
3 | ||
9ffffee4 FG |
4 | use super::utils::remove_dir_if_exists; |
5 | ||
9c376795 FG |
6 | #[derive(Debug, Clone)] |
7 | pub(crate) struct Dirs { | |
8 | pub(crate) source_dir: PathBuf, | |
9 | pub(crate) download_dir: PathBuf, | |
10 | pub(crate) build_dir: PathBuf, | |
11 | pub(crate) dist_dir: PathBuf, | |
12 | } | |
13 | ||
14 | #[doc(hidden)] | |
15 | #[derive(Debug, Copy, Clone)] | |
16 | pub(crate) enum PathBase { | |
17 | Source, | |
18 | Download, | |
19 | Build, | |
20 | Dist, | |
21 | } | |
22 | ||
23 | impl PathBase { | |
24 | fn to_path(self, dirs: &Dirs) -> PathBuf { | |
25 | match self { | |
26 | PathBase::Source => dirs.source_dir.clone(), | |
27 | PathBase::Download => dirs.download_dir.clone(), | |
28 | PathBase::Build => dirs.build_dir.clone(), | |
29 | PathBase::Dist => dirs.dist_dir.clone(), | |
30 | } | |
31 | } | |
32 | } | |
33 | ||
34 | #[derive(Debug, Copy, Clone)] | |
35 | pub(crate) enum RelPath { | |
36 | Base(PathBase), | |
37 | Join(&'static RelPath, &'static str), | |
38 | } | |
39 | ||
40 | impl RelPath { | |
41 | pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source); | |
42 | pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download); | |
43 | pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build); | |
44 | pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist); | |
45 | ||
46 | pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts"); | |
9c376795 FG |
47 | pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches"); |
48 | ||
49 | pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath { | |
50 | RelPath::Join(self, suffix) | |
51 | } | |
52 | ||
53 | pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf { | |
54 | match self { | |
55 | RelPath::Base(base) => base.to_path(dirs), | |
56 | RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix), | |
57 | } | |
58 | } | |
59 | ||
60 | pub(crate) fn ensure_exists(&self, dirs: &Dirs) { | |
61 | fs::create_dir_all(self.to_path(dirs)).unwrap(); | |
62 | } | |
63 | ||
64 | pub(crate) fn ensure_fresh(&self, dirs: &Dirs) { | |
65 | let path = self.to_path(dirs); | |
9ffffee4 | 66 | remove_dir_if_exists(&path); |
9c376795 FG |
67 | fs::create_dir_all(path).unwrap(); |
68 | } | |
69 | } |