unity_hub/unity/hub/
paths.rs1use std::fs::File;
2use std::path::PathBuf;
3
4pub fn default_install_path() -> Option<PathBuf> {
5 #[cfg(any(target_os = "windows", target_os = "macos"))]
6 let application_path = dirs_2::application_dir();
7 #[cfg(target_os = "linux")]
8 let application_path = dirs_2::home_dir();
9 application_path.map(|path| path.join(["Unity", "Hub", "Editor"].iter().collect::<PathBuf>()))
10}
11
12pub fn install_path() -> Option<PathBuf> {
13 secondary_install_path_config_path()
14 .and_then(|path| {
15 File::open(path)
16 .and_then(|file| {
17 let path: PathBuf = serde_json::from_reader(file)?;
18 Ok(path)
19 }).ok()
20 })
21 .filter(|p| p.as_os_str() != std::ffi::OsStr::new(""))
23 .or_else(default_install_path)
24}
25
26pub fn config_path() -> Option<PathBuf> {
27 #[cfg(any(target_os = "windows", target_os = "macos"))]
28 return dirs_2::data_dir().map(|path| path.join("UnityHub"));
29
30 #[cfg(target_os = "linux")]
31 return dirs_2::config_dir().map(|path| path.join("UnityHub"));
32}
33
34pub fn editors_config_path() -> Option<PathBuf> {
35 config_path().map(|path| path.join("editors.json"))
36}
37
38pub fn secondary_install_path_config_path() -> Option<PathBuf> {
39 config_path().map(|path| path.join("secondaryInstallPath.json"))
40}
41
42pub fn default_editor_config_path() -> Option<PathBuf> {
43 config_path().map(|path| path.join("defaultEditor.json"))
44}
45
46pub fn cache_dir() -> Option<PathBuf> {
47 dirs_2::cache_dir().map(|path| path.join("com.github.larusso.api-version-manager"))
48}
49
50pub fn locks_dir() -> Option<PathBuf> {
51 cache_dir().map(|path| path.join("locks"))
52}
53
54pub fn hash_cache_dir() -> Option<PathBuf> {
55 cache_dir().map(|path| path.join("versions"))
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_dirs() {
64 println!(
65 "default_editor_config_path: {:?}",
66 default_editor_config_path()
67 );
68 println!(
69 "secondary_install_path_config_path: {:?}",
70 secondary_install_path_config_path()
71 );
72 println!(
73 "editors_config_path: {:?}",
74 editors_config_path()
75 );
76 println!("config_path: {:?}", config_path());
77 println!("install_path: {:?}", install_path());
78 println!(
79 "default_install_path: {:?}",
80 default_install_path()
81 );
82 println!("cache_dir: {:?}", cache_dir());
83 println!("locks: {:?}", locks_dir());
84 }
85}