Skip to main content

conduit_cli/
paths.rs

1use dirs::data_local_dir;
2use std::fs;
3use std::path::{Path, PathBuf};
4
5use crate::core::domain::loader::Loader;
6
7#[derive(Clone, Debug)]
8pub struct ConduitPaths {
9    pub root: PathBuf,
10    pub store: PathBuf,
11}
12
13impl ConduitPaths {
14    pub fn new<P: AsRef<Path>>(project_root: P) -> Self {
15        let root = project_root.as_ref().to_path_buf();
16        let store =
17            data_local_dir().map_or_else(|| root.join(".conduit_data"), |p| p.join("conduit"));
18
19        Self { root, store }
20    }
21
22    pub fn manifest(&self) -> PathBuf {
23        self.root.join("conduit.toml")
24    }
25
26    pub fn lock(&self) -> PathBuf {
27        self.root.join("conduit.lock")
28    }
29
30    pub fn runtimes_dir(&self) -> PathBuf {
31        self.store.join("runtimes")
32    }
33
34    pub fn objects_dir(&self) -> PathBuf {
35        self.store.join("objects")
36    }
37
38    pub fn ensure_dirs(&self) -> std::io::Result<()> {
39        fs::create_dir_all(self.objects_dir())?;
40        fs::create_dir_all(self.runtimes_dir())?;
41        Ok(())
42    }
43
44    pub fn get_runtime_id(loader: &Loader, mc_version: &str) -> String {
45        let name = match loader {
46            Loader::Vanilla => "vanilla".to_string(),
47            Loader::Fabric => "fabric".to_string(),
48            Loader::Paper => "paper".to_string(),
49            Loader::Purpur => "purpur".to_string(),
50            Loader::Neoforge { version } => format!("neoforge-{version}"),
51            Loader::Forge { version } => format!("forge-{version}"),
52        };
53        format!("{name}@{mc_version}")
54    }
55
56    pub fn is_conduit_file(name: &str) -> bool {
57        matches!(
58            name,
59            "conduit.toml"
60                | "conduit.lock"
61                | ".conduit_runtimes"
62                | ".git"
63                | ".conduit"
64                | "eula.txt"
65        )
66    }
67}